在CentOS8中设置SSH密钥
首页 专栏 linux 文章详情
0
头图

在CentOS8中设置SSH密钥

刘遄 发布于 4 月 25 日

最流行的两种SSH身份验证机制是基于密码的身份验证和基于公钥的身份验证。使用SSH密钥通常比传统的密码身份验证更安全和方便。
环境
客户端:CentOS8 192.168.43.137

服务端:CentOS8 192.168.43.139

创建SSH公私钥
通过输入以下命令,生成新的4096位的SSH密钥对:

[root@localhost ~]# ssh-keygen -t rsa -b 4096
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:ycOtSDK8ud2kd6EH7OxoQuc1BFb1HJ3T/kvAQJt0LrI [email protected]
The key's randomart image is:
+---[RSA 4096]----+
| ...oo.o o |
| o .+=.+ .|
| . . . +=. o |
| . o.oo .o .|
| + .oSE. . .|
| .*..=o. ..|
| .oo.+o+ . . .|
| .oo== o . |
| .o+ooo |
+----[SHA256]-----+
在CentOS8中设置SSH密钥在CentOS8中设置SSH密钥
想要验证是否生成了新的SSH密钥对,使用ls -l命令查看~/.ssh目录是否有刚才生成的文件:

[root@localhost ~]# ll ~/.ssh/
total 8
-rw------- 1 root root 3389 May 13 08:26 id_rsa
-rw-r--r-- 1 root root 752 May 13 08:26 id_rsa.pub
在CentOS8中设置SSH密钥在CentOS8中设置SSH密钥
将公钥复制到远程服务器,使用ssh-copy-id实用程序,输入远程服务器的root密码:

[root@localhost ~]# ssh-copy-id [email protected]
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
The authenticity of host '192.168.43.139 (192.168.43.139)' can't be established.
ECDSA key fingerprint is SHA256:7O1oIOooh4NZG87aC3v1Zz/vcTXkjOhQBnlkY0CD4y0.
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
Password:

Number of key(s) added: 1

Now try logging into the machine, with: "ssh '[email protected]'"
and check to make sure that only the key(s) you wanted were added.
在CentOS8中设置SSH密钥在CentOS8中设置SSH密钥
也可以使用以下命令复制公钥:

[root@localhost .ssh]# cat ~/.ssh/id_rsa.pub | ssh [email protected] "mkdir -p ~/.ssh && chmod 700 ~/.ssh && cat >> ~/.ssh/authorized_keys && chmod 600 ~/.ssh/authorized_keys"
使用密钥登录服务器
使用以下命令登录ssh服务器:

[root@localhost ~]# ssh 192.168.43.139
Last login: Tue May 12 12:33:41 2020 from 192.168.43.137
在CentOS8中设置SSH密钥在CentOS8中设置SSH密钥

关闭密码认证
登录服务器端,关闭密码认证:

[root@localhost ~]# ssh 192.168.43.139
Last login: Tue May 12 12:33:41 2020 from 192.168.43.137
[root@localhost ~]# vim /etc/ssh/sshd_config
搜索一下三条,将选项改为No
PasswordAuthentication no
ChallengeResponseAuthentication no
UsePAM no
在CentOS8中设置SSH密钥在CentOS8中设置SSH密钥
重启sshd服务:

[root@localhost ~]# systemctl restart sshd
总结
可以使用同一密钥管理多个远程服务器。默认情况下,SSH的端口是TCP 22。更改默认SSH端口可降低自动攻击的风险。

linux 运维 centos ubuntu docker
阅读 65 发布于 4 月 25 日
举报
收藏
分享
本作品系原创, 采用《署名-非商业性使用-禁止演绎 4.0 国际》许可协议
avatar
刘遄

《Linux就该这么学》书籍作者,RHCA认证架构师,教育学(计算机专业硕士)。

7 声望
3 粉丝
关注作者
0 条评论
得票数 最新
提交评论
avatar
刘遄

《Linux就该这么学》书籍作者,RHCA认证架构师,教育学(计算机专业硕士)。

7 声望
3 粉丝
关注作者
宣传栏
目录

最流行的两种SSH身份验证机制是基于密码的身份验证和基于公钥的身份验证。使用SSH密钥通常比传统的密码身份验证更安全和方便。
环境
客户端:CentOS8 192.168.43.137

服务端:CentOS8 192.168.43.139

创建SSH公私钥
通过输入以下命令,生成新的4096位的SSH密钥对:

[root@localhost ~]# ssh-keygen -t rsa -b 4096
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:ycOtSDK8ud2kd6EH7OxoQuc1BFb1HJ3T/kvAQJt0LrI [email protected]
The key's randomart image is:
+---[RSA 4096]----+
| ...oo.o o |
| o .+=.+ .|
| . . . +=. o |
| . o.oo .o .|
| + .oSE. . .|
| .*..=o. ..|
| .oo.+o+ . . .|
| .oo== o . |
| .o+ooo |
+----[SHA256]-----+
在CentOS8中设置SSH密钥在CentOS8中设置SSH密钥
想要验证是否生成了新的SSH密钥对,使用ls -l命令查看~/.ssh目录是否有刚才生成的文件:

[root@localhost ~]# ll ~/.ssh/
total 8
-rw------- 1 root root 3389 May 13 08:26 id_rsa
-rw-r--r-- 1 root root 752 May 13 08:26 id_rsa.pub
在CentOS8中设置SSH密钥在CentOS8中设置SSH密钥
将公钥复制到远程服务器,使用ssh-copy-id实用程序,输入远程服务器的root密码:

[root@localhost ~]# ssh-copy-id [email protected]
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
The authenticity of host '192.168.43.139 (192.168.43.139)' can't be established.
ECDSA key fingerprint is SHA256:7O1oIOooh4NZG87aC3v1Zz/vcTXkjOhQBnlkY0CD4y0.
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
Password:

Number of key(s) added: 1

Now try logging into the machine, with: "ssh '[email protected]'"
and check to make sure that only the key(s) you wanted were added.
在CentOS8中设置SSH密钥在CentOS8中设置SSH密钥
也可以使用以下命令复制公钥:

[root@localhost .ssh]# cat ~/.ssh/id_rsa.pub | ssh [email protected] "mkdir -p ~/.ssh && chmod 700 ~/.ssh && cat >> ~/.ssh/authorized_keys && chmod 600 ~/.ssh/authorized_keys"
使用密钥登录服务器
使用以下命令登录ssh服务器:

[root@localhost ~]# ssh 192.168.43.139
Last login: Tue May 12 12:33:41 2020 from 192.168.43.137
在CentOS8中设置SSH密钥在CentOS8中设置SSH密钥

关闭密码认证
登录服务器端,关闭密码认证:

[root@localhost ~]# ssh 192.168.43.139
Last login: Tue May 12 12:33:41 2020 from 192.168.43.137
[root@localhost ~]# vim /etc/ssh/sshd_config
搜索一下三条,将选项改为No
PasswordAuthentication no
ChallengeResponseAuthentication no
UsePAM no
在CentOS8中设置SSH密钥在CentOS8中设置SSH密钥
重启sshd服务:

[root@localhost ~]# systemctl restart sshd
总结
可以使用同一密钥管理多个远程服务器。默认情况下,SSH的端口是TCP 22。更改默认SSH端口可降低自动攻击的风险。