创建演示用户并生成 RSA 密钥对
在这一步中,你将首先创建一个专门用于演示 SSH 公钥身份验证的用户账户,然后生成一个 RSA 加密密钥对。这种方法可以确保我们有一个干净的演示环境,而不会影响现有的用户配置。
创建演示用户
首先,让我们创建一个名为 sshuser 的新用户,用于我们的 SSH 演示:
sudo adduser sshuser
系统会提示你设置密码并提供用户信息。在本实验中,请使用 password123 作为密码。你可以按回车键跳过可选字段(全名、房间号等)。
Adding user `sshuser' ...
Adding new group `sshuser' (1001) ...
Adding new user `sshuser' (1001) with group `sshuser' ...
Creating home directory `/home/sshuser' ...
Copying files from `/etc/skel' ...
New password:
Retype new password:
passwd: password updated successfully
Changing the user information for sshuser
Enter the new value, or press ENTER for the default
Full Name []:
Room Number []:
Work Phone []:
Home Phone []:
Other []:
Is the information correct? [Y/n] Y
生成 RSA 密钥对
现在,让我们切换到 sshuser 账户来生成密钥对。这可以确保密钥创建在正确用户的家目录中:
sudo su - sshuser
你现在以 sshuser 身份进行操作。使用 ssh-keygen 命令生成 RSA 密钥对。-t 参数指定要创建的密钥类型,在本例中为 rsa。
ssh-keygen -t rsa
运行命令后,系统会提示你选择保存密钥的位置。默认位置是 ~/.ssh/id_rsa,位于用户家目录下名为 .ssh 的隐藏目录中。按 回车键 接受此默认位置。
Generating public/private rsa key pair.
Enter file in which to save the key (/home/sshuser/.ssh/id_rsa):
接下来,系统会要求你输入密码短语。密码短语为你的私钥增加了额外的安全保护。在本实验中,我们使用 sshkey-password 作为密码短语。你需要输入两次。
注意:当你输入密码短语时,屏幕上不会显示任何字符。这是标准的安全性设计。
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/sshuser/.ssh/id_rsa
Your public key has been saved in /home/sshuser/.ssh/id_rsa.pub
The key fingerprint is:
SHA256:<FINGERPRINT_STRING> sshuser@ubuntu
The key's randomart image is:
+---[RSA 3072]----+
| .. . .o.. |
| . . . o.. |
| . . = . . |
| . B o . |
| S= * . |
| o*B o . |
| .oO=B . . |
| .o+*+oE. . |
| .o*o=o.o. |
+----[SHA256]-----+
密钥生成后,你可以验证文件是否已正确创建。使用 ls -l 命令列出 ~/.ssh 目录的内容:
ls -l ~/.ssh
你应该会看到类似以下内容的输出,显示了你的新密钥文件:
total 8
-rw------- 1 sshuser sshuser 2610 Jun 30 10:30 id_rsa
-rw-r--r-- 1 sshuser sshuser 575 Jun 30 10:30 id_rsa.pub
请注意文件权限。私钥 id_rsa 仅对所有者具有读/写权限,而公钥 id_rsa.pub 可以被其他人读取。保护好你的私钥且永不共享是至关重要的。