VirtualBox访问外网,并能从主机访问的设置

https://geert.vanderkelen.org/2011/virtualbox-give-host-only-access-to-internet/

这个配置貌似是可用的:



VirtualBox: give Host-Only access to internet

This post describes a simple trick for VirtualBox to give host-only virtual machines access the internet or access your host from the guest using NAT.

Problem

The original problem or requirements were as follows:

  • The host should be able to browser the webserver on the Guest (the virtual machine)
  • The guest should be able to access the outside world (intranet/internet)
  • Use fixed IP address

Yes, you can use port-forwarding using NAT network interface for the virtual machine. But configuring this is just way to complicate and error prone. A few googles more, and I couldn’t find anything good.

Solution

The solution I came up with: configure 2 network interfaces for your Virtual Box host. Simple, isn’t it?

First, we need to make a new network in VirtualBox. You can do this in the GUI preference, which is a bit platform specific, but it’s not easy to miss.

Create a new network in VirtualBox, named like vboxnet2 with following settings (change to suit your needs):

  • IPv4 Address: 10.88.12.0
  • IPv4 Network Mask: 255.0.0.0
  • Leave DHCP disabled if you don’t need it. Fixed IP addresses are just more predictable

Setup your virtual machine using 2 network adapters. The order doesn’t really matter, but you’ll need one NAT and one Host-Only:

  1. NAT (not much to configure).
  2. Host-Only using the network you created earlier, for example, vboxnet2.

Boot your virtual machine, and configure the network interfaces in your guest OS (same order as previous point):

  1. First interface using NAT, which you can use with the internal DHCP
  2. Second interface using Host-Only will use a fixed IP address and netmask which you configured earlier for the vboxnet2 VirtualBox network. For example, 10.88.12.4.

Guest has access to outside, and host can access services running on guest. This is handy when you have a demo or develop using a service running in the virtual machine, and the virtual machine needs to access the outside world.

(If the above has been posted somewhere else: I was either blind, or it was not visible or indexed enough.)


### 如何配置VirtualBox实现虚拟机内外网互通 #### 虚拟机网络模式的选择 为了使虚拟机能够与外部网络通信,通常采用NAT模式和Host-Only模式相结合的方式。NAT模式允许虚拟机通过宿主机访问互联网,而Host-Only模式则用于建立宿主机与虚拟机之间的专用网络连接[^2]。 #### 创建虚拟机并选择网络适配器类型 当创建新的CentOS虚拟机时,在网络设置中启用两块网卡:一块设为NAT模式,另一块设为主机仅限(Host-Only Adapter)[^3]。 #### 修改虚拟机中的网络配置文件 对于基于Red Hat的企业级Linux发行版如CentOS来说,需要编辑`/etc/sysconfig/network-scripts/ifcfg-enp0s3`(假设此名称代表第一个以太网接口)以及对应的第二个网口配置文件来设定静态IP地址和其他必要的参数[^1]。 ```bash # 对于Host-Only模式下的网卡(例如ifcfg-enp0s8) TYPE=Ethernet BOOTPROTO=static NAME=enp0s8 DEVICE=enp0s8 ONBOOT=yes IPADDR=192.168.56.101 # 这里填写一个未被占用的IP地址 NETMASK=255.255.255.0 # 子网掩码应匹配你的实际环境 GATEWAY=192.168.56.1 # 如果有默认路由,则在此处指定,默认情况下不需要填此项 DNS1=8.8.8.8 # 可选:添加Google DNS服务器或其他公共DNS服务提供商 ``` 保存更改后的配置文件,并重启网络服务或整个系统以便应用这些改动: ```bash sudo systemctl restart network ``` 此时应该可以在宿主机上使用SSH客户端尝试登录到新分配给虚拟机的那个固定的内部IP地址了。与此同时,由于存在NAT模式的支持,虚拟机也应当具备对外部网络发起请求的能力[^4]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值