CentOS8中怎么设置SSH密钥
短信预约 -IT技能 免费直播动态提醒
小编给大家分享一下CentOS8中怎么设置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:7O1oIOooh5NZG87aC3v1Zz/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.
也可以使用以下命令复制公钥:
[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
重启sshd服务:
[root@localhost ~]# systemctl restart sshd
以上是“CentOS8中怎么设置SSH密钥”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注编程网行业资讯频道!
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341