这篇文章我们总结一下Linux系统中如何使用iptables屏蔽IP地址保障系统的安全或满足一些特殊的业务需求。这些知识点是一些日常操作的总结,如有不足遗漏,欢迎补充和指教。
屏蔽单个IP
屏蔽单个IP的命令如下,需要用具体IP替换:
#iptables -I INPUT -s xxx.xxx.xxx.xxx -j DROP#iptables -A INPUT -s xxx.xxx.xxx.xxx -j DROP
复制
列子:
# iptables -I INPUT -s 10.20.57.24 -j DROP# iptables -A INPUT -s 10.20.57.24 -j DROP
复制
这两个命令有啥区别呢?其实-A表示"追加" 一条规则,只能追加到末尾。而-I表示插入一条规则。
命令简单解释:
-I 表示添加-A 表示追加INPUT 表示入站xxx.xxx.xxxxxx 表示要屏蔽的IPDROP 表示丢弃数据包。
复制
其实上面命令只是修改了当前iptables的配置,然而它并没有保存这些配置修改,如果系统重启,这些配置就会丢失。所以如果想这些配置永久生效,那么你必须保存这些修改配置。一般而言使用service iptables save命令保存配置
CentOS/RHEL/Fedora系统
# service iptables save
复制
Ubuntu系统:
#sudo invoke-rc.d iptables-persistent save
复制
另外,iptables-save > etc/sysconfig/iptables可以把你当前的iptables规则放到/etc/sysconfig/iptables中,系统启动iptables时自动执行。
注意:本文后续的一些命令都是需要用上面命令保存的,后续不再重复强调。
屏蔽单个IP访问某个端口
如果我想阻止来自10.20.57.24的IP访问服务器的3306端口,可以这样处理:
# iptables -A INPUT -s IP-ADDRESS -p tcp --destination-port port_number -j DROP
复制
例子:
# iptables -A INPUT -s 10.20.57.24 -p tcp --destination-port 3306 -j DROP
复制
测试验证如下:

禁止服务器访问某IP
我们知道,上面是禁止其它IP访问服务器,那么如果要禁止服务器访问某些IP呢?其实上面是禁止进入服务器,那么如何禁止服务器出去呢?
#iptables -I OUTPUT -s xxx.xxx.xxx.xxx -j DROP#iptables -A OUTPUT -s xxx.xxx.xxx.xxx -j DROP
复制
例子:
#禁止服务器访问此IP#iptables -A OUTPUT -d 10.20.57.24 -j DROP# ping 10.20.57.24
复制

