ubuntu24.04开启root登录

一、如果是本地(服务器键盘/控制台)登录 root

只需设置 root 密码,一条命令即可:

sudo passwd root

输入两次新密码后,退出当前用户就能用 root 登录了。

二、如果是 SSH 远程登录 root

1. 设置 root 密码

sudo passwd root

2. 修改 SSH 配置

sudo nano /etc/ssh/sshd_config

找到并改成(默认前面带 #,去掉注释):

PermitRootLogin yesPasswordAuthentication yes

3. 检查覆盖配置(Ubuntu 24.04 关键一步)

ls /etc/ssh/sshd_config.d/

SSH 规则是先读到的配置生效,这个目录下的文件优先于主配置文件。如果里面有 50-cloud-init.conf 之类写死了 PasswordAuthentication no,主配置里改了也不生效。最省事的做法是新建一个排序靠前的文件:

sudo nano /etc/ssh/sshd_config.d/00-root-login.conf

写入:

PermitRootLogin yesPasswordAuthentication yes

4. 重启 SSH 服务

sudo systemctl restart ssh

5. 验证配置是否生效

sudo sshd -T | grep -E 'permitrootlogin|passwordauthentication'

输出显示 permitrootlogin yes 和 passwordauthentication yes 就说明成功了,然后就可以用 root + 密码远程登录。

三、安全提醒(重要)

  • root 密码登录暴露在公网风险很高,会被大量暴力破解扫描。强烈建议:
    • 设置 16 位以上长复杂密码,并安装 fail2ban 自动封禁爆破 IP;
    • 或者更安全的做法:只用密钥登录(PermitRootLogin prohibit-password),禁用密码方式。

Share:

admin writes for freebak.

1 comment

  • A WordPress Commenter November 11, 2026 at 3:53 am

    Hi, this is a comment.
    To get started with moderating, editing, and deleting comments, please visit the Comments screen in the dashboard.
    Commenter avatars come from Gravatar.

    Reply

Leave a comment