在Linux中,如果你的Linux系統是有界面的可以用vim /etc/sysconfig/network-scripts/ifcfg-eth0 命令,打開文件編輯界面,其中ifcfg-eth0表示配置eth0這個網卡,假如其他網卡,則使用ifcfg-eth1、ifcfg-eth2諸如此類的。
或者可以用本文的方法,直接配置interfaces配置網卡,先進入到根目錄的etc/network文件夾,打開interfaces添加配置,配置格式如下:
基本的配置格式
auto lo iface lo inet loopback # The primary network interface 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、網絡號、掩碼、廣播地址和網關。
配置為自動獲取IP
auto lo iface lo inet loopback # The primary network interface auto eth0 iface eth0 inet dhcp
復雜一點的配置格式
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 del default 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地址。
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服務,如果你要直接ping域名地址就需要開啟dns服務
#如果配置好dns服務后,你使用 udhcpc -i eth0 指令,系統會重新給ech0分配個臨時IP,這時候使用eth0就用分配的臨時IP,要用會原來的IP則需要重啟系統才行
最后附網卡設置相關命令
查看網卡信息: 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 <子網掩碼> 此命令會立即生效,但不會修改配置文件。
