SSH密钥登录让Linux VPS/服务器更安全

Linux SSH登录有两种:

1、使用密码验证登录

通常VPS或服务器开通后都是直接提供IP和root密码,使用这种方式就是通过密码方式登录。如果密码不够强壮,而且没有安装DenyHosts之类的防止SSH密码破解的软件,那么系统安全将存在很大的隐患。

2、使用密钥验证登录

基于密钥的安全验证必须为用户自己创建一对密钥,并把共有的密钥放在需要访问的服务器上。当需要连接到SSH服务器上时,客户端软件就会向服务器发出请求,请求使用客户端的密钥进行安全验证。服务器收到请求之后,先在该用户的根目录下寻找共有密钥,然后把它和发送过来的公有密钥进行比较。如果两个密钥一致,服务器就用公有的密钥加密“质询”,并把它发送给客户端软件(putty,xshell等)。客户端收到质询之后,就可以用本地的私人密钥解密再把它发送给服务器,这种方式是相当安全的。

 

一、生成密钥

因为puttygen生成的密钥有问题可能会出现:“Server refused our key”,最好使用XShell生成密钥或者在远程Linux VPS/服务器生成密钥。

1、在Linux远程服务器生成密钥:

登录远程Linux  VPS/服务器,执行:
root@vpser:~# 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.         //提示公钥和私钥已经存放在/root/.ssh/目录下
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
15:23:a1:41:90:10:05:29:4c:d6:c0:11:61:13:23:dd root@vpser.net
The key's randomart image is:
+--[ RSA 2048]----+
|=&@Bo+o o.o      |
|=o=.E  o . o     |
| .    .   .      |
|         .       |
|        S        |
|                 |
|                 |
|                 |
|                 |
+-----------------+
root@vpser:~#
将/root/.ssh/下面的id_rsa和id_rsd.pub妥善保存。

二、将密钥添加到远程Linux服务器

1、用winscp,将/root/.ssh/下面(如果没有则创建此目录)下载到本地备用,在服务器上.ssh/目录下,cat id_rsa.pub >> authorized_keys,再执行:chmod 600 /root/.ssh/authorized_keys 修改权限。

2、修改/etc/ssh/sshd_config 文件,将RSAAuthentication 和 PubkeyAuthentication 后面的值都改成yes ,保存。如果这两项前面有 # 符号说明是注释掉的,需要去掉 # 符号。

3、重启sshd服务,Debian/Ubuntu执行/etc/init.d/ssh restart

CentOS执行:/etc/init.d/sshd restart 如果不存在可以执行:service sshd restart。 不行的话使用 /bin/systemctl restart sshd.service

三、客户端测试使用密钥登录

XShell登录

运行XShell,选择菜单File->New,按如下提示填写:

打开创建好的Session

如果设置没问题就会登录成功,出现用户提示符。

 

 

四、修改远程Linux服务器sshd服务配置

1、修改/etc/ssh/sshd_config 文件

将PasswordAuthentication yes 修改成 PasswordAuthentication no

2、重启sshd服务

Debian/Ubuntu执行/etc/init.d/ssh restart ;CentOS执行:/bin/systemctl restart sshd.service

ok,设置完成。

 

 

posted @ 2019-07-31 14:07  天道酬勤,坚持!  阅读(741)  评论(0编辑  收藏  举报