iptables四表五链

netfilter/iptables

netfilterLinux内核中的一个框架,工作在网络层,用于处理ip数据包,iptables 则是一个命令行工具,通过与netfilter框架交互,实现对数据包的过滤和转发等操作

常见的UFW防火墙、firewalld防火墙都是基于iptables的,它们提供了更简单的管理iptablesrules的命令

Tables 表

filter

过滤本机数据包

管理INPUTFORWARDOUTPUT三个链

nat

网络地址转换

管理PREROUTINGPOSTROUTINGOUTPUT三个链

mangle

修改数据包的TOS(``Type Of Service,服务类型)、TTLTime To Live,生存周期)指以及为数据包设置Mark标记,以实现Qos(Quality Of Service`,服务质量)调整以及策略路由等

管理PREROUTINGPOSTROUTINGINPUTOUTPUTFORWARD五个链

raw

决定数据包是否被状态跟踪机制处理

管理OUTPUTPREROUTING两个链

Chain 链

  • PREROUTING:预路由,所有数据包进入路由前由此链负责
  • INPUT:进站,数据包目标是本机由此链负责
  • OUTPUT:出站,数据包从本机发送由此链负责
  • FORWORD:转发,数据包的目标地址是其它外部地址由此链负责
  • POSTROUTING:后路由,所有数据包离开本机时由此链负责

在这里插入图片描述

Rules 规则

  • ACCEPT:允许数据包通过
  • DROP:直接丢弃数据包
  • REJECT:拒绝数据包通过,必要时给数据发送端一个响应信息
  • SNAT:源地址转换
  • DNAT:目标地址转换
  • REDIRECT:重定向

命令

命令解释
iptables --table filter --listoriptables -t filter -Loriptables -L查看当前规则,不指定表默认查看filter
iptables -L --line-number查看当前规则并显示行号
iptables -A <chain> <options>添加规则
iptables -I <chain> <rule_number> <options>插入规则
iptables -D <chain> <rule_number>删除规则
iptables -F清除规则
iptables-save > /etc/iptables/rules.v4保存规则
iptables-restore < /etc/iptables/rules.v4从文件中加载规则
echo 1 > /proc/sys/net/ipv4/ip_forward启动转发

加载iptables模块

modprobe iptable_filter 
modprobe iptable_nat 
modprobe iptable_mangle 
modprobe iptable_raw ip_tables

设置每次开机自动加载vim /etc/modules

cat > /etc/modules-load.d/iptables.conf << EOF 
iptable_filter
iptable_nat
iptable_mangle
iptable_raw
ip_tables
EOF

验证

lsmod | grep iptable_filter
lsmod | grep iptable_nat
lsmod | grep iptable_mangle
lsmod | grep ip_tables

指定源ip192.168.1.4到本机的所有协议的数据包都拒绝

iptables --table filter --append INPUT --source 192.168.1.4 --jump REJECT

查看当前规则

iptables -L

指定源ip192.168.1.4到本机的所有协议的数据包都直接丢弃

iptables --table filter --append INPUT --source 192.168.1.4 --jump DROP

查看当前规则并显示行号

iptables -L --line-number

删除INPUT链的第一个规则

iptables --table filter --delete INPUT 1
or
iptables -t filter -D INPUT 1

iptables开放端口

开放 SSH 端口(22)

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

开放 HTTP 端口(80)

iptables -A INPUT -p tcp --dport 80 -j ACCEPT

开放 HTTPS 端口(443)

iptables -A INPUT -p tcp --dport 443 -j ACCEPT

开放 MySQL 端口(3306)

iptables -A INPUT -p tcp --dport 3306 -j ACCEPT

端口转发/映射

将所有公网ip84.100.100.111的数据包都映射到私网ip172.27.225.55

iptables -t nat -A PREROUTING -d 84.100.100.111 -j DNAT --to-destination 172.27.225.55

将所有公网ip84.100.100.111:6443的数据包都映射到私网ip172.27.225.55:6443

iptables -t nat -A PREROUTING -p tcp -d 84.100.100.111 --dport 6443 -j DNAT --to 172.27.225.55:6443

查看规则

iptables -t nat -L

拒绝某个国家的ip

https://www.ipdeny.com/ipblocks/右键复制链接,然后wget下载文件

使用ipsetapt install ipset

创建一个拒绝阿富汗ip的网络号集合

ipset create afuhan hash:net

查看集合

ipset list afuhan

保存ipsetiptables规则

ipset save > /etc/rules.ipset
iptables-save > /etc/iptables/rules.v4

加载ipsetiptables规则

ipset restore < /etc/rules.ipset
iptables-restore < /etc/iptables/rules.v4

清除规则(flush:清除所有规则,delete:删除所有自定义链,zero:将所有链的计数和流量统计清零)

iptables -F & iptables -X & iptables -Z
  • 15
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

cci497

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值