https://blog.51cto.com/zzzhao/5178855

1、环境

操作系统:CentOS 7.x
代理机:
  内网地址:1.1.1.1
  外网地址:192.168.1.1
局域网主机:
  内网地址:192.168.1.2

2、安装iptables服务

yum install iptables-services
systemctl enable --now iptables

3、开启ip转发功能

vi /etc/sysctl.conf
net.ipv4.ip_forward=1
sysctl -p

4、配置iptables

iptables -t nat -I POSTROUTING -s 192.168.1.0/24 -j SNAT --to-source 1.1.1.1
systemctl iptables save
systemctl restart iptables

5、局域网主机配置默认路由

route add -net 0.0.0.0/0 gw 192.168.1.1
或:
vi ifcfg-ens192
GATEWAY=192.168.1.1

6、验证

登陆局域网服务器访问百度
https://www.baidu.com

7、配置端口映射

iptables -t nat -I PREROUTING -p tcp --dport 80 -j DNAT --to 192.168.1.2:80
iptables -t nat -I POSTROUTING -p tcp --dport 80 -j MASQUERADE
systemctl restart iptables
systemctl iptables
作者:Jeebiz  创建时间:2022-09-29 09:57
 更新时间:2024-08-20 17:39