在Ubuntu 18.04中配置使用SSH Keys进行远程登录

使用 ssh 远程登录 Linux 时,使用公钥认证的方式比采用密码的方式更安全、更方便。本文简单的介绍如何在 Ubuntu 18.04 Server 中设置如何公钥认证方式登录。

本文假设的环境, 服务器OS: Ubuntu 18.04 Server, 其IP地址为: 192.168.31.60, 服务器已经配置了一个名为 stu 的用户,并通过一台安装 Ubuntu 18.04 的客户机来准备环境

在客户机上生成密钥对

在客户机上打开一个终端窗口,进入当前用户的用户目录。首先准备一个 .ssh 目录用来存放后面要生成的 key 的信息。顺序执行以下命令:

1
2
mkdir -p $HOME/.ssh
chmod 0700 $HOME/.ssh

准备好目录以后,执行以下命令生成 key:

1
ssh-keygen -t rsa -b 4096 -C "master-srv-01"

程序会提示一些问题,全面选择默认设置(回车即可)。 完成以后,系统显示:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
Your identification has been saved in /home/stu/.ssh/id_rsa.
Your public key has been saved in /home/stu/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:ppKqA/AMnFd1V1+9Y6qPIQz7EHAjr2XgB+O+T//4z30 master-srv-01
The key's randomart image is:
+---[RSA 4096]----+
| .. . ... o|
| . . . . o|
|. . .* o ..|
|oo .o O . + |
|.+. o BS o .|
|. o ..=o= . |
|. oo.+ o .. |
|. . .o + o.+ . E|
|.o. ... +o+o+ ..|
+----[SHA256]-----+

生成以后,可以查看 .ssh 目录,可以看到生成了相关的文件,执行:

1
ls -la .ssh

系统显示:

1
2
-rw------- 1 stu stu 3243 Feb 13 00:36 id_rsa
-rw-r--r-- 1 stu stu 739 Feb 13 00:36 id_rsa.pub

把public key 安装到服务器上

这里,我们是使用 ssh-copy-id 命令来完成把 public key 安装道服务器上。执行

1
ssh-copy-id -i $HOME/.ssh/id_rsa.pub stu@192.168.31.60

系统显示:

1
2
3
4
5
6
7
8
9
10
11
12
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/home/stu/.ssh/id_rsa.pub"
The authenticity of host '192.168.31.60 (192.168.31.60)' can't be established.
ECDSA key fingerprint is SHA256:jCPY99rJzLBUlUpBetgSr0RWbwRs/EGPhgcdJ0DS7bo.
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
stu@192.168.31.60's password:

Number of key(s) added: 1

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

说明安装成功。

测试登录

在客户机上执行:

1
sh stu@192.168.31.60

如果前面的步骤都正常,应该可以登录到服务器上了,系统显示类似以下的信息:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
Welcome to Ubuntu 18.04.3 LTS (GNU/Linux 4.15.0-76-generic x86_64)

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

System information as of Thu Feb 13 00:57:38 UTC 2020

System load: 0.0 Processes: 96
Usage of /: 49.5% of 5.84GB Users logged in: 0
Memory usage: 7% IP address for enp0s3: 192.168.31.60
Swap usage: 0%

* Multipass 1.0 is out! Get Ubuntu VMs on demand on your Linux, Windows or
Mac. Supports cloud-init for fast, local, cloud devops simulation.

https://multipass.run/

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


Last login: Thu Feb 13 00:50:39 2020 from 192.168.31.10

本文标题:在Ubuntu 18.04中配置使用SSH Keys进行远程登录

文章作者:Morning Star

发布时间:2020年02月13日 - 09:02

最后更新:2021年04月16日 - 15:04

原始链接:https://www.mls-tech.info/linux/ubuntu-18-setup-ssh-key-login/

许可协议: 署名-非商业性使用-禁止演绎 4.0 国际 转载请保留原文链接及作者。