Ubuntu 作为网关 - 客户端 PC 无互联网

Ubuntu 作为网关 - 客户端 PC 无互联网

Ubuntu 配置

配置文件

ens33     Link encap:Ethernet  HWaddr 00:0c:29:0e:15:94  
          inet addr:192.168.1.129  Bcast:192.168.1.255  Mask:255.255.255.0
          inet6 addr: fe80::2d58:5175:4354:3a62/64 Scope:Link
          inet6 addr: 2a02:120b:2c4f:1e60:9573:9a86:d356:ad94/64 Scope:Global
          inet6 addr: 2a02:120b:2c4f:1e60:4098:7cf5:6187:401a/64 Scope:Global
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:151 errors:0 dropped:0 overruns:0 frame:0
          TX packets:145 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:13451 (13.4 KB)  TX bytes:16919 (16.9 KB)

ens34     Link encap:Ethernet  HWaddr 00:0c:29:0e:15:9e  
          inet addr:10.0.0.1  Bcast:10.0.0.255  Mask:255.255.255.0
          inet6 addr: fe80::20c:29ff:fe0e:159e/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:50 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:0 (0.0 B)  TX bytes:5691 (5.6 KB)

lo        Link encap:Local Loopback  
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:65536  Metric:1
          RX packets:237 errors:0 dropped:0 overruns:0 frame:0
          TX packets:237 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1 
          RX bytes:17506 (17.5 KB)  TX bytes:17506 (17.5 KB)

界面 ubuntu

# interfaces(5) file used by ifup(8) and ifdown(8)

    auto lo
    iface lo inet loopback

    auto ens34

    iface ens34 inet static
      address 10.0.0.1
      netmask 255.255.255.0

转发端口

net.ipv4.ip_forward = 1

航线

Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         192.168.1.1     0.0.0.0         UG    100    0        0 ens33
10.0.0.0        0.0.0.0         255.255.255.0   U     0      0        0 ens34
169.254.0.0     0.0.0.0         255.255.0.0     U     1000   0        0 ens34
192.168.1.0     0.0.0.0         255.255.255.0   U     100    0        0 ens33

iptables 目录

sudo iptables -L
Chain INPUT (policy ACCEPT)
target     prot opt source               destination         

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination         
ACCEPT     all  --  anywhere             anywhere            
ACCEPT     all  --  anywhere             anywhere             state RELATED,ESTABLISHED

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination

客户端 PC 配置 - Windows 10 - Lan nic

IP: 10.0.0.2
MASK: 255.255.255.0
Gateway: 10.0.0.1

Dns Server: 10.0.0.1

在 ubuntu 服务器上我可以访问互联网,但从客户端电脑(Windows)使用上述配置却无法访问互联网。

从客户端 Windows PC 对 10.0.0.1 网关进行 ping 操作。

答案1

您需要使用IPTABLES来完成此操作。您需要将流量从一个接口(您的 10.0.0.1 接口)转发到 192.168.129 接口。

运行以下规则:

#/sbin/iptables -t nat -A POSTROUTING -o ens33 -j MASQUERADE
#/sbin/iptables -A FORWARD -i ens33 -o ens34 -m state --state RELATED,ESTABLISHED -j ACCEPT
#/sbin/iptables -A FORWARD -i ens34 -o ens33 -j ACCEPT

相关内容