网络环境如下:
机器1(可上外网):
enp0s3: 192.168.100.55/24(可上网的网卡)
enp0s8: 192.168.56.103/24(内网)
# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 192.168.100.1 0.0.0.0 UG 0 0 0 enp0s3
0.0.0.0 192.168.100.1 0.0.0.0 UG 100 0 0 enp0s3
192.168.56.0 0.0.0.0 255.255.255.0 U 0 0 0 enp0s8
192.168.100.0 0.0.0.0 255.255.255.0 U 100 0 0 enp0s3
机器2(内网):
enp0s3: 192.168.56.101/24
# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 192.168.56.103 0.0.0.0 UG 0 0 0 enp0s3
192.168.56.0 0.0.0.0 255.255.255.0 U 100 0 0 enp0s3
如何让机器2通过机器1上网?
我已经在机器1上搭建了squid
,通过在机器2上配置
export http_proxy=http://192.168.56.103:3128
export https_proxy=http://192.168.56.103:3128
是可以正常上网的,但是发现有些程序不走系统代理。听说用 iptables
可以实现全局的代理,但是不知道怎么设置。
我尝试了在机器1上打开转发echo "1" > /proc/sys/net/ipv4/ip_forward
添加了iptables -t nat -A POSTROUTING -o enp0s3 -s 192.168.56.0/24 -j SNAT --to 192.168.100.55
为机器2添加了route add default gw 192.168.56.103
但是依旧无法上网。
大概有两种方式,看你的网络结构。
这种代理叫透明代理,设置方法参考