IPtables同网段IP:Port跳转

image.png

如题

  1. 需求如上图

  2. 如果Client是公网IP不知道可不可行(未测试)


环境

[root@Client ~]# cat /etc/redhat-release 
CentOS Linux release 7.7.1908 (Core)
[root@Client ~]# uname -r
3.10.0-693.el7.x86_64
[root@Client ~]# uname -m
x86_64


IPtables配置

#开启内核转发功能
[root@IPtables ~]# echo "net.ipv4.ip_forward = 1" >> /etc/sysctl.conf
[root@IPtables ~]# sysctl -p
net.ipv4.ip_forward = 1

#Centos7默认是使用firewalld做为防火墙,先删除再安装iptables。
[root@IPtables ~]# yum remove firewalld -y
[root@IPtables ~]# yum install iptables-servers -y
[root@IPtables ~]# systemctl start iptables
[root@IPtables ~]# systemctl enable iptables

#清除iptables默认规则,再添加自定义规则。
[root@IPtables ~]# iptables -F
[root@IPtables ~]# iptables -t nat -A PREROUTING -d 192.168.17.5 -p tcp --dport 80 -j DNAT --to-destination 192.168.17.13:80
[root@IPtables ~]# iptables -t nat -A POSTROUTING -p tcp -d 192.168.17.13 --dport 80 -j SNAT --to-source 192.168.17.5


测试

#Server端安装NGINX,并可以正常访问80端口。
[root@Nginx1 ~]# netstat -tnlp|grep 80
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      11372/nginx: master

#Client直接访问Server
[root@Client ~]# curl 192.168.17.13
192.168.17.13

#Client经过IPtables正常访问Server
[root@Client ~]# curl 192.168.17.5
192.168.17.13




猜你喜欢

转载自blog.51cto.com/w877183008/2485110