Linux: iptables && ufw 配置防火墙规则
Published in:2024-11-19 |

iptables 简介

iptables 是一个用户空间实用程序,它允许系统管理员配置 Linux 内核防火墙的IP 数据包过滤规则,这些规则以不同的 Netfilter 模块实现。过滤器组织在一组表中,其中包含如何处理网络流量数据包的规则链。目前,不同的内核模块和程序用于不同的协议;iptables 适用于 IPv4,ip6tables 适用于 IPv6,arptables 适用于 ARP,ebtables 适用于以太网帧。

iptables 需要提升权限才能运行,并且必须由root用户执行(sudo && sudo -i),否则无法正常运行。

man 命令查看使用手册

  • man iptables :查看 iptables 使用手册。
  • man ip6tables :查看 ip6tables 使用手册。
  • man arptables :查看 arptables 使用手册。
  • man ebtables :查看 ebtables 使用手册。

iptables DESCRIPTION

1
2
3
4
5
6
7
8
Iptables  and  ip6tables are used to set up, maintain, and inspect the tables of
IPv4 and IPv6 packet filter rules in the Linux kernel. Several different tables
may be defined. Each table contains a number of built-in chains and may also
contain user-defined chains.

Each chain is a list of rules which can match a set of packets. Each rule spec‐
ifies what to do with a packet that matches. This is called a `target', which
may be a jump to a user-defined chain in the same table.

iptables [options -L] 列出所有的 IPv4 防火墙规则

1
2
3
4
5
6
7
8
9
10
11
12
13
-L, --list [chain]
List all rules in the selected chain. If no chain is selected, all
chains are listed. Like every other iptables command, it applies to the
specified table (filter is the default), so NAT rules get listed by
iptables -t nat -n -L
Please note that it is often used with the -n option, in order to avoid
long reverse DNS lookups. It is legal to specify the -Z (zero) option as
well, in which case the chain(s) will be atomically listed and zeroed.
The exact output is affected by the other arguments given. The exact
rules are suppressed until you use
iptables -L -v
or iptables-save(8).

请添加图片描述

Options: -v

1
2
3
4
5
6
7
8
9
10
11
12
-v, --verbose
Verbose output. This option makes the list command show the interface
name, the rule options (if any), and the TOS masks. The packet and byte
counters are also listed, with the suffix 'K', 'M' or 'G' for 1000,
1,000,000 and 1,000,000,000 multipliers respectively (but see the -x flag
to change this). For appending, insertion, deletion and replacement,
this causes detailed information on the rule or rules to be printed. -v
may be specified multiple times to possibly emit more detailed debug
statements: Specified twice, iptables-legacy will dump table info and en‐
tries in libiptc, iptables-nft dumps rules in netlink (VM code) presenta‐
tion. Specified three times, iptables-nft will also dump any netlink
messages sent to kernel.

Options: -n --numeric

1
2
3
4
-n, --numeric
Numeric output. IP addresses and port numbers will be printed in numeric
format. By default, the program will try to display them as host names,
network names, or services (whenever applicable).

请添加图片描述

iptables 配置防火墙规则 [允许/禁止] 被其他主机 ping 到

iptables 默认允许主机被其他机器 ping 到(即允许 ICMP echo 请求和回显应答)。如果你的机器是一台 Linux 云服务器,除了 Linux 系统内的防火墙规则正确配置以外,还要正确配置云服务器提供商的控制台界面的防火墙规则,如下:(放行 IPv4 ICMP 流量)
请添加图片描述
允许被 ping 到:
请添加图片描述

iptables 禁止 ICMP echo

命令 iptables -A INPUT -p icmp --icmp-type echo-request -j DROP 执行该命令后,即使云服务器提供商的控制台界面放行 ICMP,但是由于 Linux 系统中的防火墙规则中丢弃了 ICMP echo,这仍然会导致服务器无法被 ping 到。如果你的Linux机器在云服务器环境下,通常有两层防火墙,一层是云服务器提供商的控制台界面的防火墙,一层是 Linux 系统内的防火墙,两侧防火墙都正确配置,才能放行指定流量。

执行了以上命令以后,防火墙规则中多出了一条 icmp 类型的规则:
291 8448 DROP 1 -- * * 0.0.0.0/0 0.0.0.0/0 icmptype 8,DROP 意思是丢弃。在这条规则上面,我也配置了 TCP 8080/443/12000/80 ACCEPT 等规则。
请添加图片描述

允许 ICMP echo:iptables -D INPUT -p icmp --icmp-type 8 -j DROP

  • -D:这个选项表示要删除(Delete)一条规则。
  • INPUT:指定要删除规则所在的链。INPUT 链用于处理进入本机的数据包。
  • -p icmp:指定要匹配的协议类型。在这个例子中,-p icmp 表示匹配 ICMP(Internet Control Message Protocol,互联网控制消息协议)数据包。ICMP 通常用于发送错误消息、操作消息等,比如 ping 命令就使用 ICMP echo 请求和回复。
  • --icmp-type 8:进一步指定要匹配的 ICMP 数据包类型。在这个例子中,8 是 ICMP echo 请求的类型码。这意味着这条规则之前是用来匹配并处理 ICMP echo 请求(即 ping 请求)的。
  • -j DROP:指定匹配到规则的数据包应该采取的动作。DROP 表示丢弃数据包,不给出任何回应。

iptables -D INPUT -p icmp --icmp-type 8 -j DROP 这条命令的意思是:从 INPUT 链中删除一条规则,该规则之前用于丢弃所有 ICMP echo 请求(即 ping 请求)数据包。

命令执行后:
请添加图片描述
为了节省网络带宽,并提高安全性,我个人比较喜欢禁止 ICMP 流量。

iptables 放行一个指定的端口

比如放行 16888 端口的 TCP 流量:
iptables -A INPUT -p tcp --dport 16888 -j ACCEPT

1
2
3
4
-A, --append chain rule-specification
Append one or more rules to the end of the selected chain. When the
source and/or destination names resolve to more than one address, a rule
will be added for each possible address combination.
  • -A INPUT:-A 选项表示添加(Append)一条规则到指定的链中。在这个例子中,INPUT 是链的名称,它用于处理进入本机的数据包。
  • -p tcp-p 选项指定了要匹配的协议类型。在这个例子中,-p tcp 表示匹配 TCP(Transmission Control Protocol,传输控制协议)数据包。
  • --dport 16888--dport 选项指定了目的端口(Destination Port)的号码。在这个例子中,16888 是要匹配的 TCP 目的端口号。
  • -j ACCEPT-j 选项指定了匹配到规则的数据包应该采取的动作。ACCEPT 表示接受数据包,允许它继续通过防火墙进入本机。
1
2
3
4
5
6
7
8
-j, --jump target
This specifies the target of the rule; i.e., what to do if the packet
matches it. The target can be a user-defined chain (other than the one
this rule is in), one of the special builtin targets which decide the
fate of the packet immediately, or an extension (see MATCH AND TARGET EX‐
TENSIONS below). If this option is omitted in a rule (and -g is not
used), then matching the rule will have no effect on the packet's fate,
but the counters on the rule will be incremented.
1
2
3
4
5
6
7
8
9
10
11
12
TARGETS
A firewall rule specifies criteria for a packet and a target. If the packet
does not match, the next rule in the chain is examined; if it does match, then
the next rule is specified by the value of the target, which can be the name of
a user-defined chain, one of the targets described in iptables-extensions(8), or
one of the special values ACCEPT, DROP or RETURN.

ACCEPT means to let the packet through. DROP means to drop the packet on the
floor. RETURN means stop traversing this chain and resume at the next rule in
the previous (calling) chain. If the end of a built-in chain is reached or a
rule in a built-in chain with target RETURN is matched, the target specified by
the chain policy determines the fate of the packet.

target 有 ACCEPT / DROP / RETURN / REJECT … …

  • REJECT:拒绝数据包,并向发送方发送一个错误响应。与 DROP 不同,REJECT 会让发送方知道数据包被拒绝了。

请添加图片描述

ufw 简介

Uncomplicated Firewall ( UFW ) 是一款用于管理netfilter 防火墙的程序,设计为易于使用。它使用由少量简单命令组成的命令行界面,并使用iptables进行配置。自 8.04 LTS 以来,所有Ubuntu安装中都默认提供 UFW 。自 10 以来, 所有Debian安装中都默认提供 UFW 。

ufw 建立在 iptables 之上的。从前面的 iptables 配置防火墙规则来看,iptables 的使用很繁琐,因为 iptables 是一个非常底层也比较老的一个工具。ufw 就是为了简化 iptables 而出现的。

安装 ufw,sudo apt-get install ufw

启用 / 禁用 ufw

sudo ufw enable
sudo ufw disable

设置默认入站策略为拒绝

1
2
3
bash复制代码

sudo ufw default deny incoming

设置默认出站策略为允许

1
2
3
bash复制代码

sudo ufw default allow outgoing

允许特定端口

1
2
3
bash复制代码

sudo ufw allow <port>

例如,允许SSH端口(默认22):

1
2
3
bash复制代码

sudo ufw allow ssh

或者允许特定端口号(如8080):

1
2
3
bash复制代码

sudo ufw allow 8080

拒绝特定端口

1
2
3
bash复制代码

sudo ufw deny <port>

例如,拒绝HTTP端口(默认80):

1
2
3
bash复制代码

sudo ufw deny http

或者拒绝特定端口号(如12345):

1
2
3
bash复制代码

sudo ufw deny 12345

允许或拒绝特定服务

ufw也允许你使用服务名来管理规则。例如,允许HTTP服务:

1
2
3
bash复制代码

sudo ufw allow http

拒绝HTTPS服务:

1
2
3
bash复制代码

sudo ufw deny https

允许或拒绝特定IP地址

允许来自特定IP地址的连接

1
2
3
bash复制代码

sudo ufw allow from <ip_address>

例如,允许来自192.168.1.100的连接:

1
2
3
bash复制代码

sudo ufw allow from 192.168.1.100

拒绝来自特定IP地址的连接

1
2
3
bash复制代码

sudo ufw deny from <ip_address>

例如,拒绝来自192.168.1.100的连接:

1
2
3
bash复制代码

sudo ufw deny from 192.168.1.100

查看UFW状态和规则

查看UFW状态

1
2
3
bash复制代码

sudo ufw status

此命令将显示UFW的当前状态(启用或禁用)以及已配置的规则概览。

查看详细状态

1
2
3
bash复制代码

sudo ufw status verbose

此命令将显示所有打开和拒绝的端口、正在运行的服务以及详细的规则信息。

删除规则

通过规则号删除

首先,使用sudo ufw status numbered命令查看带有编号的规则列表。然后,使用以下命令删除特定编号的规则:

1
2
3
bash复制代码

sudo ufw delete <number>

通过规则内容删除

可以直接通过规则内容(如端口号或服务名)来删除规则。例如,删除允许SSH连接的规则:

1
2
3
bash复制代码

sudo ufw delete allow ssh

或者删除允许特定端口(如8080)的规则:

1
2
3
bash复制代码

sudo ufw delete allow 8080

重置UFW

重置ufw将删除所有规则并禁用防火墙:

1
2
3
bash复制代码

sudo ufw reset

日志记录

ufw还支持日志记录功能,可以跟踪防火墙的活动。

启用日志记录

1
2
3
bash复制代码

sudo ufw logging on

设置日志级别

日志级别可以是offlowmediumhighfull。例如,将日志级别设置为medium

1
2
3
bash复制代码

sudo ufw logging medium

禁用日志记录

1
2
3
bash复制代码

sudo ufw logging off
Prev:
Linux: C语言发起 DNS 查询报文
Next:
Linux C/C++ Socket 编程