共计 971 个字符,预计需要花费 3 分钟才能阅读完成。
在使用 Google Cloud Platform(GCP)搭建服务器的过程中,你可能会遇到无法通过 SSH 使用 root 用户直接登录的情况。默认情况下,GCP 出于安全考虑禁用了 root 登录。如果你需要 开启 root 登录权限(比如为了方便远程管理或自动化任务),可以通过以下方法来实现。
✅步骤一:登录 Google Cloud 控制台并连接实例
进入 Google Cloud 控制台,选择你的虚拟机,点击【SSH】按钮连接到服务器。
✅步骤二:切换到 Root 用户并设置密码
sudo -i
passwd root
说明:
sudo -i:切换到 root 用户passwd root:为 root 用户设置新密码
✅步骤三:修改 SSH 配置文件,开启 Root 登录和密码认证
根据你的系统类型选择执行下面的命令:
🐧CentOS / Debian 系统:
sed -i 's/PermitRootLogin no/PermitRootLogin yes/g' /etc/ssh/sshd_config
sed -i 's/PasswordAuthentication no/PasswordAuthentication yes/g' /etc/ssh/sshd_config
🐧Ubuntu 系统:
sed -i 's/#PermitRootLogin prohibit-password/PermitRootLogin yes/g' /etc/ssh/sshd_config
sed -i 's/PasswordAuthentication no/PasswordAuthentication yes/g' /etc/ssh/sshd_config
📌说明:
PermitRootLogin yes:允许 Root 用户登录PasswordAuthentication yes:允许密码方式登录
✅步骤四:重启服务器
修改完成后,执行下面的命令以应用配置:
reboot
系统将重启,大约一分钟后可重新通过 SSH 使用 root 用户和你刚设置的密码登录。
✅测试效果:用 Root 登录服务器
你现在可以通过工具(如 XShell、FinalShell 或 ssh root@服务器 IP)使用 root 用户远程登录服务器了。
⚠️安全提示
开启 root 登录可能会带来安全隐患,请务必做好以下防护措施:
- 配置防火墙,仅允许指定 IP 登录
- 开启 Fail2Ban 或其他暴力破解防护工具
- 使用强密码,或尽早恢复为密钥登录
正文完