本文主要是在linux中安装VirtualBox后,使用VBoxManage配置虚拟机网络。

本文命令支行环境如下:

宿主机OS:fedora33

VirtualBox版本:6.1.22

虚拟机OS:CentOS7

虚拟机name:centos-7-20210521

好的,开始上命令:

对了,开始配置前先把虚拟机关闭:

VBoxManage controlvm centos-7-20210521 poweroff

新建宿主机hostonly网卡
 

[root@localhost ~]# VBoxManage hostonlyif create

在此未指定hostonly网卡的名,系统默认为“vboxnet0”

查看系统新建的hostonly网卡信息,命令:

[root@localhost ~]# VBoxManage list hostonlyifs
Name:            vboxnet0
GUID:            786f6276-656e-4074-8000-0a0027000000
DHCP:            Disabled
IPAddress:       192.168.56.254
NetworkMask:     255.255.255.0
IPV6Address:     fe80::800:27ff:fe00:0
IPV6NetworkMaskPrefixLength: 64
HardwareAddress: 0a:00:27:00:00:00
MediumType:      Ethernet
Wireless:        No
Status:          Up
VBoxNetworkName: HostInterfaceNetworking-vboxnet0

此时ifconfig还看不到,需要做如下操作后方可在宿主机中看到。

指定宿主机IP

[root@localhost ~]# VBoxManage hostonlyif ipconfig vboxnet0 --ip 192.168.56.254 --netmask 255.255.255.0

此时在宿主机中使用ifconfig可以看到“vboxnet0”的信息。

[root@localhost ~]# ifconfig
eno1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.0.104  netmask 255.255.255.0  broadcast 192.168.0.255
        inet6 fe80::bacb:29ff:feaf:90e1  prefixlen 64  scopeid 0x20<link>
        ether b8:cb:29:af:90:e1  txqueuelen 1000  (Ethernet)
        RX packets 10342  bytes 1663982 (1.5 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 1996  bytes 209859 (204.9 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
        device interrupt 17  


lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 25  bytes 2184 (2.1 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 25  bytes 2184 (2.1 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

vboxnet0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.56.254  netmask 255.255.255.0  broadcast 192.168.56.255
        inet6 fe80::800:27ff:fe00:0  prefixlen 64  scopeid 0x20<link>
        ether 0a:00:27:00:00:00  txqueuelen 1000  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 44  bytes 4952 (4.8 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

设置hostonly网卡的DHCP:

[root@localhost ~]# VBoxManage dhcpserver add --ifname vboxnet0 --ip 192.168.56.254 --netmask 255.255.255.0 --lowerip 192.168.56.101 --upperip 192.168.56.105  --enable

上面命令中的lowerip和upperip分别是DHCP动态分配IP的最小IP和最大IP,请一定要设置的范围小一下,这样有助于你使用ping命令来查看虚拟机分配的具体 IP;


查看虚拟DHCP信息,命令如下:

[root@localhost ~]# VBoxManage list dhcpservers
NetworkName:    HostInterfaceNetworking-vboxnet0
Dhcpd IP:       192.168.56.254
LowerIPAddress: 192.168.56.101
UpperIPAddress: 192.168.56.105
NetworkMask:    255.255.255.0
Enabled:        Yes
Global Configuration:
    minLeaseTime:     default
    defaultLeaseTime: default
    maxLeaseTime:     default
    Forced options:   None
    Suppressed opts.: None
        1/legacy: 255.255.255.0
Groups:               None
Individual Configs:   None

设置虚拟机中的网卡关联使用hostonly,我这里将第三张网卡设置成hostonly网络;
命令如下:

[root@localhost ~]# VBoxManage modifyvm centos-7-20210521 --nic3 hostonly --nictype3 82540EM --cableconnected3 on --hostonlyadapter3 vboxnet0

启动虚拟机:

[root@localhost ~]# VBoxManage startvm centos-7-20210521 --type headless

使用ping命令在指定的DHCP动态分配的IP中一个个试,当ping 192.168.56.102时,通了,于是确定虚拟机的IP是192.168.56.102。


进行SSH登录:

[root@localhost ~]# ssh root@192.168.56.102

按提示录入 yes及密码后,顺利登录到虚拟机啦,

至此可以在虚拟机上安装软件及你需要的操作啦。

-------------------------------------------

1、windows下使用VirtualBox的网络配置请参考《VirtualBox网络之仅主机(Host-Only)网络

2、linux下配置可参照《Virtual Box下配置Host-Only联网方式详解

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