* 把遠端主機的自定義主機名aliyun添加到/etc/hosts
xx.xx.xx.xx 寫成自己的遠端主機IP位址
* 登入到遠端主機 建立 ~/.ssh目錄
* 在目前的機器上生成密鑰
一路回車
* 将生成的公鑰複制到遠端主機上的~/.ssh/authorized_keys中 (append)
這種方法不好,~/.ssh/authorized_keys 直接被覆寫了, 應該把生成的公鑰追加到這個檔案
可以先傳到一個臨時檔案
* 測試免密碼登入
還是需要輸入密碼
* 登入到遠端主機檢視log
762:Aug 25 11:28:30 iz2ze5rptd09gfzl0jpc3gz sshd[28259]: Authentication refused: bad ownership or modes for directory /home/mzh/.ssh
763:Aug 25 11:28:30 iz2ze5rptd09gfzl0jpc3gz sshd[28259]: Authentication refused: bad ownership or modes for directory /home/mzh/.ssh
767:Aug 25 11:28:59 iz2ze5rptd09gfzl0jpc3gz sudo: mzh : TTY=pts/0 ; PWD=/home/mzh ; USER=root ; COMMAND=/bin/grep -n Authentication refused: /var/log/secure
* 修改權限
** SSH不希望home目錄和~/.ssh目錄對組有寫權限
drwx------ 20 mzh mzh 4096 Aug 25 11:19 /home/mzh/
** 對 .ssh 檔案夾和 authorized_keys 檔案同樣也要限制
* 再測試登入
Welcome to Alibaba Cloud Elastic Compute Service !
另外一個方法:
https://www.linuxidc.com/Linux/2018-02/150812.htm
一、ssh免密登入
1、測試是否能免密登入
The authenticity of host 'localhost (::1)' can't be established.
2、設定免密登入
1)、去掉 /etc/ssh/sshd_config中的兩行注釋,如沒有則添加,所有伺服器都要設定的:
2)、生成秘鑰:
備注:輸入指令後回車4次
3)、複制到公共密鑰中:
4)、将秘鑰複制到目标伺服器:
5)、測試:(沒有報錯,并沒有提示輸入目标伺服器使用者密碼,使用者切換到目标伺服器使用者名則成功)