温馨提示×

centos如何开启ssh root

小樊
38
2026-09-21 00:29:30
栏目: 智能运维

在 CentOS 上开启 SSH 并使用 root 登录,一般包含以下几个步骤(以 CentOS 7 / 8 / Stream 为例):


一、确认并安装 SSH 服务

1. 检查是否已安装

rpm -qa | grep openssh-server

如果没有输出,安装:

yum install -y openssh-server

二、启动并设置 SSH 开机自启

systemctl start sshd
systemctl enable sshd

确认状态:

systemctl status sshd

三、允许 root 通过 SSH 登录(默认可能禁止)

1. 编辑 SSH 配置文件

vi /etc/ssh/sshd_config

找到以下配置(可能被注释):

#PermitRootLogin prohibit-password

修改为:

PermitRootLogin yes

⚠️ 如果是 CentOS 默认,可能是:

PermitRootLogin no

2. 确保以下项没有被禁用

PasswordAuthentication yes

四、重启 SSH 服务使配置生效

systemctl restart sshd

五、防火墙放行 SSH(如开启 firewalld)

firewall-cmd --permanent --add-service=ssh
firewall-cmd --reload

六、确认 SELinux(一般不影响,但排查用)

查看:

getenforce

如果是 Enforcing,一般无需改动。


七、测试连接

从另一台机器:

ssh root@服务器IP

⚠️ 安全建议(很重要)

  • 生产环境不建议直接开启 root SSH 登录
  • 推荐方案:
    • 使用普通用户 + sudo
    • 或修改端口、禁用密码登录、使用密钥登录

如需要,我可以帮你配置 密钥登录 + 禁用密码 + 禁止 root 登录 的安全方案。

0