centos7-修改网卡名称

需求

今天测试了一下centos7,发现centos7的网卡名的后缀变成随机的了。
以前写的基于eth0和eth1的脚本已经不能用了,为了我的脚本,我只能把centos7的网卡命名规则改回来了。

实战过程

1
2
3
cd /etc/sysconfig/network-scripts/
mv ifcfg-eno1* ifcfg-eth0
mv ifcfg-eno3* ifcfg-eth1

重写网卡的配置文件

快速修改etho0网卡配置文件

1
2
3
4
5
6
cat >ifcfg-eth0<<EOF
DEVICE=eth0
ONBOOT=yes
BOOTPROTO=dhcp
DNS=8.8.8.8
EOF

快速修改etho1网卡配置文件

1
2
3
4
5
6
7
cat >ifcfg-eth1<<EOF
DEVICE=eth1
ONBOOT=yes
BOOTPROTO=static
IPADDR=10.0.2.192
NETWORK=255.255.255.0
EOF

备份grub配置文件,方便回滚

1
2
3
cp /etc/default/grub /etc/default/grub_`date +%F`
vim /etc/sysconfig/grub
GRUB_CMDLINE_LINUX="net.ifnames=0 biosdevname=0 rhgb quiet"

生成新的配置文件

1
grub2-mkconfig -o /boot/grub2/grub.cfg

关闭多余的开机启动项

1
systemctl list-unit-files |grep enabled |awk '{print $1}' |egrep -v "sshd.service|rsyslog.service|crond.service"| sed -rn 's#(.*)#systemctl disable \1 #gp' |bash

重启系统

1
reboot

重启后登录系统查看IP地址已经修改成功

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

ip addr

1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:50:56:39:b1:45 brd ff:ff:ff:ff:ff:ff
inet 192.168.0.128/24 brd 192.168.0.255 scope global dynamic eth0
valid_lft 1122sec preferred_lft 1122sec
inet6 fe80::250:56ff:fe39:b145/64 scope link
valid_lft forever preferred_lft forever
3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:50:56:36:b7:0a brd ff:ff:ff:ff:ff:ff
inet 192.168.2.169/24 brd 192.168.2.255 scope global dynamic eth1
valid_lft 1721sec preferred_lft 1721sec
inet6 fe80::250:56ff:fe36:b70a/64 scope link
valid_lft forever preferred_lft forever