亚马逊AWS EC2上Debian和Ubuntu系统使用root用户登录-无需秘钥–开机自动配置脚本

如果使用了Debian ubuntu系统,那么开机前输入如下脚本,就可以设定为密码方式登录EC2了。


#!/bin/bash
# 1. 设置 root 密码
echo “root:Password123@” | chpasswd

# 2. 修改主配置文件
sed -i ‘s/^#\?PermitRootLogin.*/PermitRootLogin yes/g’ /etc/ssh/sshd_config
sed -i ‘s/^#\?PasswordAuthentication.*/PasswordAuthentication yes/g’ /etc/ssh/sshd_config

# 3. 强制覆盖所有子配置文件(关键步骤)
# 这会把所有 conf 文件里的 PasswordAuthentication 改为 yes
if [ -d /etc/ssh/sshd_config.d ]; then
sed -i ‘s/PasswordAuthentication no/PasswordAuthentication yes/g’ /etc/ssh/sshd_config.d/*.conf
sed -i ‘s/KbdInteractiveAuthentication no/KbdInteractiveAuthentication yes/g’ /etc/ssh/sshd_config.d/*.conf
fi

# 4. 确保键盘交互认证也开启(有些客户端需要这个)
sed -i ‘s/^#\?KbdInteractiveAuthentication.*/KbdInteractiveAuthentication yes/g’ /etc/ssh/sshd_config

# 5. 重启服务
systemctl restart ssh


 

滚动至顶部