谷姐:我们都是谷歌迷
We are all Google fans

防黑客爆破:配置SSH密钥对用于远程访问服务器

5/5 - (3 votes)

主要是为了安全考虑,暴露在公网上的服务器,如果用密码认证授权登陆,即使密码再复杂,也有被爆破的可能。而SSH密钥授权是一种更加简单安全的授权登录远程服务器的认证方式。
RSA SSH密钥对

爆破?可以查看服务器有多少次失败的登陆 sudo lastb | wc -l

一、生成SSH密钥

尽量不要在目标服务器上进行SSH密钥对生成,因为生成密钥对后还需要将私钥复制到本地,这样私钥在网络传输过程有可能被截获。所以我们在本地设备上来生成密钥对,然后将公钥进行网络传输,导入到要访问的远程服务器上。

Windows:PuTTYgen

PuTTYgen是有名的开源小程序,也是有名的图形界面SSH客户端PuTTY的一部分。PuTTYgen小巧安全,可用于生成SSH密钥对十分简单。可以直接访问官网下载: https://www.puttygen.com/。生成密钥对的步骤官网给了很详细的介绍:

- Once you install the PuTTY on your machine, you can easily run PuTTYgen. For the same, go to Windows -> Start Menu -> All Programs -> PuTTY -> PuTTYgen.
- You will see the PuTTY key generator dialog box on your screen
- You will find a “Generate” button in that dialog. Clicking on it will lead to generating the keys for you.
- Now you will need to add a unique key passphrase in the Key passphrase and Confirm passphrase field.
- Click on the “Save Public Key” and “Save Private Key” buttons to save your public and private keys.
- You will see the text starting with ssh-RSA in the Public key for pasting into OpenSSH authorized_keys file field which is located at the top of the window. Copy that entire text to your clipboard by pressing ctrl+c as you will require the key to paste on your clipboard in the public key tool of control panel or directly on the cloud server.

Linux:ssh-keygen命令

直接在命令窗口输入ssh-keygen。会有一些提示

Generating public/private rsa key pair.
Enter file in which to save the key (/your_home/.ssh/id_rsa):

提示存储路径,直接用默认,回车即可。接着询问是否为生成的密钥对再设置一层密码

Enter passphrase (empty for no passphrase):

可置空,直接回车。最后输出

Your identification has been saved in /your_home/.ssh/id_rsa.
Your public key has been saved in /your_home/.ssh/id_rsa.pub.
The key fingerprint is:
a9:49:2e:2a:5e:33:3e:a9:de:4e:77:11:58:b6:90:26 username@remote_host
The key's randomart image is:
+--[ RSA 2048]----+
|     ..o         |
|   E o= .        |
|    o. o         |
|        ..       |
|      ..S        |
|     o o.        |
|   =o.+.         |
|. =++..          |
|o=++.            |
+-----------------+

表示成功生成了私钥和公钥对。接下来要做的是将公钥复制到远程服务器。

二、将公钥复制到远程服务器

编辑远程服务器上的文件 ~/.ssh/authorized_keys,将公钥文件中的内容复制并添加到文件的结尾。如果远程服务器上不存在~/.ssh文件夹或authorized_keys文件,直接创建mkdir ~/.ssh && touch ~/.ssh/authorized_keys

递归地置空~/.ssh目录所在“组”的成员用户和“其他”(group,other)用户的所有权限,就是将权限只给创建用户。

chmod -R go= ~/.ssh

如果当前是用root用户为普通用户创建登陆授权密钥对,建议将权限压缩到普通用户,比如将创建用户从root用户改为普通用户sammy

chown -R sammy:sammy ~/.ssh

如果是准备用root用户直接登陆服务器,那就不用做此安全设定。

三、将私钥保存到本地,并至少备份到三个其他地方

Windows上的.ssh目录一般在C:\Users\用户\.ssh,其实也可以通过路径方式指定给各个SSH客户端,甚至可以文本方式将私钥的内容复制到某些SSH客户端,不同SSH客户端有不同调用私钥的方式。

可以网盘,邮箱,U盘都备份并记录。如果私钥丢失,而且服务器还没有救援模式的话,那就只能重装远程服务器的系统了,意味着将失去远程服务器上的资料。所以建议将私钥多地备份,最好公钥也同时多备份,防止远程服务器的误操作将公钥弄丢,如果有备份还能趁会话没关闭前重新导入公钥。

四、成功认证后,取消远程服务器密码登陆

以下操作,请确保至少有一个连接稳定的SSH会话,并确保用于远程登陆的用户是root或者具有sudo权限。

1. 编辑sudo vim /etc/ssh/sshd_config,搜索PasswordAuthentication,找到

PasswordAuthentication no

设置为no,并取消注释,或者利用一键命令

sed -i 's/^#\?PasswordAuthentication yes/PasswordAuthentication no/' /etc/ssh/sshd_config

即可禁用掉用户通过密码认证的方式登陆SSH到远程服务器。

2. 重启sshd服务:

sudo systemctl restart ssh

3. 重开一个SSH会话,确保可以用SSH密钥方式登陆,然后就可以放心关闭连接中的SSH会话。

至此,便再也不用密码去登陆远程服务器了,同样攻击者也没法通过猜密码黑远程服务器。

参考

1. https://www.computerhope.com/unix/uchmod.htm
2. https://www.digitalocean.com/community/tutorials/how-to-set-up-ssh-keys-on-debian-10

未经谷姐允许不得转载:谷姐靓号网 » 防黑客爆破:配置SSH密钥对用于远程访问服务器
分享到: 生成海报

热门文章

评论 抢沙发

评论前必须登录!

立即登录   注册

买Google Voice认准【谷姐靓号网】

Google Voice靓号列表Google Voice自助购买
切换注册

登录

忘记密码 ?

切换登录

注册

我们将发送一封验证邮件至你的邮箱, 请正确填写以完成账号注册和激活