admin 发表于 2019-11-15 09:27:24

通过iptables 实现端口转发

要求:访问本机3389端口,转发到目标机器3389端口,实现转发远程。

机器环境:本机:192.168.1.1    目标服务器:192.168.2.1

实现过程:

1,开启系统的转发功能
vi /etc/sysctl.conf

net.ipv4.ip_forward=0
修改成
net.ipv4.ip_forward=1

编辑后使用一下命令让配置马上生效:
sysctl -p2,停止iptables并将基于FORWAR默认策略为ACCEPT

service iptables stopiptables -t filter -P FORWARDACCEPT


3,执行增加iptables 命令
iptables -A INPUT -p tcp --dport 22 -j ACCEPT
iptables -t nat -A PREROUTING --dst 192.168.1.1 -p tcp --dport 3389 -j DNAT --to-destination 192.168.2.1:3389
iptables -t nat -A POSTROUTING --dst 192.168.2.1 -p tcp --dport 3389 -j SNAT --to-source 192.168.1.1

将当前规则保存到 /etc/sysconfig/iptables
service iptables save


启动iptables 服务
service iptables start
4,查看策略是否生效。
iptables -t nat    -nvL--line
5,# 关于策略的删除,将-A 改为 -D,或者指定策略行号iptables -t nat -DPREROUTING -p tcp --dport 8080 -j REDIRECT --to-ports 80

iptables -t nat -D相应策略的行号



5,进行正常测试
页: [1]
查看完整版本: 通过iptables 实现端口转发