bingoogolapple / bingoogolapple.github.io

个人主页。同时也通过 Issues 记录学习笔记
http://www.bingoogolapple.cn
86 stars 22 forks source link

VirtualBox 安装 Ubuntu 和 CentOS 虚拟机时的网络配置 #147

Open bingoogolapple opened 7 years ago

bingoogolapple commented 7 years ago

NAT 方式配置端口转发,提供外部网络的连接

  1. 配置端口转发

image image

  1. ssh 登陆 Ubuntu Server

image

NAT 方式 CentOS 无法上网

添加下的信息 /etc/sysconfig/network-scripts/ifcfg-enp0s3

# ONBOOT 默认是 no,将其改成 false,添加两个 DNS
ONBOOT=yes  
DNS1=8.8.8.8
DNS2=8.8.4.4

重启网络服务

service network restart
bingoogolapple commented 7 years ago

Host-Only 方式配置主机和虚拟机之间、虚拟机和虚拟机之间互通

image

CentOS 不配置 ip 也能用,只是 ip 是随机分配的。在 VirtualBox 中创建新网卡后 CentOS 没法自动设定 ,这里配置一下 ip

1.跳转到 network-scripts 目录

➜  centosthree cd /etc/sysconfig/network-scripts/
➜  network-scripts

2.查看新网卡 MAC 地址,我这里是 eth1 的 08:00:27:15:bc:91

➜  network-scripts dmesg | grep -in eth
395:[    1.701685] e1000 0000:00:03.0 eth0: (PCI:33MHz:32-bit) 08:00:27:57:fb:76
396:[    1.701692] e1000 0000:00:03.0 eth0: Intel(R) PRO/1000 Network Connection
422:[    2.069056] e1000 0000:00:08.0 eth1: (PCI:33MHz:32-bit) 08:00:27:15:bc:91
423:[    2.069061] e1000 0000:00:08.0 eth1: Intel(R) PRO/1000 Network Connection
➜  network-scripts

3.为新网卡生成 UUID

➜  network-scripts uuidgen enp0s8
c75d2c52-2974-45f9-82c7-a14c95afef1f
➜  network-scripts

4.拷贝 ifcfg-enp0s3 命名为 ifcfg-enp0s8,并编辑

➜  network-scripts cp ifcfg-enp0s3 ifcfg-enp0s8
➜  network-scripts vim ifcfg-enp0s8

5.修改 ifcfg-enp0s8 中的「NAME」和「DEVICE」为 enp0s8 修改「BOOTPROTO」为 static 修改「UUID」为第3步中的 c75d2c52-2974-45f9-82c7-a14c95afef1f 新增「HWADDR」为第2步中的 08:00:27:15:bc:91 新增「IPADDR」为 192.168.56.223(改成你自己需要的 ip) 下面是配置完后的 ifcfg-enp0s8

TYPE=Ethernet
BOOTPROTO=static
DEFROUTE=yes
PEERDNS=yes
PEERROUTES=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_PEERDNS=yes
IPV6_PEERROUTES=yes
IPV6_FAILURE_FATAL=no
IPV6_ADDR_GEN_MODE=stable-privacy
NAME=enp0s8
UUID=c75d2c52-2974-45f9-82c7-a14c95afef1f
DEVICE=enp0s8
HWADDR=08:00:27:15:bc:91
IPADDR=192.168.56.223
ONBOOT=yes
DNS1=8.8.8.8
DNS2=8.8.4.4

6.先断开网卡 enp0s8

➜  network-scripts ifdown enp0s8
成功断开设备 'enp0s8'。

7.然后再激活网卡 enp0s8

➜  network-scripts ifup enp0s8
成功激活的连接(D-Bus 激活路径:/org/freedesktop/NetworkManager/ActiveConnection/2)

Ubuntu 的话需要配置 interfaces 才能用

1.sudo vim /etc/network/interfaces

# This file describes the network interfaces available on your system
# and how to activate them. For more information, see interfaces(5).

source /etc/network/interfaces.d/*

# The loopback network interface
auto lo
iface lo inet loopback

# The primary network interface
auto enp0s3
iface enp0s3 inet dhcp

# 下面这一段是需要新添加的
auto enp0s8
iface enp0s8 inet static
address 192.168.56.111
network 192.168.56.0
netmask 255.255.255.0
broadcast 192.168.56.255

2.重启网卡

sudo /etc/init.d/networking restart