Skip to content

Instantly share code, notes, and snippets.

@darylyu
Created June 19, 2018 15:26
Show Gist options
  • Select an option

  • Save darylyu/75d5dd1d63c317dee236f1894de3e5d7 to your computer and use it in GitHub Desktop.

Select an option

Save darylyu/75d5dd1d63c317dee236f1894de3e5d7 to your computer and use it in GitHub Desktop.
# VirtualBox: Host<->Guest Networking
To simplify performing/testing automation on VirtualBox, we need to assign static IP Addresses to our VMs.
Create a Host Only network.
Virtual Box -> Preferences -> Network -> Host-only Networks
For this example, we will use `vboxnet0`
Use the following settings:
```
IPv4 Address: 192.168.59.1
IPv4 Network Mask: 255.255.255.0
IPv6 Address: <blank>
IPv6 Network Mask Length: 0
```
By default a new VM in VirtualBox will use a NAT adapter. This allows it to connect to the Internet, but prevents us from SSH-ing into the server so we need to attach a second network adapter to our VM.
```
VM -> Settings -> Network -> Adapter 2
```
We use a host-only adapter instead of a bridge adapter, because a bridge adapter adds the VM to your actual network. If it communicates with your DHCP server, there is a good chance that the IP address changes every time you restart.
Make sure:
- the checkbox for `Enable Network Adapter` is on.
- we are attached to the `Host-only Adapter` named `vboxnet0`
When we run our VM and check the interfaces it will only show 2: the loopback interface and the NAT interface.
```
$ ifconfig
enp0s3 Link encap:Ethernet HWaddr 08:00:27:6a:99:77
inet addr:10.0.2.15 Bcast:10.0.2.255 Mask:255.255.255.0
inet6 addr: fe80::a00:27ff:fe6a:9977/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:27 errors:0 dropped:0 overruns:0 frame:0
TX packets:45 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:4349 (4.3 KB) TX bytes:4903 (4.9 KB)
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:65536 Metric:1
RX packets:160 errors:0 dropped:0 overruns:0 frame:0
TX packets:160 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1
RX bytes:11840 (11.8 KB) TX bytes:11840 (11.8 KB)
```
We need to manually add our Host-only Adapter by editing `/etc/network/interfaces`.
When we open it, this will be the file content:
```
# This file describes the network interfaces available on your system
# and how to activate them. For more information, see interfaces(5).
source /etc/network/interfaces.d/*
# The loopback network interface
auto lo
iface lo inet loopback
# The primary network interface
auto enp0s3
iface enp0s3 inet dhcp
```
At the end of the file we need to add a new interface.
```
# Host-only network
auto enp0s8
iface enp0s8 inet static
dns-nameservers 8.8.8.8 8.8.4.4
address 192.168.59.2
network 192.168.59.1
netmask 255.255.255.0
broadcast 192.168.59.255
```
After saving, we need to pick up the new configuration.
There are two ways to do this.
Activating the interface:
```
sudo ifup enp0s8
```
Or restarting the whole networking service:
```
sudo service networking restart
```
## OpenSSH setup
Since are trying to emulate the servers in our local machines, we need to be able to SSH into the VMs using our public keys.
Make sure openssh-server is installed.
```
sudo apt-get install openssh-server
```
Copy your public key to `~/.ssh/authorized_keys`
```
mkdir ~/.ssh`
vi ~/.ssh/authorized_keys
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment