jenkins配置ssh免密码登录

 

说明

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

 

步骤

 

1. 切换到qiaofeng用户

    su - qiaofengssh

 

2. 用ssh-keygen -t rsa产生密钥对

[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 |
+-----------------+

 

3. 用ssh-copy-id将公钥从A复制到B

[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命令。教程

 

4. 验证登陆

[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

相关文章
相关标签/搜索