centos6.5 iptables实现端口转发

时间:2021-12-03 10:18:58
将本地3389端口 转发到192.168.10.210的22(主要访问到本机的15210端口,就会跳转到192.168.10.210的22)


【步骤】
1、 首先应该做的是/etc/sysctl.conf配置文件的 net.ipv4.ip_forward = 1 默认是0    这样允许iptalbes FORWARD。


vim /etc/sysctl.conf
# 找到下面的值并将0改成1
net.ipv4.ip_forward = 1
# sysctl –p(使之立即生效)
2、 修改 iptables
vim /etc/sysconfig/iptables


# Firewall configuration written by system-config-firewall
# Manual customization of this file is not recommended.
# nat部分正是需要求
*nat
:PREROUTING ACCEPT [10000:20000]
:POSTROUTING ACCEPT [20:20000]
:OUTPUT ACCEPT [125:20000]
-A PREROUTING -p tcp -m tcp --dport 15210 -j DNAT --to-destination 192.168.10.210:22
-A POSTROUTING -d 192.168.10.210/32 -p tcp -m tcp --dport 22 -j SNAT --to-source 192.168.10.250
COMMIT




*filter
:INPUT ACCEPT [25350:2120857]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [26183:2224589]
-A INPUT -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 808 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 3128 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 8080 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 8088 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 15210 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 30001 -j ACCEPT
-A INPUT -j REJECT --reject-with icmp-host-prohibited
#下面一句默认是有的,需要去掉,否则nat配置不起效
#-A FORWARD -j REJECT --reject-with icmp-host-prohibited
COMMIT


3、 重启iptables
service iptables restart