一些云服务商出于安全性考虑,将旗下 VPS 设为仅允许通过 SSH Key 登陆并禁用了 root 账户,例如 Amazon Web Services、Google Compute Engine。

其实,个人非生产环境并不需要这么严格的权限控制,下面是一些相关方案。


请注意,以下操作会降低登陆安全性,请自行斟酌是否开启。


在 CentOS、Debian、Ubuntu 测试通过,其他发行版可以参考以下步骤。

开启 root 账户并配置登录密码:

  1. # 切换到 root
  2. sudo -i
  3. cd /etc/ssh/
  4. # 开启 root 登录
  5. sed -i 's/^.*PermitRootLogin.*/PermitRootLogin yes/g' sshd_config
  6. # 开启密码认证
  7. sed -i 's/^.*PasswordAuthentication.*/PasswordAuthentication yes/g' sshd_config
  8. # 设置 root 账户的新密码
  9. passwd root
  10. # 重启服务
  11. service sshd restart
  12. service ssh restart

开启 root 账户并配置 SSH Key:

  1. # 切换到 root
  2. sudo -i
  3. cd /etc/ssh/
  4. # 开启 root 登录
  5. sed -i 's/^.*PermitRootLogin.*/PermitRootLogin yes/g' sshd_config
  6. # 开启 Key 认证
  7. sed -i 's/^.*RSAAuthentication.*/RSAAuthentication yes/g' sshd_config
  8. sed -i 's/^.*PubkeyAuthentication.*/PubkeyAuthentication yes/g' sshd_config
  9. cd ~
  10. mkdir .ssh
  11. cd .ssh
  12. # 此处引号内替换为你的公钥
  13. echo "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAAAgQDO7wSG85qjN/J8Krf8lMjWJMsWZ6WGWSiTh0zMIt0OFK4/tu2U2wQxZfodYPGght3TiS1YdgZi8q9yaxvy2uSTgCVeSDHwZm3suuGA7ofshY6LNowI7DWJTOlJ3Z0u63bOslzxv2sIuFWndw4GHNn7MBF1CBE33GaGJ7Yx8thhAQ== SaintW" > authorized_keys
  14. chmod 600 authorized_keys
  15. cd ../
  16. chmod 700 .ssh
  17. # 重启服务
  18. service sshd restart
  19. service ssh restart