如何在Jenkins中设置无密码的SSH登录?
最编程
2024-08-03 12:49:57
...
说明
jenkins构建时需要连接远程服务器并且执行脚本,这时就需要配置ssh免密钥登录。jenkins构建时都是以jenkins用户进行操作,所以要以jeknins用户来配置免密钥登录。本教程以用户名qiaofeng为例说明。 A:192.168.10.78, B:192.168.10.76,需求:A登录B时免密钥。
命令
ssh-keygen -t rsa 产生公钥与私钥对
ssh-copy-id 将本机的公钥复制到远程机器的authorized_keys文件中
步骤
1. 切换到qiaofeng用户
su - qiaofeng
2. 用ssh-keygen -t rsa产生密钥对
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
[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 image
is
:
+
-
-
[ RSA
2048
]
-
-
-
-
+
| |
| |
| . |
| .
+
.. |
| S.
+
. |
| . ...o . |
| ..O o |
|
*
.
=
.o. |
| oE
=
+
ooo |
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
|
3. 用ssh-copy-id将公钥从A复制到B
1
2
3
4
5
|
[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
:
|