jenkins構建時須要鏈接遠程服務器而且執行腳本,這時就須要配置ssh免密鑰登陸。jenkins構建時都是以jenkins用戶進行操做,因此要以jeknins用戶來配置免密鑰登陸。本教程以用戶名qiaofeng爲例說明。 A:192.168.10.78, B:192.168.10.76,需求:A登陸B時免密鑰。python
ssh-keygen -t rsa 產生公鑰與私鑰對服務器
ssh-copy-id 將本機的公鑰複製到遠程機器的authorized_keys文件中dom
su - qiaofengssh
[qiaofeng@jenkins ~]$ ssh-keygen -t rsa Generating public/private rsa key pair. Enter file in which to save the key (/home/qiaofeng/.ssh/id_rsa): Enter passphrase (empty for no passphrase): Enter same passphrase again: Your identification has been saved in /home/qiaofeng/.ssh/id_rsa. Your public key has been saved in /home/qiaofeng/.ssh/id_rsa.pub. The key fingerprint is: 56:3e:89:fc:6f:8c:4d:06:ee:5f:b9:6b:a3:6a:d9:e2 qiaofeng@jenkins The key's randomart p_w_picpath is: +--[ RSA 2048]----+ | | | | | . | | . +.. | | S.+. | | . ...o . | | ..O o | | *.=.o. | | oE=+ooo | +-----------------+
[qiaofeng@jenkins ~]$ ssh-copy-id -i ~/.ssh/id_rsa.pub "-p 22223 root@192.168.10.76" root@192.168.10.76's password: Now try logging into the machine, with "ssh '-p 22223 root@192.168.10.76'", and check in: .ssh/authorized_keys to make sure we haven't added extra keys that you weren't expecting.
ssh-copy-id 將key寫到遠程機器的 ~/ .ssh/authorized_key.文件中。ide
若是你遠程服務器SSH端口沒有修改過,能夠不用加-p 22223,要特別注意雙引號。spa
固然你複製文件到遠程服務器也能夠用SCP命令。教程
[qiaofeng@jenkins ~]$ ssh -p 22223 root@192.168.10.76ip
Last login: Mon Apr 17 15:10:04 2017 from 192.168.10.78jenkins
[root@hundsun ~]#it
1. 如今實現了需求,若是你想B訪問A時也免密鑰,只要在B上按上面的步驟來。
2. 若是添加指紋的時候提示添加失敗,是由於你之前添加過了這個ip的指紋。
解決辦法:將.ssh目錄的known_hosts文件刪除掉。也能夠打開這個文件把對應ip的那條記錄刪除。
3. 注意文件權限的問題
A
[qiaofeng@jenkins ~]$ ll .ssh/
-rw-------. 1 qiaofeng qiaofeng 1671 4月 19 05:10 id_rsa
-rw-r--r--. 1 qiaofeng qiaofeng 398 4月 19 05:10 id_rsa.pub
-rw-r--r--. 1 qiaofeng qiaofeng 417 4月 19 05:08 known_hosts
B
-rw------- 1 root root 1217 Apr 21 10:46 /root/.ssh/authorized_keys