I’m currently indulging in the pastime that’s sweeping the country – trying not to think about Brexit.
It’s a craze that’s even spread as far our political elite. In their case, it manifests itself in slightly different ways.
On the one hand, there are those who are refusing to accept any solution offered to maintain a “soft” border on the island of Ireland. As far as I can tell, they haven’t managed to offer any practical solution that they would accept as that would involve thinking about Brexit.
On the other hand there are those who are pushing for a new referendum because, apparently, some politicians lied when campaigning. Maybe someone was “Putin” ’em up to it ?
For my part, as I don’t quite have the space for a bunker at the bottom of my garden, I’ve decided to hide out in to a world of make-believe…well Virtual Machines at any rate.
I want to setup a CentOS Virtual Machine (VM) that I can then use as to clone environments to host various software stacks that I may want to play with.
I’d like to be able to connect to these VMs directly from my host OS, just like a real-world server. However, I’d also like to be able to connect the VM to the outside world occasionally so I can run package updates via yum.
The specific steps I’m going to go through are :
- Install CentOS7 into a Virtualbox VM
- Setup Host Only Network in VirtualBox
- Create a Network Interface on the Guest to use the Host Only Network
- Assign a static IP address to the Guest
The software I’m using for this is :
- Ubuntu 16.04 LTS (the Host)
- VirtualBox 5.1.38 – available to download here
- CentOS7 (the Guest) – which you can download from here
Before we get cracking, it’s probably a good idea to have a quick look at…
VirtualBox supports four networking modes of which Network Address Translation (NAT) is the default.
Whilst this is perfectly fine for allowing internet access directly from the Guest, it does have some limitations, including the inability to connect via ssh from the Host machine.
To overcome this, we can setup a Host Only Network.
The catch here is that, using this networking mode, the Guest will not be able to see the internet so updates via yum will be problematic.
Fortunately, VirtualBox allows you to configure multiple Network Cards (NICs) on a VM so it’s possible to toggle between the two modes depending on what’s required.
This is the approach I’m going to take here.
Incidentally, the VirtualBox documentation includes a chapter on Virtual Networking which you may find useful.
Installing CentOS7 into a VirtualBox VM
Essentially, I’ve followed these steps to do the basic installation. However there are some changes around configuring the Guest Additions, which I’ll come onto shortly.
When you first fire-up CentOS in the VM, the installer kicks in.
At this point, I’ve selected Server with GUI as the Base Environment along with the Development Tools add-on :
The next step is to rename the server to something a bit more memorable. Now, I’ve known data centres where server names followed a theme – they could all be artists or even race horses.
In this case I’ve decided to call this server “thor” – as it’s the only one you’re going to see in this article, you’ll be left to ponder whether I’m following a high-brow intellectual theme ( Norse Gods), or whether I’m just using characters from the Marvel Comic Universe.
We also need to make sure that networking is enabled by ensuring the default NIC ( enp0s3 in this case) is on :
Note that, at this point, the domain name “virtualbox” is arbitrary.
After the installation is complete, you’ll need to restart the machine and accept the license.
Before we go any further we should now see that networking is enabled and that we have access to the internet :
Getting sudo access
In order to keep things simple, I’m going to give my user sudo privileges. In CentOS, these are derived from the wheel group. So, in a Terminal:
su root usermod -aG wheel mike
NOTE – you’ll need to logout and log back in (or simply restart the VM) for this change to take effect.
Once the server has restarted, you can check that things have worked as expected :
groups mike mike : mike wheel
Update packages with yum
However recently (or otherwise) you downloaded your CentOS iso, it’s always a good idea to update the packages before we get too much further :
sudo yum update
Installing Guest Additions
There’s a whole chapter on Guest Additions in the documentation.
For this VM, I’m installing Guest Additions 5.1.38. This is done in the usual way – i.e.
With the Guest running, go to the VirtualBox Devices Menu and select Insert Guest Additions CD Image.
When prompted, hit the Run button.
For my part, I’m installing Guest Additions to take advantage of the bi-directional clibpoard and ability to cut and paste between Guest and Host.
If you’re tempted to go further and try to maximise the viewport for the GUI, a word of warning, enabling 3D Accelaration for the VM’s display caused the VM to fail to start.
This may simply be an issue with the versions of CentOS/Virtualbox/Guest Additions that I’m using, but I thought I should mention it, just in case.
Now we’ve completed the initial setup of the CentOS VM, the next step is to…
Create a Host Only Network
In VirtualBox itself, go to the File menu and select Preferences.
Then choose the Network icon and go to the Host-only Networks tab.
Click on the Add icon on the right-hand side and create a new network :
Click on the screwdriver icon and you should be able to see Details of the new network, including it’s starting IP address :
Note that, in this example, I’ve set the Lower and Upper Address bounds manually.
Of course, using DHCP means that an IP address will be allocated to the VM each time it starts. We’ll come onto how to configure a fixed IP address in a bit. For now though, we just want to make sure everything is working.
Add a Host Only NIC to the VM
For our VM to use our new network, we need to add a Host Only Network Interface to it.
To do this, we need to open the Network settings for that VM in Virtualbox and click on the Adapter 2 tab.
Make sure that Enable Network Adapter is checked
Then set Attached to to Host Only Adapter and the Name to that of the network we’ve just created :
If we now start the VM again, we can see that we have a second NIC, which is now connected :
We can, however, connect to the running VM via ssh.
First of all, we need to determine the IP address that’s been allocated to the VM by the DHCP server. We can do this from the host using :
vboxmanage guestproperty enumerate CentOS7_HO1 |grep VirtualBox/GuestInfo/Net/0/V4/IP Name: /VirtualBox/GuestInfo/Net/0/V4/IP, value: 192.168.57.100, timestamp: 1541956114954604000, flags:
Now, using this IP address, we can connect from the host …
…and set it to Connect Automatically …
We then need to uncheck the Connect Automatically checkbox for the original NIC ( enp0s3 in my case), so that it does not connect unless we want it to.
Now, when the VM starts up, it will be using the Host Only Network by default.
Right, we’ve got the Host Only Network up and running but finding out the IP address for the VM every time we start it up is going to be a bit of messing about.
Fortunately we can dispense with that by simply…
Assigning a Static IP Address
To be on the safe side, I had the VM powered down when I started these steps.
First, we need to go back to the Host Only Network Settings in Virtualbox for our network, go to the DHCP tab and uncheck the enable server option :
Next we need to fire up the VM and create a config file for the NIC we’re using for the Host Only Network ( en0s8) :
sudo nano /etc/sysconfig/network-scripts/ifcfg-en0s8
The file should look like this. Well, actually it’s probably more accurate to say that my file looks like this :
TYPE=ETHERNET BOOTPROTO=none DEFROUTE=yes IPV4_FAILURE_FATAL=yes IPV6INIT=yes IPV6_AUTOCONF=yes IPV6_DEFROUTE=yes IPV6_FAILURE_FATAL=no IPV6_ADDR_GEN_MODE=stable-privacy DEVICE=enp0s8 ONBOOT=yes IPADDR=192.168.57.123 PREFIX=24 GATEWAY=192.168.57.254 IPV6_PEERDNS=yes IPV6_PEERROUTES=yes IPV6_PRIVACY=no
- DEVICE is the name of the Host-only NIC we created
- IPADDR is the static IP address you want to assign to the VM
- GATEWAY is the upper IP address in the range where IPADDR is located (as far as I can tell)
Restart networking on the VM …
sudo systemctl restart network
…and confirm that the static IP address is now being used…
ip address show dev enp0s8
…in my case returns…
3: enp0s8: mtu 1500 qdisc pfifo_fast state UP group default qlen 1000 link/ether 08:00:27:5b:02:a1 brd ff:ff:ff:ff:ff:ff inet 192.168.57.123/24 brd 192.168.57.255 scope global noprefixroute enp0s8 valid_lft forever preferred_lft forever inet6 fe80::ad98:f0f4:9406:5348/64 scope link noprefixroute valid_lft forever preferred_lft forever
At this point I’ve chosen to re-start the VM completely to allow it to pick up the new networking changes.
In fact, as I’m feeling brave, I’ve started the VM headless.
I don’t want to have to remember the IP address every time I want to connect to the server so I’ve simply added an entry for it in the hosts file on the Host :
sudo nano /etc/hosts
At the end of the file I’ve added the lines…
#VirtualBox Host Only Network hosts 192.168.57.123 thor.virtualbox
I can now use the alias I’ve set to hit the VM :
Using the Host-Only network should be fine for me for the most part. However, if I do want to update the packages on the CentOS VM, all I need to do is to switch to the NAT network card and I’ll be able to run an update with no issues.
Well, that’s taken my mind off things for a bit, now I just need to find something else to distract myself until the end of March when Brexit will (might) finally be over.