1. 环境说明:
服务器A(能访问外网):
内网 IP: 192.168.1.101
能访问外网的IP: 172.26.1.188
服务器B(同一交换机下,仅能访问内网):
内网 IP:192.168.1.102
a. 修改服务器A上,两个网卡的。(假设eno1访问外网,eno2 访问内网)
network:
version: 2
renderer: NetworkManager
ethernets:
eno1:
dhcp4: no
addresses: [172.26.1.188/24] # 外网访问的IP
optional: true
gateway4: 172.26.1.1
nameservers:
addresses: [223.5.5.5,8.8.8.8]
eno2:
dhcp4: no
addresses: [192.168.1.101/24] # 内网IP作为,同一网络下其他服务器的网关
optional: true
gateway4:
nameservers:
addresses: [223.5.5.5,8.8.8.8]
b. 修改服务器B的IP:
network:
version: 2
renderer: NetworkManager
ethernets:
eno2:
dhcp4: no
addresses: [192.168.1.102/24]
optional: true
gateway4: 192.168.1.101 # 能访问外放的服务器的内网IP
nameservers:
addresses: [223.5.5.5,8.8.8.8]
2. 配置服务器A
## 1. 修改/etc/sysctl.conf
# net.ipv4.ip_forward =1 # 将这个的注解放开
net.ipv4.ip_forward =1
## 2. 让其生效
sysctl -p
## 3. 在服务器A上配置路由规则:
### a. 使用firewalld
firewall-cmd --permanent --zone=public --add-masquerade
### b.使用iptables
### 第一句是清除掉之前所有的iptables规则,第二第三句是允许接收和发送数据包,第四句是在eth1网口上NAT。注意,要在有外部IP的网口上做NAT。
iptables -F
iptables -P INPUT ACCEPT
iptables -P FORWARD ACCEPT
iptables -t nat -A POSTROUTING -s 192.168.1.0/24 -o eno1 -j MASQUERADE
iptables -I FORWARD 1 -s 172.26.1.0/24 -j ACCEPT
## 如果怕重启后失效,可以将其写入到rc.local中,或者使用iptables save
参考文档:
- https://blog.csdn.net/tmaccs/article/details/104677730?utm_medium=distribute.pc_aggpage_search_result.none-task-blog-2~aggregatepage~first_rank_ecpm_v1~rank_v31_ecpm-1-104677730.pc_agg_new_rank&utm_term=%E5%86%85%E7%BD%91%E5%9C%B0%E5%9D%80%E9%80%9A%E8%BF%87%E5%8F%A6%E5%A4%96%E4%B8%80%E5%8F%B0%E8%AE%BF%E9%97%AE%E5%A4%96%E7%BD%91&spm=1000.2123.3001.4430
- https://www.cnblogs.com/anyux/articles/7772228.html