侧边栏壁纸
  • 累计撰写 136 篇文章
  • 累计创建 19 个标签
  • 累计收到 3 条评论

目 录CONTENT

文章目录

谷歌云SSH账号密码配置

YaFuX
2022-11-18 / 0 评论 / 0 点赞 / 59 阅读 / 7236 字
温馨提示:
本文最后更新于 2026-05-30,若内容或图片失效,请留言反馈。部分素材来自网络,若不小心影响到您的利益,请联系我们删除。

前言

谷歌云创建云服务器后,不支持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

0

评论区