Linux 配置interfaces進行網絡卡配置
在Linux中,如果你的Linux系統是有介面的可以用vim /etc/sysconfig/network-scripts/ifcfg-eth0 命令,開啟檔案編輯介面,其中ifcfg-eth0表示配置eth0這個網絡卡,假如其他網絡卡,則使用ifcfg-eth1、ifcfg-eth2諸如此類的。
如果你的Linux系統比較小,是沒有介面的,就可以用本文的方法,直接配置interfaces配置網絡卡,先進入到根目錄的etc/network資料夾,開啟interfaces新增配置,配置格式如下:
基本的配置格式
auto lo iface lo inet loopback # The primary networkinterface auto eth0 iface eth0 inet static address 192.168.0.42 network 192.168.0.0 netmask 255.255.255.0 broadcast 192.168.0.255 gateway 192.168.0.1
上面的配置中,
第1行跟第5行說明lo介面跟eth0介面會在系統啟動時被自動配置;
好像不同的介面之間配置部分必須留有一個空格,比如第3行的空格
第2行將lo介面設定為一個本地迴環(loopback)地址;
第6行指出eth0介面具有一個靜態的(static)IP配置;
第7行-第11行分別設定eth0介面的ip、網路號、掩碼、廣播地址和閘道器。
新增多個網絡卡的時候也安照上面的格式,在文字後面新增即可。
複雜一點的配置格式
auto eth0 iface eth0 inet static address 192.168.1.42 network 192.168.1.0 netmask 255.255.255.128 broadcast 192.168.1.0 up route add -net 192.168.1.128 netmask 255.255.255.128 gw 192.168.1.2 up route add default gw 192.168.1.200 down route deldefault gw 192.168.1.200 down route del -net 192.168.1.128 netmask 255.255.255.128 gw 192.168.1.2
這次,有了一個複雜一些的掩碼,和一個比較奇怪的廣播地址。還有就是增加的介面啟用、禁用時的路由設定;
第7行和8行配置的左右是在介面啟用的時候,新增一條靜態路由和一個預設路由;
第9行和10行會在介面禁用的時候,刪掉這兩條路由配置。
至於配置路由的寫法,仔細看,它就是route命令嘛。
在一個物理網絡卡上配置多個網口(多個IP)的配置格式
auto eth0 eth0:1 iface eth0 inet static address 192.168.0.100 network 192.168.0.0 netmask 255.255.255.0 broadcast 192.168.0.255 gateway 192.168.0.1 iface eth0:1 inet static #多配置的網口 address 192.168.0.200 network 192.168.0.0 netmask 255.255.255.0
第8行到11行在eth0上配置了另外一個地址,這種配置方法在配置一塊網絡卡多個地址的時候很常見:有幾個地址就配置幾個介面。冒號後面的數字可以隨便寫的,只要幾個配置的名字不重複就可以。
下面是pre-up和post-down命令時間。這是一組命令(pre-up、up、post-up、pre-down、down、post-down),分別定義在對應的時刻需要執行的命令。
auto eth0
iface eth0 inet dhcp
pre-up [ -f /etc/local-network-ok ]
第3行會在啟用eth0之前檢查/etc/local-network-ok檔案是否存在,如果不存在,則不會啟用eth0。
更進一步的配置格式
auto eth0 eth1 iface eth0 inet static address 192.168.42.1 netmask 255.255.255.0 pre-up /path/to/check-mac-address.sh eth0 11:22:33:44:55:66 pre-up /usr/local/sbin/enable-masq iface eth1 inet dhcp pre-up /path/to/check-mac-address.sh eth1 AA:BB:CC:DD:EE:FF pre-up /usr/local/sbin/firewall
第5行和第8行中,check-mac-address.sh 放在/usr/share/doc/ifupdown/examples/目錄 中,使用的時候需要給它加上可執行許可權。這兩行命令會檢測兩塊網絡卡的MAC地址是否為11:22:33:44:55:66和 AA:BB:CC:DD:EE:FF,如果正確,則啟用網絡卡。如果MAC地址錯誤,就不會啟用這兩塊網絡卡。
第6行和第9行是假定在這兩塊網絡卡上分別執行的命令,你可以把它們替換成你想要的任何玩意。
手冊上說,這種方法主要是用來檢測兩塊網絡卡的MAC地址交換(If their MAC addresses get swapped),其實就是兩塊網絡卡名互換了,這種情況在debian系統上再常見不過了,主要是因為核心識別網絡卡的順序發生了變化。這個問題可以用下面 的這種方法來避免。
auto eth0 eth1 mapping eth0 eth1 script /path/to/get-mac-address.sh map 11:22:33:44:55:66 lan map AA:BB:CC:DD:EE:FF internet iface lan inet static address 192.168.42.1 netmask 255.255.255.0 pre-up /usr/local/sbin/enable-masq $IFACE iface internet inet dhcp pre-up /usr/local/sbin/firewall $IFACE
第3行中的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)到物理介面上去。 再來看下面這段配置:auto eth0 iface eth0 inet manual up ifconfig $IFACE 0.0.0.0 up up /usr/local/bin/myconfigscript down ifconfig $IFACE down
這段配置只是啟用一個網絡卡,但是ifupdown不對這個網絡卡設定任何ip,而是由外部程式來設定ip。
最後一段配置,這段配置啟用了網絡卡的混雜模式,用來當監聽介面。
auto eth0 iface eth0 inet manual up ifconfig $IFACE 0.0.0.0 up up ip link set $IFACE promisc on down ip link set $IFACE promisc off down ifconfig $IFACE down
到這裡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服務
最後附網絡卡設定相關命令
檢視網絡卡資訊: ifconfigsu
設定一個網絡卡IP:ifconfig eth0 192.168.1.10 netmask 255.255.255.0
重啟網絡卡使設定生效:sudo /etc/init.d/networking restart
更改MAC地址:ifconfig eth0 hw ether xx:xx:xx:xx:xx:xx
檢視路由相關資訊:route -n
ifconfig 網絡卡名 <IP地址> netmask <子網掩碼> 此命令會立即生效,但不會修改配置檔案。