屏蔽IP网段
上面是屏蔽单个地址,实际环境中,我们可能需要屏蔽掉某个IP地址段。
屏蔽IP地址段的命令:
#iptables -I INPUT -s xxx.xxx.0.0/16 -j DROP#iptables -I INPUT -s xxx.xxx.xxx.0/24 -j DROP#iptables -A INPUT -s xxx.xxx.0.0/16 -j DROP#iptables -A INPUT -s xxx.xxx.xxx.0/24 -j DROP
复制
如果你设置过云端数据库或服务器的白名单或Linux服务器的防火墙,你可能见过IP地址后面接xxx.xxx.xxx.xxx/8 、xxx.xxx.xxx.xxx/16 、 xxx.xxx.xxx.xxx/24、xxx.xxx.xxx.xxx/32 这样的设置,那么/8, 16,/24 ,/32分别代表啥意思?这个数字标示了我们的网络号的位数,也就是子网掩码中前多少号为1,也就是掩码的位数。
在了解这个之前,我们先了解一下网络地址的相关概念:最初设计互联网络时,为了便于寻找以及层次化构造网络,每个IP地址包括两个标识码(ID),既网络ID和主机ID。同一个物理网络上的所有主机都使用同一个网络ID,网络上的一个主机(包括网络上工作站,服务器和路由器等)有一个主机ID与其对应。IP地址根据网络ID的不同分为5种类型,A类地址、B类地址、C类地址、D类地址和E类地址。其中D类和E类被用于特殊用途。我们一般很少用到。
A类:
A类地址第1字节为网络地址,其它3个字节为主机地址。
A类IP地址的默认子网掩码为255.0.0.0(由于255相当于二进制的8位1,所以也缩写成“/8”,表示网络号占了8位); 即11111111.00000000.00000000.00000000(1.0.0.0-126.0.0.0)(默认子网掩码:255.0.0.0或 0xFF000000)第一个字节为网络号,后三个字节为主机号。该类IP地址的最前面为“0”。一般用于大型网络。
A类地址范围:1.0.0.1—126.155.255.254
B类:
B类地址第1字节和第2字节为网络地址,其它2个字节为主机地址。B类IP地址的默认子网掩码为255.255.0.0(/16); 即11111111.11111111.00000000.00000000(128.0.0.0-191.255.0.0)。(默认子网掩码:255.255.0.0或0xFFFF0000)前两个字节为网络号,后两个字节为主机号。该类IP地址的最前面为“10”。一般用于中等规模网络。
B类地址范围:128.0.0.1—191.255.255.254。
C类:
C类地址第1字节、第2字节和第3个字节为网络地址,第4个个字节为主机地址。另外第1个字节的前三位固定为110 (192.0.0.0-223.255.255.0)(子网掩码:255.255.255.0或 0xFFFFFF00)前三个字节为网络号,最后一个字节为主机号。该类IP地址的最前面为“110”。一般用于小型网络。
C类地址范围:192.0.0.1—223.255.255.254。
D类:
D类地址是多播地址,一般用于多路广播用户[1]。它的第1个字节的前四位固定为1110。
D类地址范围:224.0.0.1—239.255.255.254
E类:
E类是保留地址。E类地址也不分网络地址和主机地址,它的第1个字节的前五位固定为11110。
E类地址范围:地址的网络号取值于240~255之间, 具体为240.0.0.1—255.255.255.254。
所以你在设置防火墙或云端数据库的白名单时,就会发现一些 xxx.xxx.xxx.xxx/8 这类设置是啥意思了,它们表示一段IP地址。
/8 表示子网掩码为255.0.0.0/16 表示子网掩码为255.255.0.0/24 表示子网掩码为255.255.255.0/30 表示子网掩码为255.255.255.252/32 表示子网掩码为255.255.255.255
复制
例如,指定IP段:若输入192.168.0.0/24,表明允许192.168.0.0到192.168.0.255之间所有的IP地址访问当前实例。而,192.168.0.0/16,表示运行192.68.0.0到192.168.255.255之间所有的IP地址。有些云平台仅仅支持 "/8","/16","/24"
禁止服务器访问IP段
#禁止服务器访问此IP段#iptables -A OUTPUT -d xxx.xxx.xxx.xxx/8 -j DROP#iptables -A OUTPUT -d xxx.xxx.xxx.xxx/16 -j DROP#iptables -A OUTPUT -d xxx.xxx.xxx.xxx/24 -j DROP#iptables -A OUTPUT -d xxx.xxx.xxx.xxx/32 -j DROP
复制
例子:
#iptables -A OUTPUT -d 10.20.0.0/16 -j DROP
复制
查看屏蔽了哪些IP
既然你屏蔽了一些IP,那么我们怎么查看呢?下面是查看屏蔽IP的相关命令
# iptables --list INPUT# iptables --list OUTPUT# iptables -L INPUT# iptables -L OUTPUT
复制
例子1:
# iptables -L INPUTChain INPUT (policy ACCEPT)target prot opt source destination DROP all -- 10.20.57.24 anywhere RH-Firewall-1-INPUT all -- anywhere anywhere
复制
例子2:
注意:屏蔽的IP地址,可能会转换为机器名(xxxx.xxx.xxx.com表示对于机器的机器名),如下例子所示
#iptables -L INPUTChain INPUT (policy ACCEPT)target prot opt source destination DROP all -- xxxx.xxx.xxx.com anywhere DROP all -- 10.20.57.24 anywhere
复制
例子3:
#iptables -L OUTPUTChain OUTPUT (policy ACCEPT)target prot opt source destination DROP all -- anywhere 10.20.57.24 DROP all -- anywhere 10.20.0.0/16
复制
清空屏蔽的IP
有时候需要屏蔽IP,但是随着业务的变化或情况变化,也有可能需要取消限制一些IP(解封IP)。
# iptables -D INPUT -s xxx.xxx.xxx.xxx -j DROP # iptables -D INPUT -s IP-ADDRESS -p tcp --destination-port port_number -j DROP# iptables --flush #表示全部清理# iptables -t filter -D INPUT -s xxx.xxx.xxx.xxx -j DROP# iptables -t filter -D OUTPUT -s xxx.xxx.xxx.xxx -j DROP
复制
例子1:
#iptables -L INPUTChain INPUT (policy ACCEPT)target prot opt source destination DROP tcp -- 10.20.57.24 anywhere tcp dpt:mysql# iptables -D INPUT -s 10.20.57.24 -p tcp --destination-port 3306 -j DROP# iptables -L INPUTChain INPUT (policy ACCEPT)target prot opt source destination
复制
例子2:
#iptables -L INPUT Chain INPUT (policy ACCEPT)target prot opt source destination DROP all -- 10.20.57.24 anywhere RH-Firewall-1-INPUT all -- anywhere anywhere # iptables -t filter -D INPUT -s 10.20.57.24 -j DROP# iptables -L INPUT
Chain INPUT (policy ACCEPT)
target prot opt source destination
RH-Firewall-1-INPUT all -- anywhere anywhere复制
例子3:
#iptables -L INPUT
Chain INPUT (policy ACCEPT)
target prot opt source destination
DROP all -- 10.20.0.0/16 anywhere
RH-Firewall-1-INPUT all -- anywhere anywhere
#iptables -t filter -D INPUT -s 10.20.0.0/16 -j DROP
#iptables -L INPUT
Chain INPUT (policy ACCEPT)
target prot opt source destination
RH-Firewall-1-INPUT all -- anywhere anywhere复制
例子4:
#iptables -L OUTPUT
Chain OUTPUT (policy ACCEPT)
target prot opt source destination
DROP all -- anywhere 10.20.57.24
# iptables -t filter -D INPUT -s 10.20.57.24 -j DROP
iptables: Bad rule (does a matching rule exist in that chain?).
# iptables -t filter -D OUTPUT -d 10.20.57.24 -j DROP复制
注意:删除屏蔽服务器访问其他IP地址,必须加上-t filter,否则就会有上面信息输出。