VirtualBox配置NAT+HOST-ONLY实现物理机和虚拟机互相访问

in 大杂烩 with 0 comments, 848 views

预期目标

结合两种网络配置方式实现物理宿主机和虚拟机互通,虚拟机能使用固定ip还能正常上网,这样我们在macOs下也可以愉快的玩耍虚拟机了.

环境相关

网络配置简单对比

实施步骤

创建一个全局的NAT网络

VirtualBox -> 偏好设置... -> 网络 -> 添加并配置

QQ20200417-204202@2x.png

QQ20200417-205005@2x.png

QQ20200417-205032@2x.png

选一个安装的虚拟机进行NAT网络设置

QQ20200417-214123@2x.png

QQ20200417-214229@2x.png

任务完成一半

创建一个HOST-ONLY主机网络

VirtualBox -> 管理 -> 主机网络管理器... -> 添加并配置

QQ20200417-221443@2x.png

QQ20200417-221452@2x.png

这里需要再添加一张网卡来配置HOST-ONLY模式

QQ20200417-215713@2x.png

在虚拟机中配置静态IP

创建 /etc/netplan/netcfg.yaml 文件,添加如下内容

network:
  version: 2
  renderer: networkd
  ethernets:
    enp0s8: # this is your interface name for your NAT network
      dhcp4: no
      addresses: [192.168.56.102/24]
      gateway4: 192.168.56.1

执行命令应用配置

sudo netplan generate
sudo netplan apply

再次验证

cmgddd@cmgddd-server:~$ ping www.baidu.com
PING www.wshifen.com (103.235.46.39) 56(84) bytes of data.
64 bytes from 103.235.46.39 (103.235.46.39): icmp_seq=1 ttl=44 time=364 ms
64 bytes from 103.235.46.39 (103.235.46.39): icmp_seq=2 ttl=44 time=383 ms

cmgddd@cmgddd-server:~$ ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: enp0s3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether 08:00:27:9b:c5:ce brd ff:ff:ff:ff:ff:ff
    inet 10.0.2.100/24 brd 10.0.2.255 scope global enp0s3
       valid_lft forever preferred_lft forever
    inet6 fe80::a00:27ff:fe9b:c5ce/64 scope link 
       valid_lft forever preferred_lft forever
3: enp0s8: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether 08:00:27:e2:3c:2d brd ff:ff:ff:ff:ff:ff
    inet 192.168.56.102/24 brd 192.168.56.255 scope global dynamic enp0s8
       valid_lft 1025sec preferred_lft 1025sec
    inet6 fe80::a00:27ff:fee2:3c2d/64 scope link 
       valid_lft forever preferred_lft forever

结束语

至此我们已经完成了所有配置,物理机和虚拟机完成了互通,我们的虚拟机看起来更像一台服务器了。

参考文档

Setting Up Ubuntu Server 18.04 on Virtualbox 6

VirtualBox 绑定固定 IP

Responses ${replyToWho} / Cancel Reply