我正在尝试使用ssh-agent
远程ssh
服务器。
据我所知,当我将本地主机添加public_key
到远程服务器时./ssh/authorized_keys
,我可以 ssh 进入服务器而无需输入密码。
但是,我尝试使用它来执行此操作,ssh-agent
因为我需要ssh
进入 Jenkins 管道中的远程服务器。在Jenkins上做之前,我尝试在本地做。
我所做的是
eval $(ssh-agent -s)
chmod 600 ./key_key2.key
ssh-add ./key_key2.key
ssh root@<remote_server> docker ps -v
其中key_key2.key
包含private_key
远程服务器的。
这仍然让我输入密码。这是正确的方法吗?
我觉得使用ssh-agent
是跳过将 添加public_key
到远程服务器的部分authorized_keys
,但我认为跳过该部分是使用 的要点之一public_key
。
答案1
假设 ServerA 是您家里的计算机,ServerB 是远程服务器。为了通过公钥/私钥进行连接,我们需要在 ServerA 上创建一组密钥。请注意,我没有输入密码。
ServerA $ ssh-keygen -t rsa -b 4096
Generating public/private rsa key pair.
Enter file in which to save the key (/home/admin/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/admin/.ssh/id_rsa.
Your public key has been saved in /home/admin/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:11tmtkpYjGCPLc8I59kV1QySEYfwpJt76BDMX01aTkU admin@localhost
The key's randomart image is:
+---[RSA 4096]----+
| ..==+=E|
| +o+ .o|
| o . o + |
| + = * O |
| . S B B B |
| + @ B * . |
| = O + . |
| o o . |
| . . |
+----[SHA256]-----+
如您所见,密钥是在 /home/admin/.ssh/id_rsa(私钥)和 /home/admin/.ssh/id_rsa.pub(公钥)生成的。
现在,我们登录到 ServerB 并将 ServerA 中的公钥添加到位于 ~/.ssh/authorized_keys 中的authorized_keys中。
ServerA $ cat ~/.ssh/id_rsa.pub
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAACAQDP4BIiaHUm6Ow9SGFOdHpsaAu2odnBLfDu5YjwQeESLe7ubfQ62v21PXKAJUVS0ZcAN7KfAfHv+f8Los8GE7NhAJUeOB18YpaQohLVaajKb0pSJnAfcYMqH38Ouq3Q23GA61ZqUF4o3pJpRf9O2V/qpIkGQtMZ1/EbYApvh0y+MSlmrDHNjm6n6LO1P31WqsAwzTlZilcaDJTDu3ILW+bd+b1LzMr2oQUPnrc2YIiGRt8PvvM5YcGfN9/4D1vKyLNgF3c+KhbEapsoxfsGmEl1Z+jR/ldep0jwEh7XVXAMrLWhtUdi/a40R0Zisj3b27gfrQLmEMBOS4X0nfDJ9/QoxVl16SwZWxFuDX1xV/n2XIAg1OQ== admin@localhost
将 ServerA 的输出复制并粘贴到 ServerBauthorized_keys 中。
ServerB $ cat ~/.ssh/authorized_keys
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAACAQDP4BIiaHUm6Ow9SGFOdHpsaAu2odnBLfDu5YjwQeESLe7ubfQ62v21PXKAJUVS0ZcAN7KfAfHv+f8Los8GE7NhAJUeOB18YpaQohLVaajKb0pSJnAfcYMqH38Ouq3Q23GA61ZqUF4o3pJpRf9O2V/qpIkGQtMZ1/EbYApvh0y+MSlmrDHNjm6n6LO1P31WqsAwzTlZilcaDJTDu3ILW+bd+b1LzMr2oQUPnrc2YIiGRt8PvvM5YcGfN9/4D1vKyLNgF3c+KhbEapsoxfsGmEl1Z+jR/ldep0jwEh7XVXAMrLWhtUdi/a40R0Zisj3b27gfrQLmEMBOS4X0nfDJ9/QoxVl16SwZWxFuDX1xV/n2XIAg1OQ== admin@localhost
现在在 serverA 上加载 ssh-agent 并添加私钥
eval $(ssh-agent)
ServerA $ ssh-add ~/.ssh/id_rsa
Identity added: /home/admin/.ssh/id_rsa (/home/admin/.ssh/id_rsa)
ServerA $ ssh [email protected]
The authenticity of host '[someIP.com]:2220 ([xx.xxx.xxx.xxx]:2220)' can't be established.
ECDSA key fingerprint is SHA256:K6nMI2Dxb7t6oj00HlKmPCCuGorgQoBJCeXB31B9VTY.
ECDSA key fingerprint is MD5:aa:20:4f:08:bb:5d:c5:8e:fd:cb:4c:5f:b9:3b:44:59.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '[someIP.com]:2220 ([xx.xxx.xxx.xxx]:2220)' (ECDSA) to the list of known hosts.
Last login: Tue Nov 26 21:20:14 2019
admin@mail ~]$