星彩网app下载:virtualbox虚构机NAT形式下不可能三

试验申明:

在非常多自动化义务中,脚本往往是经过读取配置文件来获取新闻的,红帽系的系统自晋级之后(CentOS7/LANDHEL7),网卡命名选择“一致性网络设施接口”的命名格局,导致不一样道具的不如网卡名称各分化样。为了统一网卡名称,方便配置文件的书写,不得不修改网卡名称。


背景

  给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

定位

互联网安顿文件如下:

# 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

 

本文由星彩网app下载发布于星彩网app下载,转载请注明出处:星彩网app下载:virtualbox虚构机NAT形式下不可能三

TAG标签: 星彩网app下载
Ctrl+D 将本页面保存为书签,全面了解最新资讯,方便快捷。