IBM Cloud Docs
针对远程主机认证生成并使用 SSH 密钥

针对远程主机认证生成并使用 SSH 密钥

SSH 密钥是一种用于向使用公用密钥密码术和提问应答认证的 SSH 服务器进行自身识别的方式。 跟传统的密码认证相比,此方法的显著优势在于无需通过网络发送密码即可由服务器进行认证。 此外,因为它支持无人照管的服务器通信,所以还可以实现自动化。

在 Linux 上生成 SSH 密钥

要在 Linux 服务器上生成 SSH 密钥,请运行命令 ssh-keygen。 如果要定制生成的密钥类型以及用于生成密钥的签名算法,那么该命令可以使用标志。 此示例会生成没有口令的标准 2048 位 RSA 密钥。 此命令提示您输入用于存储密钥的位置 (缺省值为 $HOME/.ssh/) 和用于保护 SSH 密钥的口令。

root@bck2:/etc# ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx root@bck2.example.com
The key's randomart image is:
+---[RSA 2048]----+
|.  oo*%=+o..     |
|.++.oX+=. .      |
|..+ooo=. .       |
|   E.+. .o.      |
|    +   S..+     |
|     . +. =      |
|      o  = o     |
|      .o+ +      |
|       +o.       |
+----[SHA256]-----+

将公用密钥复制到远程主机

通过使用公用 SSH 密钥来使用 ssh-copy-id 命令向远程主机进行认证。 使用 -i 标记来指定要复制到远程主机的公用密钥。

root@bck2: # ssh-copy-id -i /root/.ssh/id_rsa.pub root@10.176.18.15
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
The authenticity of host '10.176.18.15 (10.176.18.15)' can't be established.
ECDSA key fingerprint is SHA256:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.

Are you sure you want to continue connecting (yes/no)? yes
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
root@10.176.18.15's password:

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh 'root@10.176.18.15'"
and check to make sure that only the key(s) you wanted were added.

ssh-copy-id 命令将密钥附加到远程主机 .ssh/authorized_key 文件。

测试是否已正确复制密钥

要测试公用密钥是否已正确复制到远程主机,请 ssh 到远程主机。

root@bck2:/etc# ssh root@10.176.18.15
Welcome to Ubuntu 16.04.2 LTS (GNU/Linux 4.4.0-53-generic x86_64)

* Documentation:  https://help.ubuntu.com
* Management:     https://landscape.canonical.com
* Support:        https://ubuntu.com/advantage

Get cloud support with Ubuntu Advantage Cloud Guest:
http://www.ubuntu.com/business/services/cloud

0 packages can be updated.
0 updates are security updates.

Last login: Fri Feb 10 16:51:51 2017 from 169.46.3.91
root@bck1:~#

当您通过 SSH 登录到远程主机时,系统不会提示您输入密码。

带口令的 SSH 密钥

为 SSH 密钥提供口令可提供额外的安全层,但在运行需要受保护密钥的自动化脚本时也会导致问题。

ssh-agent 可以管理密钥。 您输入口令一次。 ssh-agent 会将密钥保存在内存中,并在需要时将其拉出。 如果您希望 ssh-agent 管理密钥,请发出以下命令:

eval $(ssh-agent)

在程序启动 ssh-add 命令以将公用密钥添加到代理程序之后, ssh-add 实用程序将搜索缺省密钥名称 (其中 id_rsa 是缺省密钥名称) ,并将它们添加到 ssh-agent。 在您输入密码之后,会使用 ssh-agent 存储“已解锁的”密钥,并可在针对其他服务器进行认证时使用该密钥。

root@bck1:~# ssh-add
Enter passphrase for /root/.ssh/id_rsa:
Identity added: /root/.ssh/id_rsa (/root/.ssh/id_rsa)
root@bck1:~#

每次打开新的终端会话时,都会提示您输入密钥口令。 请考虑运行以下命令来附加 .bash_profile 文件,以便 ssh-agent 从每个 bash 会话开始并添加密钥。

echo ‘eval $(ssh-agent)’ >> ~/.bash_profile
echo ‘ssh-add’ >> ~/.bash_profile