1)禁止系统响应任何从外部/内部来的ping请求攻击者通常首先经过ping命令检测此主机或者IP是否处于活动状态
若是可以ping通 某个主机或者IP,那么攻击者就认为此系统处于活动状态,继而进行攻击或破坏。若是没有人能ping通机器并收到响应,那么就能够大大加强服务器的安全性,
linux下能够执行以下设置,禁止ping请求:
[root@localhost ~]# echo "1"> /proc/sys/net/ipv4/icmp_echo_ignore_allhtml
默认状况下"icmp_echo_ignore_all"的值为"0",表示响应ping操做。
能够加上面的一行命令到/etc/rc.d/rc.local文件中,以使每次系统重启后自动运行。node
2)禁止Control-Alt-Delete组合键重启系统
在linux的默认设置下,同时按下Control-Alt-Delete键,系统将自动重启,这是很不安全的,所以要禁止Control-Alt-Delete组合键重启系统,查看/etc/inittab文件:
[root@localhost ~]# cat /etc/inittab
.......
# Ctrl-Alt-Delete is handled by /etc/init/control-alt-delete.conf
.......python
[root@localhost ~]# cat /etc/init/control-alt-delete.conf
# control-alt-delete - emergency keypress handling
#
# This task is run whenever the Control-Alt-Delete key combination is
# pressed. Usually used to shut down the machine.
#
# Do not edit this file directly. If you want to change the behaviour,
# please create a file control-alt-delete.override and put your changes there.linux
start on control-alt-deleteshell
exec /sbin/shutdown -r now "Control-Alt-Delete pressed"vim
能够将/etc/init/control-alt-delete.conf文件删除或移走或改名。
[root@localhost ~]# mv /etc/init/control-alt-delete.conf /etc/init/control-alt-delete.conf.bakcentos
或者将/etc/init/control-alt-delete.conf文件里的内容注释掉!api
3)限制Shell记录历史命令大小
默认状况下,bash shell会在文件$HOME/.bash_history中存放多达1000条命令记录(根据系统不一样,默认记录条数不一样)。系统中每一个用户的主目录下都有一个这样的文件。
这么多的历史命令记录,确定是不安全的,所以必须限制该文件的大小。
能够编辑/etc/profile文件,修改其中的选项以下:
[root@localhost ~]# vim /etc/profile
.......
HISTSIZE=1000
.......安全
默认Shell记录历史命令的条数是1000,能够修改成100条,即HISTSIZE=1000,表示在文件$HOME/.bash_history中记录最近的30条历史命令。
若是将"HISTSIZE"设置为0,则表示不记录历史命令,那么也就不能用键盘的上下键查找历史命令了。bash
4)设定tcp_wrappers防火墙
Tcp_Wrappers是一个用来分析TCP/IP封包的软件,相似的IP封包软件还有iptables,linux默认都安装了此软件,做为一个安全的系统,Linux自己有两层安全防火墙,经过IP
过滤机制的iptables实现第一层防御,iptables防火墙经过直观地监视系统的运行情况,阻挡网络中的一些恶意攻击,保护整个系统正常运行,免遭攻击和破坏。
若是经过了iptables的第一层防御,那么下一层防御就是tcp_wrappers了,经过Tcp_Wrappers能够实现对系统中提供的某些服务的开放与关闭、容许和禁止,从而更有效地保
证系统安全运行。
4.1)Tcp_Wrappers的使用很简单,仅仅两个配置文件:/etc/hosts.allow和/etc/hosts.deny查看系统是否安装了Tcp_Wrappers:
[root@localhost ~]# rpm -q tcp_wrappers
tcp_wrappers-7.6-58.el6.x86_64
若是有上面的相似输出,表示系统已经安装了tcp_wrappers模块。若是没有显示,多是没有安装,能够从linux系统安装盘找到对应RPM包进行安装。
4.2)tcp_wrappers防火墙的局限性
系统中的某个服务是否可使用tcp_wrappers防火墙,取决于该服务是否应用了libwrapped库文件,若是应用了就可使用tcp_wrappers防火墙,系统中默认的一些服务如:
sshd、portmap、sendmail、xinetd、vsftpd、tcpd等均可以使用tcp_wrappers防火墙。
4.3)tcp_wrappers设定的规则(使用例子能够参考:http://www.cnblogs.com/kevingrace/p/6245859.html)
tcp_wrappers防火墙的实现是经过/etc/hosts.allow和/etc/hosts.deny两个文件来完成的,首先看一下设定的格式:
service:host(s) [:action]
service:表明服务名,例如sshd、vsftpd、sendmail等。
host(s):主机名或者IP地址,能够有多个,例如192.168.60.0、www.ixdba.netl action:动做, 符合条件后所采起的动做。
几个关键字:
ALL:全部服务或者全部IP。
ALL EXCEPT:全部的服务或者全部IP除去指定的。
例如:ALL:ALL EXCEPT 192.168.60.132
表示除了192.168.60.132这台机器,任何机器执行全部服务时或被容许或被拒绝。
了解了设定语法后,下面就能够对服务进行访问限定。
例如:互联网上一台linux服务器,实现的目标是:仅仅容许222.90.66.四、61.185.224.66以及域名softpark.com经过SSH服务远程登陆到系统,设置以下:
首先设定容许登陆的计算机,即配置/etc/hosts.allow文件,设置很简单,只要修改/etc/hosts.allow(若是没有此文件,请自行创建)这个文件便可。
只需将下面规则加入/etc/hosts.allow便可。
sshd: 222.90.66.4 61.185.224.66 softpark.com
接着设置不容许登陆的机器,也就是配置/etc/hosts.deny文件了。
通常状况下,linux会首先判断/etc/hosts.allow这个文件,若是远程登陆的计算机知足文件/etc/hosts.allow设定的话,就不会去使用/etc/hosts.deny文件了,相反,若是
不知足hosts.allow文件设定的规则的话,就会去使用hosts.deny文件了,若是知足hosts.deny的规则,此主机就被限制为不可访问linux服务器,若是也不知足hosts.deny的
设定,此主机默认是能够访问linux服务器的,所以,当设定好/etc/hosts.allow文件访问规则以后,只需设置/etc/hosts.deny为"全部计算机都不能登陆状态"便可。
sshd:ALL
这样,一个简单的tcp_wrappers防火墙就设置完毕了。
5)网络安全选项的设定
编辑 "/etc/sysctl.conf" 档案,并加入下面几行,
[root@localhost ~]# vim /etc/sysctl.conf
# Enable ignoring broadcasts request(让系统对广播没有反应)
net.ipv4.icmp_echo_ignore_broadcasts = 1
# Disables IP source routing(取消 IP source routing)
net.ipv4.conf.all.accept_source_route = 0
# Enable TCP SYN Cookie Protection(开启 TCP SYN Cookie 保护)
net.ipv4.tcp_syncookies = 1
# Disable ICMP Redirect Acceptance(取消 ICMP 接受 Redirect)
net.ipv4.conf.all.accept_redirects = 0
# Enable bad error message Protection(开启错误讯息保护)
net.ipv4.icmp_ignore_bogus_error_responses = 1
# Enable IP spoofing protection, turn on Source Address Verification(开启 IP 欺骗保护)
net.ipv4.conf.all.rp_filter = 1
# Log Spoofed Packets, Source Routed Packets, Redirect Packets(记录Spoofed Packets, Source Routed Packets, Redirect Packets)
net.ipv4.conf.all.log_martians = 1
最后从新启动network
[root@localhost ~]# /etc/rc.d/init.d/network restart
或者sysctl –p 生效
[root@localhost ~]# echo 1 > /proc/sys/net/ipv4/icmp_echo_ignore_all
6)系统文件和日志的安全
更改各类服务启动脚本目录的访问权限:
[root@localhost ~]# chmod -R 700 /etc/rc.d/
更改系统文件的访问权限:
[root@localhost ~]# chmod 700 /etc/services
[root@localhost ~]# chmod 700 /etc/xinetd.conf
[root@localhost ~]# chmod 700 /etc/inittab
更改日志的访访权限:
[root@localhost ~]# chmod 700 /var/log/secure*
[root@localhost ~]# chmod 700 /var/log/messages*
7)系统登录的设定
每次登录退出时,清除所用过命令的历史纪: 在.bash_logout文档中增长一行:
[root@localhost ~]# vim /root/.bashrc
......
history –c
设置系统全局环境变量文件/etc/profile 增长一行:
[root@localhost ~]# vim /etc/profile
......
TMOUT=600
单位为秒,表示若是在600秒(10分钟)以内没有作任何的操做,登录终端将自动注销。 3.资源限制
编辑/etc/security/limits.conf 加入:
[root@localhost ~]# vim /etc/security/limits.conf
......
* hard core 0
* hard rss 5000
* hard nproc 20
同时必须编辑/etc/pam.d/login文件加/检查下面这一行的存在:
[root@localhost ~]# vim /etc/pam.d/login
.......
session required /lib/security/pam_limits.so
上面的命令分别是禁止建立core文件,其余用户(除root)最多使用5M内存,限制最多进程数为20
8)vsftp配置
vsftpd的配置文件有三个,分别是:
/etc/vsftpd/vsftpd.conf
/etc/ vsftpd/vsftpd.ftpusers
/etc/ vsftpd/vsftpd.user_list
其中:
/etc/vsftpd.conf是主配置文件
/etc/vsftpd.ftpusers中指定了哪些用户不能访问FTP服务器
/etc/vsftpd.user_list中指定的用户默认状况下(即在/etc/vsftpd.conf中设置了userlist_deny= YES)不能访问FTP服务器
当在/etc/vsftpd.conf中设置了userlist_deny=NO时,仅仅容许/etc/vsftpd.user_list中指定的用户访问FTP服务器。
通常要在/etc/vsftpd.conf中设置userlist_deny=NO,经过/etc/vsftpd.user_list严格控制FTP用户。
配置基本的性能和安全选项
//禁止匿名登录
anonymous_enable=NO (默认为YES)
//设置空闲的用户会话的中断时间 例以下面的配置:
idle_session_timeout=600
将在用户会话空闲10分钟后被中断。
//设置空闲的数据链接的的中断时间 例以下面的配置:
data_connection_timeout=120
将在数据链接空闲2分钟后被中断。
//设置客户端空闲时的自动中断和激活链接的时间 例以下面的配置: accept_timeout=60 connect_timeout=60
将使客户端空闲1分钟后自动中断链接,并在中断1分钟后自动激活链接
//设置最大传输速率限制 例以下面的配置:(传输速率可根据实际状况自行修改) local_max_rate=50000 anon_max_rate=30000
将使本地用户的最大传输速率为50kbytes / sec,匿名用户的 传输速率为30 kbytes / sec。
//设置客户端链接时的端口范围 例以下面的配置: pasv_min_port=50000 pasv_max_port=60000
将使客户端链接时的端口范围在50000和60000之间。这提升了系统的安全性
截短历史命令 "~/.bash_history"文件,这个文件中保存着之前使用的命令列表。截短这个文件可使您之前执行过的命令暴露在别人眼光下的机会减少.
(在您的命令中极可能包括象密码信息这样的保密信息)。经过编辑/etc/profile的下面两项能够作到这一点:
HISTFILESIZE=20
经过下面的措施能够防止任何人均可以su为root:
编辑su文件(vi /etc/pam.d/su)在文件的头部加入下面两行:
auth sufficient /lib/security/pam_rootok.so debug
auth required /lib/security/pam_wheel.so group=wheel
9)使用DenyHosts阻止SSH暴力破解(同时能够保护FTP)
当你的Linux服务器暴露在互联网之中,该服务器将会遭到互联网上的扫描软件进行扫描,并试图猜想SSH登陆口令,不可避免的有病毒或无聊的人会尝试攻击SSH。。你会发现,天天会有多条SSH登陆失败纪录。那些扫描工具将对你的服务器构成威胁,你必须设置复杂登陆口令,并将尝试屡次登陆失败的IP给阻止掉,让其在一段时间内不能访问该服务器。DenyHosts是Python语言写的一个程序,用DenyHosts能够阻止试图猜想SSH登陆口令,它会分析/var/log/secure等日志文件,当发现有重复的IP在进行屡次SSH密码尝试时就会记录IP到/etc/hosts.deny文件,从而达到自动屏蔽该IP的目的。
下载DenyHosts-2.6.tar.gz安装包如下是安装记录:
[root@localhost ~]# wget http://sourceforge.net/projects/denyhosts/files/denyhosts/2.6/DenyHosts-2.6.tar.gz
[root@localhost ~]# tar -zxvf DenyHosts-2.6.tar.gz
[root@localhost ~]# cd DenyHosts-2.6
[root@localhost DenyHosts-2.6]# python setup.py install
默认是安装到/usr/share/denyhosts/目录的。
[root@localhost DenyHosts-2.6]# cd /usr/share/denyhosts/
[root@localhost denyhosts]# cp denyhosts.cfg-dist denyhosts.cfg
[root@localhost denyhosts]# cp daemon-control-dist daemon-control
[root@localhost denyhosts]# vim daemon-control
DENYHOSTS_BIN = "/usr/bin/denyhosts.py"
DENYHOSTS_LOCK = "/var/lock/subsys/denyhosts"
DENYHOSTS_CFG = "/usr/share/denyhosts/denyhosts.cfg"
[root@localhost denyhosts]# chown root daemon-control
[root@localhost denyhosts]# chmod 700 daemon-control
完了以后执行daemon-contron start就能够了(重启是restart)。
[root@localhost denyhosts]# ./daemon-control start //或者执行"service denyhosts start"命令来启动
若是要使DenyHosts每次重起后自动启动还需作以下设置:
[root@localhost denyhosts]# cd /etc/init.d
[root@localhost denyhosts]# ln -s /usr/share/denyhosts/daemon-control denyhosts
[root@localhost denyhosts]# chkconfig --add denyhosts
[root@localhost denyhosts]# chkconfig --level 2345 denyhosts on
或者修改/etc/rc.local文件:
[root@localhost denyhosts]# vim /etc/rc.local //加入下面这条命令
/usr/share/denyhosts/daemon-control start
DenyHosts配置文件说明:
[root@localhost denyhosts]# vim denyhosts.cfg
#这里根据本身须要进行相应的配置
SECURE_LOG = /var/log/secure
#sshd日志文件,它是根据这个文件来判断的,不一样的操做系统,文件名稍有不一样。
HOSTS_DENY = /etc/hosts.deny #控制用户登录的文件
PURGE_DENY = 5m
#过多久后清除已经禁止的
BLOCK_SERVICE = sshd
BLOCK_SERVICE = vsftpd #(若是启用FTP服务,务必加上这一行) ,禁止的服务名
DENY_THRESHOLD_INVALID = 1 #容许无效用户失败的次数
DENY_THRESHOLD_VALID = 10 #容许普通用户登录失败的次数
DENY_THRESHOLD_ROOT = 5 #容许root登录失败的次数
HOSTNAME_LOOKUP=NO #是否作域名反解
DAEMON_LOG = /var/log/denyhosts #DenyHosts的日志文件
DenyHosts防SSH暴力破解测试:
192.168.10.200服务器按照上面步骤安装并启动了DenyHosts 如今在另外一台服务器192.168.10.205上用ssh工具链接192.168.10.200,用错误的密码尝试几回: [root@host-205 ~]# ssh -p22 root@192.168.10.200 root@192.168.10.200's password: Permission denied, please try again. root@192.168.10.200's password: Permission denied, please try again. root@192.168.10.200's password: Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password). [root@host-205 ~]# ssh -p22 root@192.168.10.200 root@192.168.10.200's password: Permission denied, please try again. root@192.168.10.200's password: Permission denied, please try again. root@192.168.10.200's password: Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password). [root@host-205 ~]# ssh -p22 root@192.168.10.200 ssh_exchange_identification: Connection closed by remote host [root@host-205 ~]# ssh -p22 root@192.168.10.200 ssh_exchange_identification: Connection closed by remote host 发如今192.168.10.205上使用错误密码去ssh链接192.168.10.200机器失败多数后,就是禁止205这台机器去ssh链接200机器了。 查看192.168.10.200机器的/var/log/denyhosts,能够发现以下信息: [root@host-200 ~]# tail -f /var/log/denyhosts ...... 2018-02-12 00:59:59,320 - denyhosts : INFO monitoring log: /var/log/secure 2018-02-12 00:59:59,320 - denyhosts : INFO sync_time: 3600 2018-02-12 00:59:59,320 - denyhosts : INFO purging of /etc/hosts.deny is disabled 2018-02-12 00:59:59,320 - denyhosts : INFO denyhosts synchronization disabled 2018-02-12 01:16:00,203 - denyhosts : INFO new denied hosts: ['192.168.10.205'] 同时,/etc/hosts.deny中也会加入192.168.10.205,即禁止这台机器使用ssh来链接: [root@host-200 ~]# cat /etc/hosts.deny # # hosts.deny This file contains access rules which are used to # deny connections to network services that either use # the tcp_wrappers library or that have been # started through a tcp_wrappers-enabled xinetd. # # The rules in this file can also be set up in # /etc/hosts.allow with a 'deny' option instead. # # See 'man 5 hosts_options' and 'man 5 hosts_access' # for information on rule syntax. # See 'man tcpd' for information on tcp_wrappers # sshd: 192.168.10.205 若要手动容许该IP经过ssh访问,请把对应的IP从/etc/hosts.deny中删掉,而后,重启denyhosts服务便可。
除了上面源码方式安装,还能够采用yum安装DenyHosts
1)yum 安装 denyhosts [root@denyhosts ~]# yum search denyhosts [root@denyhosts ~]# yum install denyhosts [root@denyhosts ~]# rpm -ql denyhosts 2)denyhosts几个比较重要的目录 该目录中主要存放计划任务,日志压缩 以及 chkconfig 和 service 启动的文档 /etc/cron.d/denyhosts /etc/denyhosts.conf /etc/logrotate.d/denyhosts /etc/rc.d/init.d/denyhosts /etc/sysconfig/denyhosts 该目录中主要存放 denyhosts 所拒绝及容许的一些主机信息 /var/lib/denyhosts/allowed-hosts /var/lib/denyhosts/allowed-warned-hosts /var/lib/denyhosts/hosts /var/lib/denyhosts/hosts-restricted /var/lib/denyhosts/hosts-root /var/lib/denyhosts/hosts-valid /var/lib/denyhosts/offset /var/lib/denyhosts/suspicious-logins /var/lib/denyhosts/sync-hosts /var/lib/denyhosts/users-hosts /var/lib/denyhosts/users-invalid /var/lib/denyhosts/users-valid /var/log/denyhosts 3)来看看 /etc/denyhosts.conf 中的配置参数 [root@denyhosts ~]# egrep -v "(^$|^#)" /etc/denyhosts.conf ############ THESE SETTINGS ARE REQUIRED ############ # 系统安全日志文件,主要获取ssh信息 SECURE_LOG = /var/log/secure # 拒绝写入IP文件 hosts.deny HOSTS_DENY = /etc/hosts.deny # 过多久后清除已经禁止的,其中w表明周,d表明天,h表明小时,s表明秒,m表明分钟 PURGE_DENY = 4w # denyhosts所要阻止的服务名称 BLOCK_SERVICE = sshd # 容许无效用户登陆失败的次数 DENY_THRESHOLD_INVALID = 3 # 容许普通用户登陆失败的次数 DENY_THRESHOLD_VALID = 10 # 容许ROOT用户登陆失败的次数 DENY_THRESHOLD_ROOT = 6 # 设定 deny host 写入到该资料夹 DENY_THRESHOLD_RESTRICTED = 1 # 将deny的host或ip纪录到Work_dir中 WORK_DIR = /var/lib/denyhosts SUSPICIOUS_LOGIN_REPORT_ALLOWED_HOSTS=YES # 是否作域名反解 HOSTNAME_LOOKUP=YES # 将DenyHOts启动的pid纪录到LOCK_FILE中,已确保服务正确启动,防止同时启动多个服务 LOCK_FILE = /var/lock/subsys/denyhosts ############ THESE SETTINGS ARE OPTIONAL ############ # 管理员Mail地址(能够不用设置) ADMIN_EMAIL = root SMTP_HOST = localhost SMTP_PORT = 25 SMTP_FROM = DenyHosts <nobody@localhost> SMTP_SUBJECT = DenyHosts Report from $[HOSTNAME] # 有效用户登陆失败计数归零的时间 AGE_RESET_VALID=5d # ROOT用户登陆失败计数归零的时间 AGE_RESET_ROOT=25d # 用户的失败登陆计数重置为0的时间(/usr/share/denyhosts/restricted-usernames) AGE_RESET_RESTRICTED=25d # 无效用户登陆失败计数归零的时间 AGE_RESET_INVALID=10d ######### THESE SETTINGS ARE SPECIFIC TO DAEMON MODE ########## # denyhosts log文件 DAEMON_LOG = /var/log/denyhosts DAEMON_SLEEP = 30s # 该项与PURGE_DENY 设置成同样,也是清除hosts.deniedssh 用户的时间 DAEMON_PURGE = 1h 4)启动 denyhosts 服务并查看状态 [root@denyhosts ~]# service denyhosts status denyhosts.py is stopped [root@denyhosts ~]# service denyhosts start Starting denyhosts: [ OK ] [root@denyhosts ~]# service denyhosts status denyhosts.py (pid 19784) is running... 5)经过测试 invalid、valid、root 等用户设置不一样的ssh链接失败次数,来测试 denyhosts。 这里只测试使用系统中不存在的用户进行失败登陆尝试~ 这里容许 invalid 用户只能失败4次、ROOT 用户失败7次、valid用户失败10次 DENY_THRESHOLD_INVALID = 4 DENY_THRESHOLD_VALID = 10 DENY_THRESHOLD_ROOT = 7 测试: 在客户机(192.168.10.204)上使用一个没有建立的用户失败登陆denyhosts机器(192.168.10.202)四次 [root@centos6-06 ~]# ssh -p22 haha@192.168.10.202 ....... 屡次尝试登录失败后,就会发现客户机就不能使用ssh登录denyhosts服务器了 [root@centos6-06 ~]# ssh -p22 haha@192.168.10.202 ssh_exchange_identification: Connection closed by remote host 查看denyhosts服务器的/etc/hosts.deny [root@denyhosts ~]# echo -n "" > /var/log/secure [root@denyhosts ~]# tail -f /var/log/secure Feb 12 01:47:14 ceph-node3 sshd[19885]: refused connect from 192.168.10.204 (192.168.10.204) [root@denyhosts ~]# tail -f /etc/hosts.deny # # The rules in this file can also be set up in # /etc/hosts.allow with a 'deny' option instead. # # See 'man 5 hosts_options' and 'man 5 hosts_access' # for information on rule syntax. # See 'man tcpd' for information on tcp_wrappers # # DenyHosts: Mon Feb 12 01:44:48 2018 | sshd: 192.168.10.204 sshd: 192.168.10.204 /var/log/secure 日志信息: [root@denyhosts ~]# tail -f /var/log/secure ....... Feb 12 01:48:15 ceph-node3 sshd[19892]: Invalid user haha from 192.168.10.204 Feb 12 01:48:15 ceph-node3 sshd[19893]: input_userauth_request: invalid user haha Feb 12 01:48:17 ceph-node3 sshd[19892]: pam_unix(sshd:auth): check pass; user unknown Feb 12 01:48:17 ceph-node3 sshd[19892]: pam_unix(sshd:auth): authentication failure; logname= uid=0 euid=0 tty=ssh ruser= rhost=192.168.10.204 Feb 12 01:48:17 ceph-node3 sshd[19892]: pam_succeed_if(sshd:auth): error retrieving information about user haha Feb 12 01:48:18 ceph-node3 sshd[19892]: Failed password for invalid user haha from 192.168.10.204 port 60196 ssh2 Feb 12 01:48:19 ceph-node3 sshd[19892]: pam_unix(sshd:auth): check pass; user unknown Feb 12 01:48:19 ceph-node3 sshd[19892]: pam_succeed_if(sshd:auth): error retrieving information about user haha Feb 12 01:48:22 ceph-node3 sshd[19892]: Failed password for invalid user haha from 192.168.10.204 port 60196 ssh2 Feb 12 01:48:23 ceph-node3 sshd[19892]: pam_unix(sshd:auth): check pass; user unknown Feb 12 01:48:23 ceph-node3 sshd[19892]: pam_succeed_if(sshd:auth): error retrieving information about user haha Feb 12 01:48:25 ceph-node3 sshd[19892]: Failed password for invalid user haha from 192.168.10.204 port 60196 ssh2 Feb 12 01:48:25 ceph-node3 sshd[19893]: Connection closed by 192.168.10.204 Feb 12 01:48:25 ceph-node3 sshd[19892]: PAM 2 more authentication failures; logname= uid=0 euid=0 tty=ssh ruser= rhost=192.168.10.204 Feb 12 01:48:27 ceph-node3 sshd[19899]: refused connect from 192.168.10.204 (192.168.10.204)
关于DenyHosts中清除及添加可信主机记录
DenyHosts会读取多个记录确保没有漏网之鱼,若是想删除一个或多个已经禁止的主机IP,仅清空/var/log/secure和/etc/hosts.deny并不能完美解除已被锁定的IP, 很快就会被DenyHosts从新锁定,这也是DenyHosts强大的地方!!须要进入 /var/lib/denyhosts 目录,进入如下操做: 针对源码方式安装DenyHosts后的解锁ip的措施: 1)中止DenyHosts服务:service denyhosts stop 2)在下列文件中找到想解锁的IP记录并删除(一般只修改前两个文件就OK了)。不推荐这种方法,由于用vim在下面的文件中找到解锁IP是很是费时费力的. /var/log/secure /etc/hosts.deny /usr/share/denyhosts/data/hosts /usr/share/denyhosts/data/hosts-restricted /usr/share/denyhosts/data/hosts-root /usr/share/denyhosts/data/hosts-valid 若是不在意上面的记录文件, 推荐清空上面几个Linux系统日志而后从新开启DennyHosts. 清空这些Linux系统日志不会影响任何功能. 若是你以为这几个文件记录对你很重要(真的?), 请不要随意清空, 老老实实使用上面的方法. 清空上面几个Linux系统日志很简单, 在SSH中敲入下面的命令: cat /dev/null > /var/log/secure cat /dev/null > /etc/hosts.deny cat /dev/null > /usr/share/denyhosts/data/hosts cat /dev/null > /usr/share/denyhosts/data/hosts-restricted cat /dev/null > /usr/share/denyhosts/data/hosts-root cat /dev/null > /usr/share/denyhosts/data/hosts-valid cat /dev/null > /usr/share/denyhosts/data/offset cat /dev/null > /usr/share/denyhosts/data/suspicious-logins cat /dev/null > /usr/share/denyhosts/data/users-hosts cat /dev/null > /usr/share/denyhosts/data/users-invalid cat /dev/null > /usr/share/denyhosts/data/users-valid 把日志文件所有清空!要注意的是,清空后必定要 # service rsyslog restart 重启日志记数器! 由于若是你不重启服务,你会发现secure日志文件会中止记录一切活动了!那么denyhosts也就无效了 而后从新开启Deny Hosts # service denyhosts start 最好重启ssh服务和iptables防火墙(若是打开的话) #service sshd restart #service iptables restart 针对yum方式安装DenyHosts后的解锁ip的措施: 1)中止DenyHosts服务:service denyhosts stop 2)在 /etc/hosts.deny 中删除你想取消的主机IP 3)编辑 DenyHosts 工做目录的全部文件 /var/lib/denyhosts,而且删除已被添加的主机信息。 /var/lib/denyhosts/hosts /var/lib/denyhosts/hosts-restricted /var/lib/denyhosts/hosts-root /var/lib/denyhosts/hosts-valid /var/lib/denyhosts/users-hosts /var/lib/denyhosts/users-invalid /var/lib/denyhosts/users-valid 4)添加你想容许的主机IP地址到 /var/lib/denyhosts/allowed-hosts 5)启动DenyHosts服务: service denyhosts start
经过邮件接收 denyhosts 所发送的信息
1)安装sendmail邮件服务,请参考:http://www.cnblogs.com/kevingrace/p/6143977.html 2)denyhosts机器的主机名配置 [root@ceph-node1 ~]# cat /etc/hosts 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6 192.168.10.202 ceph-node1.localdomain ceph-node1 [root@ceph-node1 ~]# cat /etc/sysconfig/network NETWORKING=yes HOSTNAME=ceph-node1 2)修改 /etc/denyhosts.conf文件中的mail配置,并重denyhosts 服务 [root@ceph-node1 ~]# cat /etc/denyhosts.conf ...... # 管理员Mail地址 ADMIN_EMAIL = wangshibo@kevin.com //如有ip被禁用发邮件通知 SMTP_HOST = ceph-node1 //这个是mail机器的主机名。这里我是在denyhosts本机部署的mail服务 SMTP_PORT = 25 SMTP_FROM = DenyHosts <nobody@localhost> //邮件的发送发 SMTP_SUBJECT = DenyHosts Report from $[HOSTNAME] //邮件主题 [root@ceph-node1 ~]# /etc/init.d/denyhosts restart Stopping denyhosts: [ OK ] Starting denyhosts: [ OK ] 3)在客户机(192.168.10.210)上经过ssh链接denyhosts部署机(192.168.10.202),ssh链接失败次数到达denyhosts设置的失败次数后, 192.168.10.210的ip就会被denyhosts机器自动屏幕,这时候就会触发邮件发信动做。 注意:这里仅仅是在denyhosts机器刚发现客户机的ip被禁用的时候发送邮件,当已经禁用后,再次ssh链接失败后就不会重复发送邮件了,及邮件只会发送一次! [root@zabbix-server ~]# ssh -p22 root@192.168.10.202 The authenticity of host '192.168.10.202 (192.168.10.202)' can't be established. RSA key fingerprint is fd:82:c3:2d:f1:3e:0f:69:39:bf:7e:f7:82:59:a3:cb. Are you sure you want to continue connecting (yes/no)? yes Warning: Permanently added '192.168.10.202' (RSA) to the list of known hosts. root@192.168.10.202's password: Permission denied, please try again. root@192.168.10.202's password: Permission denied, please try again. root@192.168.10.202's password: Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password). [root@zabbix-server ~]# ssh -p22 root@192.168.10.202 root@192.168.10.202's password: Permission denied, please try again. root@192.168.10.202's password: Permission denied, please try again. root@192.168.10.202's password: Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password). [root@zabbix-server ~]# ssh -p22 root@192.168.10.202 root@192.168.10.202's password: Permission denied, please try again. root@192.168.10.202's password: Permission denied, please try again. root@192.168.10.202's password: Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password). [root@zabbix-server ~]# ssh -p22 root@192.168.10.202 root@192.168.10.202's password: Permission denied, please try again. root@192.168.10.202's password: Permission denied, please try again. root@192.168.10.202's password: Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password). [root@zabbix-server ~]# ssh -p22 root@192.168.10.202 ssh_exchange_identification: Connection closed by remote host 此时,客户机192.168.10.210的ip已经被denyhosts机器那边给自动屏蔽掉了,也是在这个时候,通知邮件发出去了。 [root@ceph-node1 ~]# cat /etc/hosts.deny ....... # DenyHosts: Mon Feb 12 04:34:10 2018 | sshd: 192.168.10.210 sshd: 192.168.10.210