linux+iptables搭建网关服务器

凉白开 Linux226,0763字数 1123阅读3分44秒阅读模式

公司购买的一批云服务器只带内网,配置了一个负载均衡器(lb),这批服务器通过lb可以对外提供服务,但是这批服务器不能主动连接外网,例如使用wget下载文件,或者curl访问ttlsa.com站点。

额外购买一台带外网的云服务器,将这台服务器配置成网关服务器,其他服务器网关改为这台服务器,问题得到解决。方法很简单文章源自运维生存时间-https://www.ttlsa.com/linux/linux-iptables-building-gateway/

网关服务器IP:172.16.0.1
客户端IP:172.16.0.0/24文章源自运维生存时间-https://www.ttlsa.com/linux/linux-iptables-building-gateway/

1. 开启forward

# cat /etc/sysctl.conf | grep forw
net.ipv4.ip_forward = 1

2. 配置转发

# iptables -t nat -A POSTROUTING -s 172.16.0.0/24 -j MASQUERADE

转发网段172.16.0.0/24传过来的包,你也可以指定特定的ip地址。例如:文章源自运维生存时间-https://www.ttlsa.com/linux/linux-iptables-building-gateway/

# iptables -t nat -A POSTROUTING -s 172.16.0.10 -j MASQUERADE

3. 重启iptables

# service iptables restart

4. 修改网关

以上三步都在网关服务器上操作,这步在需要连接外网的客户端操作文章源自运维生存时间-https://www.ttlsa.com/linux/linux-iptables-building-gateway/

# cat /etc/sysconfig/network-scripts/ifcfg-eth0 | grep GATEWAY
GATEWAY=172.16.0.1

5. 测试

# ping www.ttlsa.com -c 4
PING www.ttlsa.com (114.215.173.139) 56(84) bytes of data.
64 bytes from ip215.hichina.com (114.215.173.139): icmp_seq=1 ttl=46 time=28.1 ms
64 bytes from ip215.hichina.com (114.215.173.139): icmp_seq=2 ttl=46 time=27.9 ms
64 bytes from ip215.hichina.com (114.215.173.139): icmp_seq=3 ttl=46 time=27.8 ms
64 bytes from ip215.hichina.com (114.215.173.139): icmp_seq=4 ttl=46 time=27.8 ms

--- www.ttlsa.com ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3032ms
rtt min/avg/max/mdev = 27.815/27.951/28.193/0.220 ms

6. 结束

是不是很简单?搭建一个虚拟机网关IP 172.16.0.1,客户端IP172.16.0.2。按照上面的步骤完成你的实验。有疑问请留言文章源自运维生存时间-https://www.ttlsa.com/linux/linux-iptables-building-gateway/ 文章源自运维生存时间-https://www.ttlsa.com/linux/linux-iptables-building-gateway/

weinxin
我的微信
微信公众号
扫一扫关注运维生存时间公众号,获取最新技术文章~
凉白开
  • 本文由 发表于 06/06/2014 01:00:15
  • 转载请务必保留本文链接:https://www.ttlsa.com/linux/linux-iptables-building-gateway/
  • iptables
  • Linux
  • linux网关
  • 网关
评论  2  访客  2
    • 匿名
      匿名 9

      你好,我用的是ubuntu server 重启iptables服务的时候提示 unit iptables.service failed to load :no such file or directory

      • mdh6789
        mdh6789 9

        白开,,,,,,我无语了

      评论已关闭!