防爆破登录:配置/etc/hosts.deny禁止ip尝试ssh或者telnet操作

买的tx云服务器,每天登录上去就提示成千上万条尝试登录记录,看着就烦。于是找到个禁止ip登录的方法。

/etc/hosts.allow和/etc/hosts.deny,这两个文件中添加ip可以允许和禁止指定ip登录。不过由于我登录的ip是动态的,只能使用deny文件去禁止其他ip登录。如果自己登录的IP是固定的,可以直接在deny中配置sshd:all,再在allow中配置sshd:你的IP,这样就只有你自己能登录了,不用再担心别人恶意爆破了。

配置如下

$ cat /etc/hosts.deny |head -n 20
#
# 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:209.141.35.3
sshd:45.61.188.131
sshd:137.184.7.179
sshd:178.62.3.183
sshd:91.215.137.38

默认修改就生效了的,不用重启什么的操作。/etc/hosts.allow不配置就是允许所有IP。若配置了会先读取allow文件,没有匹配再读取deny文件。

写了个脚本,对尝试登录10次失败的IP加入到deny文件中:

#!/bin/sh

#登录失败次数大于10的ip
IP=$(awk '/Failed/{print $(NF-3)}' /var/log/secure | sort |uniq -c |awk '{if($1>10) print $2}')
hostdeny=/etc/hosts.deny

for i in $IP
do
	#如果ip不存在,则写入deny文件
	if [ ! $(grep $i $hostdeny) ];then 
		echo "sshd:$i" >> $hostdeny
	fi
done

登录失败的操作记录可以在/var/log/secure日志文件中查看,也可以使用lastb命令查看。

$ lastb |head
testing  ssh:notty    47.103.112.43    Wed Feb 16 11:23 - 11:23  (00:00)    
jp       ssh:notty    177.74.124.97    Wed Feb 16 11:23 - 11:23  (00:00)    
mi       ssh:notty    1.117.157.44     Wed Feb 16 11:23 - 11:23  (00:00)    
jp       ssh:notty    177.74.124.97    Wed Feb 16 11:23 - 11:23  (00:00)    
root     ssh:notty    109.237.110.198  Wed Feb 16 11:23 - 11:23  (00:00)    
mi       ssh:notty    1.117.157.44     Wed Feb 16 11:23 - 11:23  (00:00)    
summit   ssh:notty    43.156.42.20     Wed Feb 16 11:23 - 11:23  (00:00)    
root     ssh:notty    42.194.142.143   Wed Feb 16 11:23 - 11:23  (00:00)    
summit   ssh:notty    43.156.42.20     Wed Feb 16 11:23 - 11:23  (00:00)    
paco     ssh:notty    181.61.221.93    Wed Feb 16 11:23 - 11:23  (00:00)
  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值