RHEL / CentOS 安装 DenyHosts


DenyHosts 是一个防止暴力攻击 SSH Server 的工具, 它会监察及分析 SSH 的 log file, 当有数次错误登入后, 就会将尝试登入的 IP 地址加入到 /etc/hosts 作出封锁。 Fail2Ban 的防护较全面, 可以透过正规表达式正义不同的 log files, 但设定较复杂, 而 DenyHosts 只会针对 SSH Server 作防护, 而且设定较简单。以下是在 RHEL 及 CentOS 安装及设定 DenyHosts 的方法。

安装 EPEL Repository
首先要安装 EPEL Repository, 不同版本需要安装不同的 rpm:

RHEL / CentOS 7.x (x86_64)
rpm -ivh http://dl.fedoraproject.org/pub/epel/7/x86_64/e/epel-release-7-5.noarch.rpm

RHEL / CentOS 6.x (x86_64)
rpm -ivh http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm

RHEL / CentOS 6.x (i386)
rpm -ivh http://dl.fedoraproject.org/pub/epel/6/i386/epel-release-6-8.noarch.rpm

RHEL / CentOS 5.x (x86_64)
rpm -ivh http://dl.fedoraproject.org/pub/epel/5/x86_64/epel-release-5-4.noarch.rpm

RHEL / CentOS 5.x (i386)
rpm -ivh http://dl.fedoraproject.org/pub/epel/5/i386/epel-release-5-4.noarch.rpm

安装 DenyHosts
安装好 EPEL Repository 后, 便可以直接用 YUM 安装 DenyHosts:

然后开启档案 /etc/hosts.allow, 加入你的 IP 地址, 防止 DenyHosts 将你的 IP 也封掉:

然后加入你的 IP 地址, 以下用 192.168.1.11:

储存盘案后再开启 /etc/hosts.deny 档案:

在最后的位置加入这行:

如需要修改 DenyHosts 的设定, 可以开启档案 /etc/denyhosts.conf, 没有的话可以重新启动 DenyHosts:

解封个别 IP

这时 DenyHosts 已经开始运作, 如果需要手动解封个别 IP 比较麻烦, 做法是开启以下 6 个档案, 将要解封的 IP 移除:

移除以上档案的 IP 后需要重新启动 DenyHosts:

Leave a Reply