0%

Network Configuration for VirtualBox

In some cases, when we import VM into the “Oracle VM VirtualBox Manager”, the network is not well configured, and we cannot connect it by SSH/other things on the host machine. The default network setting may looks like this:

  • The Adapter 1 (an NAT adapter):
Network-setting1
  • The Adapter 2-4 (Not Enabled):
Network-setting2
  • After typing ip a in the terminal of the VM (assume it is a Linux machine), the result looks like below:
1
2
3
4
5
6
7
8
9
10
11
12
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:b3:8d:9b brd ff:ff:ff:ff:ff:ff
inet 10.0.2.15/24 brd 10.0.2.255 scope global dynamic noprefixroute enp0s3
valid_lft 86376sec preferred_lft 86376sec
inet6 fe80::6dea:8795:c7b1:a1a5/64 scope link noprefixroute
valid_lft forever preferred_lft forever

There are no available network adapter to the host.

Method 1: By Host-only Adapter

Important!

Pros: It has a stable IP address, with all ports available. (e.g., setup different services on different ports, 22, 3389, ...)

Cons: The VM cannot be accessed from machine outside of the host (e.g., we cannot ssh host_name@host_ip to connect to the VM from other machines than host), unless you perform some port forwarding strategies on the host.

Steps:

To create a network connection from VM to host, we can set the Adapter 2 like below (Use a “Host-only Adapter”):

Network-setting3

After typing ip a in the terminal of the VM (assume it is a Linux machine) NOW, the result looks like below:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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:b3:8d:9b brd ff:ff:ff:ff:ff:ff
inet 10.0.2.15/24 brd 10.0.2.255 scope global dynamic noprefixroute enp0s3
valid_lft 86337sec preferred_lft 86337sec
inet6 fe80::6dea:8795:c7b1:a1a5/64 scope link noprefixroute
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:85:54:b0 brd ff:ff:ff:ff:ff:ff
inet 192.168.56.101/24 brd 192.168.56.255 scope global dynamic noprefixroute enp0s8
valid_lft 537sec preferred_lft 537sec
inet6 fe80::a598:692:2336:67d9/64 scope link noprefixroute
valid_lft forever preferred_lft forever

The third network adapter, with an ip address 192.168.56.101, is the ip address of the “VirtualBox Host-Only Ethernet Adapter”. On the host machine, we can connect it by SSH fluently:

1
ssh ubuntu@192.168.56.101

By so far, you can connect the machine via SSH well. You can also connect to other services by ip address 192.168.56.101 on the host machine.

Method 2: Forward Port to Host

Important!

Pros: You can access the VM outside of the host machine, from a different machine.

Cons: Only forwarded ports available. Ports may conflict with others.

Steps:

Go to the Adapter 1, click the “Advanced” button to expand the menu. Click the “Port Forwarding”.

Network-setting4

In the “Port Forwarding Rules”, Set up new rules. The “Host Port” is the port number on the host machine. The “Guest Port” is the corresponding port number on the VM. In this example, I forwarded port 4016 on host to the port 22 on the VM.

Network-setting5

After that, you can connect to the VM on the host machine. Type:

1
ssh -p 4016 ubuntu@127.0.0.1

You can also connect to the VM outside of the host machine. Assume in the local network, my ip address is 10.31.38.94. So from the computer in the same local network, type:

1
ssh -p 4016 ubuntu@10.31.38.94

And everything should go fine. If something goes run, please check whether we have firewall permissions. If not, you can set a firewall rule for port 4016 (The port number you forwarded).

Conclusion

You can setup both Method 1 & Method 2!! Then you can experience all of these benefits (connect from other machines, all ports available on local machines, etc.) and avoid the disadvantages.