Managing and provisioning VMs
Port Forwarding
An important setting for my particular use of VMs is port forwarding, which allows me to access services running inside the VM from the host. Vagrant supports this type of networking as well. I can tweak the Vagrantfile to set up a port on the host machine to forward to a port in the VM. In effect, this allows me to access the services running inside the VM.
For instance, if I forward the standard port for HTTP content (port 80) in the VM to port 8081 on the host, then I can access the web server running in the VM by pointing the web browser on the host machine to http://localhost:8081. In the background, the traffic sent to port 8081 is actually forwarded to port 80 on the VM.
Fire up the Vagrantfile in a text editor and enter the following parameter:
config.vm.network "forwarded_port", guest: 80, host: 8081
With this parameter, I am instructing Vagrant to forward port 80 from inside the VM to port 8081 on the host. Save the file and restart the VM:
$ vagrant reload
To check the settings, you can use the SimpleHTTPServer
module in Python to read the contents of the /vagrant
directory. Once the VM is up and running, SSH into it and bring the web server online with:
$ vagrant ssh [vagrant@localhost ~]$ cd /vagrant [vagrant@localhost ~]$ sudo python -m SimpleHTTPServer 80
Now open a browser on the host and point it to http://localhost:8081, and you'll get a /vagrant
directory listing served from inside the VM. You can now tweak the example to install any service inside the VM and modify the Vagrantfile to shuttle traffic between the host and the VM.
Provisioning VMs
At the moment, the CentOS VM is pretty basic. To harness Vagrant's power, you'll have to flesh out the installation. You can do this in a couple of ways. You can use the CentOS package manager to add packages manually. However, the better option is to automatically install the software as part of the VM's creation process, which is known as provisioning. Vagrant supports provisioning with shell scripts, Chef, or Puppet, and you can add more provisioners via plugins.
I personally prefer provisioning via shell scripts as opposed to Chef or Puppet, which are overkill for my simple requirements. Working with shell scripts is fairly straightforward. All you need to do is simply gather all the required operations that need to be handled automatically inside a script (see Listing 1).
Listing 1
Provisioning Shell Script
01 $ sudo nano provision.sh 02 03 # !/usr/bin/env bash 04 05 echo "Now installing the Apache web server quietly in the background" 06 yum update httpd > /dev/null 2>&1 07 yum install -y httpd > /dev/null 2>&1 08 systemctl start httpd
Line 3 of Listing 1 specifies which shell to use to execute the rest of the file (bash
in this case). The script will then display that it's about to install the Apache web server without any further prompts or outputs, so as to not fill the screen with unnecessary output.
Vagrant will run the script as root, which is why there is no need to actually use sudo
on lines 6, 7, and 8. The -y
flag tells yum
to automatically respond "yes" to any prompts. This is important since I am provisioning the VMs automatically. Because there is no human interaction, if yum
were to ask for confirmation, the script would simply never finish. As previously mentioned, yum
's output is sent to /dev/null
instead of the terminal.
Once the shell script has been created, the next step is to configure Vagrant to use it to provision the VMs during boot. For this, you can point to the script from the Vagrantfile with the following parameter:
config.vm.provision "shell", path: "provision.sh"
This tells Vagrant to provision the machine with the shell
provisioner and to use the shell script named provision.sh
that's available in the project directory (~/demo
on the host).
Now bring up or reload the VM. Thanks to this parameter, Vagrant will execute the provision.sh
script after the VM is up and running (Figure 4).
GUI Guest
While Vagrant is primarily used to create and provision headless VMs, you can also create a VM with a full blown graphical desktop. Open the Vagrantfile, scroll down, and uncomment the following lines or add them manually:
config.vm.provider "virtualbox" do |vb| vb.gui = true vb.memory = "1024" end
These lines tell Vagrant to enable the VirtualBox GUI mode and allocate 1024MB of RAM to it. Once you've saved the changes, you can restart the VM. This time, it'll pop out a VirtualBox window and drop you at the shell login prompt. Even though Vagrant uses key-based authentication by default, it is a general convention to set the password for the "vagrant" user to vagrant. You can use these login credentials to log into the VM manually.
Since the VM I specified earlier is a CentOS server distribution, you'll have to pull in packages for a full-blown CentOS graphical desktop, which is fairly well-documented [5], after logging into this VM.
« Previous 1 2 3 Next »
Buy this article as PDF
(incl. VAT)
Buy Linux Magazine
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters
Support Our Work
Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.
News
-
Plasma 6.3 Ready for Public Beta Testing
Plasma 6.3 will ship with KDE Gear 24.12.1 and KDE Frameworks 6.10, along with some new and exciting features.
-
Budgie 10.10 Scheduled for Q1 2025 with a Surprising Desktop Update
If Budgie is your desktop environment of choice, 2025 is going to be a great year for you.
-
Firefox 134 Offers Improvements for Linux Version
Fans of Linux and Firefox rejoice, as there's a new version available that includes some handy updates.
-
Serpent OS Arrives with a New Alpha Release
After months of silence, Ikey Doherty has released a new alpha for his Serpent OS.
-
HashiCorp Cofounder Unveils Ghostty, a Linux Terminal App
Ghostty is a new Linux terminal app that's fast, feature-rich, and offers a platform-native GUI while remaining cross-platform.
-
Fedora Asahi Remix 41 Available for Apple Silicon
If you have an Apple Silicon Mac and you're hoping to install Fedora, you're in luck because the latest release supports the M1 and M2 chips.
-
Systemd Fixes Bug While Facing New Challenger in GNU Shepherd
The systemd developers have fixed a really nasty bug amid the release of the new GNU Shepherd init system.
-
AlmaLinux 10.0 Beta Released
The AlmaLinux OS Foundation has announced the availability of AlmaLinux 10.0 Beta ("Purple Lion") for all supported devices with significant changes.
-
Gnome 47.2 Now Available
Gnome 47.2 is now available for general use but don't expect much in the way of newness, as this is all about improvements and bug fixes.
-
Latest Cinnamon Desktop Releases with a Bold New Look
Just in time for the holidays, the developer of the Cinnamon desktop has shipped a new release to help spice up your eggnog with new features and a new look.