重命名网卡名称,virtualbox虚拟机NAT模式下不能连接外网

实验说明:

在许多自动化任务中,脚本往往是通过读取配置文件来获取信息的,红帽系的系统自升级之后(CentOS7/RHEL7),网卡命名采用“一致性网络设备接口”的命名方法,导致不同设备的不同网卡名称各不相同。为了统一网卡名称,方便配置文件的书写,不得不修改网卡名称。


实验说明:

背景

  给VirtualBox虚拟机(装载了Ubuntu16.04系统)配置了两张网卡,网络模式分别为“网络地址转换(NAT)”和“仅主机(Host-Only)适配器”,其中,enp0s3网卡(NAT)用于外网访问,而enp0s8网卡(Host-Only)用于主机访问虚拟机。然而,虚拟机启动后,却不能访问外网。
  

实验环境:

  • 宿主机系统   :Fedora 28 WorkStation
  • 虚拟机管理器 :Virtual-Box  5.2.16
  • 虚拟机配置   :系统:CentOS 7.2 1511  (minimal)
                 网络:enp0s3   Host-Only  192.168.56.0/24
                      enp0s8   Host-Only  192.168.56.0/24
                      enp0s9   Host-Only  192.168.56.0/24
                      enp0s10  Host-Only  192.168.56.0/24

在许多自动化任务中,脚本往往是通过读取配置文件来获取信息的,红帽系的系统自升级之后(CentOS7/RHEL7),网卡命名采用“一致性网络设备接口”的命名方法,导致不同设备的不同网卡名称各不相同。为了统一网卡名称,方便配置文件的书写,不得不修改网卡名称。

定位

网络配置文件如下:

# vi /etc/network/interface

...
# The primary network interface
auto enp0s3
iface enp0s3 inet dhcp

auto enp0s8
iface enp0s8 inet static
address 192.168.137.16
netmask 255.255.255.0
gateway 192.168.137.1

  eth0使用dhcp,eth1使用static。eth0的实际网络如下:

