需求:外网ip数量限制,内部服务器想访问公网。
原理:https://www.cnblogs.com/fanggege/p/11312050.html
操作如下:
服务端-10.14.101.49
开启路由转发 echo 1 >/proc/sys/net/ipv4/ip_forward 或者vim /etc/sysctl.conf: net.ipv4.ip_forward = 1
查看
[root@v_yunwei4bu49 ~]# sysctl -p |grep ip_forward
net.ipv4.ip_forward = 1
开启FORWARD
iptables -P FORWARD ACCEPT
使用 iptables -nvL 若有拒绝策略请删除
iptables -D FORWARD -j DROP
保存策略
service iptables save
#指定网段 iptables -t nat -A POSTROUTING -s 192.168.60.0/24 -o em1 -j MASQUERADE
#指定ip iptables -t nat -A POSTROUTING -s 10.14.101.48 -o eth1 -j MASQUERADE
-s 进口ip(客户端) -o 出口网卡名
查看
iptables -t nat -nL -v --line-numbers
保存策略
service iptables save
客户端服务器-10.14.101.48
删除之前默认路由
route -n 查看
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 10.14.101.1 0.0.0.0 UG 0 0 0 eth0
route del default gw 10.14.101.1
route add default gw 10.14.101.49 (网关ip) 默认路由
保存默认路由
方式1:/etc/sysconfig/network-scripts/
default via 10.14.101.49
方式2:/etc/sysconfig/static-routes : (没有static-routes的话就手动建立一个这样的文件)
any net 10.14.0.0 netmask 255.255.0.0 gw 10.14.101.49
若出公网 请配置好对应的DNS