時(shí)間:2015-06-28 00:00:00 來源:IT貓撲網(wǎng) 作者:網(wǎng)管聯(lián)盟 我要評(píng)論(1)
一個(gè)基本的配置大概是下面這個(gè)樣子:
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接口會(huì)在系統(tǒng)啟動(dòng)時(shí)被自動(dòng)配置;
第2行將lo接口設(shè)置為一個(gè)本地回環(huán)(loopback)地址;
第6行指出eth0接口具有一個(gè)靜態(tài)的(static)IP配置;
第7行-第11行分別設(shè)置eth0接口的ip、網(wǎng)絡(luò)號(hào)、掩碼、廣播地址和網(wǎng)關(guān)。
再來看一個(gè)更復(fù)雜點(diǎn)的:
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
這次,有了一個(gè)復(fù)雜一些的掩碼,和一個(gè)比較奇怪的廣播地址。還有就是增加的接口啟用、禁用時(shí)的路由設(shè)置;
第19行和20行配置的左右是在接口啟用的時(shí)候,添加一條靜態(tài)路由和一個(gè)缺省路由;
第21行和22行會(huì)在接口禁用的時(shí)候,刪掉這兩條路由配置。
至于配置路由的寫法,仔細(xì)看,它就是route命令嘛。
繼續(xù),下面是一個(gè)物理網(wǎng)卡上多個(gè)接口的配置方法:
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上配置了另外一個(gè)地址,這種配置方法在配置一塊網(wǎng)卡多個(gè)地址的時(shí)候很常見:有幾個(gè)地址就配置幾個(gè)接口。冒號(hào)后面的數(shù)字可以隨便寫的,只要幾個(gè)配置的名字不重復(fù)就可以。
下面是pre-up和post-down命令時(shí)間。這是一組命令(pre-up、up、post-up、pre-down、down、post-down),分別定義在對(duì)應(yīng)的時(shí)刻需要執(zhí)行的命令。
34 auto eth0
35 iface eth0 inet dhcp
36???? pre-up [ -f /etc/local-network-ok ]
第36行會(huì)在激活eth0之前檢查/etc/local-network-ok文件是否存在,如果不存在,則不會(huì)激活eth0。
再更進(jìn)一步的例子:
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/目錄 中,使用的時(shí)候需要給它加上可執(zhí)行權(quán)限。這兩行命令會(huì)檢測兩塊網(wǎng)卡的MAC地址是否為11:22:33:44:55:66和 AA:BB:CC:DD:EE:FF,如果正確,則啟用網(wǎng)卡。如果MAC地址錯(cuò)誤,就不會(huì)啟用這兩塊網(wǎng)卡。
第42行和第45行是假定在這兩塊網(wǎng)卡上分別執(zhí)行的命令,你可以把它們替換成你想要的任何玩意 :)
手冊(cè)上說,這種方法主要是用來檢測兩塊網(wǎng)卡的MAC地址交換(If their MAC addresses get swapped),其實(shí)就是兩塊網(wǎng)卡名互換了,這種情況在debian系統(tǒng)上再常見不過了,主要是因?yàn)閮?nèi)核識(shí)別網(wǎng)卡的順序發(fā)生了變化。這個(gè)問題可以用下面 的這種方法來避免。
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/目錄里,也同樣要加可執(zhí)行權(quán)限。這個(gè)腳本的作用,就是獲得每塊網(wǎng)卡的MAC地址。
這段配置首先配置了兩個(gè)邏輯接口(這個(gè)名詞的定義請(qǐng)參見debian參考手冊(cè)
再來看下面這段配置:
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
這段配置只是啟用一個(gè)網(wǎng)卡,但是ifupdown不對(duì)這個(gè)網(wǎng)卡設(shè)置任何ip,而是由外部程序來設(shè)置ip。
最后一段配置,這段配置啟用了網(wǎng)卡的混雜模式,用來當(dāng)監(jiān)聽接口。
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中對(duì)于以太網(wǎng)卡的配置基本上介紹完了
關(guān)鍵詞標(biāo)簽:linux,interfaces
相關(guān)閱讀
熱門文章
安裝紅帽子RedHat Linux9.0操作系統(tǒng)教程
Tomcat9.0如何安裝_Tomcat9.0環(huán)境變量配置方法
多種操作系統(tǒng)NTP客戶端配置
Linux操作系統(tǒng)修改IP
人氣排行 Linux下獲取CPUID、硬盤序列號(hào)與MAC地址 dmidecode命令查看內(nèi)存型號(hào) linux tc實(shí)現(xiàn)ip流量限制 安裝紅帽子RedHat Linux9.0操作系統(tǒng)教程 linux下解壓rar文件 lcx.exe、nc.exe、sc.exe入侵中的使用方法 Ubuntu linux 關(guān)機(jī)、重啟、注銷 命令 查看linux服務(wù)器硬盤IO讀寫負(fù)載