網卡配置/etc/network/interfaces

來源:互聯網
上載者:User

標籤:lan   監聽   sha   script   地址   設定   har   man   需要   

話說Debian系的網卡配置跟Redhat系很不一樣,Redhat是放在/etc/sysconfig/network-scripts目錄下面的一大堆檔案裡面,要修改?你一個一個檔案來過吧。Debian系的則是存在/etc/network/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介面會在系統啟動時被自動設定; 第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.030行到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/network/local-network-ok ]第36行會在啟用eth0之前檢查/etc/network/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參考手冊)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 eth0178 iface eth0 inet manual 179 up ifconfig $IFACE 0.0.0.0 up180 up ip link set $IFACE promisc on 181 down ip link set $IFACE promisc off182 down ifconfig $IFACE down好了,interfaces中對於乙太網路卡的配置基本上介紹完了。

網卡配置/etc/network/interfaces

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.