Linux系统 iptables防火墙

Linux系统 iptables防火墙

一、iptables 基础概念

1. 什么是 iptables?
  • 定义:iptables 是 Linux 系统中基于内核 netfilter 框架的防火墙工具,用于过滤、管理网络数据包。
  • 作用:控制进出服务器的流量,实现访问控制、地址转换(NAT)、端口转发等功能。
2. 核心组件
  • netfilter:内核中的数据包处理框架,提供钩子函数(Hook Points)用于拦截数据包 内核态
  • iptables:用户空间的命令行工具,通过操作内核中的规则表(Tables)和链(Chains)管理防火墙规则 用户态。
3. 四表五链(默认)
表(Table)作用链(Chain)
filter数据包过滤(入站、出站、转发)INPUT、OUTPUT、FORWARD
nat网络地址转换(SNAT/DNAT)PREROUTING、OUTPUT、POSTROUTING
mangle数据包修改(标记、拆分等)PREROUTING、OUTPUT、INPUT、FORWARD、POSTROUTING
raw决定是否让数据包进入 netfilter 框架PREROUTING、OUTPUT

表匹配的顺序 先匹配raw–mangle–nat–filter

二、iptables 基本操作

1. 命令格式

iptables [ -t table ] [ -chain ] [ -action ] [ -j target ] [ -options ]

  • 常用参数
    • -t table:指定表(默认 filter)。
    • -A:追加规则到链末尾。
    • -I:插入规则到链顶部(-I chain num 指定位置)。
    • -D:删除链中的规则。
    • -L:列出链中规则(-n 以数字形式显示地址,-v 显示详细信息)。
    • –line(–line-numbers) 显示行号
    • -F:清空链中所有规则。
    • -P:设置链的默认策略(ACCEPT/DROP/REJECT)允许 丢弃 拒绝。
2. 规则匹配条件
  • 通用匹配
    • -s, --source:源 IP 地址 / 子网(如 -s 192.168.1.0/24)。
    • -d, --destination:目标 IP 地址 / 子网。
    • -p, --protocol:协议(tcp/udp/icmp 等)。
  • 端口匹配(需结合 -p tcp-p udp
    • --sport:源端口(如 --sport 80)。
    • --dport:目标端口(如 --dport 22)。
  • ICMP 匹配
    • --icmp-type:指定 ICMP 类型(如 echo-request/echo-reply)。
    • 编号类型 8请求包 0 回应包 3目标不可达
3. 目标动作(Target)
  • ACCEPT:允许数据包通过。
  • DROP:静默丢弃数据包(无响应)。
  • REJECT:拒绝数据包并返回错误信息(如 --reject-with icmp-port-unreachable)。
  • LOG:记录数据包信息到系统日志(需配合其他动作,如 -j LOG --log-prefix "iptables_log:")。

三、iptables 规则示例

1. 基础过滤规则
  • 允许 SSH 访问(TCP 22 端口):

    [root@localhost ~]# iptables -I INPUT -p tcp --dport 22 -j ACCEPT
    
    

    使用iptables -t filter -L --line-numbers 命令可以查看

    [root@localhost ~]# iptables -t filter -L --line-numbers
    Chain INPUT (policy ACCEPT)
    num  target     prot opt source               destination         
    1    ACCEPT     tcp  --  anywhere             anywhere             tcp dpt:ssh
    2    ACCEPT     tcp  --  anywhere             anywhere             tcp dpt:ssh
    3    ACCEPT     all  --  anywhere             anywhere             state RELATED,ESTABLISHED
    4    ACCEPT     icmp --  anywhere             anywhere            
    5    ACCEPT     all  --  anywhere             anywhere            
    6    ACCEPT     tcp  --  anywhere             anywhere             state NEW tcp dpt:ssh
    7    REJECT     all  --  anywhere             anywhere             reject-with icmp-host-prohibited
    
    Chain FORWARD (policy ACCEPT)
    num  target     prot opt source               destination         
    1    REJECT     all  --  anywhere             anywhere             reject-with icmp-host-prohibited
    
    Chain OUTPUT (policy ACCEPT)
    num  target     prot opt source               destination       
    
  • 禁止 ping(ICMP 请求):

    禁止别人ping我 我可以ping别人

    [root@localhost ~]# iptables -I INPUT -p icmp --icmp-type echo-request -j DROP  ###本机是101
    [root@101 ~]# ping 192.168.10.102
    PING 192.168.10.102 (192.168.10.102) 56(84) 字节的数据。
    64 字节,来自 192.168.10.102: icmp_seq=1 ttl=64 时间=0.948 毫秒
    64 字节,来自 192.168.10.102: icmp_seq=2 ttl=64 时间=0.757 毫秒
    64 字节,来自 192.168.10.102: icmp_seq=3 ttl=64 时间=0.867 毫秒
    ^C
    --- 192.168.10.102 ping 统计 ---
    已发送 3 个包, 已接收 3 个包, 0% packet loss, time 2000ms
    rtt min/avg/max/mdev = 0.757/0.857/0.948/0.078 ms
    
    
    [root@localhost ~]# ping 192.168.10.101
    PING 192.168.10.101 (192.168.10.101) 56(84) 字节的数据。
    ###本机是102
    
2. NAT 规则(地址转换)
  • SNAT(源地址转换,内网主机共享公网 IP 上网)

    格式:iptables -t 表类型 -A 路由后动作末尾添加 -s 源地址 -o网卡 -j 动作

    [root@101 ~]# iptables -t nat -A POSTROUTING -s 192.168.10.0/24 -o ens224 -j MASQUERADE
    
    [root@102 ~]# ping 192.168.20.103
    PING 192.168.20.103 (192.168.20.103) 56(84) 字节的数据。
    64 字节,来自 192.168.20.103: icmp_seq=1 ttl=63 时间=3.00 毫秒
    64 字节,来自 192.168.20.103: icmp_seq=2 ttl=63 时间=1.35
  • DNAT(目标地址转换,公网访问内网服务)

    iptables -t nat -A PREROUTING -d 192.168.20.103  -p tcp --dport 80 -j DNAT --to-destination 192.168.10.103:80
    
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值