关于iptables 的几点说明

  • iptables需要提升权限才能操作,必须以root用户执行,否则无法运行。
  • 在大多数 Linux 系统上,iptables 安装为 /usr/sbin/iptables 并记录在其手册页中,安装时可以使用 man iptables 打开。它也可以在 /sbin/iptables 中找到,但由于 iptables 更像是一种服务而不是“基本二进制文件”,首选位置仍然是 /usr/sbin。
  • 它一般工作在第 3 层和第 4 层,例如:网络和传输层。
  • iptables 还负责管理数据链路层中的 ICMP(Internet 控制消息传递协议)
  • iptables 还支持 MAC 级过滤,因此它也适用于第 2 层(数据链路层)
  • 第 3 层侧重于源 (192.168.0.x) 和目标 (172.168.0.x) 地址。
  • 第 4 层侧重于协议、端口,TCP:80,UDP:69(大多数应用程序都依赖于 TCP 和 UDP 端口。

说明:

TCP/UDP 端口使用 16 位范围(0-65535),IP 地址基于 32 位范围(40 亿)

iptables软件包

检查服务器是否安装了 iptables rpm软件包

# rpm -qa | grep iptables
iptables-1.4.7-4.el6.i686
iptables-ipv6-1.4.7-4.el6.i686

检查内核是否编译为使用 iptables(此处 config-2.6.x.x 可能因内核而异)

# less /boot/config-2.6.32-220.el6.i686 | grep CONFIG_NETFILTER
CONFIG_NETFILTER=y

确保如上所示的第一行应为“y”

iptables 中的表类型

  • mangle - 使用 TCP/UDP/ICMP 更改数据包 (TOS/TTL)
  • NAT(网络地址转换)
  • Filter (IP包过滤)

说明:

NAT 允许更改 IP 地址和端口

iptables 的 ACL 语法

  • 链名称 - 动作(追加/插入/替换)
  • 表名(过滤器)- mangle/nat/user-defined
  • 第 3 层对象(源/目标)
  • 可选的第 4 层主题(tcp/udp 协议/端口)
  • 跳转/目标 -j - 接受/丢弃/拒绝/拒绝/记录
Linux iptables

iptables 是由 Linux 内核防火墙(作为不同的 Netfilter 模块实现)及其存储的链和规则提供的表。

iptables示例

阻止源 IP 192.168.0.20 与我们的系统通信

# iptables -A INPUT -s 192.168.0.30 -j DROP

所以在这里我将一条规则附加到源 192.168.0.30 的输入链中,要采取的操作是丢弃(DROP)所有来自源机器的数据包。

查看iptables中的当前规则

# iptables -L
Chain INPUT (policy ACCEPT)
target     prot opt source               destination
DROP       all  --  192.168.0.30         anywhere
ACCEPT     tcp  --  anywhere             anywhere            tcp spt:ssh
DROP       tcp  --  anywhere             anywhere            tcp dpt:telnet
Chain FORWARD (policy ACCEPT)
target     prot opt source               destination
Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination

所以现在如果 192.168.0.30 尝试连接到我们的本地机器,它会得到一个请求超时的答复。

Linux 查看iptables的其他命令

# iptables -L -v
Chain INPUT (policy ACCEPT 2559 packets, 223K bytes)
 pkts bytes target     prot opt in     out   source        destination
    0    0 DROP       all  --  any    any   192.168.0.30    anywhere
    0    0 ACCEPT     tcp  --  any    any   anywhere        anywhere            tcp spt:ssh
    0    0 DROP       tcp  --  any    any   anywhere        anywhere            tcp dpt:telnet
Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination
Chain OUTPUT (policy ACCEPT 297 packets, 40151 bytes)
 pkts bytes target     prot opt in     out     source               destination

这里 -v 显示 (k/M/G) 中的字节,这意味着被 iptables 应用的任何规则阻止或者允许的数据包字节

# iptables -L --line-numbers
Chain INPUT (policy ACCEPT)
num  target     prot opt source               destination
1    DROP       all  --  192.168.0.30         anywhere
2    ACCEPT     tcp  --  anywhere             anywhere          tcp spt:ssh
3    DROP       tcp  --  anywhere             anywhere          tcp dpt:telnet
Chain FORWARD (policy ACCEPT)
num  target     prot opt source               destination
Chain OUTPUT (policy ACCEPT)
num  target     prot opt source               destination
# iptables --list
Chain INPUT (policy ACCEPT)
target     prot opt source               destination
DROP       all  --  192.168.0.30         anywhere
ACCEPT     tcp  --  anywhere             anywhere            tcp spt:ssh
DROP       tcp  --  anywhere             anywhere            tcp dpt:telnet
Chain FORWARD (policy ACCEPT)
target     prot opt source               destination
Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination

Linux 添加/插入iptables 规则

我们可以将新规则添加到任何链中,也可以插入规则的不同之处,而添加规则将在最后一行结束,而如果我们希望规则首先优先于链中的任何其他规则,然后使用与 iptables 一起插入,如下所示

# iptables -I INSERT -s 192.168.0.30 -j DROP

其他例子

创建规则以允许所有人通过 ssh 连接到本地机器

# iptables -A INPUT -p tcp --dport 22 -j ACCEPT

创建规则以拒绝所有人对本地计算机的 telnet 访问

# iptables -A INPUT -p tcp --dport telnet -j DROP

Linux 删除iptables 规则

要从链中删除任何规则,我们将需要行号

例如:

# iptables -L --line-numbers
Chain INPUT (policy ACCEPT)
num  target     prot opt source               destination
1    DROP       all  --  192.168.0.30         anywhere
2    ACCEPT     tcp  --  anywhere             anywhere          tcp spt:ssh
3    DROP       tcp  --  anywhere             anywhere          tcp dpt:telnet
Chain FORWARD (policy ACCEPT)
num  target     prot opt source               destination
Chain OUTPUT (policy ACCEPT)
num  target     prot opt source               destination

假设我想删除源 192.168.0.30 的规则

# iptables -D INPUT 1

另外较复杂的方法,在第一个匹配的基础上通过提供完整的规则和 D 选项来删除规则

# iptables -D INPUT -s 192.168.0.30 -j DROP

替换iptables 规则

如果我们想要进行一些更改,我们还可以替换规则而不是删除和创建任何规则。

例如在上面的问题中假设我们要阻止来自 192.168.0.25 而不是 192.168.0.30 的通信,因此我们可以轻松修改规则

# iptables -R INPUT 1 -s 192.168.0.25 -j DROP

在 iptables 中保存或者恢复规则

# iptables -save (defaults dumps to STDOUT)
# iptables -restore (default reads rule from STDIN)

例子:

# iptables-save > rules.txt
# iptables-restore < rules.txt

清空规则

该术语用于从所有链中删除所有规则。

# iptables -F

此命令将暂时删除所有规则,但一旦我们重新启动 iptables 服务,所有规则将恢复为默认设置。

日期:2020-06-02 22:17:07 来源:oir作者:oir