Friday, July 21, 2017

Oracle Linux - Change hostname for Vagrant host

Vagrant is an open-source software product build by HashiCorp for building and maintaining portable virtual development environments. The core idea behind its creation lies in the fact that the environment maintenance becomes increasingly difficult in a large project with multiple technical stacks. Vagrant manages all the necessary configurations for the developers in order to avoid the unnecessary maintenance and setup time, and increases development productivity. Vagrant is written in the Ruby language, but its ecosystem supports development in almost all major languages.

I use Vagrant a lot, really a lot, and especially in combination with Oracle Linux. Oracle ships a number of default vagrant boxes from within oracle.com which speeds up the development, test and experimental way of working a lot. Without having the need to manually maintain local clones of Oracle virtualbox images you can now use vagrant to extremely fast run Oracle Linux instances.  A short guide on how to get started with vagrant can be found in this specific blogpost on my blog.

When you do a default start of a Vagrant box, in our example an Oracle Linux 6.9 instance we will see that the hostname is not explicitly stated. In most cases this is not an issue, however, in some cases the hostname is a vital part of how your software might work. The most common way is changing the hostname by changing it directly within the Oracle Linux operating system. However, a better way of doing things when working with Vagrant is to do it by editing the Vagrantfile config file which can be found in the directory where you did a "vagrant init".

Change hostname in Vagrantfile
When using vagrant you should use the power of vagrant. This means, if you want your machine to have a specific hostname you can do so by changing the Vagrantfile instead of doing it on the Oracle Linux operating system within the box when it is running. If you read the Vagrant documentation you will find the following on this subject :

"config.vm.hostname - The hostname the machine should have. Defaults to nil. If nil, Vagrant will not manage the hostname. If set to a string, the hostname will be set on boot. "

if we take for example a running box which we initiated without having done anything for the hostname in the Vagrantfile you will notice the hostname is localhost.

[vagrant@localhost ~]$ 
[vagrant@localhost ~]$ uname -a
Linux localhost 4.1.12-61.1.33.el6uek.x86_64 #2 SMP Thu Mar 30 18:39:45 PDT 2017 x86_64 x86_64 x86_64 GNU/Linux
[vagrant@localhost ~]$ 
[vagrant@localhost ~]$ cat /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6

[vagrant@localhost ~]$ 

If we want to have a box named somehost.example.com we could ensure we have the below line in our Vagrantfile config file when we start it:

config.vm.hostname = "somehost.example.com"

When you would login to the Oracle Linux operating system within the box and you would check the same as in the above example you would be able to see the difference;

[vagrant@somehost ~]$ 
[vagrant@somehost ~]$ uname -a
Linux somehost.example.com 4.1.12-61.1.33.el6uek.x86_64 #2 SMP Thu Mar 30 18:39:45 PDT 2017 x86_64 x86_64 x86_64 GNU/Linux
[vagrant@somehost ~]$ 
[vagrant@somehost ~]$ cat /etc/hosts
127.0.0.1 somehost.example.com somehost
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
[vagrant@somehost ~]$ 

As you can see, changing the Vagrantfile will change the hostname within the box. Instead of changing it manually you should use the power of Vagrant to state the correct hostname in your Oracle Linux instance when using Vagrant.

No comments: