linux网卡配置解析及相关命令

A.lspci命令

[root@localhost ~]# lspci |grep Ethernet   #查看网卡是否被识别及型号

Linux删除网卡docker0 Linux删除网卡配置_操作系统

 #使用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                       对应驱动模块为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
#自适应模式开启
#当前连接速度 ,如未接网线此处显示unknown
当前双工模式-全双工,如未接网线此处显示unknown
#网络类型-又绞线,如果未接网线此处显示unknown

        PHYAD: 0
        Transceiver: internal
        Auto-negotiation: on
        MDI-X: Unknown
禁用唤醒模式 ( pumbg 起用唤醒)
#禁用唤醒  (g 唤醒起用) ;远程关机开机需设置
        Current message level: 0x00000007 (7)
#链路检测
[root@localhost ~]#


F.网卡数据包收发统计

root@localhost ~]# ethtool -S eth0
NIC statistics:
#接收数据包
#发送数据包
#接收字节数
#发送字节数
     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 配置文件为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.双网卡绑定


下双网卡绑定比较简单,总共有七种模式,七种模式都有不同的功能,其中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。该策略通过全部设备来传输所有数据,提供容错能力。

 

动态链接聚合(IEEE 802.3ad Dynamic link aggregation),模式代号是4。该策略通过创建聚合组来共享相同的传输速度,需要交换机也支持 802.3ad 模式,提供容错能力。

 

适配器传输负载均衡(Adaptive transmit load balancing),模式代号是5。该策略是根据当前的负载把发出的数据分给每一个设备,由当前使用的设备处理收到的数据。本策略的通道联合不需要专用的交换机支持,提供负载均衡和容错能力。

 

mode=6  adaptive load balancing适配器负载均衡(Adaptive load balancing),模式代号是6。该策略在IPV4情况下包含适配器传输负载均衡策略,由ARP协商完成接收的负载,通道联合驱动程序截获ARP在本地系统发送出的请求,用其中一个设备的硬件地址覆盖从属设备的原地址。


K.网卡绑定步骤

修改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                 地址

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