之前用过 fail2ban,后来发现还是有问题,每天 last 看有大量的尝试,看着就烦,后来研究了一下 iptables,具体如下
新建自己 IP 库 链
iptables -N myip
所有访问 22 端口的先检查 myip 链
iptables -A INPUT -p tcp --dport 22 -j myip
检查后返回来的都扔掉
iptables -A INPUT -p tcp --dport 22 -j DROP
在 myip 链上加入自己的 IP (段),允许通过
iptables -A myip -s 222.222.222.222/32 -j ACCEPT
iptables -A myip -s 111.224.0.0/16 -j ACCEPT
其它 ip 返回去
iptables -A myip -j RETURN
整体效果
Chain INPUT (policy ACCEPT 1566 packets, 551K bytes)
pkts bytes target prot opt in out source destination
1362 136K myip tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:22
6 328 DROP tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:22
Chain FORWARD (policy ACCEPT 1086 packets, 435K bytes)
pkts bytes target prot opt in out source destination
Chain OUTPUT (policy ACCEPT 2006 packets, 595K bytes)
pkts bytes target prot opt in out source destination
Chain myip (1 references)
pkts bytes target prot opt in out source destination
676 78901 ACCEPT all -- * * 222.223.222.222/32 0.0.0.0/0
678 56618 ACCEPT all -- * * 111.224.0.0/16 0.0.0.0/0
8 408 RETURN all -- * * 0.0.0.0/0 0.0.0.0/0
保存
iptables-save > /etc/iptables.rules
在网卡中生效
vi /etc/network/interfaces
pre-up iptables-restore < /etc/iptables.rules
新建自己 IP 库 链
iptables -N myip
所有访问 22 端口的先检查 myip 链
iptables -A INPUT -p tcp --dport 22 -j myip
检查后返回来的都扔掉
iptables -A INPUT -p tcp --dport 22 -j DROP
在 myip 链上加入自己的 IP (段),允许通过
iptables -A myip -s 222.222.222.222/32 -j ACCEPT
iptables -A myip -s 111.224.0.0/16 -j ACCEPT
其它 ip 返回去
iptables -A myip -j RETURN
整体效果
Chain INPUT (policy ACCEPT 1566 packets, 551K bytes)
pkts bytes target prot opt in out source destination
1362 136K myip tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:22
6 328 DROP tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:22
Chain FORWARD (policy ACCEPT 1086 packets, 435K bytes)
pkts bytes target prot opt in out source destination
Chain OUTPUT (policy ACCEPT 2006 packets, 595K bytes)
pkts bytes target prot opt in out source destination
Chain myip (1 references)
pkts bytes target prot opt in out source destination
676 78901 ACCEPT all -- * * 222.223.222.222/32 0.0.0.0/0
678 56618 ACCEPT all -- * * 111.224.0.0/16 0.0.0.0/0
8 408 RETURN all -- * * 0.0.0.0/0 0.0.0.0/0
保存
iptables-save > /etc/iptables.rules
在网卡中生效
vi /etc/network/interfaces
pre-up iptables-restore < /etc/iptables.rules