ntftables
安装
yum install nftables

清理规则
nft flush ruleset
nft list ruleset

iptables
安装
apt install iptables

检查防火墙效果
iptables -vnL

iptables命令
未加规则
ping 10.0.0.13

加入规则
iptables -A INPUT -s 10.0.0.12 -j DROP

查看规则
iptables -vnL

查看能否ping通
ping 10.0.0.13

清除规则
iptables -F


命令操作
清除规则
iptables -F
iptables -vnL INPUT

确保主机远程连接不被隔绝
iptables -A INPUT -s 10.0.0.1 -j ACCEPT
iptables -vnL INPUT

保存当前的防火墙配置文件
iptables-save > iptables.rules
cat iptables.rules

黑白名单
增加规则
iptables -A INPUT -j REJECT

测试
ping 10.0.0.13

规则取反
增加拒绝12主机规则
iptables -A INPUT -s 10.0.0.12 -j DROP


设置取反规则
iptables -vnL --line-numbers
iptables -R INPUT 1 ! -s 10.0.0.12 -j REJECT


根据目标地址匹配
增加ip
ip a a 10.0.0.110/24 dev ens33 label ens33:110

其他主机ping通
ping 10.0.0.110
ping 10.0.0.13

增加规则
iptables -A INPUT -d 10.0.0.110 -j REJECT
iptables -vnL

测试
ping 10.0.0.110


根据协议过滤
增加规则
iptables -A INPUT -d 10.0.0.110 -p icmp -j REJECT
iptables -vnL

改为禁止ssh通信,其他协议正常
清空规则
iptables -nL INPUT --line-numbers
iptables -D INPUT 2

先测试能否ssh连接
ssh root@10.0.0.110

增加规则
iptables -A INPUT -d 10.0.0.110 -p tcp -j REJECT
iptables -vnL

测试连接
ssh root@10.0.0.110
ping 10.0.0.110

多端口实践
查看关联的模块multiport
ls /usr/lib/x86_64-linux-gnu/xtables/*.so | grep mult

还原规则
iptables-restore < iptables.rules

增加端口规则
iptables -A INPUT -s 10.0.0.12 -d 10.0.0.110 -p tcp -m multiport --dports 22,80 -j REJECT

12主机测试效果
ping 10.0.0.110
ssh root@10.0.0.110
curl 10.0.0.110

iprange 实践
还原规则
iptables-restore < iptables.rules

增加多个ip范围规则
iptables -A INPUT -m iprange --src-range 10.0.0.12-10.0.0.100 -p tcp -m multiport --dports 22,80 -j REJECT
iptables -nL INPUT --line-numbers

12主机测试效果


target
DROP 和 REJECT
还原规则
iptables-restore < iptables.rules

增加规则
iptables -A INPUT -s 10.0.0.12 -j DROP
iptables -A INPUT -s 10.0.0.112 -j REJECT

12主机测试

ping -I 10.0.0.12 10.0.0.13
ping -I 10.0.0.112 10.0.0.13
ping -I 10.0.0.212 10.0.0.13

iptables 中的 NAT
准备工作
12主机
网卡配置
NAT-10.0.0.12

关闭防火墙
systemctl disable --now firewalld.service

修改网关路由
13主机
网卡配置
NAT-10.0.0.13
仅主机-192.168.8.13

关闭防火墙
systemctl disable --now ufw

路由设置
删除默认路由
ip route list
ip route del default

增加路由
ip route add default via 10.0.0.13 dev ens160

开启转发功能(ip_forward)
sysctl -a | grep ip_forward
sysctl -w "net.ipv4.ip_forward = 1"
sysctl -p

14主机
网卡配置
仅主机-192.168.8.14

关闭防火墙
systemctl disable --now firewalld.service

开启nginx
systemctl start nginx
curl 192.168.8.14

增加防火墙规则
增加前效果
ping 192.168.8.14

增加规则
iptables -t nat -A POSTROUTING -s 10.0.0.0/24 ! -d 10.0.0.0/24 -j SNAT --to-source 192.168.8.13
iptables -t nat -vnL

查看效果
ping 192.168.8.13
ping 192.168.8.14
curl 192.168.8.14

SNAT
实现多出口IP地址转换
修改规则
iptables -t nat -R POSTROUTING 1 -s 10.0.0.0/24 ! -d 10.0.0.0/24 -j SNAT --to-source 192.168.8.100-192.168.8.200
iptables -t nat -vnL POSTROUTING

查看效果
12主机:ping 192.168.8.14
14主机:tcpdump -nn icmp


实现端口转发
修改规则
iptables -t nat -R POSTROUTING 1 -s 10.0.0.0/24 ! -d 10.0.0.0/24 -o ens37 -p tcp --dport 80 -j SNAT --to-source 192.168.8.13:12345

查看效果
12主机:curl 192.168.8.14
14主机:tcpdump -nn 'tcp port 80'


MASQUERADE
实现源IP地址转换
修改规则
iptables -t nat -R POSTROUTING 1 -s 10.0.0.0/24 ! -d 10.0.0.0/24 -j MASQUERADE

查看效果
12主机:ping 192.168.8.14
14主机: tcpdump -nn icmp


DNAT
准备工作
12主机
ip route del default
ip route list

13主机
ip route del default

iptables -t nat -F

14主机
ip route add default via 192.168.8.13

实现目标IP地址转换
修改规则
iptables -t nat -A PREROUTING -d 10.0.0.13 -p tcp --dport 80 -j DNAT --to-destination 192.168.8.14:80
如果有tomcat:iptables -t nat -A PREROUTING -d 10.0.0.13 -p tcp --dport 8080 -j DNAT --to-destination 192.168.8.14:80
iptables -t nat -vnL PREROUTING

查看效果
12主机:curl 192.168.8.14
14主机:tcpdump -nn 'tcp port 80'

