linux網卡配置解析及相關命令
A.lspci命令
[root@localhost ~]# lspci |grep Ethernet #查看網卡是否被識別及型號
#使用lspci不加參數可以查看當前系統的所有硬件設備
B.查看網卡當前的驅動程序版本
[root@localhost ~]# ethtool -i eth0
driver: e1000 #intel 8254EM 對應驅動模塊爲e1000
version: 7.3.21-k6-NAPI
firmware-version: N/A
bus-info: 0000:02:01.0
C.更新網卡驅動
[root@localhost ~]#tar –zxvf igbvf-3.1.7.tar.gz #解壓驅動程序包
[root@localhost ~]#cd igbvf-3.1.7/src #進入驅動源目錄
[root@localhost src]#make install #安裝
[root@localhost src]#rmmod igb #刪除舊的igb模塊
[root@localhost src]#modprobe igb #重新加載igb模塊
[root@localhost ~]# ethtool -i eth2 #查看驅動是否生效
driver: igb #Intel 82576對應驅動模塊爲igb
version: 3.1.7 #驅動程序版本已更新
firmware-version: 1.8-6 #網卡的固件版本
bus-info: 0000:01:00.0
D.定位那塊網卡爲eth0,或eth1
[root@localhost ~]# ethtool -p eth0 #此時看網卡會閃燈的就是eth0,按ctrl+c結束測試。
[root@localhost ~]# ethtool -p eth1 #此時看網卡會閃燈的就是eth1,按ctrl+c結束測試。
.... ...
其它以此類推即可,如果網卡沒有指示燈的,只能通過一塊一塊網上分別插上網線後,使用 ethtool命令查看當然網卡的連接狀態。(ethtool eth0/eth1 利用mii-tool更加方便)
[root@localhost ~]# mii-tool eth0
eth0: negotiated 100baseTx-FD, link ok
[root@localhost ~]# mii-tool eth1
SIOCGMIIPHY on 'eth1' failed: No such device
[root@localhost ~]#
E.查看網卡狀態信息
[root@localhost ~]# ethtool eth0
Settings for eth0:
Supported ports: [ TP ]
Supported link modes: 10baseT/Half 10baseT/Full
100baseT/Half 100baseT/Full
1000baseT/Full ##網卡支持連接模式
Supports auto-negotiation: Yes
Advertised link modes: 10baseT/Half 10baseT/Full
100baseT/Half 100baseT/Full
1000baseT/Full
Advertised pause frame use: No
Advertised auto-negotiation: Yes #自適應模式開啓
Speed: 1000Mb/s #當前連接速度 ,如未接網線此處顯示unknown
Duplex: Full #當前雙工模式-全雙工,如未接網線此處顯示unknown
Port: Twisted Pair #網絡類型-又絞線,如果未接網線此處顯示unknown
PHYAD: 0
Transceiver: internal
Auto-negotiation: on
MDI-X: Unknown
Supports Wake-on: d #禁用喚醒模式 ( pumbg 起用喚醒)
Wake-on: d #禁用喚醒 (g 喚醒起用) ;遠程關機開機需設置
Current message level: 0x00000007 (7)
Link detected: yes #鏈路檢測
[root@localhost ~]#
F.網卡數據包收發統計
root@localhost ~]# ethtool -S eth0
NIC statistics:
rx_packets: 1001 #接收數據包
tx_packets: 334 #發送數據包
rx_bytes: 91224 #接收字節數
tx_bytes: 40267 #發送字節數
rx_broadcast: 0
tx_broadcast: 0
rx_multicast: 0
G.關閉自動適用,強制使用百兆或者千兆
[root@localhost ~]#ethtool -s eth1 speed 100 duplex full autoneg off
#本例是將eth1強制變更爲100Mbps全雙工模式
[root@localhost ~]# ethtool -s eth1 autoneg on #恢復網卡爲自適應模式
上面的設置,服務器重啓過之後即失效,如果需要重啓後也生效,有兩種辦法,
7.1、將ethtool -s eth1 speed 100 duplex full autoneg off寫入/etc/rc.local
7.2、將ETHTOOL_OPTS="speed 100 duplex full autoneg off"添加到網卡配置文件中。
H.網卡配置說明
網卡的配置文件位於 /etc/sysconfig/network-scripts/ifcfg-eth0中,該配置文件內容如下:
[root@localhost ~]#cat /etc/sysconfig/network-scripts/ifcfg-eth0 #ib配置文件爲ifcfg-ib0
BOOTPROTO=static #指定爲靜態IP,如果動態,將此改爲dhcp
DEVICE=eth0 #網卡號 ,如果爲ib網,此處爲ib0
HWADDR=00:9f:9e:6c:8e:b6 #MAC地址,有多塊網卡時,必須有,防止配置文件飄移
IPADDR=192.168.1.1 #IP地址,如果動態,刪除此行
NETMASK=255.255.255.0 #掩碼地址,如果動態,刪除此行
USERCTL=no
ONBOOT=yes #重啓網絡或重啓服務器時讓配置文件生效
TYPE=Ethernet #網絡類型,可以不要
網卡的mac地址,可以通過查看以下文件看到:
[root@localhost ~]#cat /sys/class/net/eth0/address
00:9f:9e:6c:8e:b6
配置好配置文件後,需要重啓服務器或重啓網絡服務才能生效,重啓網絡服務的命令如下:
[root@localhost ~]# service network restart #重啓網絡
[root@localhost ~]#etc/init.d/network restart #使用這個命令格式也可以
J.雙網卡綁定
Linux下雙網卡綁定比較簡單,總共有七種模式,七種模式都有不同的功能,其中0、2、3、4需要交換機支持,1、5、6不需要交換機配置支持
模式解析:
mode=0 round-robin輪詢策略(Round-robin policy),模式代號是0。該策略是按照設備順序依次傳輸數據包,直到最後一個設備。這種模式提供負載均衡和容錯能力。
mode=1 active-backup活動備份策略(Active-backup policy),模式代號是1。該策略只有一個設備處理數據,當它宕機的時候就會由備份代替,僅提供容錯能力。
mode=2 load balancing (xor)異或策略(XOR policy),模式代號是2。該策略是根據MAC地址異或運算的結果來選擇傳輸設備,提供負載均衡和容錯能力。
mode=3 fault-tolerance (broadcast)廣播策略(Broadcast policy),模式代號是3。該策略通過全部設備來傳輸所有數據,提供容錯能力。
mode=4 lacp IEEE 802.3ad 動態鏈接聚合(IEEE 802.3ad Dynamic link aggregation),模式代號是4。該策略通過創建聚合組來共享相同的傳輸速度,需要交換機也支持 802.3ad 模式,提供容錯能力。
mode=5 transmit load balancing適配器傳輸負載均衡(Adaptive transmit load balancing),模式代號是5。該策略是根據當前的負載把發出的數據分給每一個設備,由當前使用的設備處理收到的數據。本策略的通道聯合不需要專用的交換機支持,提供負載均衡和容錯能力。
mode=6 adaptive load balancing適配器負載均衡(Adaptive load balancing),模式代號是6。該策略在IPV4情況下包含適配器傳輸負載均衡策略,由ARP協商完成接收的負載,通道聯合驅動程序截獲ARP在本地系統發送出的請求,用其中一個設備的硬件地址覆蓋從屬設備的原地址。
K.網卡綁定步驟
1.修改eth0和eth1網卡配置
以下以將eth0與eth1按mode=6模式進行綁定的例子;
修改eth0與eth1配置文件內容如下(如果需要綁定更多網卡,則即更多的網卡配置文件按下面兩塊網卡的配置文件格式進行修改即可):
[root@localhost ~]# cd /etc/sysconfig/network –scripts
cat ifcfg-eth0 cat ifcfg-eth1
BOOTPROTO=none BOOTPROTO=none
SLAVE=yes #重點 SLAVE=yes #重點
DEVICE=eth0 DEVICE=eth1
MASTER=bond0 #重點 MASTER=bond0 #重點
USERCTL=no USERCTL=no
ONBOOT=yes ONBOOT=yes
2.新建ifcfg-bond0網卡配置文件(虛擬)
新建一個ifcfg-bond0文件,其內容如下:
[root@localhost ~]# cd /etc/sysconfig/network –scripts
root@localhost network –scripts]#vi ifcfg-bond0
BOOTPROTO=none
DEVICE=bond0 #網卡號 ,虛擬網卡號
NETMASK=255.255.255.0 #子網掩碼
IPADDR=192.168.1.10 #IP地址
GATEWAY=192.168..1 #網關
USERCTL=no
ONBOOT=yes
如果需要新建另一組綁定,則新建一個ifcfg-bond1即可,ifcfg-bond1內容按ifcfg-bond0配置文件格式修改即可。
3.網卡綁定
修改完網卡配置文件後,執行下面兩條命令:
[root@localhost ~]# echo "alias bond0 bonding" >> /etc/modprobe.conf
[root@localhost ~]# echo "options bond0 mode=6" >> /etc/modprobe.conf
#根據需要設定mode後面的數字,此處可以加參數miimon=500,即檢測間隔時間。
也可以不寫此行,而在bond0的配置文件中,添加如下一行:
[root@localhost ~]#cd /etc/sysconfig/network –scripts/
[root@localhost network –scripts]# echo BONDING_OPTS="mode=1" >> ifcfg-bond0
#此處可加參數miimon=500,即狀態檢測時間,如:
echo BONDING_OPTS="mode=1 miimon=500" >> ifcfg-bond0
至此,網卡綁定相關配置文件已修改完畢,這時重啓網絡即可讓綁定生效。注意:重啓過程中需要綁定組中的網卡至少有一塊已經正確連接在交換機上,否則可能會造成重啓進入操作系統後,bond0虛擬網卡不生效的問題,這時插上網線,重啓系統即可。
L.曙光服務器Redhat 6換主板問題,換卡變動解決
1.Redhat 6換主板後,eht0、eth1網卡會變成eth3、eth4
在紅帽6系統下面,如果換完主板後,原主板記錄網卡的/etc/udev/rules.d文件夾下面一個與net相關的配置文件不能自動刪除原網卡信息造成的,需要進行手動刪除,否則就會造成eth0變成eth3,eth1變成eth4,解決該問題只需修改以下文件即可。
[root@localhost rules.d]# cd /etc/udev/rules.d
[root@localhost rules.d]# ls
40-hplip.rules 70-persistent-net.rules 97-bluetooth-serial.ru
56-hpmud_support.rules 85-pcscd_ccid.rules 98-kexec.rules
60-fprint-autosuspend.rules 90-alsa.rules 99-fuse.rules
60-pcmcia.rules 90-hal.rules 99-lustre.rules
60-raw.rules 90-rdma.rules
70-persistent-cd.rules 91-drm-modeset.rules
2.修改配置文件
[root@localhost rules.d]# cat 70-persistent-net.rules
# This file was automatically generated by the /lib/udev/write_net_rules
# program, run by the persistent-net-generator.rules rules file.
# You can modify it, as long as you keep each rule on a single
# line, and change only the value of the NAME= key.
# PCI device 0x8086:0x10d3 (e1000e)
SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="48:5b:39:12:51:90", ATTR{type}=="1", KERNEL=="eth*", NAME="eth0" (如果換過主板後,將該條刪除)
# PCI device 0x8086:0x10d3 (e1000e)
SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="48:5b:39:12:4c:57", ATTR{type}=="1", KERNEL=="eth*", NAME="eth1"(如果換過主板後,將該條刪除)
3.停止網絡管理服務
按照上述方法刪除後,請按如下操作後重啓,即可正常按照常規網卡配置IP的方法進行網卡的IP配置,重啓後eth2\eth3也會變成eth0\eth1。
[root@localhost ~]# chkconfig NetworkManager off
[root@localhost ~]# reboot