echo "1" >/proc/sys/net/ipv4/ip_forward 注:这是执行后立即生效,但重起后失效,如果想一直生效可以修改/etc/sysctl.conf文件net.ipv4.ip_forward 的值为1 在使用命令:sysctl -p 让配置生效
iptables -F
iptables -P INPUT ACCEPT
iptables -P FORWARD ACCEPT
iptables -t nat -A POSTROUTING -o eth1 -j MASQUERADE
*nat
:PREROUTING ACCEPT [0:0]
:POSTROUTING ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
-A POSTROUTING -o eth1 -j MASQUERADE
-A POSTROUTING -d 218.6.132.0/255.255.255.0 -o eth1 -j MASQUERADE
-A POSTROUTING -d 221.236.31.149/255.255.255.255 -o eth1 -j MASQUERADE
-A PREROUTING -d 218.6.132.2 -p tcp -m tcp --dport 82 -j DNAT --to-destination 210.41.128.19:8280
示例:
:PREROUTING ACCEPT [0:0]
:POSTROUTING ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
-A POSTROUTING -d 192.168.3.0/255.255.255.0 -o eth1 -j MASQUERADE
-A PREROUTING -d 114.113.149.68 -p tcp --dport 3307 -j DNAT --to 192.168.3.98:3306
COMMIT
~
~
# Firewall configuration written by system-config-firewall
# Manual customization of this file is not recommended.
*filter
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
-A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
-A FORWARD -m state --state ESTABLISHED,RELATED -j ACCEPT
-A INPUT -p icmp -j ACCEPT
-A INPUT -i lo -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 84 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 85 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 83 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 443 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 5901 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 5801 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 3307 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 823 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 89 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 2433 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 801 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 802 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 803 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 808 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 804 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 15590 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 8090 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 8084 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 8888 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 1521 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 3306 -j ACCEPT
-A INPUT -j REJECT --reject-with icmp-host-prohibited
#全部转发,从tap0网卡到eth0网卡
-A FORWARD -i tap0 -o eth0 -j ACCEPT
#-A FORWARD -d 192.168.3.98 -p tcp --dport 3306 -j ACCEPT 此配置是配置限定端口,该ip和端口为目标地址
-A FORWARD -j REJECT --reject-with icmp-host-prohibited
COMMIT
*nat
:PREROUTING ACCEPT [0:0]
:POSTROUTING ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
-A POSTROUTING -d 192.168.3.0/255.255.255.0 -o eth1 -j MASQUERADE
-A PREROUTING -d 122.123.159.68 -p tcp --dport 3307 -j DNAT --to 192.168.3.98:3306
COMMIT
相关理论:
实例:端口转发
环境介绍:
2个网络接口
Lan口:192.168.1.1/24 eth0
Lan内web server: 192.168.1.100:80
Wan口:10.111.1.199/24 eth1
确认linux的各项配置正常;
1. 使用sysctl net.ipv4.ip_forward=1打开linux的转发功能。
2. iptables -P FORWARD DROP 将FORWARD链的策略设置为DROP,这样做的目的是做到对内网ip的控制,你允许哪一个访问internet就可以增加一个规则,不在规则中的ip将无法访问internet
3. iptables -A FORWARD -m state --state ESTABLISHED,RELATED -j ACCEPT 加入确认包和关联包的允许通过
4. iptables -t nat -A PREROUTING -d 10.111.1.199 -p tcp --dport 80 -j DNAT --to 192.168.1.100:80
5. iptables -A FORWARD -d 192.168.1.100 -p tcp --dport 80 -j ACCEPT FORWARD链在PREROUTING之后,也就是说当这个包到达FORWARD链的时候,目的地址已经变成内网IP了,假如 internet用户的请求是这样202.1.1.1:1333-->10.111.1.199:80,在经过了我们的PREROUTING链之后将变成 202.1.1.1:1333-->192.168.1.100:80,这个时候如果你设置一个目的地址为10.111.1.199的规则没用的
6. iptables -t nat -A POSTROUTING -d 192.168.1.100 -p tcp --dport 80 -j SNAT --to 192.168.1.1 或设置192.168.1.100的网关为 192.168.1.1