Setting IP address and hostname of a Vagrant box

Table of Contents

Audience

This article assumes familiarity with the use of Vagrant and Linux/BSD based operating systems.

Objective

When provisioning Vagrant boxes, it is often useful to refer to them by hostnames instead of their IP addresses. For example, if there is a Vagrant box running a webserver, it is much easier to access it as http://mywebserver.example.com instead of http://192.168.121.55. This article explores how to do so.

Setup

This article uses a FreeBSD 13 Vagrant box running on Arch Linux with the libvirt provider; however, the same should be applicable for any combination of POSIX-compliant guest and host operating systems.

Procedure

The following steps will all be done through the command line shell of a user account with sudo privileges on the Arch Linux host.

Create a new FreeBSD 13 VM

mkdir -p testsrv
cd testsrv
vagrant init generic/freebsd13

A new `Vagrantfile` will be created and the following message will be displayed:

A `Vagrantfile` has been placed in this directory. You are now
ready to `vagrant up` your first virtual environment! Please read
the comments in the Vagrantfile as well as documentation on
`vagrantup.com` for more information on using Vagrant.

Assign static IP address and hostname

In the Vagrantfile, the following line is commented out:

# config.vm.network "private_network", ip: "192.168.33.10"

We replace this line with the following two lines, which set the IP address to `192.168.121.101` and the hostname to `testsrv.example.com`.

config.vm.network "private_network", ip: "192.168.121.101"
config.vm.hostname = "testsrv.example.com"

Update hosts file

In the host machine on which the Vagrant box is running, we need to add an entry to the `/etc/hosts` file so that we can refer to the Vagrant box by its hostname instead of its IP address.

echo '192.168.121.101 testsrv testsrv.example.com' | sudo tee -a /etc/hosts

The following output should be displayed:

192.168.121.101 testsrv testsrv.example.com

Spin up the VM

vagrant up

A successful boot will result in output similar to the following:

Bringing machine 'default' up with 'libvirt' provider...
==> default: Checking if box 'generic/freebsd13' version '4.1.6' is up to date...
==> default: Creating image (snapshot of base box volume).
==> default: Creating domain with the following settings...
==> default:  -- Name:              testsrv_default
==> default:  -- Description:       Source: /home/abhishek/var/tmp/testsrv/Vagrantfile
==> default:  -- Domain type:       kvm
==> default:  -- Cpus:              2
==> default:  -- Feature:           acpi
==> default:  -- Feature:           apic
==> default:  -- Feature:           pae
==> default:  -- Clock offset:      utc
==> default:  -- Memory:            2048M
==> default:  -- Management MAC:    
==> default:  -- Loader:            
==> default:  -- Nvram:             
==> default:  -- Base box:          generic/freebsd13
==> default:  -- Storage pool:      default
==> default:  -- Image(vda):        /var/lib/libvirt/images/testsrv_default.img, scsi, 128G
==> default:  -- Disk driver opts:  cache='default'
==> default:  -- Kernel:            
==> default:  -- Initrd:            
==> default:  -- Graphics Type:     vnc
==> default:  -- Graphics Port:     -1
==> default:  -- Graphics IP:       127.0.0.1
==> default:  -- Graphics Password: Not defined
==> default:  -- Video Type:        cirrus
==> default:  -- Video VRAM:        256
==> default:  -- Video 3D accel:    false
==> default:  -- Sound Type:	
==> default:  -- Keymap:            en-us
==> default:  -- TPM Backend:       passthrough
==> default:  -- TPM Path:          
==> default:  -- INPUT:             type=mouse, bus=ps2
==> default: Creating shared folders metadata...
==> default: Starting domain.
==> default: Waiting for domain to get an IP address...
==> default: Waiting for machine to boot. This may take a few minutes...
    default: SSH address: 192.168.121.35:22
    default: SSH username: vagrant
    default: SSH auth method: private key
    default: Warning: Connection refused. Retrying...
    default: 
    default: Vagrant insecure key detected. Vagrant will automatically replace
    default: this with a newly generated keypair for better security.
    default: 
    default: Inserting generated public key within guest...
    default: Removing insecure key from the guest if it's present...
    default: Key inserted! Disconnecting and reconnecting using new SSH key...
==> default: Machine booted and ready!
==> default: Setting hostname...
==> default: Configuring and enabling network interfaces...

Results

We now should be able to ping `testsrv` now.

ping -c 4 testsrv
PING testsrv (192.168.121.101) 56(84) bytes of data.
64 bytes from testsrv (192.168.121.101): icmp_seq=1 ttl=64 time=0.221 ms
64 bytes from testsrv (192.168.121.101): icmp_seq=2 ttl=64 time=0.238 ms
64 bytes from testsrv (192.168.121.101): icmp_seq=3 ttl=64 time=0.230 ms
64 bytes from testsrv (192.168.121.101): icmp_seq=4 ttl=64 time=0.256 ms

--- testsrv ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3052ms
rtt min/avg/max/mdev = 0.221/0.236/0.256/0.012 ms

We get the same result if we ping `testsrv.example.com`

ping -c 4 testsrv.example.com
PING testsrv (192.168.121.101) 56(84) bytes of data.
64 bytes from testsrv (192.168.121.101): icmp_seq=1 ttl=64 time=0.141 ms
64 bytes from testsrv (192.168.121.101): icmp_seq=2 ttl=64 time=3.44 ms
64 bytes from testsrv (192.168.121.101): icmp_seq=3 ttl=64 time=0.319 ms
64 bytes from testsrv (192.168.121.101): icmp_seq=4 ttl=64 time=0.415 ms

--- testsrv ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3037ms
rtt min/avg/max/mdev = 0.141/1.078/3.440/1.366 ms

References

Quick Links

Author: Abhishek Chakravarti

Created: 2022-08-23 Tue 12:57

Validate