Linux网卡重命名规则

作者

QQ群:852283276
微信:arm80x86
微信公众号:青儿创客基地
B站:主页 https://space.bilibili.com/208826118

参考

linux网卡命名规则
Linux重命名网卡名称
Linux网卡命名enp3s0说明
Linux Ubuntu 修改网卡名字
Predictable Network Interface Names
systemd/src/udev/udev-builtin-net_id.c
redhat修改网卡名称
linux修改网卡名称(一般修改为eth0)(redHat7)
centos7/redhat7更改网卡名称为eth0
Linux系统修改网卡名称(eth1修改为eth0)

ubuntu

$ sudo vim /etc/default/grub
GRUB_CMDLINE_LINUX="net.ifnames=0 biosdevname=0"
$ sudo apt-get install grub2-common
$ sudo update-grub
$ sudo vim /etc/network/interfaces

另外一种需要编辑/etc/udev/rules.d/70-persistent-net.rules,例如,ATTR{address}=="bc:30:5b:9c:ae:79" 表示MAC地址,KERNEL=="eth*" 是原网卡名,NAME="eth0" 更改网卡名。

SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="bc:30:5b:b1:cd:be", ATTR{dev_id}=="0x0", ATTR{type}=="1", KERNEL=="eth*", NAME="eth0"

systemd,

/* retrieve on-board index number and label from firmware */
static int dev_pci_onboard(sd_device *dev, struct netnames *names) {
        unsigned long idx, dev_port = 0;
        const char *attr, *port_name = NULL;
        size_t l;
        char *s;
        int r;

        /* ACPI _DSM — device specific method for naming a PCI or PCI Express device */
        if (sd_device_get_sysattr_value(names->pcidev, "acpi_index", &attr) < 0) {
                /* SMBIOS type 41 — Onboard Devices Extended Information */
                r = sd_device_get_sysattr_value(names->pcidev, "index", &attr);
                if (r < 0)
                        return r;
        }

        r = safe_atolu(attr, &idx);
        if (r < 0)
                return r;
        if (idx == 0 && !naming_scheme_has(NAMING_ZERO_ACPI_INDEX))
                return -EINVAL;

        /* Some BIOSes report rubbish indexes that are excessively high (2^24-1 is an index VMware likes to
         * report for example). Let's define a cut-off where we don't consider the index reliable anymore. We
         * pick some arbitrary cut-off, which is somewhere beyond the realistic number of physical network
         * interface a system might have. Ideally the kernel would already filter his crap for us, but it
         * doesn't currently. */
        if (idx > ONBOARD_INDEX_MAX)
                return -ENOENT;

        /* kernel provided port index for multiple ports on a single PCI function */
        if (sd_device_get_sysattr_value(dev, "dev_port", &attr) >= 0)
                dev_port = strtoul(attr, NULL, 10);

        /* kernel provided front panel port name for multiple port PCI device */
        (void) sd_device_get_sysattr_value(dev, "phys_port_name", &port_name);

        s = names->pci_onboard;
        l = sizeof(names->pci_onboard);
        l = strpcpyf(&s, l, "o%lu", idx);
        if (port_name)
                l = strpcpyf(&s, l, "n%s", port_name);
        else if (dev_port > 0)
                l = strpcpyf(&s, l, "d%lu", dev_port);
        if (l == 0)
                names->pci_onboard[0] = '\0';

        if (sd_device_get_sysattr_value(names->pcidev, "label", &names->pci_onboard_label) < 0)
                names->pci_onboard_label = NULL;

        return 0;
}

嵌入式平台

开机打印,

[    1.062029] macb ff0b0000.ethernet eth0: Cadence GEM rev 0x50070106 at 0xff0b0000 irq 30 (1e:44:19:4f:f0:07)
[    1.071658] Atheros 8031 ethernet ff0b0000.ethernet-ffffffff:01: attached PHY driver [Atheros 8031 ethernet] (mii_bus:phy_addr=ff0b0000.ethernet-ffffffff:01, irq=POLL)
[    1.087025] macb ff0d0000.ethernet: Not enabling partial store and forward
[    1.094189] libphy: MACB_mii_bus: probed
[    1.169628] macb ff0d0000.ethernet eth1: Cadence GEM rev 0x50070106 at 0xff0d0000 irq 31 (52:76:2c:f7:02:65)
[    1.179262] Xilinx ZynqMP PS PCS/PMA PHY ff0d0000.ethernet-ffffffff:01: attached PHY driver [Xilinx ZynqMP PS PCS/PMA PHY] (mii_bus:phy_addr=ff0d0000.ethernet-ffffffff:01, irq=POLL)
[    1.195810] macb ff0e0000.ethernet: Not enabling partial store and forward
[    1.202964] libphy: MACB_mii_bus: probed
[    1.277627] macb ff0e0000.ethernet eth2: Cadence GEM rev 0x50070106 at 0xff0e0000 irq 32 (be:79:46:c8:71:fd)
[    1.287261] Xilinx ZynqMP PS PCS/PMA PHY ff0e0000.ethernet-ffffffff:01: attached PHY driver [Xilinx ZynqMP PS PCS/PMA PHY] (mii_bus:phy_addr=ff0e0000.ethernet-ffffffff:01, irq=POLL)

