zl程序教程

您现在的位置是:首页 >  系统

当前栏目

CentOS 7 禁止 root 直接登陆 及 修改默认端口

centos 修改 默认 端口 直接 root 登陆 禁止
2023-09-14 09:06:42 时间

一、禁止 Root 直接登陆

1、新建登陆用户

useradd tom   # 添加用户

passwd tom    # 设置密码

2、修改 sshd 配置文件

vi /etc/ssh/sshd_config


# 修改内容
PermitRootLogin no      # 改成 no
Port 8222               # 改端口号   

3、重启 sshd

systemctl restart sshd

4、登陆 通过 tom 账号登陆后,在切换 root

su - root

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

1、生成

ssh-keygen

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 登陆

6、其它 Linux 主机登陆

先在 客户端 生成密钥,然后把 id_rsa.pub 拷到 服务器端 authorized_keys 中

注意:authorized_keys 文件的权限为 644