ubuntu使用iptables 持久化


iptables 持久化

 

安装持久化工具
apt-get install iptables-persistent

 

Ubuntu 16.04 调用语法
netfilter-persistent save
netfilter-persistent reload

 

一键清除iptables规则
cat clear_iptables_rule.sh
#!/bin/bash
iptables -F
iptables -X
iptables -Z
iptables -P INPUT ACCEPT
iptables -P OUTPUT ACCEPT
iptables -P FORWARD ACCEP

 

iptables 转发请求到80端口
1.使用DNAT实现
iptables -t nat -A PREROUTING -p tcp -i ens33 -d 192.168.122.128 --dport 8089 -j DNAT --to 192.168.122.128:80
2.使用redirect实现
iptables -t nat -A PREROUTING -p tcp --dport 8088 -j REDIRECT --to-port 80

====================

iptables 端口转发

1. 所有的81请求转发到了8080上.

# iptables -t nat -A PREROUTING -p tcp --dport 81 -j REDIRECT --to-ports 8080
1
# iptables -t nat -A PREROUTING -p tcp --dport 81 -j REDIRECT --to-ports 8080
如果需要本机也可以访问,则需要配置OUTPUT链:

iptables -t nat -A OUTPUT -p tcp --dport 81 -j REDIRECT --to-ports 8080
1
iptables -t nat -A OUTPUT -p tcp --dport 81 -j REDIRECT --to-ports 8080
原因:外网访问需要经过PREROUTING链,但是localhost不经过该链,因此需要用OUTPUT,或者POSTROUTING。POSTROUTING不行,需要看看。

 

上一篇:nginx 配置简介


下一篇:81. lvm+mysql你们是如何备份的