阿里云AlmaLinux操作系统允许root登录配置步骤

先使用almalinux用户,使用密钥登录。

执行sudo -i切换到root

然后上传以下脚本root.sh,执行bash root.sh就允许root用户登录了

bash 复制代码
#!/bin/bash
# 最终修复脚本

echo "=== 开始最终修复 ==="

# 1. 清理配置片段
echo "清理配置片段..."
rm -f /etc/ssh/sshd_config.d/*-password*.conf 2>/dev/null

# 2. 备份并创建干净的配置
cp /etc/ssh/sshd_config /etc/ssh/sshd_config.before_final
cat > /etc/ssh/sshd_config << 'EOF'
Port 22
Protocol 2
HostKey /etc/ssh/ssh_host_rsa_key
HostKey /etc/ssh/ssh_host_ecdsa_key
HostKey /etc/ssh/ssh_host_ed25519_key
SyslogFacility AUTH
LogLevel INFO
LoginGraceTime 120
PermitRootLogin yes
StrictModes yes
PubkeyAuthentication yes
IgnoreRhosts yes
HostbasedAuthentication no
PermitEmptyPasswords no
ChallengeResponseAuthentication yes
PasswordAuthentication yes
X11Forwarding yes
X11DisplayOffset 10
PrintMotd no
PrintLastLog yes
TCPKeepAlive yes
AcceptEnv LANG LC_*
Subsystem sftp /usr/libexec/openssh/sftp-server
UsePAM yes
EOF

# 3. 修复PAM配置
cp /etc/pam.d/sshd /etc/pam.d/sshd.before_final
cat > /etc/pam.d/sshd << 'EOF'
#%PAM-1.0
auth       required     pam_sepermit.so
auth       substack     password-auth
auth       include      postlogin
account    required     pam_nologin.so
account    include      password-auth
password   include      password-auth
session    required     pam_selinux.so close
session    required     pam_loginuid.so
session    required     pam_selinux.so open env_params
session    optional     pam_keyinit.so force revoke
session    include      password-auth
session    include      postlogin
EOF

# 4. 临时关闭SELinux
setenforce 0

# 5. 验证配置
echo "验证SSH配置..."
if sshd -t; then
    echo "✓ 配置验证通过"
else
    echo "✗ 配置验证失败"
    exit 1
fi

# 6. 重启服务
systemctl restart sshd

# 7. 显示生效配置
echo ""
echo "=== 生效的配置 ==="
sshd -T | grep -E "passwordauthentication|usepam|permitrootlogin"

# 8. 检查服务状态
if systemctl is-active --quiet sshd; then
    echo ""
    echo "✓ SSH服务运行正常"
    echo ""
    echo "=== 修复完成 ==="
    echo "现在请测试SSH连接:"
    echo "ssh -o PreferredAuthentications=password root@0.0.0.0"
else
    echo "✗ SSH服务启动失败"
    journalctl -u sshd -n 20
fi
相关推荐
小生不才yz24 分钟前
Shell脚本精读 · S13-01 | POSIX sh 与 Bash:语法对照表
linux
阿拉斯攀登41 分钟前
平台设备驱动:platform bus 与设备树
linux·嵌入式硬件·嵌入式·linux驱动·字符设备驱动
阿拉斯攀登1 小时前
并发与同步:自旋锁、信号量、互斥锁
linux·网络·嵌入式硬件·linux驱动·字符设备驱动
网络小白不怕黑1 小时前
10.邮件服务器搭建并脚本实现监控httpd状态
linux·运维·服务器
CN_HW2 小时前
Nginx 流量镜像配置文档-双轨国产化
服务器·前端·网络
Carlos_Xu2 小时前
Linux基础-基础命令
linux
风曦Kisaki2 小时前
# Linux实战笔记-HIS项目实战Day01:版本控制与Git基础
linux·笔记·git
jsons13 小时前
rocky8内网离线批量补丁(你之前搭建的架构,多台服务器首选,无订阅)
linux·运维·服务器
进击切图仔3 小时前
SAM3 微调标注流水线和 Label Studio
java·服务器·前端