#!/bin/bash

# 备份 sshd_config 文件
cp /etc/ssh/sshd_config /etc/ssh/sshd_config.bak.$(date +%F-%T)

# 修改配置允许 root 登录和密码认证
sed -i 's/^.*PermitRootLogin.*/PermitRootLogin yes/g' /etc/ssh/sshd_config
sed -i 's/^.*PasswordAuthentication.*/PasswordAuthentication yes/g' /etc/ssh/sshd_config

# 如果配置文件中没有这两项，则追加
grep -q "^PermitRootLogin" /etc/ssh/sshd_config || echo "PermitRootLogin yes" >> /etc/ssh/sshd_config
grep -q "^PasswordAuthentication" /etc/ssh/sshd_config || echo "PasswordAuthentication yes" >> /etc/ssh/sshd_config

# 重启 ssh 服务（兼容不同发行版）
if command -v systemctl >/dev/null 2>&1; then
    systemctl restart sshd || systemctl restart ssh
else
    service sshd restart || service ssh restart
fi

echo "已修改 SSH 配置，允许 root 登录并启用密码认证，SSH 服务已重启。"
