- 設置當前用戶及root用戶的密碼
sudo passwd xx-user
# 輸入新密碼
sudo passwd root
# 輸入新密碼(建議保持一樣)
- 在本地生成私鑰和公鑰
cd ~/.ssh
ssh-keygen -f myKey
。。。
# 此時會生成 公鑰 myKey.pub 和 私鑰 myKey
- 復制公鑰及導入公鑰
- 復制公鑰
cat myKey.pub
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCjHKPaeglRVJzAhNq+W
中間部分省略。。。
dKx8sJ0Rw4kUqm2eU2vo8S5IEA0Nk2f7BtVGE8VOCHgmDbv2tLp9845UVp1 user@computer-name.local
# 全復制下來,把其中的 user@machin-1 改為你在瀏覽器 SSH 登入之后的當前用戶名 xx-user
- 導入公鑰
# 進入谷歌雲平台頁面 -> 計算引擎 -> 元數據 -> SSH 密鑰,粘貼保存即可
# 谷歌就會把上面這段 public key 寫入到 ~/.ssh/authorized_keys
- 添加密碼驗證登陸
sudo vi /etc/ssh/sshd_config
# 找到PasswordAuthentication 並修改為
PasswordAuthentication yes
# wq!保存退出
# 修改完重啟 ssh 服務
$ sudo service sshd restart
- 給當前用戶
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
這一行
在起下面添加xx-user ALL=(ALL) ALL
(這里的xx-user
是你的用戶名),添加完后wq!
保存退出
- 通過 SSH 密碼驗證登錄
ssh xx-user@1.1.1.1
# 輸入密碼后即可登陸
- 通過私鑰登錄
ssh -i myKey xx-user@1.1.1.1