1、確保本地local_host和遠程remote_host都安裝了openssh
2、local_host創建ssh密鑰對:ssh-keygen -t rsa, 然后一路enter
3、remote_host創建.ssh目錄和authorized_keys文件,然后修改權限
mkdir -p ~/.ssh && touch ~/.ssh/authorized_keys
chmod 700 ~/.ssh && chmod 600 ~/.ssh/authorized_keys
4、將local_host創建的公鑰rsa_rsa.pub傳到remote_host的authorized_keys里面。可通過scp:
scp ~/.ssh/id_rsa.pub username@remote_host:~/.ssh/authorized_keys
也可通過ssh-copy-id命令(建議):
ssh-copy-id username@remote_host
5、ssh-copy-id執行成功后即可在本地主機上使用ssh免密登陸遠程主機。之后可以禁用遠程主機的密碼登陸:
vim /etc/ssh/sshd_config
修改這一行為 no
PasswordAuthentication yes
PasswordAuthentication no