最流行的两种SSH身份验证机制是基于密码的身份验证和基于公钥的身份验证 。使用SSH密钥通常比传统的密码身份验证更安全和方便 。
文章插图
环 境
客户端:centos8 192.168.43.137
服务端:CentOS8 192.168.43.139
创建SSH公私钥
通过输入以下命令,生成新的4096位的SSH密钥对:
[root@localhost ~]# ssh-keygen -t rsa -b 4096Generating 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 root@localhost.localdomainThe key's randomart image is:+---[RSA 4096]----+| ...oo.o o || o .+=.+ .|| . . . +=. o || . o.oo .o .|| + .oSE. . .|| .*..=o. ..|| .oo.+o+ . . .|| .oo== o . || .o+ooo |+----[SHA256]-----+
<以上代码可复制粘贴,可往左滑>想要验证是否生成了新的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
<以上代码可复制粘贴,可往左滑>将公钥复制到远程服务器,使用ssh-copy-id实用程序,输入远程服务器的root密码:
[root@localhost ~]# ssh-copy-id root@192.168.43.139/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 keysPassword:Number of key(s) added: 1Now try logging into the machine, with: "ssh 'root@192.168.43.139'"and check to make sure that only the key(s) you wanted were added.
<以上代码可复制粘贴,可往左滑>文章插图
也可以使用以下命令复制公钥:
【教你在CentOS8中设置SSH密钥】
[root@localhost .ssh]# cat ~/.ssh/id_rsa.pub | ssh root@192.168.43.139 "mkdir -p ~/.ssh && chmod 700 ~/.ssh && cat >> ~/.ssh/authorized_keys && chmod 600 ~/.ssh/authorized_keys"
<以上代码可复制粘贴,可往左滑>使用密钥登录服务器
使用以下命令登录ssh服务器:
[root@localhost ~]# ssh 192.168.43.139Last login: Tue May 12 12:33:41 2020 from 192.168.43.137
<以上代码可复制粘贴,可往左滑>文章插图
关闭密码认证
登录服务器端,关闭密码认证:
[root@localhost ~]# ssh 192.168.43.139Last login: Tue May 12 12:33:41 2020 from 192.168.43.137[root@localhost ~]# vim /etc/ssh/sshd_config搜索一下三条,将选项改为NoPasswordAuthentication noChallengeResponseAuthentication noUsePAM no
推荐阅读
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- 教你如何查看linux当前shell
- 快速对齐word中的姓名,不需要敲空格!教你一招2秒搞定简单
- 黑客常说的Kali Linux 是什么?附Kali Linux 系统安装详细教程
- HTML背景色教程–如何更改Div背景色,并通过代码示例进行了说明
- 破解6位密码只需4秒!教你3步设置强密码挡住黑客
- 升级仅需5分钟,台式机和笔记本电脑升级Wi-Fi 6图文教程
- 教师节送老师什么礼物合适?
- 淘宝店铺盗图怎么申诉 淘宝被投诉盗图怎么办?教您一招搞定!
- 发工资同事要你请客,低情商的只会“哭穷”,教你高情商这样做
- 一分钟教你看懂汽车所有按键,很实用