linux网卡命名

linux网卡命名

Linux网卡命名规则

传统的 Linux 下面看到的网卡名称都是从 eth0, eth1, eth2 …. 一直连续下去,一般我们使用的电脑只有 1 , 2 个网卡,所以在使用上不会遇到太大的难题,但是在多个网卡就头大了,到底哪一个ethx 是对应到哪一个实体网卡.

还好现在有新的装置命名规则,目前看到有两种方式 Biosdevname & net.ifnames

Biosdevname

在讨论 Biosdevname 这种一致性的网络设备命名规则 device naming rule ,我们先来看看什么是 udev.
Linux 内核 2.6.15 之后的版本采用 sysfs 取代过去的 devfs ,我们就不需要像是传统的 Linux 把所有设备存放在 /dev/ 目录下(不管是不是会用到先通通产生),sysfs 多了一个 /sys/ 的目录存放动态设备节点 udev (sysfs), 优点是 /dev/ 不再需要预先产生很多不必要的设备.当有需要时系统再自行产生. udev 还提供了自己的规则来命名设备. 关于更多 udev 请自行参考 http://benjr.tw/10632

但老实说这还是很麻烦, Dell 戴尔开发了一个 udev 的协助工具 Biosdevname,这是一个以 GNU General Public License(GPL)下所开发的工具,它提供了一个一致性的命名机制,实体网卡的名称会依据 BIOS 所建议的方式来命名,命名规则大致如下.

  • Embedded network interface (LOM)
    em[1234…] ⁠[New enumeration starts at 1]

  • PCI card network interface
    p[slot]p[ethernet port] ⁠[For example: p3p4]

  • Virtual function
    p[slot]p[ethernet port]_ ⁠[For example: p3p4_1]

biosdevname 会依据系统的SMBIOS来为你的设备命名,不过系统的 BIOS 须为 SMBIOS 2.6 版本或是更高,当然如果你要使用此功能,必须使用 root 身份使用 yum 来安装 biosdevname 套件.

  • RHEL (RedHat)
1
#yum install biosdevname

接着在 Kernel 加入参数 biosdevname=1

1
2
3
#vi /boot/grub/grub.conf
kernel /boot/vmlinuz biosdevname=1
initrd /boot/initrd.img

如果系统使用的是 GRUB2 ,要修改的档案为 /etc/default/grub 内的 GRUB_CMDLINE_LINUX .请参考 GRUB2

1
2
3
4
5
6
7
8
9
[root@localhost ~]# cat /etc/default/grub
GRUB_TIMEOUT=5
GRUB_DISTRIBUTOR="$(sed 's, release .*$,,g' /etc/system-release)"
GRUB_DEFAULT=saved
GRUB_DISABLE_SUBMENU=true
GRUB_TERMINAL_OUTPUT="console"
GRUB_CMDLINE_LINUX="crashkernel=auto rd.lvm.lv=centos/root rd.lvm.lv=centos/swap rhgb quiet net.ifnames=0 biosdevname=0"
GRUB_DISABLE_RECOVERY="true"
[root@localhost ~]# grub2-mkconfig
  • Ubuntu
1
#yum install biosdevname

一样在 append 加入参数 biosdevname=1

1
2
3
#vi /boot/grub/grub.conf
kernel /boot/vmlinuz
append initrd=/boot/initrd.img biosdevname=1

如果要取消将 biosdevname=0 即可.

或者是手动更改 /etc/udev/rules.d/70-persistent-net.rules 改变其命名规则.下面示范列参考

1
2
3
# vi /etc/udev/rules.d/70-persistent-net.rules
# PCI device 0x14e4:0x1680 (tg3)
ACTION=="add", SUBSYSTEM=="net", DRIVER=="tg3", ATTR{address}=="00:1A:64:22:04:83", NAME="em1"

只要修改 NAME 的部分,如果系统没有 /etc/udev/rules.d/70-persistent-net.rules 请自行创建.
这一篇就讨论到如何自行修改网路设备的命名规则. http://benjr.tw/10626

net.ifnames

要查 net.ifnames 请用这个关键字 Predictable Network Interface Names,这个是预设的功能,除非你在开机的时候加入内核参数 net.ifnames=0 ,前面介绍的 biosdevname 也就没有再用了,先来看看 Predictable Network Interface 的命名规则长怎样!!

装置名称由 装置类型 + 装置位置+数字 组成:

装置类型
  1. en for Ethernet,
  2. wl for wireless LAN (WLAN),
  3. ww for wireless wide area network (WWAN).
装置位置+数字
  • o on-board device index number
  • s[f][d] hotplug slot index number
  • x MAC address
  • ps[f][d] PCI geographical location
  • ps[f][u][..][i] USB port number chain

enp0s3 就代表 Ethernet ,PCI bus 0,slot 3

Biosdevname & net.ifnames

问题来了,如果 Biosdevname & net.ifnames 都用了,那名称会变成怎麽样!!!

  1. No parameters (net.ifnames 为预设): NIC identified as “enp5s2”.
  2. Parameter biosdevname=0 (net.ifnames 为预设): NIC identified as “enp5s2”.
  3. Parameter net.ifnames=0 (采用 biosdevname 规则): NIC identified as “em1”.
  4. Parameter net.ifnames=0 AND biosdevname=0 (最传统的方式,eth0 eth1 傻傻分不清): NIC identified as “eth0”.

参考:

Linux – Biosdevname & net.ifnames

# 推荐文章
  1.git推送到vps
  2.http服务器搭建
  3.linux网卡命名
  4.docker使用
  5.hexo创建博客

评论


:D 一言句子获取中...

加载中,最新评论有1分钟延迟...