Ubuntu

設置預設網路介面

  • September 13, 2013

我的/etc/network/interfaces樣子是這樣的:

auto lo
iface lo inet loopback

auto eth1
iface eth1 inet static
     address 10.0.0.99
     netmask 255.255.255.0
     gateway 10.0.0.1

auto eth0
iface eth0 inet dhcp

重新啟動機器後,我得到

vagrant@precise64:~$ ip route
default via 10.0.2.2 dev eth0 
default via 10.0.0.1 dev eth1  metric 100 
10.0.0.0/24 dev eth1  proto kernel  scope link  src 10.0.0.99 
10.0.2.0/24 dev eth0  proto kernel  scope link  src 10.0.2.15

我該如何修改/etc/network/interfaces,以便我的流量預設通過 10.0.0.1 而不是 10.0.2.2?我可以按如下方式手動執行此操作,但我不想在每次重新啟動時都執行此操作:

vagrant@precise64:~$ sudo ip route del default via 10.0.2.2 dev eth0
vagrant@precise64:~$ ip route
default via 10.0.0.1 dev eth1  metric 100 
10.0.0.0/24 dev eth1  proto kernel  scope link  src 10.0.0.99 
10.0.2.0/24 dev eth0  proto kernel  scope link  src 10.0.2.15

事實證明,原因與/etc/network/interfaces. 這台機器是一個Vagrant VM,預設情況下有一些額外的東西/etc/rc.local

#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.

# Make sure eth0 is working. This works around Vagrant issue #391
dhclient eth0

exit 0

註釋掉該dhclient eth0行並重新啟動產生:

vagrant@precise64:~$ ip r
default via 10.0.0.1 dev eth1  metric 100 
10.0.0.0/24 dev eth1  proto kernel  scope link  src 10.0.0.99 
10.0.2.0/24 dev eth0  proto kernel  scope link  src 10.0.2.15 

引用自:https://serverfault.com/questions/538849