在 Ubuntu 系统中,默认是禁止 root 用户通过 SSH 登录的。如果你确实需要启用 root 远程 SSH 登录(请务必注意安全风险),可以按如下步骤进行配置:
一、修改 SSH 配置文件
打开配置文件:
sudo nano /etc/ssh/sshd_config
找到以下配置项,并进行修改(或添加):
PermitRootLogin yes
如果是:
PermitRootLogin prohibit-password
或
PermitRootLogin no
请修改为
yes
。可选:确保
PasswordAuthentication
也开启(如果你是用密码登录):PasswordAuthentication yes
保存并关闭文件。
二、设置 root 密码(如未设置)
如果 root 用户没有密码,设置一个密码:
sudo passwd root
然后输入两次新密码。
三、重启 SSH 服务【注意服务非sshd】
sudo systemctl restart ssh
四、安全建议
⚠️ 开启 root 登录存在严重安全隐患,建议使用以下方式提升安全性:
使用密钥登录而非密码:
将公钥放入
/root/.ssh/authorized_keys
设置
PermitRootLogin without-password
限制允许登录的 IP:
通过防火墙(如 UFW)或 SSH 配置
AllowUsers
使用 fail2ban 防暴力破解
示例配置段(推荐密钥登录):
PermitRootLogin prohibit-password
PasswordAuthentication no
PubkeyAuthentication yes
如你需要开启密码登录测试,但上线后又关闭密码登录,也可以分阶段控制。