Google Cloud(gcp) ssh 链接配置

2020-02-03  本文已影响0人  不问客

首先在gcp创建实例
1 . 在gcp管理后台登录,查看当前账号信息

test@instance-2:~# whoami  
test  

2 . 在本地生成私钥和公钥。从上面这里获得当前用户账号情况。填入下面密钥命令中

$ cd ~/.ssh
$ ssh-keygen -t rsa -f myKey -C test [test是上面的用户名]

Generating public/private rsa key pair.
Enter passphrase (empty for no passphrase): ( 按enter键即可)
Enter same passphrase again: ( 按enter键即可)
Your identification has been saved in myKey.
Your public key has been saved in myKey.pub.
The key fingerprint is:
SHA256:EW7ow1aaaaaf8mNvk user@computer-name.local
The key's randomart image is:
+---[RSA 2048]----+
|=.o+= o .        |
|o+.o+= + .       |
|o.o..oo *        |
|..o+ +o+ o       |
|.oo+  =+S o      |
| o. * o. = o     |
| ..o =  . =      |
|. o.. .    E     |
| . .o.           |
+----[SHA256]-----+
# 此时会生成 公钥 myKey.pub 和 私钥 myKey

3 . 复制公钥及导入公钥

cat myKey.pub
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCjHKPaeglRVJzAhNq+W
中间部分省略。。。
dKx8sJ0Rw4kUqm2eU2vo8S5IEA0Nk2f7BtVGE8VOCHgmDbv2tLp9845UVp1 user@computer-name.local
# 全复制下来
# 进入谷歌云平台页面 -> 计算引擎 -> 元数据 -> SSH 密钥,粘贴保存即可
# 谷歌就会把上面这段 public key 写入到 ~/.ssh/authorized_keys

4 . 添加密码验证登陆

sudo vi /etc/ssh/sshd_config
# 找到PasswordAuthentication 并修改为
PasswordAuthentication yes 
# wq!保存退出

# 修改完重启 ssh 服务
$ sudo service sshd restart
  1. 给当前用户xx-user添加权限

如果不添加在之后的ssh登陆后,要进入root时,会报xx-user is not in the sudoers file. This incident will be reported.

sudo vi /etc/sudoers
# 找到root ALL=(ALL) ALL这一行
#在起下面添加
test ALL=(ALL) ALL
#添加完后wq!保存退出

6 . 通过 SSH 密码验证登录

ssh xx-user@1.1.1.1
# 输入密码后即可登陆

7 . 通过私钥登录

ssh -i myKey xx-user@1.1.1.1
上一篇 下一篇

猜你喜欢

热点阅读