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