使用 Ubuntu 14.04 作为带有 iptables 的路由器,客户端无法上网。可能是 DNS 问题,需要 iptables 设置

zer*_*eck 5 iptables networking routing dns nat

更正3:解决了!我需要将我部门 VLAN 的官方 DNS 服务器作为 DNS 添加到客户端。

我已经阅读了大量关于使用 Linux 作为路由器时 iptables 问题的帖子,但没有一个可以帮助我解决以下问题:

我正在我们大学部门设置许多学生工作站,我们在 VLAN 中拥有有限的 IP。工作站需要相互通信并联机。明确地不应从 Internet 访问它们。我想使用以下设置,而不是为每个工作站提供大学 VLAN 中罕见的 IP 之一:

我们有一台运行 Ubuntu 14.04 服务器的服务器,带有 4 个以太网端口(我使用 2 个进行此设置)。

第一个端口是 em1,与所有工作站一起插入 L2 交换机。

第二个端口是 em2 并插入大学 VLAN。

首先是问题,然后是我的设置:

  1. 我可以通过网络上的 ssh 访问服务器,我可以从它 ping 8.8.8.8、www.example.com 和局域网中的其他 192.168.99.x 客户端,因此对于该客户端来说一切正常。
  2. 客户端可以互相ping通,也可以ping通8.8.8.8,但不能ping通www.example.com。这就是为什么我认为我需要为 DNS 设置另一个 iptable 设置。在任何浏览器中都一样,我无法解析外部网站。在 Windows 客户端中,网络设置显示到 Internet 的连接,在 Linux 客户端中,诸如 wget 和 apt-get install 之类的东西可以工作。我曾尝试为客户端手动设置 Google DNS 服务器 8.8.8.8 和 8.8.4.4,但无济于事,即使他们可以 ping 它们。(解决办法是使用VLAN的官方DNS)

更正: wget/apt-get 的东西似乎只在某些时候有效。

更正 2:我至少在一个客户端的 DNS 条目之一中有错字,我将在明天尝试其余的条目,然后将其标记为已关闭。

设置等:

我通过取消注释net.ipv4.ip_forward=1在内核中激活了转发 /etc/sysctl.conf

/etc/network/interfaces的 2 个以太网端口条目如下所示:

auto em2
iface em2 inet static
    address x.x.x.x
    netmask x.x.x.x
    network x.x.x.x
    gateway x.x.x.x
    dns-nameservers x.x.x.x x.x.x.x
    dns-search x.example.com
    pre-up iptables-restore < /etc/network/iptables.up.rules
    post-down iptables-save > /etc/network/iptables.up.rules

auto em1
iface em1 inet static
    address 192.168.99.252
    netmask 255.255.255.128
    pre-up iptables-restore < /etc/network/iptables.up.rules
    post-down iptables-save > /etc/network/iptables.up.rules
Run Code Online (Sandbox Code Playgroud)

(出于隐私原因,对 em2 参数进行了编辑,但我认为它们在这里不起作用。请注意我们有两个内部 DNS 服务器,这可能在问题中起作用吗?)

我也有设置 iptables,sudo iptables -t nat -A POSTROUTING -o em2 -j MASQUERADE 所以sudo iptables -t nat -L给了我:

Chain PREROUTING (policy ACCEPT)
target     prot opt source               destination

Chain INPUT (policy ACCEPT)
target     prot opt source               destination

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination

Chain POSTROUTING (policy ACCEPT)
target     prot opt source               destination
MASQUERADE  all  --  anywhere             anywhere
MASQUERADE  all  --  anywhere             anywhere
Run Code Online (Sandbox Code Playgroud)

(第二个条目一定是偶然发生的,但不应该令人担忧,对吧?)

所有客户端都具有以下设置的静态 IP:

  • IP:192.168.99.22 或类似
  • 子网掩码:255.255.255.128
  • 网关:192.168.99.252

解决方案是将我部门 VLAN 中的官方 DNS 条目添加到客户端 DNS 条目中。路由器未配置为单独用作 DNS,也未将 DNS 请求重定向到外部 DNS。因此对于每个客户端: - DNS:外部网络的官方 VLAN DNS