ubuntu22/24/26允许使用root登录设置
1, 给 root 设置登录密码(必须,否则 root 密码登录直接失败)
sudo passwd root
# 输入两次自定义root密码
2, 修改配置文件 /etc/ssh/sshd_config
vi /etc/ssh/sshd_config # 修改/确保以下配置,去掉#注释 PermitRootLogin yes PasswordAuthentication yes PubkeyAuthentication yes
校验语法并重启
sudo sshd -t # 无任何输出=语法正常,再重启ssh sudo systemctl restart ssh
验证配置是否真正生效(判断有没有改成功)
# 查看最终运行的配置 sshd -T | grep -E "permitrootlogin|passwordauthentication"
正确输出:
permitrootlogin yes
passwordauthentication yes
3, 修改并重启ssh之后,如果 ubuntu26.04仍然不能用root远程登录,继续下面的配置:
步骤 1:注释掉 cloudimg 自带的禁用规则(核心,24 版本没有这一步)
# 注释PasswordAuthentication no、PermitRootLogin prohibit-password sudo sed -i 's/PasswordAuthentication no/#PasswordAuthentication no/' /etc/ssh/sshd_config.d/60-cloudimg-settings.conf sudo sed -i 's/PermitRootLogin prohibit-password/#PermitRootLogin prohibit-password/' /etc/ssh/sshd_config.d/60-cloudimg-settings.conf
步骤 2:新建最高优先级自定义配置(99 开头,数字越大优先级越高,覆盖所有)
sudo tee /etc/ssh/sshd_config.d/99-k8s-custom.conf <<'EOF' PermitRootLogin yes PasswordAuthentication yes PubkeyAuthentication yes ChallengeResponseAuthentication yes UsePAM yes EOF
步骤 3:主配置文件兜底(和 Ubuntu24 操作一致)
vi /etc/ssh/sshd_config # 修改/确保以下配置,去掉#注释 PermitRootLogin yes PasswordAuthentication yes PubkeyAuthentication yes
步骤 4:校验配置语法(改错会直接锁死 VNC,必执行)
sudo sshd -t # 无任何输出=语法正常,再重启ssh sudo systemctl restart ssh
4. 如果报连接拒绝 Network error: Connection refused,是没有开放22端口,或者没有安装openssh-server
1) 安装openssh,防火墙放行22端口
# 安装ssh服务(新版最小镜像默认不带) sudo apt update && sudo apt install openssh-server -y # 放行防火墙22端口 sudo ufw allow 22/tcp # 开机自启并启动ssh sudo systemctl enable --now ssh # 验证端口监听,有:22输出即正常 ss -tulpn | grep ssh
2) 云服务器安全组放行 22 端口

浙公网安备 33010602011771号