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):
- The Adapter 2-4 (Not Enabled):
- After typing
ip a
in the terminal of the VM (assume it is a Linux machine), the result looks like below:
1 | 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000 |
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”):
After typing ip a
in the terminal of the VM (assume it is a Linux machine) NOW, the result looks like below:
1 | 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000 |
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”.
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.
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.