阅读量: 419
表主要用于将不同的规则存储在不同的表中
- filter 表:默认表,负责过滤数据包,使用频率最高
- nat 表:用于网络地址转换(IP、端口)和流量转发,使用频率较低
- mangle 表:主要应用在修改数据包、流量整形、给数据包打标识
- raw 表:这个表很少被用到,主要用于配置连接跟踪相关内容,使用频率较少
- security 表:这个表用于安全 linux 的防火墙规则,是 iptables 最近的新增表,使用频率较少
流量的方向
- input:匹配目标 IP 是本机的数据包,入站
- output:出口数据包,出站
- forward:匹配流经本机的数据包,转发
- prerouting:修改目的地址,用来作 DNAT。如:把内网中的 80 端口映射到互联网端口
- postrouting:修改源地址,用来做 SNAT。如:局域网共享一个公用 IP 接入 Internet
- 基于防火墙策略设置的各类防护规则,防火墙规则的执行顺序认为从前到后一次执行,遇到匹配的规则就不再继续向下检查,如果遇到不匹配的规则则会继续向下进行。
| systemctl stop firewalld |
| firewalld默认在没有设定规则的情况下,是拒绝所有流量,而iptables默认没有设定规则的情况下,允许所有流量 |
| |
| yum install iptables iptables-services |
| iptables -nL:将端口号以数字的形式显示默认表filrter中的规则 |
| |
| iptables -I INPUT -j DROP:所有入站流量全部丢弃,包括SSH请求 |
| iptables -I OUTPUT -j DROP:所有出站流量全部丢弃,包括SSH响应 |
| 上述两条命令一旦执行,所有流量无法进来也无法出去,断网状态 |
| |
| iptables -A INPUT -j DROP |
| iptables -A OUTPT -j DROP |
| |
| iptables -I INPUT -p tcp |
| iptables -I INPUT -p tcp |
| |
| iptables -I INPUT -p tcp -m multipiort |
| |
| iptables -I INPUT -p icmp -j DROP |
| iptables -I INPUT -p icmp -j REJRCT |
| iptables -I OUTPUT -p icmp -j DROP |
| |
| DROP:直接丢弃数据包,不会向源端做任何回复 |
| REJRCT:拒绝接收数据包,并向源端发送拒绝响应 |
| |
| iptables -I INPUT -p icmp |
| iptables -I INPUT -p icmp |
| iptables -I INPUT 1 -p icmp |
| |
| iptables -L INPUT |
| iptables -D INPUT 1 |
| |
| iptables -F:清空规则 |
| sercvice iptables save:保存规则 |
| cat /etc/sysconfig/iptables:保存在这个文件中 |
| |
| |
| |
| iptables -I INPUT -i ens33 -p tcp -s 192.168.112.153 |
| iptables -I OUTPUT -o ens33 -p tcp -d 192.168.112.153 |
| |
| |
| iptables -A INPUT -p tcp |
| |
| |
| iptables -I INPUT -p tcp -m multiport |
| |
| |
| |
| iptables -t nat -A PREROUTING -p tcp |
| |
| |
| vi /etc/sysctl.conf |
| net.ipv4.ip_forard = 1 |
| sysctl -p /etc/sysctl.conf |
| |
| |
| iptables -t nat -A PREROUTING -d 192.168.112.188 -p tcp |
| iptables -t nat -A POSTROUTING -d 192.168.112.153 -p tcp |
参数 |
说明 |
示例 |
-F |
清空规则链 |
iptables -F |
-L |
查看规则链 |
iptables -L |
-A |
追加规则 |
iptables -A INPUT |
-D |
删除规则 |
iptables -D INPUT 1 |
-R |
修改规则 |
iptable -R INPUT 1 -s 192.168.120.0 -j DROP |
-I |
在头部插入规则 |
iptables -I INPUT 1 --dport 80 -j ACCEPT |
-L |
查看规则 |
iptables -L INPUT |
-N |
新的规则 |
iptables -N allowed |
-V |
查看 iptables 版本 |
iptables -V |
-p |
协议(tcp/udp/icmp) |
iptables -A INPUT -p tcp |
-s |
匹配原地址,加 "!" 表示除这个 IP 外 |
iptables -A INPUT -s 192.168.1.1 |
-d |
匹配目的地址 |
iptables -A INPUT -d 192.168.12.1 |
--sport |
匹配源端口流入的数据 |
iptables -A INPUT -p tcp --sport 22 |
--dport |
匹配目的端口流出的数据 |
iptables -A INPUT -p tcp --dport 22 |
-i |
匹配入口网卡流入的数据 |
iptables -A INPUT -i eth0 |
-o |
匹配出口网卡流出的数据 |
iptables -A FORWARD -o eth0 |
-j |
要进行的处理动作:DROP (丢弃),REJECT (拒绝),ACCEPT (接受),SANT (基于原地址的转换) |
iptable -A INPUT 1 -s 192.168.120.0 -j DROP |
--to-source |
指定 SANT 转换后的地址 |
iptables -t nat -A POSTROUTING -s 192.168.10.0/24 -j SANT --to-source 172.16.100.1 |
-t |
表名 (raw、mangle、nat、filter) |
iptables -t nat |
-m |
使用扩展模块来进行数据包的匹配 (multiport/tcp/state/addrtype) |
iptables -m multiport |
- 常用的两张表:filter,nat,filter 用于过滤数据包,nat 用于路由转发功能
- 常用两条链:INPUT、OUTPUT
- 常见的三个行为:ACCEPT、DROP、REJECCT
- 限制流量的三个特征:端口、协议、IP 对应的五元组:-d -s --dport --sport -p
- 端口转发:本机端口、远程端口