阿里云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
相关推荐
邪修king3 小时前
Re:Linux 系统篇(三十五):进程间通信Chapter2:从匿名管道到进程池:Linux 进程间通信实战(深度理解「先描述,再组织」)
java·linux·运维
2301_777998344 小时前
ARP协议进阶:ARP缓存、RARP与ARP欺骗原理解析
linux·服务器·网络·网络协议·缓存
运维行者_4 小时前
应用性能监控怎么选?从调用链到用户体验的五个能力维度
运维·服务器·数据库·人工智能·mysql·性能优化·应用性能监控
Zenova EdgeOS10 小时前
Linux 内核日志 dmesg 进阶实战:从现场过滤到持久化闭环
linux·运维·服务器
郝亚军11 小时前
ec_private.pem的生成方法
linux·运维·服务器
NendoZen11 小时前
Arch Linux解决Grub无法SAVEDEFAULT问题(uki启动问题)
linux·运维·服务器
Elastic 中国社区官方博客12 小时前
Elasticsearch:安装自行管理的 Elastic Stack 集群
大数据·运维·服务器·数据库·elasticsearch·搜索引擎·全文检索
忆挽篱笙歌13 小时前
进程控制管理
linux·运维·服务器
Escalating_xu13 小时前
【Linux 网络编程】connect 的断线重连:用状态机写一个能恢复连接的 TCP 客户端
linux·网络·tcp/ip
Zucker n14 小时前
英语“论文”语法检查工具
linux·windows