方法一:
ssh-keygen -t rsa -f hello建立公鑰cd ~/.ssh cat hello.pub查看公鑰并復(fù)制到對方服務(wù)器.ssh下方的authority中目標主機之間的~/.ssh目錄下面新建authorized_keys文件
將復(fù)制的公鑰粘貼追加到目標主機的authorized_keys文件即可
(注意:將目標主機下的.ssh權(quán)限為700:chmod 700 -R .ssh,authorized_keys設(shè)為600:chmod 600 .ssh/accio .ssh/authorized_keys)
-
ssh -i hello user@192.168.1.211即可免密碼連接
方法二:
原機器生成秘鑰
ssh-keygen -t hello復(fù)制到新機器上
ssh-copy-id -i ~/.ssh/hello.pub root@192.168.1.211直接免密碼登錄即可
ssh root@192.168.1.211
動手實踐:
進入本地ServerA的usera賬戶下的.ssh目錄
本地設(shè)置公鑰:
ssh-keygen -t rsa -f accio(accio為私鑰名稱)ls -la .ssh此時本地.ssh目錄下會產(chǎn)生 accio(私鑰) accio.pub(公鑰)cat accio.pub查看公鑰
5.將公鑰上傳到serverB并以userb登錄:ssh-copy-id userb@ServerBip
6.此時在查看ServerB中userb的.ssh目錄,會發(fā)現(xiàn)usera的公鑰文件內(nèi)容已經(jīng)復(fù)制到accio目錄下面cat accio.pub;同時也追加到了.ssh/authorized_keys 文件中,內(nèi)容是一致的
7.設(shè)置userb中.ssh權(quán)限為700,chmod 700 -R .ssh,authorized_keys的權(quán)限為600 chmod 600 .ssh/accio .ssh/authorized_keys;并查看ls -la .ssh
8.使用 免密登錄ssh -i accio userb@ServerBip即可
9.也可以通過cat ~/.ssh/id_rsa.pub | ssh -p 22 user@host ‘cat >> ~/.ssh/authorized_keys的方式復(fù)制公鑰
實現(xiàn)原理:
