前言
谷歌云创建云服务器后,不支持SSH账号密码登陆,需要在配置文件中修改几处地方,十分的麻烦,楼主写了一个脚本,直接执行即可解锁。
CentOS / Ubuntu / Debian
本文用于给 GCP 云服务器开启或关闭 SSH 密码登录。
适用系统:
CentOS
Ubuntu
Debian
一、CentOS 开启 root 密码登录
echo root:你的密码 | sudo chpasswd
sudo sed -i 's/^#\?PermitRootLogin.*/PermitRootLogin yes/g' /etc/ssh/sshd_config
sudo sed -i 's/^#\?PasswordAuthentication.*/PasswordAuthentication yes/g' /etc/ssh/sshd_config
sudo systemctl restart sshd示例:
echo root:Mg@2026_SSH_Login | sudo chpasswd
sudo sed -i 's/^#\?PermitRootLogin.*/PermitRootLogin yes/g' /etc/ssh/sshd_config
sudo sed -i 's/^#\?PasswordAuthentication.*/PasswordAuthentication yes/g' /etc/ssh/sshd_config
sudo systemctl restart sshd命令作用:
设置 root 密码
允许 root 登录
开启 SSH 密码登录
重启 SSH 服务
二、CentOS 关闭 root 密码登录
sudo sed -i 's/^#\?PermitRootLogin.*/PermitRootLogin prohibit-password/g' /etc/ssh/sshd_config
sudo sed -i 's/^#\?PasswordAuthentication.*/PasswordAuthentication no/g' /etc/ssh/sshd_config
sudo systemctl restart sshd命令作用:
禁止 root 密码登录
关闭 SSH 密码登录
重启 SSH 服务
三、Ubuntu / Debian 开启 root 密码登录
echo root:你的密码 | sudo chpasswd
sudo passwd -u root 2>/dev/null
sudo sed -i 's/^#\?PasswordAuthentication.*/PasswordAuthentication yes/g' /etc/ssh/sshd_config
sudo sed -i 's/^#\?PermitRootLogin.*/PermitRootLogin yes/g' /etc/ssh/sshd_config
sudo sed -i 's/^#\?KbdInteractiveAuthentication.*/KbdInteractiveAuthentication yes/g' /etc/ssh/sshd_config
sudo sed -i 's/^#\?UsePAM.*/UsePAM yes/g' /etc/ssh/sshd_config
if [ -d /etc/ssh/sshd_config.d ]; then
sudo sed -i 's/^#\?PasswordAuthentication.*/PasswordAuthentication yes/g' /etc/ssh/sshd_config.d/*.conf 2>/dev/null
sudo sed -i 's/^#\?PermitRootLogin.*/PermitRootLogin yes/g' /etc/ssh/sshd_config.d/*.conf 2>/dev/null
sudo sed -i 's/^#\?KbdInteractiveAuthentication.*/KbdInteractiveAuthentication yes/g' /etc/ssh/sshd_config.d/*.conf 2>/dev/null
sudo sed -i 's/^#\?ChallengeResponseAuthentication.*/ChallengeResponseAuthentication yes/g' /etc/ssh/sshd_config.d/*.conf 2>/dev/null
fi
sudo sshd -t && sudo systemctl restart ssh示例:
echo root:Mg@2026_SSH_Login | sudo chpasswd
sudo passwd -u root 2>/dev/null
sudo sed -i 's/^#\?PasswordAuthentication.*/PasswordAuthentication yes/g' /etc/ssh/sshd_config
sudo sed -i 's/^#\?PermitRootLogin.*/PermitRootLogin yes/g' /etc/ssh/sshd_config
sudo sed -i 's/^#\?KbdInteractiveAuthentication.*/KbdInteractiveAuthentication yes/g' /etc/ssh/sshd_config
sudo sed -i 's/^#\?UsePAM.*/UsePAM yes/g' /etc/ssh/sshd_config
if [ -d /etc/ssh/sshd_config.d ]; then
sudo sed -i 's/^#\?PasswordAuthentication.*/PasswordAuthentication yes/g' /etc/ssh/sshd_config.d/*.conf 2>/dev/null
sudo sed -i 's/^#\?PermitRootLogin.*/PermitRootLogin yes/g' /etc/ssh/sshd_config.d/*.conf 2>/dev/null
sudo sed -i 's/^#\?KbdInteractiveAuthentication.*/KbdInteractiveAuthentication yes/g' /etc/ssh/sshd_config.d/*.conf 2>/dev/null
sudo sed -i 's/^#\?ChallengeResponseAuthentication.*/ChallengeResponseAuthentication yes/g' /etc/ssh/sshd_config.d/*.conf 2>/dev/null
fi
sudo sshd -t && sudo systemctl restart ssh命令作用:
设置 root 密码
解锁 root 用户
允许 root 登录
开启 SSH 密码登录
处理 Ubuntu / Debian 额外 SSH 配置
检查配置并重启 SSH 服务
四、Ubuntu / Debian 关闭 root 密码登录
sudo sed -i 's/^#\?PasswordAuthentication.*/PasswordAuthentication no/g' /etc/ssh/sshd_config
sudo sed -i 's/^#\?PermitRootLogin.*/PermitRootLogin prohibit-password/g' /etc/ssh/sshd_config
sudo sed -i 's/^#\?KbdInteractiveAuthentication.*/KbdInteractiveAuthentication no/g' /etc/ssh/sshd_config
if [ -d /etc/ssh/sshd_config.d ]; then
sudo sed -i 's/^#\?PasswordAuthentication.*/PasswordAuthentication no/g' /etc/ssh/sshd_config.d/*.conf 2>/dev/null
sudo sed -i 's/^#\?PermitRootLogin.*/PermitRootLogin prohibit-password/g' /etc/ssh/sshd_config.d/*.conf 2>/dev/null
sudo sed -i 's/^#\?KbdInteractiveAuthentication.*/KbdInteractiveAuthentication no/g' /etc/ssh/sshd_config.d/*.conf 2>/dev/null
sudo sed -i 's/^#\?ChallengeResponseAuthentication.*/ChallengeResponseAuthentication no/g' /etc/ssh/sshd_config.d/*.conf 2>/dev/null
fi
sudo sshd -t && sudo systemctl restart ssh命令作用:
关闭 SSH 密码登录
禁止 root 密码登录
处理 Ubuntu / Debian 额外 SSH 配置
检查配置并重启 SSH 服务
五、检查是否开启成功
sudo sshd -T | grep -i "passwordauthentication\|permitrootlogin"开启成功一般显示:
passwordauthentication yes
permitrootlogin yes关闭成功一般显示:
passwordauthentication no
permitrootlogin prohibit-password
评论区