首页 > 系统相关 >CentOS7禁止 root 直接登陆,只能通过 SSH key 登陆

CentOS7禁止 root 直接登陆,只能通过 SSH key 登陆

时间:2023-03-06 17:23:15浏览次数:51  
标签:rsa CentOS7 登陆 key root localhost ssh

一、禁止 Root 直接登陆

1、新建登陆用户

[root@localhost ~]# useradd haolb
[root@localhost ~]# passwd haolb
Changing password for user haolb.
New password:
BAD PASSWORD: The password is shorter than 8 characters
Retype new password:
passwd: all authentication tokens updated successfully.
[root@localhost ~]#

2、修改 sshd 配置文件,禁用root用户登录

vi /etc/ssh/sshd_config

# 修改内容
PermitRootLogin yes      # 此处请留意 root 用户能否通过 SSH 登录,默认为yes。本人暂不禁用。

3、重启 sshd

systemctl restart sshd

4、登陆 通过 haolb 账号登陆后,再切换 root

su root

二、禁止 Root 密码登陆,只能通过 SSH key 登陆

1、生成 key

[root@localhost ~]# ssh-keygen
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:8mOUzS3bGA/eF/cgY5KihOMtlnqHeVMXUm0qTBeSTRM [email protected]
The key's randomart image is:
+---[RSA 2048]----+
|        .+E+     |
|        o.+.o    |
|       o o o     |
|     .  ++oo     |
|    o o SoO.= o .|
|   . = =.o.% o +.|
|    =oo.+.+ + . .|
|   o+.+. .   .   |
|  .. o .         |
+----[SHA256]-----+
[root@localhost ~]#

2、导入公钥

cat /root/.ssh/id_rsa.pub >> /root/.ssh/authorized_keys

3、设置禁止密码登录

vim /etc/ssh/sshd_config

# 禁止密码登陆
PasswordAuthentication no

4、重启服务

systemctl restart sshd

5、本地 Windows 登陆

把私钥拷贝到本机上,Xshell 登陆

image

参考

https://blog.csdn.net/mshxuyi/article/details/100517924

https://www.cnblogs.com/yzgblogs/p/15191338.html

标签:rsa,CentOS7,登陆,key,root,localhost,ssh
From: https://www.cnblogs.com/haolb123/p/17184623.html

相关文章

  • CentOS7.3中编译clangd
    CentOS7.3中编译clangd在CentOS7.3中进行C++的开发,使用VSCode的WSL模式,需要在CentOS中安装clangd,从GitHub官方发布版本下载的clangd是需要依赖GLIBC2.18的,这样就需要在Cen......
  • centos7.9系统禁用selinux脚本
    临时关闭setenforcepermissive永久关闭配置文件路径/etc/sysconfig/selinux修改配置项SELINUX=enforcing为SELINUX=disabledsed-i's/SELINUX=enforcing/'SEL......
  • [Bugfix] org.yaml.snakeyaml.scanner.ScannerException
    背景SpringBoot的yml文件中使用了Mavenpom.xml中的properties:<properties><active>dev</active></properties>这些properties预期是在mvncompile阶......
  • centos7离线部署ansible并通过ansible批量修改用户密码
    centos7离线部署ansible并通过ansible批量修改用户密码找台可以联网的机器下载好程序包,上传到对应主机mkdir/app/ansible-p#下载bzip2yuminstall-ybzip2--downlo......
  • 聊聊分库分表后非Sharding Key查询的三种方案~(建议收藏)
    对于toC的业务来说,需要选择用户属性如user_id作为分片键。那问题来了,对于订单表来说,选择了user_id作为分片键以后如何查看订单详情呢?比如下面这样一条SQL:SELECT*FROM......
  • centos7在线扩容根分区LVM
    背景:现已有lvm根分区,不能满足业务需求,在业务不下线的情况下对/分区扩容思路:创建新分区=>创建物理卷=>加入卷组=>扩容1、找到要扩容的分区2、找到新添加的磁盘......
  • 修复 Ubuntu 中的 “Key is stored in legacy trusted.gpg keyring” 问题
    正如我之前所说,这是一个警告信息,目前可以忽略。解决这个问题的责任在于外部软件开发者和Ubuntu开发者。如果你在Ubuntu22.04及以后的版本中使用PPA或添加外......
  • selenium+python实例详解:邮箱登陆
    1、访问百度首页的selenium+python示例代码fromseleniumimportwebdriverimporttimedriver=webdriver.Ie()driver.get("http://www.baidu.com")time.sleep(10)driver.c......
  • 使用HashMap的containsKey查找键,时间复杂度为什么是O(1)?
    [1]总览  在Java中,"containsKey"是Map接口中定义的一个方法,用于判断给定的键(key)是否存在于Map中。Map是Java中的一种数据结构,用于存储键值对(key-valuepairs),其中每个键......
  • CentOS7配置iSCSI共享-tgtd
    作者:独笔孤行官网:​​ ​​http://anyamaze.com​​公众号:云实战前言iSCSI又称为IP-SAN,是一种基于因特网及SCSI-3协议下的存储技术,iSCSI利用了TCP/IP的port860和3260......