基于 iptables 设置只有自己的 IP(段)能 ssh 登录 vps

2019-01-16 09:36:22 +08:00
 snoopygao
之前用过 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
4161 次点击
所在节点    信息安全
21 条回复
tvirus
2019-02-09 13:45:10 +08:00
hosts.allow 和 hosts.deny

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/527455

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX