Linux : 使用 ssh-keygen 和 ssh-copy-id 执行无密码 SSH 登录的 3 个步骤
如本文所述,可以使用 ssky-keygen 和 ssh-copy-id 通过 3 个简单步骤登录远程 Linux 服务器,而无需输入密码。
ssh-keygen创建公钥和私钥。ssh-copy-id将本地主机的公钥复制到远程主机的authorized_keys 文件中。ssh-copy-id 还为远程主机的 home、~/.ssh 和 ~/.ssh/authorized_keys 分配适当的权限。
本文还解释了使用 ssh-copy-id 的 3 个小烦恼以及如何将 ssh-copy-id 与 ssh-agent 一起使用。
步骤 1:在本地主机上使用 ssh-key-gen 创建公钥和私钥
tiamo@local-host$ [Note: You are on local-host here]
tiamo@local-host$ ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/home/tiamo/.ssh/id_rsa):[Enter key]
Enter passphrase (empty for no passphrase): [Press enter key]
Enter same passphrase again: [Pess enter key]
Your identification has been saved in /home/tiamo/.ssh/id_rsa.
Your public key has been saved in /home/tiamo/.ssh/id_rsa.pub.
The key fingerprint is:
43:b3:fe:af:75:95:18:11:31:d5:de:96:2f:f2:35:f9 tiamo@local-host
第 2 步:使用 ssh-copy-id 将公钥复制到远程主机
tiamo@local-host$ ssh-copy-id -i ~/.ssh/id_rsa.pub remote-host
tiamo@remote-host's password:
Now try logging into the machine, with "ssh 'remote-host'", and check in:
.ssh/authorized_keys
to make sure we haven't added extra keys that you weren't expecting.
注意: ssh-copy-id将密钥附加到远程主机的 .ssh/authorized_key。
第 3 步:无需输入密码即可登录远程主机
tiamo@local-host$ ssh remote-host
Last login: Sun Nov 16 17:22:33 2020 from 192.168.1.2
[Note: SSH did not ask for password.]
tiamo@remote-host$ [Note: You are on remote-host here]
在大多数情况下,上述 3 个简单步骤应该可以完成工作。
我们之前还详细讨论了从openSSH 到 openSSH执行 SSH 和 SCP而不输入密码。
如果您使用的是 SSH2,我们之前讨论了从SSH2 到 SSH2、从OpenSSH 到 SSH2以及从SSH2 到 OpenSSH执行 SSH 和 SCP 的问题。
将 ssh-copy-id 与 ssh-add/ssh-agent 一起使用
没有为选项-I传递任何值--I而且如果〜/ .ssh / identity.pub不可用,则ssh-copy-id将显示以下错误消息。
tiamo@local-host$ ssh-copy-id -i remote-host
/usr/bin/ssh-copy-id: ERROR: No identities found
如果您已使用ssh-add 将密钥加载到ssh-agent,则ssh-copy-id将从ssh-agent获取密钥以复制到远程主机。即,当您不将选项 -i传递给ssh-copy-id时,它会将ssh-add -L命令提供的密钥复制到远程主机。
tiamo@local-host$ ssh-agent $SHELL
tiamo@local-host$ ssh-add -L
The agent has no identities.
tiamo@local-host$ ssh-add
Identity added: /home/tiamo/.ssh/id_rsa (/home/tiamo/.ssh/id_rsa)
tiamo@local-host$ ssh-add -L
ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAsJIEILxftj8aSxMa3d8t6JvM79DyBV
aHrtPhTYpq7kIEMUNzApnyxsHpH1tQ/Ow== /home/tiamo/.ssh/id_rsa
tiamo@local-host$ ssh-copy-id -i remote-host
tiamo@remote-host's password:
Now try logging into the machine, with "ssh 'remote-host'", and check in:
.ssh/authorized_keys
to make sure we haven't added extra keys that you weren't expecting.
[Note: This has added the key displayed by ssh-add -L]
ssh-copy-id 的三个小烦恼
以下是 ssh-copy-id 的一些小烦恼。
- 默认公钥: ssh-copy-id 使用 ~/.ssh/identity.pub 作为默认公钥文件(即当没有值传递给选项 -i 时)。相反,我希望它使用 id_dsa.pub、id_rsa.pub 或 identity.pub 作为默认键。即如果其中任何一个存在,它应该将其复制到远程主机。如果存在两个或三个,则应默认复制 identity.pub。
- 代理没有身份:当ssh-agent正在运行并且ssh-add -L返回“代理没有身份”(即没有向 ssh-agent 添加密钥)时,ssh-copy-id 仍将复制消息“代理没有身份”到远程主机的authorized_keys 条目。
- 授权密钥中的重复条目:我希望 ssh-copy-id 验证远程主机的授权密钥上的重复条目。如果您在本地主机上多次执行 ssh-copy-id,它将继续在远程主机的 authorized_keys 文件上附加相同的密钥,而不检查重复项。即使有重复的条目,一切都按预期工作。但是,我想让我的authorized_keys 文件没有杂乱。
- 点赞
- 收藏
- 关注作者
评论(0)