NAT proxy Internet access based on iptables

Background:
There is a server A that cannot access the Internet, and is connected to the server B through the intranet. The server B can access the Internet, and the server A can also access the Internet.
A IP: 192.168.0.35
B IP: 192.168.0.146

SNAT: Change the source address of the packet. The firewall replaces the packet's local network address with the external address. This enables hosts inside the network to communicate with those outside the network.

1. On the server B that can access the Internet, enable the kernel routing and forwarding function
echo 1 > /proc/sys/net/ipv4/ip_forward

2. Check the routing table on server A that needs to access the Internet through a proxy. and add the default gateway. route add default gw 192.168.0.146
[root@localhost ~]# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
192.168.0.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0
169.254.0.0 0.0.0.0 255.255 .0.0 U 0 0 0 eth0
0.0.0.0 192.168.0.146 0.0.0.0 UG 0 0 0 eth0


3. Add the SNAT rule iptables -t nat -A POSTROUTING -o eth0 -s 192.168.0.0/24 -j SNAT –-to 123.196.112.146 on the server B that can access the Internet

4.保存
service iptables save

5. Verify that you can access the Internet normally.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326612050&siteId=291194637