Thursday, May 9, 2024
 Popular · Latest · Hot · Upcoming
117
rated 0 times [  117] [ 0]  / answers: 1 / hits: 270601  / 1 Year ago, wed, january 11, 2023, 10:10:43

In setting up virtual machines with VirtualBox, I often want the following characteristics



  • VM has a static IP

  • host can access VM without port forwarding

  • VM can access the internet

  • I can move my laptop from network to network (e.g. from home to office to coffee shop) without worrying about securing or reconfiguring the VM


None of the VirtualBox network connection methods satisfies these requirements on their own.



  • NAT

    Requires port forwarding if you want to connect to the VM from the host.



  • Host-only

    The VM can not access the internet unless the host is a router.



  • Bridged

    Exposes the VM to the network; not portable.




More From » networking

 Answers
7

I can get the setup I want by setting up two adapters on the vm.



VirtualBox 4.2.12

Ubuntu 12.04 guest



In VirtualBox > Preferences > Network, set up a host-only network.



Mine is called vboxnet0, it is manually configured:

ip 192.168.56.1

netmask 255.255.255.0

no dhcp



VirtualBox network configuration
VirtualBox network configuration



Then, in the network settings for the virtual machine, set up two adapters:



Adapter 1

host only, vboxnet0



Adapter2

NAT



Boot the virtual machine and log in through the console VirtualBox provides.



Run this to see your adapters:



ls /sys/class/net


In my case the adapters were named eth1 and eth2 (and lo, the loopback interface).



Then, edit your network configuration.



sudoedit /etc/network/interfaces


# The loopback network interface
auto lo
iface lo inet loopback

# Host-only interface
auto eth1
iface eth1 inet static
address 192.168.56.20
netmask 255.255.255.0
network 192.168.56.0
broadcast 192.168.56.255

# NAT interface
auto eth2
iface eth2 inet dhcp


Note that eth1 has no default gateway specified. eth2 will get a default gateway from dhcp.






Update March 2018



See this answer from @Hugo14453 for an updated version that works with Ubuntu 17.10 and newer.


[#31261] Thursday, January 12, 2023, 1 Year  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
llianconclad

Total Points: 65
Total Questions: 109
Total Answers: 127

Location: Mali
Member since Fri, Dec 3, 2021
2 Years ago
llianconclad questions
Sun, May 1, 22, 08:47, 2 Years ago
Tue, Nov 8, 22, 02:33, 2 Years ago
Mon, Aug 23, 21, 15:58, 3 Years ago
Sun, Jul 25, 21, 18:19, 3 Years ago
;