VPS安全配置笔记

VPS安全配置笔记折腾一些VPS的安全设置问题,全部内容来自Google搜索多个网站内容。以下内容适用于CentOS。

Linux VPS安全配置笔记

SSH密钥登录

1. 网站生成密钥

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        |

|                 |

|                 |

|                 |

|                 |

+-----------------+

2. 下载服务所生成的密钥文件,保存好!!! 尤其是id_rsa!!!!!

3. 重命名加权限

mv /root/.ssh/id_rsa.pub /root/.ssh/authorized_keys //重命名公共密钥

chmod 600 /root/.ssh/authorized_keys //修改权限为600

4. 修改/etc/ssh/sshd_config配置文件禁用密码登陆

RSAAuthentication yes //把前面的#删掉,并且改为yes

PubkeyAuthentication yes //把前面的#删掉,并且改为yes

# To disable tunneled clear text passwords, change to no here!
PasswordAuthentication no //这里把注释去掉,并改成no,指不可使用密码登陆

# Change to no to disable s/key passwords
ChallengeResponseAuthentication yes //把注释去掉,并改成no,这样如果没有密钥,连输入密码的机会都没有

5. 重启sshd服务

/etc/init.d/sshd restart //重启sshd服务,或者

service sshd restart //此命令也可重启

6. 如果使用WinSCP或者Putty登陆的话需要转换密钥文件

打开pageant.exe,导入密钥,转换,然后保存为私有密钥*.ppk的形式才能使用

修改SSH端口号

Linux默认的SSH端口是22,可以修改成别的,可以通过vi命令修改,本人不会,更喜欢直接修改文件。

vi /etc/ssh/sshd_config //编辑sshd_config文件

#Port 22 //去掉前面的注释,更改为其它端口号,最好4位,避免冲突

service sshd restart //重启sshd服务

屏蔽并举报扫描 SSH 端口的 IP

Fail2ban和DenyHosts是两款可以屏蔽穷举SSH密码的IP的软件。

据称DenyHosts只能监视SSH服务,如果需要保护其它的服务Fail2ban稍好些,据称Fail2ban屏蔽之余,还能自动写举报信给 IP 所在的 ISP。那就装Fail2ban吧。

rpm -Uvh http://download.fedoraproject.org/pub/epel/6/i386/epel-release-6-8.noarch.rpm //CentOS32导入epel 源

rpm -Uvh http://download.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm //CentOS64导入epel 源

yum -y install fail2ban //安装Fail2ban

service fail2ban start //启动Fail2ban服务

/var/log/fail2ban.log //查看日志

其它配置

1. 可以安装Iptables防火墙,但是安装以后问题太多,如果想安装可以看这里教程Linux上iptables防火墙的基本应用教程

2. 可以禁止Root用户登陆,更改为一个自己的用户名称,具体网上再搜索,不过我先省了。

3. 待添加。

  • 本博客文章如未特别说明,皆为本站原创,默认采用署名-相同方式共享 4.0 国际协议
  • 相关文章

    VPS安全配置笔记》有3个想法

      1. Brainu 文章作者

        简单,但是维护起来有点烦人。虚拟主机好在不用太多的维护,不过VPS好在自主性强。

        回复

    发表回复

    您的邮箱地址不会被公开。 必填项已用 * 标注