Table of Contents
In this article, I will take you through the steps to setup a Virtual Linux Host Using rkt and Vagrant. Many times we get a requirement where we need to setup a virtual Linux host quickly to test a deployment or an application. While there are multiple ways to setup a virtual Linux host but here we will see using rkt and Vagrant only. The next important question you might ask is what is rkt and Vagrant ? Well, rkt is an app container engine and vagrant is a tool for building and maintaining virtual environments.
How to Setup a Virtual Linux Host Using rkt and Vagrant
Also Read: How to Install Flutter on Ubuntu 20.04 LTS (Focal Fossa)
Step 1: Prerequisites
a) You should have a running Linux Server(Ubuntu 20.04 LTS
in our case).
b) You should have sudo
or root
access to run privileged commands.
c) You should have atleast one package manager(apt
or apt-get
in our case) available in your System.
d) You should also have git
and curl
utility available in your Server.
Step 2: Update Your Server
The first step is to update the package cache with all the latest package information from all the enabled repositories by using apt-get update
command. If any package required upgradation, then upgrade it by using apt-get upgrade
command.
root@cyberithub:~# apt-get update
Hit:1 http://in.archive.ubuntu.com/ubuntu focal InRelease
Hit:2 http://in.archive.ubuntu.com/ubuntu focal-updates InRelease
Hit:3 http://in.archive.ubuntu.com/ubuntu focal-backports InRelease
Hit:4 https://apt.releases.hashicorp.com focal InRelease
Get:5 http://security.ubuntu.com/ubuntu focal-security InRelease [114 kB]
Get:6 http://security.ubuntu.com/ubuntu focal-security/main amd64 Packages [1,177 kB]
Get:7 http://security.ubuntu.com/ubuntu focal-security/main i386 Packages [362 kB]
Get:8 http://security.ubuntu.com/ubuntu focal-security/main Translation-en [210 kB]
Get:9 http://security.ubuntu.com/ubuntu focal-security/main amd64 DEP-11 Metadata [38.3 kB]
Get:10 http://security.ubuntu.com/ubuntu focal-security/main amd64 c-n-f Metadata [9,132 B]
If you are using any RHEL/CentOS based systems, then you can use yum update or dnf update command to update the package cache. If any package required upgradation, then upgrade it by using yum upgrade or dnf upgrade command.
Step 3: Install Vagrant
Then the next step is to install Vagrant in your System.
a) Add GPG Key
You need to go to official Vagrant website and get the step to add the GPG key to verify the integrity of the package.
root@cyberithub:~# curl -fsSL https://apt.releases.hashicorp.com/gpg | sudo apt-key add -
OK
b) Add Repository
Since Vagrant package is not available from default Ubuntu repository so you have to download and install it from Hashicorp repository. To add the repo, use below command.
root@cyberithub:~# apt-add-repository "deb [arch=amd64] https://apt.releases.hashicorp.com $(lsb_release -cs) main"
Hit:1 http://in.archive.ubuntu.com/ubuntu focal InRelease
Hit:2 http://in.archive.ubuntu.com/ubuntu focal-updates InRelease
Hit:3 http://in.archive.ubuntu.com/ubuntu focal-backports InRelease
Get:4 https://apt.releases.hashicorp.com focal InRelease [9,495 B]
Get:5 https://apt.releases.hashicorp.com focal/main amd64 Packages [42.5 kB]
Hit:6 http://security.ubuntu.com/ubuntu focal-security InRelease
Fetched 52.0 kB in 1s (58.0 kB/s)
Reading package lists... Done
c) Install Vagrant
Then run the update again and install vagrant by using below command.
root@cyberithub:~# apt-get update && sudo apt-get install vagrant
Hit:1 http://in.archive.ubuntu.com/ubuntu focal InRelease
Hit:2 http://in.archive.ubuntu.com/ubuntu focal-updates InRelease
Hit:3 http://in.archive.ubuntu.com/ubuntu focal-backports InRelease
Hit:4 https://apt.releases.hashicorp.com focal InRelease
Hit:5 http://security.ubuntu.com/ubuntu focal-security InRelease
Reading package lists... Done
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following NEW packages will be installed:
vagrant
0 upgraded, 1 newly installed, 0 to remove and 340 not upgraded.
Need to get 41.5 MB of archives.
After this operation, 117 MB of additional disk space will be used.
Get:1 https://apt.releases.hashicorp.com focal/main amd64 vagrant amd64 2.2.19 [41.5 MB]
Fetched 41.5 MB in 10s (4,306 kB/s)
Selecting previously unselected package vagrant.
(Reading database ... 183021 files and directories currently installed.)
Preparing to unpack .../vagrant_2.2.19_amd64.deb ...
Unpacking vagrant (2.2.19) ...
Setting up vagrant (2.2.19) ...
Step 4: Install rkt
Next you need to install rkt by using apt-get install rkt
command as shown below.
root@cyberithub:~# apt-get install rkt
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following additional packages will be installed:
libnss-mymachines libnss-systemd libpam-systemd libsystemd0 systemd systemd-container systemd-sysv systemd-timesyncd
Suggested packages:
docker2aci acbuild
The following NEW packages will be installed:
libnss-mymachines rkt systemd-container
The following packages will be upgraded:
libnss-systemd libpam-systemd libsystemd0 systemd systemd-sysv systemd-timesyncd
6 upgraded, 3 newly installed, 0 to remove and 329 not upgraded.
Need to get 71.8 MB of archives.
After this operation, 89.8 MB of additional disk space will be used.
Do you want to continue? [Y/n] y
Step 5: Clone Repository
You need to clone the Git rkt repository by using git clone https://github.com/rkt/rkt
command as shown below. This will create a local rkt directory with all the repo contents inside it.
root@cyberithub:~# git clone https://github.com/rkt/rkt
Cloning into 'rkt'...
remote: Enumerating objects: 34913, done.
remote: Counting objects: 100% (4/4), done.
remote: Compressing objects: 100% (4/4), done.
remote: Total 34913 (delta 0), reused 1 (delta 0), pack-reused 34909
Receiving objects: 100% (34913/34913), 25.48 MiB | 3.98 MiB/s, done.
Resolving deltas: 100% (21600/21600), done.
Step 6: Run Vagrant up
You need to change the current directory location by using cd rkt
command and then run vagrant up
command to create and configure virtual Linux host machines according to Vagrantfile.
root@cyberithub:~# cd rkt/ root@cyberithub:~/rkt# vagrant up Bringing machine 'default' up with 'virtualbox' provider... ==> default: Box 'ubuntu/xenial64' could not be found. Attempting to find and install... default: Box Provider: virtualbox default: Box Version: >= 0 ==> default: Loading metadata for box 'ubuntu/xenial64' default: URL: https://vagrantcloud.com/ubuntu/xenial64 ==> default: Adding box 'ubuntu/xenial64' (v20211001.0.0) for provider: virtualbox default: Downloading: https://vagrantcloud.com/ubuntu/boxes/xenial64/versions/20211001.0.0/providers/virtualbox.box Download redirected to host: cloud-images.ubuntu.com ==> default: Successfully added box 'ubuntu/xenial64' (v20211001.0.0) for 'virtualbox'! ==> default: Importing base box 'ubuntu/xenial64'... ==> default: Matching MAC address for NAT networking... ==> default: Checking if box 'ubuntu/xenial64' version '20211001.0.0' is up to date... ==> default: Setting the name of the VM: rkt_default_1642688172786_6427 ==> default: Clearing any previously set network interfaces...
Step 7: SSH to Virtual Host
Once the virtual Linux host is up and ready, you can login to the host by using vagrant ssh command as shown below.
root@cyberithub:~/rkt# vagrant ssh Welcome to Ubuntu 16.04.7 LTS (GNU/Linux 4.4.0-210-generic x86_64) * Documentation: https://help.ubuntu.com * Management: https://landscape.canonical.com * Support: https://ubuntu.com/advantage UA Infra: Extended Security Maintenance (ESM) is not enabled. 0 updates can be applied immediately. 45 additional security updates can be applied with UA Infra: ESM Learn more about enabling UA Infra: ESM service for Ubuntu 16.04 at https://ubuntu.com/16-04 The programs included with the Ubuntu system are free software; the exact distribution terms for each program are described in the individual files in /usr/share/doc/*/copyright. Ubuntu comes with ABSOLUTELY NO WARRANTY, to the extent permitted by applicable law. _____________________________________________________________________ WARNING! Your environment specifies an invalid locale. The unknown environment variables are: LC_CTYPE=en_IN LC_MESSAGES=en_IN LC_ALL= This can affect your user experience significantly, including the ability to manage packages. You may install the locales by running: sudo apt-get install language-pack-en or sudo locale-gen en_IN To see all available language packs, run: apt-cache search "^language-pack-[a-z][a-z]$" To disable this message for all users, run: sudo touch /var/lib/cloud/instance/locale-check.skip _____________________________________________________________________ vagrant@ubuntu-xenial:~$
Step 8: Shutdown Virtual Host
Once your work is done or if you want to shutdown your VM, then you need to use vagrant halt
command as shown below.
root@cyberithub:~/rkt# vagrant halt
==> default: Attempting graceful shutdown of VM...
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: Forcing shutdown of VM...