尝试验证:

在广大自动化职务中,脚本往往是通过读取配置文件来获取新闻的,红帽系的种类自升级之后(CentOS7/RHEL7),网卡命名采纳“一致性互连网设施接口”的命名方式,导致分歧装备的不等网卡名称各分化。为了统一网卡名称,方便配置文件的书写,不得不修改网卡名称。


实验证实:

背景

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

背景

  给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重启后就失效了,由此我们要求将配备持久化。

定位

互连网布局文件如下:

# 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 [提供方向脚本,VirtualBox虚拟机NAT形式下不可能一连外网。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.以此类推。

参考:

持久化路由安顿

  大家将路由持久化设置在互连网布署文件/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.以此类推。

参考:

正文永久更新链接地址:http://www.linuxidc.com/Linux/2017-03/141585.htm

澳门金沙国际 1

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

Linux公社的RSS地址:

本文永久更新链接地址

澳门金沙国际 2

相关文章