首页 > 系统相关 >Centos 设置密钥登陆并禁用密码

Centos 设置密钥登陆并禁用密码

时间:2022-11-12 00:22:51浏览次数:44  
标签:12 Centos 禁用 rsa centos7 密钥 root id ssh

1:备份文件

cd /etc/ssh/
scp sshd_config sshd_config.bak

2:生成密钥

一路回车按下去就可以了。

[root@centos7-1 ssh]# ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):   #回车
Created directory '/root/.ssh'.
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:ERQjqOd4sCPq0XSZvS6nDPxtqKOQtV/vmP79hbzNd4o root@centos7-1
The key's randomart image is:
+---[RSA 2048]----+
|     ...=.       |
|    .  . o       |
|   .    .        |
|  o . +  .       |
|  .B + .S        |
|.oB.+   .  . .   |
|+o.*  o.    o .  |
|o .o++o* .   *  o|
|.o..+=O=+ ..E +o.|
+----[SHA256]-----+

[root@centos7-1 ssh]# ll /root/.ssh/
total 8
-rw-------. 1 root root 1675 Jun 12 10:37 id_rsa
-rw-r--r--. 1 root root 396 Jun 12 10:37 id_rsa.pub

3:安装公钥

[root@centos7-1 ssh]# cd /root/.ssh/
[root@centos7-1 .ssh]# cat id_rsa.pub >> authorized_keys
[root@centos7-1 .ssh]# chmod 600 authorized_keys
[root@centos7-1 .ssh]# chmod 700 ~/.ssh
[root@centos7-1 .ssh]# ll
total 12
-rw-------. 1 root root  396 Jun 12 10:39 authorized_keys
-rw-------. 1 root root 1675 Jun 12 10:37 id_rsa
-rw-r--r--. 1 root root  396 Jun 12 10:37 id_rsa.pub

4:修改文件

#删除以下配置行

[root@centos7-1 ~]# sed -i '/PasswordAuthentication.*/d' /etc/ssh/sshd_config
[root@centos7-1 ~]# sed -i '/PubkeyAuthentication.*/d' /etc/ssh/sshd_config
[root@centos7-1 ~]# sed -i '/RSAAuthentication.*/d' /etc/ssh/sshd_config
[root@centos7-1 ~]# sed -i '/AuthorizedKeysFile.*/d' /etc/ssh/sshd_config

#新增以下配置行
[root@centos7-1 ~]# cat >>/etc/ssh/sshd_config<<EOF
PasswordAuthentication no
PubkeyAuthentication yes
RSAAuthentication yes
AuthorizedKeysFile /root/.ssh/authorized_keys

EOF

5:重启SSHD服务

[root@centos7-1 .ssh]# systemctl restart sshd

6:尝试密钥登陆

将文件:id_rsa 下载至本地保存

[root@centos7-1 .ssh]# ll
total 12
-rw-------. 1 root root  396 Jun 12 10:39 authorized_keys
-rw-------. 1 root root 1675 Jun 12 10:37 id_rsa
-rw-r--r--. 1 root root  396 Jun 12 10:37 id_rsa.pub

打开windows中xshell,尝试使用密钥进行连接

 

 可以看到如上页面,已经不允许通过密码进行登陆了,我们浏览将刚刚下载保存的文件密钥上传至登陆界面,然后点击确定即可登陆。

 

 

 

 

 

 

如有问题请留言,看到后会及时回复!

 

标签:12,Centos,禁用,rsa,centos7,密钥,root,id,ssh
From: https://www.cnblogs.com/xlei/p/16882511.html

相关文章

  • CentOS7升级Glibc到超过2.17版本无法启动的解决办法
    CentOS7升级Glibc到超过2.17版本无法启动的解决办法背景今天有同事告知服务器宕机无法启动.提示信息为:[sda]Assumingdrivecache:writethrough后系统没有任何反......
  • Installing Mosquitto Under CentOS【转】
       ItwasdecidedthatCEITwasinneedofalocalMQTTservertoavoidhavingtoplayinthemessthatisIBM'srealtimeserver.SoIinstalledoneonthe......
  • centos7安装docker
    环境查看centos需要7版本以上,内核最好3.10以上1.查看Linux版本:rpm-qcentos-release2.查看内核版本:uname-r安装卸载旧组件sudoyumremovedocker\......
  • 通过禁用Ping包响应、使用密钥登录等方式来提高Linux服务端系统安全
    有时候我们成功登录到Linux服务器时,会看到控制台会有类似于这样的打印Lastfailedlogin:FriNov1112:59:55CST2022fromxxx.xxx.xxx.xxxonssh:xxxTherewas999......
  • centos安装zookeeper服务
    zookeeper介绍ZooKeeper是一个开源的分布式协调服务。它是一个为分布式应用提供一致性服务的软件,分布式应用程序可以基于Zookeeper实现诸如数据发布/订阅、负载均衡、命......
  • CentOS 7 升级OpenSSH 9.1p1记录
    因服务器被扫描出漏洞,需要对OpenSSH升级,遇到一些波折,记录如下。安装配置telnet服务OpenSSH用于远程登录,一旦升级失败用不了,将无法远程登录,安装telnet-server备用。yumi......
  • CentOS中卸载安装JAVA
    一、卸载CentOS中原有JAVA1、查看原有JDKrpm-qa|grepjava2、卸载JDKsudoyum-yremovejava-1.6.0-openjdk-1.6.0.41-1.13.13.1.el6_8.x86_64sudoyum-yrem......
  • centos8下mysql创建新用户
    1、进入docker下mysqldockerexec-it容器ID/bin/bashmysql-uroot-p2、修改用户密码UPDATEuserSETpassword=PASSWORD('newpassword')whereUSER......
  • Linux Centos7 部署步骤 mysql
    0.首先查看cpu架构uname-a输出内容中有关键词ARM或aarch64就是ARM架构,有关键词x86_64就是X86架构1.下载mysql8(https://dev.mysql.com/)选择 RedHat......
  • CentOS 7 安装 ElasticSearch 8.5
    参考:InstallElasticsearchfromarchiveonLinuxorMacOS|ElasticsearchGuide[8.5]|ElasticInstallKibanafromarchiveonLinuxormacOS|KibanaGuide[......