Linux ·

VirtualBox Ubuntu Server 16.04 手动设置 网络(IP, DNS, 路由)

VirtualBox Ubuntu Server 16.04 手动设置 网络(IP, DNS, 路由)

1. VirtualBox 设置全局网络

在virtualBox点击菜单管理->全局管理

配置NAT网络

参考下图配置, 依次点击相应的按钮并设置网络(其中DHCP任意, 将来我们都会使用固定IP,DHCP不起用也没有关系)

VirtualBox Ubuntu Server 16.04 手动设置 网络(IP, DNS, 路由) Linux 第1张

 配置仅主机网络

依次点击相应按钮并填写如下内容

1. 如果仅主机网络已经存在可以直接编辑(默认在安装virtualbox时创建),如果不存在则新增

VirtualBox Ubuntu Server 16.04 手动设置 网络(IP, DNS, 路由) Linux 第2张

同样DHCP为可选

VirtualBox Ubuntu Server 16.04 手动设置 网络(IP, DNS, 路由) Linux 第3张

2. 虚拟机设置网络

需要设置两个网络,

网卡1,仅主机模式,用于共享主机网络访问外部网络

网卡2,NAT模式,用于虚拟机之间连通

需要注意如果是复制 虚拟机方式得到的多台主机,

(1)建议刷新一下MAC地址,防止多个主机有相同的MAC,发生其他网络异常

(2)虚拟机启动后如果看到4张网卡,则需要将原有的网卡删除(server版本理论上不会,desktop版本会出现此问题 原因是ubuntu desktop下有一个本地的dns服务叫做dnsmasq,它是由NetworkManager控制的, 复制虚拟机时,网卡重置了但是没有NetworkManager处理原有网卡)

VirtualBox Ubuntu Server 16.04 手动设置 网络(IP, DNS, 路由) Linux 第4张

VirtualBox Ubuntu Server 16.04 手动设置 网络(IP, DNS, 路由) Linux 第5张

3. 启动虚拟机设置网络

查看网络配置

cat /etc/network/interfaces

VirtualBox Ubuntu Server 16.04 手动设置 网络(IP, DNS, 路由) Linux 第6张

可以看到只有 lo这个网络是自动加载的, 此配置文件会 包含 /etc/network/interfaces.d 下所有文件

查看主机上所有网络信息

ip a

VirtualBox Ubuntu Server 16.04 手动设置 网络(IP, DNS, 路由) Linux 第7张

可以看到 lo已经配置,enp0s3和enp0s8 未配置

在/etc/network/interfaces.d文件夹下创建两个网卡配置文件

cd /etc/network/interfaces.d
sudo touch enp0s3
sudo touch enp0s8

配置enp0s3

sudo vi enp0s3

内容如下:

auto enp0s3
iface enp0s3 inet static
address 192.168.0.2

配置enp0s8

sudo vi enp0s8

内容如下:

auto enp0s8
iface enp0s8 inet static
address 10.10.10.2
netmask 255.255.255.0
geteway 10.10.10.1

重启网络

sudo /etc/init.d/networking restart

查看网络配置

ip a

配置路由

查看当前路由

route

VirtualBox Ubuntu Server 16.04 手动设置 网络(IP, DNS, 路由) Linux 第8张

目前只能访问10.10.10.* 和192.168.0.* 的网络, 想要访问其他网络(外网)需要新增一条路由

sudo vi /etc/network/interfaces.d/enp0s8
up route add default gw 10.10.10.1
down route del default

重启网络,如果重启网络失败需要重启计算机

sudo /etc/init.d/networking restart

添加一条默认路由,再次查看路由表

VirtualBox Ubuntu Server 16.04 手动设置 网络(IP, DNS, 路由) Linux 第9张

DNS 配置

sudo vi /etc/network/interfaces.d/enp0s3

添加以下信息(多个dns用空格分开)

dns-nameservers 8.8.8.8

参与评论