在Ubuntu Server中切换默认的Network Interface

背景

我用 VirtualBox 给Ubuntu server的虚拟机配置了两个网卡,一个为『桥接网卡』模式,一个为『网络地址转换(NAT)』模式。

通过查看 ip addr,然后手动添加interface,/etc/network/interfaces 文件中添加没有的 interface。

然后现在的Interafce情况如下:

$ ifconfig
enp0s3    Link encap:Ethernet  HWaddr 08:00:27:de:db:87  
          inet addr:192.168.1.120  Bcast:192.168.1.255  Mask:255.255.255.0
          inet6 addr: fe80::a00:27ff:fede:db87/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:727 errors:0 dropped:0 overruns:0 frame:0
          TX packets:378 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:59127 (59.1 KB)  TX bytes:64152 (64.1 KB)

enp0s8    Link encap:Ethernet  HWaddr 08:00:27:b8:08:5c  
          inet addr:10.0.3.15  Bcast:10.0.3.255  Mask:255.255.255.0
          inet6 addr: fe80::a00:27ff:feb8:85c/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:2 errors:0 dropped:0 overruns:0 frame:0
          TX packets:8 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:1180 (1.1 KB)  TX bytes:1192 (1.1 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:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1 
          RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)

enp0s3 接口为共享主机的wifi,enp0s8为直接用主机的网络,包括VPN,可以通过这种方式共享vpn。

但是现在默认为使用 enp0s3 接口访问外网

kyle@ubuntu:~$ ip route list
default via 192.168.1.1 dev enp0s3 
10.0.3.0/24 dev enp0s8  proto kernel  scope link  src 10.0.3.15 
192.168.1.0/24 dev enp0s3  proto kernel  scope link  src 192.168.1.120 

通过查看IP地址, curl ipinfo.io ,可以看到目前并没有使用vpn。

kyle@ubuntu:~$ curl ipinfo.io
{
  "ip": "58.250.xx.xx",
  "hostname": "No Hostname",
  "city": "Shenzhen",
  "region": "Guangdong",
  "country": "CN",
  "loc": "22.5333,114.1333",
  "org": "AS17623 China Unicom Shenzen network"
}

所以需要把系统的默认 interface 修改为 enp0s8 。

首先需要查到 enp0s8 接口的网关ip地址是多少。

kyle@ubuntu:~$ ls /var/lib/dhcp/
dhclient.enp0s3.leases  dhclient.enp0s8.leases
kyle@ubuntu:~$ cat /var/lib/dhcp/dhclient.enp0s8.leases 
lease {
  interface "enp0s8";
  fixed-address 10.0.3.15;
  filename "ubuntu server 16.04.pxe";
  option subnet-mask 255.255.255.0;
  option dhcp-lease-time 86400;
  option routers 10.0.3.2;
  option dhcp-message-type 5;
  option dhcp-server-identifier 10.0.3.2;
  option domain-name-servers 8.8.8.8,8.8.4.4;
  renew 2 2016/11/22 00:48:08;
  rebind 2 2016/11/22 12:29:44;
  expire 2 2016/11/22 15:29:44;
}

看到这行:option routers 10.0.3.2 ,就是我们想要的网关ip了。

然后通过如下命令设置默认interface 为 enp0s8

# sudo ip route change to default dev [interface name] via [gateway ip]
sudo ip route change to default dev enp0s8 via 10.0.3.2

这时候再查看路由表

kyle@ubuntu:~$ ip route
default via 10.0.3.2 dev enp0s8 
10.0.3.0/24 dev enp0s8  proto kernel  scope link  src 10.0.3.15 
192.168.1.0/24 dev enp0s3  proto kernel  scope link  src 192.168.1.120 

发现 default 已经变成了 enp0s8。

再查看自己的ip地址:

kyle@ubuntu:~$ curl ipinfo.io
{
  "ip": "47.xx.xx.xx",
  "hostname": "No Hostname",
  "city": "Hong Kong",
  "region": "HONG KONG",
  "country": "HK",
  "loc": "22.2833,114.1500",
  "org": "AS45102 Alibaba (China) Technology Co., Ltd."
}

已经变成VPN的IP了。

参考:http://www.jamesbaltar.com/blog/set-default-network-interface-ubuntu