适用场景:刚装好的 Ubuntu,默认只能用普通用户登录 SSH。想以后直接用 root 登录 FinalShell,而不是每次登录后再手动执行 sudo -i。
本文环境:Ubuntu + FinalShell SSH。
一、确认当前用户有 sudo 权限
先用当前普通用户登录服务器,然后执行:
sudo -v && echo "sudo正常"
如果提示输入密码,就输入当前普通用户的密码。
输入密码时,终端不会显示字符,也不会显示 *,这是正常的。
如果看到:
sudo正常
说明当前用户有管理员权限,可以继续下一步。
二、给 root 设置密码
Ubuntu 默认 root 账号通常没有设置可直接登录的密码,需要先手动设置。
执行:
sudo passwd root
然后按提示输入两次新的 root 密码:
New password:
Retype new password:
passwd: password updated successfully
看到:
passwd: password updated successfully
说明 root 密码已经设置成功。
三、查看当前 SSH 配置
先看一下 SSH 当前关于 root 登录和密码登录的配置:
sudo grep -RniE '^\s*#?\s*(PermitRootLogin|PasswordAuthentication|KbdInteractiveAuthentication|UsePAM)' /etc/ssh/sshd_config /etc/ssh/sshd_config.d 2>/dev/null
常见输出可能类似:
/etc/ssh/sshd_config:33:#PermitRootLogin prohibit-password
/etc/ssh/sshd_config:57:#PasswordAuthentication yes
/etc/ssh/sshd_config:62:KbdInteractiveAuthentication no
/etc/ssh/sshd_config:85:UsePAM yes
/etc/ssh/sshd_config.d/50-cloud-init.conf:1:PasswordAuthentication yes
重点看这两项:
PermitRootLogin
PasswordAuthentication
如果 PermitRootLogin 是注释状态,或者是 prohibit-password,一般代表 root 不能直接使用密码登录 SSH。
四、新建 root 登录配置文件
不建议直接改主配置文件,可以单独新建一个配置文件,后期也方便删除和恢复。
执行:
echo -e "PermitRootLogin yes\nPasswordAuthentication yes" | sudo tee /etc/ssh/sshd_config.d/99-root-login.conf
然后检查文件内容:
sudo cat /etc/ssh/sshd_config.d/99-root-login.conf
正常应该显示:
PermitRootLogin yes
PasswordAuthentication yes
五、检查 SSH 配置有没有写错
这一步很重要,先检查配置语法,不要急着重启 SSH。
执行:
sudo sshd -t
如果没有任何输出,直接回到命令行,说明配置没问题。
如果有报错,就不要继续重载 SSH,先根据报错内容修配置。
六、重新加载 SSH 服务
配置检查通过后,重新加载 SSH 服务:
sudo systemctl reload ssh
然后确认 SSH 服务还在运行:
systemctl is-active ssh
正常应该显示:
active
七、用 FinalShell 新开连接测试 root 登录
注意:先不要关闭当前普通用户的 SSH 窗口,留着当备用窗口。
然后在 FinalShell 里新建一个连接:
主机/IP:服务器 IP
端口:SSH 端口,默认一般是 22
用户名:root
密码:刚才设置的 root 密码
如果连接成功,终端提示符一般会变成:
root@服务器名:~#
注意最后是 #,不是普通用户常见的 $。
这就说明 root 已经可以通过 SSH 直接登录了。
八、以后怎么用
以后 FinalShell 直接使用:
用户名:root
密码:root 密码
就可以登录。
不需要再用普通用户登录后执行:
sudo -i
九、如果以后想关闭 root SSH 登录
如果不想继续允许 root 远程登录,可以删除刚才新建的配置文件:
sudo rm -f /etc/ssh/sshd_config.d/99-root-login.conf
然后检查 SSH 配置:
sudo sshd -t
重新加载 SSH:
sudo systemctl reload ssh
这样就恢复到原来的 SSH 配置逻辑了。
完整命令汇总
sudo -v && echo "sudo正常"
sudo passwd root
sudo grep -RniE '^\s*#?\s*(PermitRootLogin|PasswordAuthentication|KbdInteractiveAuthentication|UsePAM)' /etc/ssh/sshd_config /etc/ssh/sshd_config.d 2>/dev/null
echo -e "PermitRootLogin yes\nPasswordAuthentication yes" | sudo tee /etc/ssh/sshd_config.d/99-root-login.conf
sudo cat /etc/ssh/sshd_config.d/99-root-login.conf
sudo sshd -t
sudo systemctl reload ssh
systemctl is-active ssh
看到:
active
就可以用 FinalShell 新建 root 用户连接测试了。









暂无评论内容