Ubuntu无法使用root用户登录解决方法

共计 769 个字符,预计需要花费 2 分钟才能阅读完成。

当尝试以 root 用户远程登录 Ubuntu 时,提示如下错误信息:

Please login as the user "ubuntu" rather than the user "root"

解决方案:

一、编辑 SSH 配置文件

  1. 打开 SSH 配置文件:
    sudo vim /etc/ssh/sshd_config
    
  2. 修改以下设置:
    # 启用密码认证
    PasswordAuthentication yes
    #允许 root 用户登录
    PermitRootLogin yes
    #禁用 PAM 认证
    UsePAM no
    
  3. 保存并退出编辑后,重启 SSH 服务:
    sudo service ssh restart
    

二、编辑 authorized_keys 文件

    1. 打开 authorized_keys 文件
      sudo vim /root/.ssh/authorized_keys
      
    2. 删除 ssh-rsa 行前面的所有内容
    • 例如:
      no-port-forwarding,no-agent-forwarding,no-X11-forwarding,command="echo 'Please login as the user \"ubuntu\" rather than the user \"root\".';echo;sleep 10;exit 142"
      
    • 其中,命令含义如下:
      • no-port-forwarding:禁止 TCP 转发。
      • no-agent-forwarding:禁止身份验证代理转发。
      • no-X11-forwarding:禁止 X11 转发。
      • command="echo'...'":显示提示信息并终止会话。

三、重新登录

完成上述设置后,重新尝试使用 root 用户登录。如果依然无法登录,可以尝试重启实例。

这样就可以解决 Ubuntu 无法使用 root 用户登录的问题了。

正文完
 0
评论(没有评论)
验证码