这里的mii_bus:phy_addr=ff0e0000.ethernet-ffffffff:01,是下面打印的,

void phy_attached_info(struct phy_device *phydev)
{
	phy_attached_print(phydev, NULL);
}
EXPORT_SYMBOL(phy_attached_info);

#define ATTACHED_FMT "attached PHY driver [%s] (mii_bus:phy_addr=%s, irq=%s)"
void phy_attached_print(struct phy_device *phydev, const char *fmt, ...)
{
	const char *drv_name = phydev->drv ? phydev->drv->name : "unbound";
	char *irq_str;
	char irq_num[8];

	switch(phydev->irq) {
	case PHY_POLL:
		irq_str = "POLL";
		break;
	case PHY_IGNORE_INTERRUPT:
		irq_str = "IGNORE";
		break;
	default:
		snprintf(irq_num, sizeof(irq_num), "%d", phydev->irq);
		irq_str = irq_num;
		break;
	}


	if (!fmt) {
		dev_info(&phydev->mdio.dev, ATTACHED_FMT "\n",
			 drv_name, phydev_name(phydev),
			 irq_str);
	} else {
		va_list ap;

		dev_info(&phydev->mdio.dev, ATTACHED_FMT,
			 drv_name, phydev_name(phydev),
			 irq_str);

		va_start(ap, fmt);
		vprintk(fmt, ap);
		va_end(ap);
	}
}
EXPORT_SYMBOL(phy_attached_print);

其中phydev_name来源于mii_bus注册的名字,而ffffffff就是platform_deviceid没设,获取aliases id给这个变量即可。

static inline const char *phydev_name(const struct phy_device *phydev)
{
	return dev_name(&phydev->mdio.dev);
}

static int macb_mii_init(struct macb *bp)
{
...
	bp->mii_bus->name = "MACB_mii_bus";
	if (bp->int_pcs) {
		bp->mii_bus->read = &zynqmp_ps_pcs_read;
		bp->mii_bus->write = &zynqmp_ps_pcs_write;
	} else {
		bp->mii_bus->read = &macb_mdio_read;
		bp->mii_bus->write = &macb_mdio_write;
	}
	snprintf(bp->mii_bus->id, MII_BUS_ID_SIZE, "%s-%x",
		 bp->pdev->name, bp->pdev->id);
	bp->mii_bus->priv = bp;
	bp->mii_bus->parent = &bp->dev->dev;
...
}

struct platform_device {
	const char	*name;
	int		id;
	bool		id_auto;
	struct device	dev;
	u32		num_resources;
	struct resource	*resource;

	const struct platform_device_id	*id_entry;
	char *driver_override; /* Driver name to force a match */

	/* MFD cell pointer */
	struct mfd_cell *mfd_cell;

	/* arch specific additions */
	struct pdev_archdata	archdata;
};
### Linux 系统缺失 `eth0` 网卡解决方案 在较新的 Linux 发行版中,默认情况下不再使用传统的 `ethX` 命名约定,而是采用了预测性的网络接口名称(Predictable Network Interface Names)。这可能是为什么看不到 `eth0` 的原因之一。 #### 使用新命名规则 现代 Linux 版本通常会自动为网卡分配更具描述性和唯一性的名字,比如 `enp3s0`, `ens33` 或者像案例中的 `p4p1`。这些名称由 udev 规则决定,并且基于硬件位置来创建[^4]。 为了确认当前系统的网络接口列表及其状态,可以执行命令: ```bash ip link show ``` 此命令将显示所有可用的网络接口以及它们的状态信息。 #### 修改 Udev 规则恢复传统命名方式 如果确实希望继续沿用旧式的 `eth0` 类型的名字,则可以通过调整udev规则实现这一点。编辑或新建文件 `/etc/udev/rules.d/70-persistent-net.rules` 并添加如下内容: ```text SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="<your_mac_address>", NAME="eth0" ``` 请注意替换 `<your_mac_address>` 为你实际想要命名为 `eth0` 的那张网卡对应的 MAC 地址。之后重启系统使更改生效。 另外一种方法是在内核参数里加入 `net.ifnames=0 biosdevname=0` 来关闭新型号命名机制并启用经典模式,在 GRUB 菜单中找到对应条目按 e 键进入编辑界面追加上述选项至 kernel 行末尾即可临时应用;永久修改需编辑 grub 配置文件 `/etc/default/grub` 中的 `GRUB_CMDLINE_LINUX_DEFAULT` 变量保存后更新引导程序配置(`sudo update-grub`)。 #### 手动设置静态 IP 和其他属性 对于某些特定场景下可能还需要手动指定IP地址等参数给这张被重命名叫作 `eth0` 的网卡。此时可利用 `ifconfig` 工具完成基本操作,例如开启、设定MAC地址和启动网卡的操作已经展示过[^1]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值