CentOS7,win,mac NAT上网

现状

现在内网服务器 有一台可以访问互联网 其他机器不能 想通过snat 让其他服务器也能访问互联网

资源

10.16.14.5 可以上外网
10.16.14.100~200 不能上外网

基础命令[所有服务器]

systemctl stop firewalld
iptables  -F

外网机

echo 1  >/proc/sys/net/ipv4/ip_forward
sysctl -p /etc/sysctl.conf 
iptables -t nat -A POSTROUTING  -s 10.16.0.0/24 -j SNAT --to-source 10.16.14.5
iptables -t nat -nvL #可以查看上边的规则是否成功
iptables -F -t nat #清空

========
#永久生效
echo 'net.ipv4.ip_forward=1' >> /etc/sysctl.conf
sysctl -p /etc/sysctl.conf 
echo 'iptables -t nat -A POSTROUTING  -s 10.16.0.0/24 -j SNAT --to-source 10.16.14.5' >> /etc/rc.d/rc.local
chmod +x /etc/rc.d/rc.local
systemctl restart rc-local

不能上外网的机器

route add default gw 10.16.14.5
#也可以用这个命令删除route delete default gw 10.16.14.5


#win 使用 如下 目标为59.110.168.181 的请求 下一跳 print 和delete 
route add 59.110.168.181 mask 255.255.255.255 192.168.1.3
# route print 查看,route delete 59.110.168.181 mask 255.255.255.255 

#阿里云 在VPC 路由表里添加自定义路由目的地选可以上外网的ECS

mac作为外网机

#开启转发
sysctl -w net.inet.ip.forwarding=1
#在en0网卡上的所有来自192.168.1.0/24的包的源地址改为192.168.1.3
echo "nat on en0 from 192.168.1.0/24 to any -> 192.168.1.3" > /etc/pf.anchors/http
#/etc/pf.conf 按照里边的位置分别加入如下两行
nat-anchor "http"
load anchor "http" from "/etc/pf.anchors/http"
# 检查配置文件是否有问题
pfctl -vf /etc/pf.conf
#停止
pfctl -d 
#启动 也可以 -ef /abc 指定conf
pfctl -e 
#查看状态
pfctl -ss 

引用 rc.local https://blog.csdn.net/qq_38860828/article/details/85700615
阿里云 http://llxxkj.cn/content/394.html?eqid=fe5240950008502900000006642ad817

posted @ 2021-01-26 10:38  Qtong  阅读(112)  评论(0)    收藏  举报