1、Jenkins所在服務器安裝ssh
yum install ssh -y
將SSH服務設爲開機自啓
sudo systemctl enable sshd
2、生成**對
ssh-****** -t rsa
// **對生成路徑,默認爲/root/.ssh/id_rsa,可以手動輸入其他地址
Enter file in which to save the key (/root/.ssh/id_rsa):
// 這兩項可以直接敲enter
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
生成結果如下:
進入 /var/lib/jenkins/.ssh中,查看生成的**對
這裏可以看到生成了兩個文件
is_rsa 爲**
id_rsa.pub 爲公鑰
3、將公鑰傳輸到目標Linux服務器
// 這裏大家自行調整公鑰文件路徑和目標服務器IP地址
ssh-copy-id -i /root/.ssh/id_rsa.pub [email protected]
測試一下ssh免密登錄
ssh 10.51.xx.xxx "pwd"
注:ssh免密登錄第一次需要手動輸入密碼,並將這些信息保存到/root/.ssh/known_hosts中,以後就不用再輸入密碼,可以直接免密登錄了。
可以看到免密登錄配置成功了。
4、Jenkins中配置Publish over SSH
打開Manage Jenkins -> Configure System,在頁面最下方有一個Publish over SSH的配置區域
生成時沒有密碼的話,Passphrase可以不用輸入
我們只需要在Path to key 欄配置上剛纔生成的**文件路徑,然後就可以添加SSH Servers了。
在SSH Server中添加我們的傳輸了公鑰的目標Linux服務器。
點擊下方的Test Configuration,測試免密連接是否配置成功
輸出Success則說明配置成功了。
如果需要添加更多的SSH Servers則重複第3步及其後續步驟即可。