Centos7 NAT 转发上网
2张网卡。
192.168.1.10(外网)
192.168.100.1(内网,100段还有别的内网电脑。)
目的: 让100段的电脑都可以上外网通过 192.168.100.1 转发。
设置好ip
eth0: inet 192.168.1.10/24 gateway 192.168.1.1
eth1: inet 192.168.100.1/24 no gateway
重要提示: 电脑必须开启虚拟化,否则转发无效。
开启 firewalld
systemctl start firewalld
systemctl enable firewalld
开启系统转发
echo net.ipv4.ip_forward = 1 >> /etc/sysctl.conf
sysctl -p
开启地址伪装
这里不开不能通过DNS。 浏览器无法使用。
firewall-cmd --add-masquerade --permanent
–permanent 永久生效
重新加载firewalld
firewall-cmd --reload
查看firewalld 信息
[root@moban ~]# firewall-cmd --list-all
public (active)
target: default
icmp-block-inversion: no
interfaces: eth0 eth1
sources:
services: dhcpv6-client ssh
ports:
protocols:
masquerade: yes
forward-ports:
sourceports:
icmp-blocks:
rich rules:
这样转发就做好了。 100网段的电脑网关设为100.1 DNS 设置为公网DNS地址就可以上网了。
基于iptables转发
# 查看防火墙
service iptables status
#ubuntu 下是 ufw
## 单台机器
iptables -t nat -A POSTROUTING -s 192.168.100.4 -j SNAT --to 192.168.1.10
## 单个子网
iptables -t nat -A POSTROUTING -s 192.168.100.0/24 -j SNAT --to 192.168.1.10