Linux配置使用SSH Key登录并禁用root密码登录

2015年3月2日09:28:48系统教程1 18,525

DigitalOcean是国外一家不错的KVM架构VPS提供商,每月最低5美元即可拥有一台DigitalOcean主机,其较高的性价比国内用户访问San Francisco机房的速度,吸引了越来越多的中国用户。基于这些,我也搞了一台,我系统选择的是Centos,第一次配置时并未配置SSH Key登陆,默认一直使用root账户加密码登陆,在这台服务器上加载的功能多了,为了安全,就考虑使用SSH Key登陆并禁用root用户的密码登陆权限。文章源自堕落的鱼-https://www.duoluodeyu.com/1114.html

DigitalOcean已建Droplet怎样配置使用SSH公钥、私钥登陆?文章源自堕落的鱼-https://www.duoluodeyu.com/1114.html

Linux系统大多说都支持OpenSSH,生成公钥、私钥的最好用ssh-keygen命令,如果用putty自带的PUTTYGEN.EXE生成会不兼容OpenSSH,从而会导致登录时出现server refused our key错误。文章源自堕落的鱼-https://www.duoluodeyu.com/1114.html

1、root用户登陆后,运行以下第一句指令,其他根据提示进行输入:文章源自堕落的鱼-https://www.duoluodeyu.com/1114.html

ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):                 建议直接回车使用默认路径
Created directory '/root/.ssh'
Enter passphrase (empty for no passphrase):            输入密码短语(留空则直接回车)
Enter same passphrase again:                                  重复密码短语
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
05:71:53:92:96:ba:53:20:55:15:7e:5d:59:85:32:e4 root@test
The key's randomart image is:
+--[ RSA 2048]----+
|   o o ..                |
| . o oo.+ .            |
| o.+... =               |
| ...o                     |
| o S                     |
| .                         |
|                           |
|                           |
|                           |
+--------------------+文章源自堕落的鱼-https://www.duoluodeyu.com/1114.html

此时在/root/.ssh/目录下生成了2个文件,id_rsa为私钥,id_rsa.pub为公钥。私钥自己下载到本地电脑妥善保存(丢了服务器可就没法再登陆了),为安全,建议删除服务器端的私钥。公钥则可以任意公开。文章源自堕落的鱼-https://www.duoluodeyu.com/1114.html

2、使用以下命令将公钥导入到VPS:文章源自堕落的鱼-https://www.duoluodeyu.com/1114.html

cat /root/.ssh/id_rsa.pub >> /root/.ssh/authorized_keys文章源自堕落的鱼-https://www.duoluodeyu.com/1114.html

3、修改SSH的配置文件/etc/ssh/sshd_config :文章源自堕落的鱼-https://www.duoluodeyu.com/1114.html

#RSAAuthentication yes
#PubkeyAuthentication yes
#AuthorizedKeysFile .ssh/authorized_keys文章源自堕落的鱼-https://www.duoluodeyu.com/1114.html

去调上面3行前面的#,保存后重启SSH服务。文章源自堕落的鱼-https://www.duoluodeyu.com/1114.html

service sshd restart文章源自堕落的鱼-https://www.duoluodeyu.com/1114.html

至此你的DigitalOcean VPS已经支持使用SSH私钥证书登录。在你使用SSH Key登录验证成功后,还是为了安全,建议你关闭root用户使用密码登陆,关闭的方法如下:文章源自堕落的鱼-https://www.duoluodeyu.com/1114.html

修改SSH的配置文件/etc/ssh/sshd_config,找到下面1行:文章源自堕落的鱼-https://www.duoluodeyu.com/1114.html

PasswordAuthentication yes文章源自堕落的鱼-https://www.duoluodeyu.com/1114.html

修改为:文章源自堕落的鱼-https://www.duoluodeyu.com/1114.html

PasswordAuthentication no文章源自堕落的鱼-https://www.duoluodeyu.com/1114.html

保存后重启SSH服务。文章源自堕落的鱼-https://www.duoluodeyu.com/1114.html

service sshd restart文章源自堕落的鱼-https://www.duoluodeyu.com/1114.html

好了,至此只要你保存好你的私钥(为安全,建议删除服务器端的私钥),你的服务器相比原来使用root用户加密码登陆来说已经安全多了。文章源自堕落的鱼-https://www.duoluodeyu.com/1114.html

体验DigitalOcean主机文章源自堕落的鱼-https://www.duoluodeyu.com/1114.html

匿名

发表评论

匿名网友 填写信息

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定

评论:1   其中:访客  1   博主  0
    • Ninaru
      Ninaru 0

      最后的禁用密码登录是禁用了所有用户的密码登录而不仅是root