# ifconfig 
enp0s3: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 10.0.2.15  netmask 255.255.255.0  broadcast 10.0.2.255
        inet6 fe80::a00:27ff:fe55:2858  prefixlen 64  scopeid 0x20<link>
        ether 08:00:27:55:28:58  txqueuelen 1000  (Ethernet)
        RX packets 6  bytes 1476 (1.4 KB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 33  bytes 3108 (3.1 KB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

  打开其路由,才发现了问题。

# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         192.168.137.1   0.0.0.0         UG    0      0        0 enp0s8
10.0.2.0        0.0.0.0         255.255.255.0   U     0      0        0 enp0s3
192.168.137.0   0.0.0.0         255.255.255.0   U     0      0        0 enp0s8

  enp0s8网卡成为了默认路由,这就导致其他路由不能匹配到的网段都会走enp0s8这个网卡,而我们实际上配置与外网连接的虚拟网卡是enp0s3,环境自然就连接不了外网了。我们可以尝试手动来删除现在的默认路由。

# route del default
# route add default gw 10.0.2.2 dev enp0s3
# route -n

Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
default         gateway         0.0.0.0         UG    0      0        0 enp0s3
10.0.2.0        0.0.0.0         255.255.255.0   U     0      0        0 enp0s3
192.168.137.0   0.0.0.0         255.255.255.0   U     0      0        0 enp0s8

  路由设置成功,OS也可以访问外网了。但这只是修改了本次的路由设置,OS重启后就失效了,因此我们需要将配置持久化。

实验步骤:

  1. ### 查看当前网卡设备数目及名称

    1 [root@localhost ~]$ ls -l /etc/sysconfig/network-scripts | awk  '/ifcfg-e/ {print $9}'
    2 ifcfg-enp0s10
    3 ifcfg-enp0s3
    4 ifcfg-enp0s8
    5 ifcfg-enp0s9
    
  2. ### 备份系统默认网卡配置文件

    1 [root@localhost ~]$ cd /etc/sysconfig/network-scripts/
    2 [root@localhost network-scripts]$ nic_list=`ls -l /etc/sysconfig/network-scripts|awk  '/ifcfg-e/ {print $9}'`
    3 [root@localhost network-scripts]$ for nic_name in $nic_list; do 
    4  > cp $nic_name $nic_name.bak; done
    
  3. ### 重命名网卡配置文件名

    1 [root@localhost network-scripts]$ i=0 
    2 [root@localhost network-scripts]$ nic_list=`ls -l /etc/sysconfig/network-scripts|awk  '/ifcfg-e/ {print $9}'`
    3 [root@localhost network-scripts]$ for nic_name in $nic_list; do  
    4 > mv $nic_name ifcfg-eth${i}; 
    5 > i=$(expr $i + 1); 
    6 > done
    
  4. ### 修改网卡配置文件

    1 [root@localhost network-scripts]$ for new_nic_name in $new_nic_list; do 
    2 > name=$(echo $new_nic_name|cut -b 7-);
    3 > sed -i "s/NAME=.*/NAME=$name/g" $new_nic_name;
    4 > sed -i "s/DEVICE=.*/DEVICE=$name/g" $new_nic_name;
    5 > sed -i 's/ONBOOT=no/ONBOOT=yes/g' $new_nic_name
    6 > done
    
  5. ### 修改/etc/default/grub文件,禁用biosdevname程序

    1 [root@localhost network-scripts]$ sed -i '/GRUB_CMDLINE_LINUX=/d' /etc/default/grub
    2 [root@localhost network-scripts]$ echo 'GRUB_CMDLINE_LINUX="rd.lvm.lv=centos/root \
    3 > rd.lvm.lv=centos/swap crashkernel=auto \
    4 > net.ifnames=0 biosdevname=0 rhgb quiet"' \
    5 > >> /etc/default/grub
    
  6. ### 重新生成GRUB配置并更新内核参数

    1 [root@localhost network-scripts]$ grub2-mkconfig -o /boot/grub2/grub.cfg
    
  7. ### 重启

    1 [root@localhost ~]$ reboot now
    
  8. ### 查询网卡信息

    1 [root@localhost ~]$ ip a | grep eth[0-9]
    2 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    3     inet 192.168.56.114/24 brd 192.168.56.255 scope global dynamic eth0
    4 3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    5     inet 192.168.56.115/24 brd 192.168.56.255 scope global dynamic eth1
    6 4: eth2: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    7     inet 192.168.56.116/24 brd 192.168.56.255 scope global dynamic eth2
    8 5: eth3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    9     inet 192.168.56.117/24 brd 192.168.56.255 scope global dynamic eth3
    

实验环境:

持久化路由配置

  我们将路由持久化设置在网络配置文件/etc/network/interfaces中。在网卡启动后添加对应的路由增删的代码,与route命令类似,只是在句首加上up即可。

# vi /etc/network/interfaces
...
auto enp0s3
iface enp0s3 inet dhcp
up route add default gw 10.0.2.2 dev enp0s3

auto enp0s8
iface enp0s8 inet static
address 192.168.137.16
netmask 255.255.255.0
gateway 192.168.137.1
up route del default dev enp0s8

  注意:up route add default gw [gateway-addr] dev
[dev-name],该语句中,[dev-name]表示外网网卡的名称,即上面的enp0s3,而[gateway-addr]表示外网网卡使用的网关ip地址。
  那么,如何获取这个外网网卡的网关地址呢?virtualbox如下规定:

In NAT mode, the guest network interface is assigned to the IPv4 range 10.0.x.0/24 by default where x corresponds to the instance of the NAT interface +2. So x is 2 when there is only one NAT instance active. In that case the guest is assigned to the address 10.0.2.15, the gateway is set to 10.0.2.2 and the name server can be found at 10.0.2.3.

  简单的说,就是如果第0个网卡是NAT网卡,那么其网段的第三个数字就0+2=2就是10.0.2.0,网关为10.0.2.2,name
server则是10.0.2.3.以此类推。

参考:

PS:CentOS7 修改默认网卡名称脚本 (change_default_nic_name.sh),直接拷贝就可使用。

 1 #!/usr/bin/bash
 2 #this script will change default ifcfg-enoxxxx/ifcfg-enpxxxx to ifcfg-ethx 
 3 
 4 ls -l /etc/sysconfig/network-scripts|awk  '/ifcfg-eno[0-9]*/ {print $9}' > default_nic_name.txt
 5 i=0
 6 cat default_nic_name.txt| while read line
 7 do
 8     cd /etc/sysconfig/network-scripts
 9     name=$(echo $line|cut -b 7-)
10     cp $line ${line}.bak
11     sed -i "s/$name/eth${i}/g" $line
12     sed -i 's/ONBOOT=no/ONBOOT=yes/g' $line
13     mv $line ifcfg-eth${i}
14     i=$(expr $i + 1)
15
16 done
17 
18 sed -i '/GRUB_CMDLINE_LINUX=/d' /etc/default/grub
19 echo 'GRUB_CMDLINE_LINUX="rd.lvm.lv=centos/root rd.lvm.lv=centos/swap crashkernel=auto net.ifnames=0 biosdevname=0 rhgb quiet"' >> /etc/default/grub 
20 grub2-mkconfig -o /boot/grub2/grub.cfg
21 reboot

 

宿主机系统  :Fedora 28
WorkStation
虚拟机管理器 :Virtual-Box  5.2.16
虚拟机配置  :系统:CentOS 7.2 1511  (minimal)
            网络:enp0s3  Host-Only  192.168.56.0/24
                  enp0s8  Host-Only  192.168.56.0/24
                  enp0s9  Host-Only  192.168.56.0/24
                  enp0s10  Host-Only  192.168.56.0/24

实验步骤:

1.查看当前网卡设备数目及名称

[root@localhost ~]$ ls -l /etc/sysconfig/network-scripts | awk 
‘/ifcfg-e/ {print $9}’
ifcfg-enp0s10
ifcfg-enp0s3
ifcfg-enp0s8
ifcfg-enp0s9

2.备份系统默认网卡配置文件

[root@localhost ~]$ cd /etc/sysconfig/network-scripts/
[root@localhost network-scripts]$ nic_list=`ls -l
/etc/sysconfig/network-scripts|awk  ‘/ifcfg-e/ {print $9}’`
[root@localhost network-scripts]$ for nic_name in $nic_list; do
 > cp $nic_name $nic_name.bak; done

3.重命名网卡配置文件名

[root@localhost network-scripts]$ i=0
[root@localhost network-scripts]$ nic_list=`ls -l
/etc/sysconfig/network-scripts|awk  ‘/ifcfg-e/ {print $9}’`
[root@localhost network-scripts]$ for nic_name in $nic_list; do 
> mv $nic_name ifcfg-eth${i};
> i=$(expr $i + 1);
> done

4.修改网卡配置文件

[root@localhost network-scripts]$ for new_nic_name in
$new_nic_list; do
> name=$(echo $new_nic_name|cut -b 7-);
> sed -i “s/NAME=.*/NAME=$name/g” $new_nic_name;
> sed -i “s/DEVICE=.*/DEVICE=$name/g” $new_nic_name;
> sed -i ‘s/ONBOOT=no/ONBOOT=yes/g’ $new_nic_name
> done

5.修改/etc/default/grub文件,禁用biosdevname程序

[root@localhost network-scripts]$ sed -i ‘/GRUB_CMDLINE_LINUX=/d’
/etc/default/grub
[root@localhost network-scripts]$ echo
‘GRUB_CMDLINE_LINUX=”rd.lvm.lv=centos/root \
> rd.lvm.lv=centos/swap crashkernel=auto \
> net.ifnames=0 biosdevname=0 rhgb quiet”‘ \
> >> /etc/default/grub

6.重新生成GRUB配置并更新内核参数

[root@localhost network-scripts]$ grub2-mkconfig -o
/boot/grub2/grub.cfg

7.重启

[root@localhost ~]$ reboot now

8.查询网卡信息

[root@localhost ~]$ ip a | grep eth[0-9]
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc
pfifo_fast state UP qlen 1000
    inet 192.168.56.114/24 brd 192.168.56.255 scope global dynamic
eth0
3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc
pfifo_fast state UP qlen 1000
    inet 192.168.56.115/24 brd 192.168.56.255 scope global dynamic
eth1
4: eth2: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc
pfifo_fast state UP qlen 1000
    inet 192.168.56.116/24 brd 192.168.56.255 scope global dynamic
eth2
5: eth3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc
pfifo_fast state UP qlen 1000
    inet 192.168.56.117/24 brd 192.168.56.255 scope global dynamic eth3

PS:CentOS7 修改默认网卡名称脚本
(change_default_nic_name.sh),直接拷贝就可使用。

#!/usr/bin/bash
#this script will change default ifcfg-enoxxxx/ifcfg-enpxxxx to
ifcfg-ethx

ls -l /etc/sysconfig/network-scripts|awk  ‘/ifcfg-eno[0-9]*/ {print
$9}’ > default_nic_name.txt
i=0
cat default_nic_name.txt| while read line
do
    cd /etc/sysconfig/network-scripts
    name=$(echo $line|cut -b 7-)
    cp $line ${line}.bak
    sed -i “s/$name/eth${i}/g” $line
    sed -i ‘s/ONBOOT=no/ONBOOT=yes/g’ $line
    mv $line ifcfg-eth${i}
    i=$(expr $i + 1)
16 done

sed -i ‘/GRUB_CMDLINE_LINUX=/d’ /etc/default/grub
echo ‘GRUB_CMDLINE_LINUX=”rd.lvm.lv=centos/root rd.lvm.lv=centos/swap
crashkernel=auto net.ifnames=0 biosdevname=0 rhgb quiet”‘ >>
/etc/default/grub
grub2-mkconfig -o /boot/grub2/grub.cfg
reboot

必发88官网,Linux公社的RSS地址:

本文永久更新链接地址

必发88官网 1