linux interfaces配置文件詳解

linux interfaces配置文件詳解

配置文件基本格式

一個基本的配置大概是下面這個樣子:

  1 auto lo
  2 iface lo inet loopback
  3
  4 # The primary network interface
  5 auto eth0
  6 iface eth0 inet static
  7      address 192.168.0.42
  8      network 192.168.0.0
  9      netmask 255.255.255.0
  10      broadcast 192.168.0.255
  11      gateway 192.168.0.1

  上面的配置中,
  第1行跟第5行說明lo接口跟eth0接口會在系統啓動時被自動配置;
  好像不同的接口之間配置部分必須留有一個空格,比如第3行的空格
  第2行將lo接口設置爲一個本地迴環(loopback)地址;
  第6行指出eth0接口具有一個靜態的(static)IP配置;
  第7行-第11行分別設置eth0接口的ip、網絡號、掩碼、廣播地址和網關。

複雜一點

  再來看一個更復雜點的

  12 auto eth0
  13 iface eth0 inet static
  14     address 192.168.1.42
  15     network 192.168.1.0
  17     netmask 255.255.255.128
  18     broadcast 192.168.1.0
  19     up route add -net 192.168.1.128 netmask 255.255.255.128 gw 192.168.1.2
  20     up route add default gw 192.168.1.200
  21     down route del default gw 192.168.1.200
  22     down route del -net 192.168.1.128 netmask 255.255.255.128 gw 192.168.1.2

  這次,有了一個複雜一些的掩碼,和一個比較奇怪的廣播地址。還有就是增加的接口啓用、禁用時的路由設置;
  第19行和20行配置的左右是在接口啓用的時候,添加一條靜態路由和一個缺省路由;
  第21行和22行會在接口禁用的時候,刪掉這兩條路由配置。
  至於配置路由的寫法,仔細看,它就是route命令嘛。

一個物理網卡上多個接口

  繼續,下面是一個物理網卡上多個接口的配置方法:

  23 auto eth0 eth0:1
  24 iface eth0 inet static
  25     address 192.168.0.100
  26     network 192.168.0.0
  27     netmask 255.255.255.0
  28     broadcast 192.168.0.255
  29     gateway 192.168.0.1
  30 iface eth0:1 inet static
  31     address 192.168.0.200
  32     network 192.168.0.0
  33     netmask 255.255.255.0

  30行到33行在eth0上配置了另外一個地址,這種配置方法在配置一塊網卡多個地址的時候很常見:有幾個地址就配置幾個接口。冒號後面的數字可以隨便寫的,只要幾個配置的名字不重複就可以。
  下面是pre-up和post-down命令時間。這是一組命令(pre-up、up、post-up、pre-down、down、post-down),分別定義在對應的時刻需要執行的命令。

  34 auto eth0
  35 iface eth0 inet dhcp
  36     pre-up [ -f /etc/local-network-ok ]

  第36行會在激活eth0之前檢查/etc/local-network-ok文件是否存在,如果不存在,則不會激活eth0。

再更進一步的例子

  再更進一步的例子:

  37 auto eth0 eth1
  38 iface eth0 inet static
  39     address 192.168.42.1
  40     netmask 255.255.255.0
  41     pre-up /path/to/check-mac-address.sh eth0 11:22:33:44:55:66
  42     pre-up /usr/local/sbin/enable-masq
  43 iface eth1 inet dhcp
  44     pre-up /path/to/check-mac-address.sh eth1 AA:BB:CC:DD:EE:FF
  45     pre-up /usr/local/sbin/firewall

  第41行和第44行中,check-mac-address.sh放在/usr/share/doc/ifupdown/examples/目錄 中,使用的時候需要給它加上可執行權限。這兩行命令會檢測兩塊網卡的MAC地址是否爲11:22:33:44:55:66和 AA:BB:CC:DD:EE:FF,如果正確,則啓用網卡。如果MAC地址錯誤,就不會啓用這兩塊網卡。
  第42行和第45行是假定在這兩塊網卡上分別執行的命令,你可以把它們替換成你想要的任何玩意 :)
  手冊上說,這種方法主要是用來檢測兩塊網卡的MAC地址交換(If their MAC addresses get swapped),其實就是兩塊網卡名互換了,這種情況在debian系統上再常見不過了,主要是因爲內核識別網卡的順序發生了變化。這個問題可以用下面 的這種方法來避免。

  46 auto eth0 eth1
  47 mapping eth0 eth1
  48     script /path/to/get-mac-address.sh
  49     map 11:22:33:44:55:66 lan
  50     map AA:BB:CC:DD:EE:FF internet
  51 iface lan inet static
  52     address 192.168.42.1
  53     netmask 255.255.255.0
  54     pre-up /usr/local/sbin/enable-masq $IFACE
  55 iface internet inet dhcp
  56     pre-up /usr/local/sbin/firewall $IFACE

  第48行中的get-mac-address.sh也在/usr/share/doc/ifupdown/examples/目錄裏,也同樣要加可執行權限。這個腳本的作用,就是獲得每塊網卡的MAC地址。
  這段配置首先配置了兩個邏輯接口(這個名詞的定義請參見debian參考手冊 http://www.debian.org/doc/manuals/reference/ch-gateway.zh-cn.html)lan和internet,然後根據網卡的MAC地址,將邏輯接口映射(mapped)到物理接口上去。
  再來看下面這段配置:

  57 auto eth0  58 iface eth0 inet manual  59 up ifconfig $IFACE 0.0.0.0 up  60 up /usr/local/bin/myconfigscript  61 down ifconfig $IFACE down

  這段配置只是啓用一個網卡,但是ifupdown不對這個網卡設置任何ip,而是由外部程序來設置ip。
  最後一段配置,這段配置啓用了網卡的混雜模式,用來當監聽接口。

  177 auto eth0
  178 iface eth0 inet manual
  179     up ifconfig $IFACE 0.0.0.0 up
  180 up ip link set $IFACE promisc on
  181 down ip link set $IFACE promisc off
  182 down ifconfig $IFACE down

  好了,interfaces中對於以太網卡的配置基本上介紹完了
轉載:http://www.itmop.com/article/6281.html

添加dns服務器

還可以在interfaces文件中添加dns服務器

# This file describes the network interfaces available on your system
# and how to activate them. For more information, see interfaces(5).

# The loopback network interface
auto lo
iface lo inet loopback

# The primary network interface
auto eth0
iface eth0 inet static
      address 10.112.18.106
      network 10.112.18.0
      netmask 255.255.255.0
      broadcast 10.112.18.255
      gateway 10.112.18.254
      dns-nameservers 10.112.18.1

就是最後一行dns-nameservers,可以添加多個,用空格分開。

發佈了47 篇原創文章 · 獲贊 90 · 訪問量 50萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章