iptables

本文介绍了iptables防火墙的基本概念,包括匹配规则和表链结构。详细讲解了如何安装、使用iptables,如命令操作、规则添加及删除,并重点讨论了如何实现规则的永久生效。同时,给出了实例,如限制端口访问和禁止特定IP,帮助读者理解iptables的实战应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

概念

表		用来存放链的容器
链		用来存放规则的容器
规则		是否允许
名称含义举例
iptables/netfilter容器包含或被包含关系国家
表 table用来存放链的容器
链 chain用来存放规则的容器
规则 Policy县城

防火墙匹配规则

在这里插入图片描述

  1. 防火墙是层层过滤的, 实际是按照配置规则的顺序从上到下,从前到后进行过滤的
  2. 如果匹配上规则 , 即明确表示是阻止(DROP) 还是通过(ACCEPT) 数据包就不再向下匹配新的规则
  3. 如果规则中没有明确的表示是阻止还是通过的, 也就是没有匹配规则, 向下进行匹配, 知道匹配默认规则得到明确的阻止还是通过
  4. 防火墙的默认规则是所有规则执行完才执行的

表和链

四表五链

在这里插入图片描述
NAT表有INPUT
第三个表叫Mangle 图片写错了 请无视

filter表

在这里插入图片描述

NAT表
在这里插入图片描述

perrouting    数据来之前
postrouting   数据来之后

安装和使用

安装

[root@web03 ~]# yum install  iptables iptables-services -y


启动
C7
[root@web03 ~]# systemctl start iptables.service 
[root@web03 ~]# systemctl enable iptables.service 
C6
[root@web03 ~]# /etc/init.d/iptables.service
[root@web03 ~]# chkconfig iptables on

命令

参数

-n 		加n 描述那列写的是具体地址 不加n 显示英文描述 eg  any where
-t		指定表
-I		添加规则(放在最前 写拒绝规则的时候使用)  
-A		添加规则(放在最后)  后面指定链(INPUT..)
-p		指定协议
--dport	目标端口
-j		处理的方法(DROP|ACCEPT)
-D		删除规则
-s		指定源IP 范围 或者网段
--line-number		显示行号
-m multiport		可以添加多个端口

查看当前防火墙规则

[root@web03 ~]# iptables -t nat -nL
[root@web03 ~]# iptables -t filter -nL
[root@web03 ~]# iptables -nL
Chain INPUT (policy ACCEPT)  <---默认规则 放行
target     prot opt source               destination         
ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0            state RELATED,ESTABLISHED
ACCEPT     icmp --  0.0.0.0/0            0.0.0.0/0           
ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0           
ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0            state NEW tcp dpt:22
REJECT     all  --  0.0.0.0/0            0.0.0.0/0            reject-with icmp-host-prohibited

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination         
REJECT     all  --  0.0.0.0/0            0.0.0.0/0            reject-with icmp-host-prohibited

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination         

防火墙关闭后 如果使用iptables -nL查看 , iptables会自动开启

模块加载

在这里插入图片描述

modprobe ip_tables
modprobe iptable_filter
modprobe iptable_nat
modprobe ip_conntrack
modprobe ip_conntrack_ftp
modprobe ip_nat_ftp
modprobe ipt_state	

查看

[root@web03 ~]# lsmod |egrep "nat|filter|ipt"
iptable_nat            12875  0 
nf_nat_ipv4            14115  1 iptable_nat
nf_nat                 26787  1 nf_nat_ipv4
ipt_REJECT             12541  2 
nf_reject_ipv4         13373  1 ipt_REJECT
nf_conntrack          133053  4 nf_nat,nf_nat_ipv4,xt_conntrack,nf_conntrack_ipv4
iptable_filter         12810  1 
ip_tables              27126  2 iptable_filter,iptable_nat
libcrc32c              12644  3 xfs,nf_nat,nf_conntrack

重启服务后失效 可以写进/etc/rc.local

清空规则

iptables -Z	清楚所有规则,不会处理默认规则
iptables -X	删除用户自定义的链
iptables -F	链的计数器清零(数据包计数器与数据包字节计数器)

只删除某一个规则
查看规则的行号

 [root@web03 ~]# iptables -nL --line-number
Chain INPUT (policy ACCEPT)
num  target     prot opt source               destination         
1    ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0            state RELATED,ESTABLISHED
2    ACCEPT     icmp --  0.0.0.0/0            0.0.0.0/0           
3    ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0           
4    ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0            state NEW tcp dpt:22
5    REJECT     all  --  0.0.0.0/0            0.0.0.0/0            reject-with icmp-host-prohibited

Chain FORWARD (policy ACCEPT)
num  target     prot opt source               destination         
1    REJECT     all  --  0.0.0.0/0            0.0.0.0/0            reject-with icmp-host-prohibited

Chain OUTPUT (policy ACCEPT)
num  target     prot opt source               destination         

删除

[root@web03 ~]# iptables -D INPUT  1

添加规则举例

拒绝规则写在前面
实现只能通过内网访问22端口

iptables -A INPUT -p tcp ! -s 172.16.1.0/24  --dport 22 -j DROP

只能访问80 443 22

iptables -I INPUT -p tcp -m multiport ! --dport  80,443,22 -j DROP

禁用某个IP

[root@web03 ~]# iptables -I INPUT -p tcp -s 10.0.0.6 -j DROP

规则的备份 及永久生效

永久生效

[root@web03 ~]# iptables-save >/etc/sysconfig/iptables

备份

[root@web03 ~]# iptables-save >>/root/rul
[root@web03 ~]# ll 
total 8
-rw-------. 1 root root 1499 Jan 10 14:42 anaconda-ks.cfg
-rw-r--r--  1 root root  472 May 14 10:55 rul
[root@web03 ~]# cat rul 
# Generated by iptables-save v1.4.21 on Tue May 14 10:55:43 2019
*filter
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [181:16663]
-A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
-A INPUT -p icmp -j ACCEPT
-A INPUT -i lo -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 22 -j ACCEPT
-A INPUT -j REJECT --reject-with icmp-host-prohibited
-A FORWARD -j REJECT --reject-with icmp-host-prohibited
COMMIT
# Completed on Tue May 14 10:55:46 2019
[root@web03 ~]# 
[root@web03 ~]# iptables -X
[root@web03 ~]# iptables -F
[root@web03 ~]# iptables -Z
[root@web03 ~]# 
[root@web03 ~]# iptables-restore </root/rul 
[root@web03 ~]# iptables -nL
Chain INPUT (policy ACCEPT)
target     prot opt source               destination         
ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0            state RELATED,ESTABLISHED
ACCEPT     icmp --  0.0.0.0/0            0.0.0.0/0           
ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0           
ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0            state NEW tcp dpt:22
REJECT     all  --  0.0.0.0/0            0.0.0.0/0            reject-with icmp-host-prohibited

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination         
REJECT     all  --  0.0.0.0/0            0.0.0.0/0            reject-with icmp-host-prohibited

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination         
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值