Hadi Sinaee

Tech Entrepreneur, In Love With Data!

Setting Up Ubuntu Server 18.04 on Virtualbox 6

Posted at — May 23, 2019

TLDR; In this post, I am going to set up a cluster of ubuntu servers using Virtualbox 6 on a Mac OS. I will assign a static IP address to each VM using netplan and change its hostname permanently. Moreover, I will set up a NAT and Host-Only networks for having internet access and ssh accessibility from the host.

  1. Requirements
  2. Host-Only Networks
  3. NAT Networks
  4. Change Your Host Name
  5. Create Another Node
  6. Summary

Requirements

I suppose that you have installed the Virtualbox 6.0 and installed the Ubuntu Server 18.04 on a virtual machine. You can download the latest version of Virtualbox here. Also, you can find Ubuntu Server 18.04 at here.

If you want to install Virtualbox with brew, you can do it with:

$ brew update
$ brew cask install virtualbox

Install Open SSH Server in time of installing Ubuntu Server when the installer ask you for that. You should save your username and password for your ubuntu server somewhere since you need it during the rest of this post. vms-01

P.S: There is a tutorial if you need to know how to install Ubuntu Server 18.04 on a VM in the Virtualbox. Go with this tutorial and then come back.

Host-Only Networks

If you want to use ssh to connect to your VM, you can set up a Host-Only Network. This type of network in Virtualbox helps you to have both VM to Host(e.g., your laptop OS) and Host to VM connections.

Creating A Host-Only Network

Select your VM and then choose Host Network Manager from File menu. Create a network as you wish: host-only-01

Disable DHCP Server since we want to assign a static private IP address to this machine.

Set vboxnet0 To Your VM

Go to your VM settings by selecting your VM and then Settings. In Network tab set the following:

host-only-02

Setup Your Static IP

Now we need to config the VM to have a static IP address since each time the machine boots up its IP address doesn’t change. Also, we use this IP address later for connecting to the VM by ssh.

First of all, start your VM and use your user and password to login. After that, we need to assign a static IP address to this VM so that we use it as host address for our ssh connection. I am going to use Netplan for doing so. Netplan is a network configuration tool that uses an easy way to configure your networks.

Netplan reads network configuration from /etc/netplan/*.yaml which are written by administrators, installers, cloud image instantiations, or other OS deployments. During early boot, Netplan generates backend specific configuration files in /run to hand off control of devices to a particular networking daemon.

Go to the following path /etc/netplan/ and create a file named 01-host-only.yaml. You can name it anything, but for clarity, I named it in that way. Use your favorite editor and open a newly created file. Use the following configuration:

network:
  version: 2
  renderer: networkd
  ethernets:
    enp0s8: # this is your interface name for your NAT network
      dhcp4: no
      addresses: [192.168.56.100/24]
      gateway4: 192.168.1.1
      nameservers:
        addresses: [192.168.1.1, 8.8.8.8]

Save the file and apply your changes:

$ sudo netplan generate
$ sudo netplan apply

Now you can see your changes via ip a: ip-a-01

SSH Into Your VM

Now you can use the ssh command to connect to your running VM. In your host machine(your laptop/PC), open a terminal and:

$ ssh ubuntu@192.168.56.100

You should see something like this: ssh-01

NOTE: You can see the guest(your VM) IP address in the lower right part of your console either.

You don’t have any access to the internet. To have one, in the next step, we need to set up a new type of network. Since you cannot edit your networks while your machine is running, turn off your machine and go to the next step.

NAT Networks

Generally, we have two types of IP addresses, public and private. By private IP addresses, I mean those who are not registered, which means you do not have access to the internet network. On the other hand, Public IP addresses are registered ones, which say you can have access to the internet network.

Due to limitations of the number of public IP addresses in IPv4, engineers designed a network which can translate a bunch of private IP addresses to public ones. Network Address Translation or NAT is intended for such a purpose.

In our case, we have a group of nodes, our ubuntu server VMs, which need to have access to the internet. Therefore, we can benefit from NAT Networks feature in Virtualbox and create such a network.

Creating A NAT Network

Use ⌘ + , to open up virtual box preferences -> Network -> create a new NAT network: nat-network-01

Open your newly created NatNetwork and config your NAT network as you wish. You can use the following configuration if you do not know precisely what those options are: nat-network-02

Set NatNetwork To Your VM

Now you have created your NAT network; it’s time to enforce your VM to use this network. Go to your VM settings page by click on the VM and then select Settings. In the network tab, select NAT Network and then from the drop down select NatNetwork: nat-network-03

Set Your Static IP

Like what we did for the Host-Only network, we need to tell the netplan to set up a new static IP address for our NAT network. Turn on your machine and use your ssh to login to your VM. Then, go to /etc/netplan/ and create a file named 02-nat-networks.yaml which has the following content:

network:
  version: 2
  renderer: networkd
  ethernets:
    enp0s3: # this is your interface name for your NAT network
      dhcp4: no
      addresses: [10.5.3.100/24] # 10.5.3.100: it is your static ip address for your NAT
      gateway4: 10.5.3.1
      nameservers:
        addresses: [8.8.8.8]

Again, use the following commands to make your changes permanent:

$ sudo netplan generate
$ sudo netplan apply

use ip a to see your changes: ip-a-02

Now you can see that your machine has access to the internet:

$ ping google.com

Change Your Host Name

Since we are going to create a cluster of nodes, we need to have some meaningful names for our nodes. In order to do change the hostname permanently, we need to tell the OS that keep our changes intact after reboot. Therefore, edit the file /etc/cloud/cloud.cfg to have preserve_hostname: true. cloud-cfg-01

Run hostnamectl, you will see something like this: hostnamectl-01

In this machine, the hostname is node01 and it is correct. If you want to name it something different you can use the following command:

$ sudo hostnamectl set-hostname your-desired-hostname

NOTE: You will need this command to change your host name in the following of this post when you create another node.

Create Another Node

Trun off your machine and then select your node. Use ⌘ + O or use right menu click to start cloning. In the MAC Address Policy select to renew MAC addresses. clone-01

Now start your machine and login using the same credentials for node-01. Also, you can use the same ssh command for node-01 to login to node-02 since it has the same IP address. Therefore, we are going to change the IP addresses all networks and hostname using the same method for node-01.

NOTE: if you are connected to node-02 using ssh, you may disconnect from the machine as soon as you run sudo netplan apply since the IP address will be changed. You need to remove lines correspond to your hosts from $HOME/.ssh/known_hosts to be able to use ssh again

Eventually, the network configuration changes, and hosts are able to see each other: node01-01 node02-01

Summary

Now you have two virtual machines which are connected to the internet and are able to see each other. You can configure each host to use each other hostnames by editing /etc/hosts.

comments powered by Disqus