There are a variety of ways to stand up a lab environment to practice for Red Hat exams. One of the more effective ways that I’ve found is to stand up a reproducible lab environment with a tool like Vagrant.
5.1 Why Vagrant?
What Vagrant allows us to do is define one (or a few) virtual machines (VMs) with code and use the vagrant command-line utility to manage them in a local virtual environment. We can spin them up, reprovision, or destroy them as many times as we like. This means we don’t have to be afraid to break these instances; we can treat them as a throwaway sandbox. We also don’t have to worry about rolling back changes we may have made to the machine to get to a known good state. We can just destroy the instance and start over with a fresh box. This is a very important psychological safety net that allows us the freedom to learn without fear of making mistakes. Restoring our lab is minimal effort in the form of just a couple of vagrant commands (vagrant destroy and vagrant up).
5.1.1 Labs as Code
Vagrant has the added benefit of allowing me to ship chapter lab environments and practice exams in the form of Vagrantfile definitions and provisioning scripts (coming soon!).
5.1.2 Vagrant as an introduction to IaC
This also serves an introduction to patterns you will see in Infrastructure as Code (IaC) type tools like Ansible and HashiCorp’s Terraform. Servers are “cattle not pets” is something you might hear infrastructure engineers say. More and more we’re seeing the Linux System Administrator skillset morph into an “Engineering” skillset that can be scaled to a few or many hundreds or thousands of instances running in virtual environments. It makes sense that we should use some of the same tools and patterns to manage our local sandbox that we use everywhere else.
5.2 Workstation Prerequisites
This chapter assumes you have a Linux workstation that can run local VMs.
Ideally your workstation should meet the following requirements:
CPU contains virtualization extensions (Intel VT or AMD-v) and they’re enabled in the BIOS.
Minimum of 16GB RAM in order to run Linux plus a few VM’s.
x86_64 architecture is recommended for running the Vagrant RHEL boxes.
Note
ARM architecture may be workable (e.g. M-series macOS hardware), but hasn’t been tested. If you’re working through this book on ARM-based hardware, feel free to contribute to the discussion with any issues or feedback.
5.3 Install Vagrant
If you’re running Fedora, HashiCorp’s docs provide the following one-liner that can be pasted into your terminal and run to both install the hashicorp.repo file and install Vagrant.
wget-O- https://rpm.releases.hashicorp.com/fedora/hashicorp.repo |sudo tee /etc/yum.repos.d/hashicorp.reposudo yum list available |grep hashicorpsudo dnf -y install vagrant
Caution
It’s a good idea to read and understand any command like this that is copy and paste out of docs you find online. Especially if it’s downloading something and piping it to another command with | as this one is doing. We’ll cover pipes in more detail later.
If your workstation isn’t running Fedora, then follow the documented Vagrant Install for your Linux distro.
5.4 Enable Virtualization Support
There are multiple virtualization providers that Vagrant supports, but the two common ones are libvirt and VirtualBox (default). libvirt requires installing the vagrant-libvirt plugin and VirtualBox requires installing Oracle’s VirtualBox. My recommendation is to use libvirt since most Linux distributions have some kind of out-of-the box Virtualization support using libvirt + KVM.
If nothing is printed when this command is run, virtualization isn’t supported.
To enable virtualization support using libvirt we’ll use what are known as a package group to install a set of libraries, scripts, and utilities that provide support for “Virtualization” on the Fedora Desktop OS.
sudo dnf group info virtualization
Updating and loading repositories:
Repositories loaded.
Id : virtualization
Name : Virtualization
Description : These packages provide a graphical virtualization environment.
Installed : yes
Order :
Langonly :
Uservisible : no
Repositories : @System
Mandatory packages : virt-install
Default packages : libvirt-daemon-config-network
: libvirt-daemon-kvm
: qemu-kvm
: virt-manager
: virt-viewer
Optional packages : guestfs-tools
: python3-libguestfs
: virt-top
Note
Note the mandatory, default, and optional packages that will be installed if we install this group.
Next install the virtualization group.
sudo dnf install @virtualization
Then start and enable the libvirtd service daemon:
sudo systemctl enable --now libvirtd
Add your user to the libvirt group so that your local user is allowed to manage VMs without root privileges via sudo.
sudo usermod -aG libvirt <username>
Verify that your user is a member of the libvirt group:
Alternatively, you may install VirtualBox. This is the default provider that Vagrant supports out of the box (no special plugin needed as in vagrant-libvirt). If you choose to go this route, then I recommend not installing libvirt alongside it. This is known to conflict with VirtualBox as both hypervisors compete for the CPU’s virtualization extensions. Ideally pick one or the other as you’re preferred hypervisor for standing up your RHCSA lab VMs.
If you install VirtualBox on one of the rpm-family distros (i.e. Oracle Linux, RHEL, Fedora, or openSUSE) I recommend installing Oracle’s published virtualbox.repo file and installing VirtualBox using the system package manager (yum, dnf, or zypper). That way if there are updates available they will automatically become available whenever repositories are refreshed as in a system update (e.g. dnf update)
Install VirtualBox on Fedora:
wget-O- https://download.virtualbox.org/virtualbox/rpm/fedora/virtualbox.repo |sudo tee /etc/yum.repos.d/virtualbox.reposudo dnf -y install VirtualBox-7.2
Important
Be sure to verify that the version of VirtualBox you install is supported by Vagrant.
Although you may be able to install the vagrant-libvirt and vagrant-registration plugins using dnf (e.g. dnf install vagrant-registration); this is not recommended due to compatibility issues that may arise as a result of dependency and version mismatch between the vagrant packages available in repos compared to vagrant’s own plugin manager.
Stand-up the rhel10 VM using your chosen provider:
vagrant up --provider=libvirt # or --provider=virtualbox
The first time you run vagrant up the vagrant-registration plugin will automatically prompt you if you want to register the system. Go ahead and press Enter for the default, “yes”.
Note
You may see [fog][WARNING] Unrecognized arguments: libvirt_ip_command when using the libvirt provider. This is a known vagrant-libvirt/Fog compatibility warning. If vagrant status and vagrant ssh continue to work, it can be safely ignored.
Next you will be prompted to enter your username and password. Use the same username and password you used to create your Red Hat Developer’s Account.
...==> default: Registering box with vagrant-registration...default: Would you like to register the system now (default: yes)? [y|n]ydefault: username: akrakerdefault: password:==> default: Registration successful.==> default: Waiting for machine to boot. This may take a few minutes......
TipNon-interactive registration
For non-interactive username/password registration, the Vagrantfile above also accepts RHSM_USERNAME + RHSM_PASSWORD environment variables that can be set in your shell.
Although this isn’t necessarily the most secure practice, you may also add the two lines above to your ~/.bashrc so that the environment variables are persistent across shell sessions. Only do this if you’re reasonably certain your workstation is secure.
Obviously don’t use plaintext username/password on shared systems and or anything which might be exposed publicly.
Next run vagrant status and you should see the machine is running:
vagrant status
[fog][WARNING] Unrecognized arguments: libvirt_ip_command
Current machine states:
default running (libvirt)
The Libvirt domain is running. To stop this machine, you can run
`vagrant halt`. To destroy the machine, you can run `vagrant destroy`.
SSH into the machine with vagrant ssh:
vagrant ssh
[fog][WARNING] Unrecognized arguments: libvirt_ip_command
Register this system with Red Hat Insights: rhc connect
Example:
# rhc connect --activation-key <key> --organization <org>
The rhc client and Red Hat Insights will enable analytics and additional
management capabilities on your system.
View your connected systems at https://console.redhat.com/insights
You can learn more about how to register your system
using rhc at https://red.ht/registration
Verify that the system is registered by running sudo dnf repolist to refresh Red Hat repos.
sudo dnf repolist
Updating Subscription Management repositories.
repo id repo name
rhel-10-for-x86_64-appstream-rpms Red Hat Enterprise Linux 10 for x86_64 - AppStream (RPMs)
rhel-10-for-x86_64-baseos-rpms Red Hat Enterprise Linux 10 for x86_64 - BaseOS (RPMs)
Note that by default vagrant sets up vagrant as the login user:
whoami
vagrant
And gives this user passwordless sudoers privileges by default:
sudo-l
User vagrant may run the following commands on rhel10:
(ALL) NOPASSWD: ALL
Note
To support standing up reproducible lab environments on RHEL I’m publishing RHEL boxes to Vagrant Cloud. See the kraker namespace for a list of available boxes.