iptables 防火墙基础配置及常用实例
CentOS 6 防火墙设置需要用 iptables 命令,没有 Firewalld 易用。本文介绍 iptables 防火墙基本使用方法,包括启用 iptables、初始化安全配置、添加 / 删除端口等常用实例。
iptables 安装
检查 iptables 软件包是否已安装,没有用 yum install iptables
安装。
yum list installed | grep "iptables"
iptables 基本操作及配置文件路径如下。用 iptables -h
或 man iptables
命令可获取详细参数。
启动 iptables | service iptables start |
关闭 iptables | service iptables stop |
重启 iptables | service iptables restart |
iptables 规则文件 | /etc/sysconfig/iptables |
iptables 配置文件 | /etc/sysconfig/iptables-config |
iptables 设置开机自启
使用 chkconfig --list iptables
命令检查是否已设置服务自动运行。
iptables 0:off 1:off 2:on 3:on 4:on 5:on 6:off
上面结果已经设置自启,其中数字表示运行级别,预设级别是 2345。没有设置则用下面命令。
chkconfig --level 2345 iptables on
保存和查看 iptables 配置
运行 iptables 命令改变配置只是当前生效,为确保重启不会丢失设置,需要运行下面命令。
service iptables save
有时还可能需重启 iptables 服务生效。如发现添加规则不生效或丢失,先尝试重启服务。
service iptables restart
查看现有 iptables 配置用 -L
参数,-n
表示显示具体 IP / 端口,-v
显示详细信息。
iptables -L -n
如要查看 nat 链配置,用下面命令。
iptables -L -n -t nat
iptables 规则备份与恢复
如果需要,可以将 iptables 规则备份到文件(储存在当前用户目录下)。
iptables-save > $HOME/iptables-backup-`date +%F`
用备份文件恢复 iptables 配置,运行后别忘了保存设置。
iptables-restore < $HOME/iptables-backup-2019-03-08
重置清空 iptables 规则
将内建链恢复到默认允许状态(先进行此操作,否则若之前设置了丢弃 INPUT 连接,删除规则后 SSH 会立即断开且无法连接),删除所有 iptables 规则及自定义链,清零规则链中的流量统计。
iptables -P INPUT ACCEPT iptables -P FORWARD ACCEPT iptables -P OUTPUT ACCEPT iptables -F iptables -X iptables -Z
iptables 基础配置及实例
1. 创建两个自定义链(用于管理 TCP / UDP 流量,名称可随意)。此步骤非必需,不添加可用 -p tcp
和 -p udp
参数指定协议。如果不添加,下面提及命令里的 USERTCP
和 USERUDP
关键词改为 INPUT
,并且第 8 步命令稍作修改。
iptables -N USERTCP iptables -N USERUDP
2. 设置本机不转发数据包,将 FORWARD 链策略设置为 DROP(丢弃)。
iptables -P FORWARD DROP
3. 对于本机传出流量不做限制,将 OUTPUT 链策略设置 ACCEPT(接受)。
iptables -P OUTPUT ACCEPT
4. 外部访问本机流量默认拒绝,后续手动添加我们需要的传入连接,将 INPUT 链策略设置 DROP。
为避免设置后那些正在连接的会话被立即断开,如 SSH 远程连接。先设置接受这类连接。
iptables -A INPUT -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT
之后再添加 INPUT 默认处理规则。
iptables -P INPUT DROP
5. 允许来自 loopback
网卡的流量,这对于许多程序和服务来说是必需。
iptables -A INPUT -i lo -j ACCEPT
6. 丢弃无法识别的数据包,匹配 INVALID
状态流量。
iptables -A INPUT -m conntrack --ctstate INVALID -j DROP
7. 允许 ICMP 响应请求(ICMP 流量类型 ID 对照表),也称为 ping,匹配其 NEW
状态数据包。
iptables -A INPUT -p icmp --icmp-type 8 -m conntrack --ctstate NEW -j ACCEPT
8. 将之前创建的 USERTCP
和 USERUDP
自定义链连接到 INPUT
链以处理新的传入连接,匹配 NEW
状态流量(其中 --syn
参数只匹配 TCP 握手信号 SYN 数据包)。
iptables -A INPUT -p udp -m conntrack --ctstate NEW -j USERUDP iptables -A INPUT -p tcp --syn -m conntrack --ctstate NEW -j USERTCP
上面多次提到过不同连接状态流量,这里通俗解释下。
NEW |
表示数据包是新的连接,在接受流量后,则由 RELATED /ESTABLISHED 规则处理 |
ESTABLISHED |
表示已建立的连接,当接受 NEW 数据包后则切换到该状态 |
RELATED |
表示在现有连接中关联产生的新连接,例如 FTP 通过 21 端口发送命令,但其可以用 20 端口(Port 主动模式)或其它端口(PASV 被动模式)传送数据 |
INVALID |
表示未知状态的数据包,通常设置丢弃。需留意,此类型也有正常流量,例如 ICMPv6 邻居发现协议数据包,尽管被标识 INVALID ,但其流量是有意义的。可通过添加 iptables -A INPUT -p 41 -j ACCEPT 规则接受 |
9. 对于因端口没开放而拒绝的 UDP / TCP 连接,返回 ICMP 端口无法访问 和 TCP 重置 错误消息。
iptables -A INPUT -p udp -j REJECT --reject-with icmp-port-unreachable iptables -A INPUT -p tcp -j REJECT --reject-with tcp-reset
对于其它协议的类似情况,添加一个通用规则以返回 ICMP 协议无法访问的错误信息。
iptables -A INPUT -j REJECT --reject-with icmp-proto-unreachable
10. 添加规则允许 SSH 远程连接(端口号按实际情况设置,通常是 22 端口)。
iptables -A USERTCP -p tcp --dport 22 -j ACCEPT
按需添加其它常见传入连接目标端口。若要添加其它端口,相应修改命令中的 协议 / 端口 就行。
允许 HTTP / HTTPS 服务连接 | iptables -A USERTCP -p tcp --dport 80 -j ACCEPT iptables -A USERTCP -p tcp --dport 443 -j ACCEPT |
允许 FTP 服务连接 | iptables -A USERTCP -p tcp --dport 21 -j ACCEPT iptables -A USERTCP -p tcp --dport 20 -j ACCEPT |
允许 DNS 服务器请求连接 | iptables -A USERTCP -p tcp --dport 53 -j ACCEPT iptables -A USERUDP -p udp --dport 53 -j ACCEPT |
允许 Sendmail / Postfix 邮件服务 | iptables -A USERTCP -p tcp --dport 25 -j ACCEPT |
允许 IMAP / IMAPS 邮件服务 | iptables -A USERTCP -p tcp --dport 143 -j ACCEPT iptables -A USERTCP -p tcp --dport 993 -j ACCEPT |
允许 POP3 / POP3S 邮件服务 | iptables -A USERTCP -p tcp --dport 110 -j ACCEPT iptables -A USERTCP -p tcp --dport 995 -j ACCEPT |
11. 屏蔽指定 IP 或特定 IP 范围访问。
# 屏蔽单个 IP iptables -I INPUT -s 208.80.152.2 -j DROP # 屏蔽 208.80.152.1 至 208.80.152.254 范围 IP iptables -I INPUT -s 208.80.152.0/24 -j DROP # 屏蔽 208.80.0.1 至 208.80.255.254 范围 IP iptables -I INPUT -s 208.80.0.0/16 -j DROP # 屏蔽 208.0.0.1 至 208.255.255.254 范围 IP iptables -I INPUT -s 208.0.0.0/8 -j DROP
12. 屏蔽或允许指定 IP 访问特定端口。
# 屏蔽指定 IP 访问 22 端口 iptables -A INPUT -s 208.80.152.2 --dport 22 -j DROP # 允许指定 IP 访问 22 端口 iptables -A INPUT -s 208.80.152.2 --dport 22 -j ACCEPT
13. 删除已存在的指定规则。先用 iptables -L -n --line-numbers
查看规则排序 ID,例如删除第10条规则,则用 iptables -D INPUT 10
命令。
参考及延伸资料:
- https://wiki.archlinux.org/index.php/Simple_stateful_firewall
- https://www.thegeekstuff.com/2011/06/iptables-rules-examples/
- https://www.vpser.net/security/linux-iptables.html