1. 环境说明:

服务器A(能访问外网):
内网 IP: 192.168.1.101
能访问外网的IP: 172.26.1.188
服务器B(同一交换机下,仅能访问内网):
内网 IP:192.168.1.102

a. 修改服务器A上,两个网卡的。(假设eno1访问外网,eno2 访问内网)

  1. network:
  2. version: 2
  3. renderer: NetworkManager
  4. ethernets:
  5. eno1:
  6. dhcp4: no
  7. addresses: [172.26.1.188/24] # 外网访问的IP
  8. optional: true
  9. gateway4: 172.26.1.1
  10. nameservers:
  11. addresses: [223.5.5.5,8.8.8.8]
  12. eno2:
  13. dhcp4: no
  14. addresses: [192.168.1.101/24] # 内网IP作为,同一网络下其他服务器的网关
  15. optional: true
  16. gateway4:
  17. nameservers:
  18. addresses: [223.5.5.5,8.8.8.8]

b. 修改服务器B的IP:

  1. network:
  2. version: 2
  3. renderer: NetworkManager
  4. ethernets:
  5. eno2:
  6. dhcp4: no
  7. addresses: [192.168.1.102/24]
  8. optional: true
  9. gateway4: 192.168.1.101 # 能访问外放的服务器的内网IP
  10. nameservers:
  11. addresses: [223.5.5.5,8.8.8.8]

2. 配置服务器A

  1. ## 1. 修改/etc/sysctl.conf
  2. # net.ipv4.ip_forward =1 # 将这个的注解放开
  3. net.ipv4.ip_forward =1
  4. ## 2. 让其生效
  5. sysctl -p
  1. ## 3. 在服务器A上配置路由规则:
  2. ### a. 使用firewalld
  3. firewall-cmd --permanent --zone=public --add-masquerade
  4. ### b.使用iptables
  5. ### 第一句是清除掉之前所有的iptables规则,第二第三句是允许接收和发送数据包,第四句是在eth1网口上NAT。注意,要在有外部IP的网口上做NAT。
  6. iptables -F
  7. iptables -P INPUT ACCEPT
  8. iptables -P FORWARD ACCEPT
  9. iptables -t nat -A POSTROUTING -s 192.168.1.0/24 -o eno1 -j MASQUERADE
  10. iptables -I FORWARD 1 -s 172.26.1.0/24 -j ACCEPT
  11. ## 如果怕重启后失效,可以将其写入到rc.local中,或者使用iptables save

参考文档:

  1. 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
  2. https://www.cnblogs.com/anyux/articles/7772228.html