paint-brush
How to Setup Ubuntu 20.04 LTS on a Vagrant Virtual Boxby@okeybukks
4,042 reads
4,042 reads

How to Setup Ubuntu 20.04 LTS on a Vagrant Virtual Box

by Achebe Okechukwu October 24th, 2022
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

This article will teach you how to set up Ubuntu 20.04 LTS on a Vagrant virtual box. It will also show how to configure the Virtual box to suit your preferences. Once we’re done with installation and configuration, I will show you how to move the ip configuration file from your virtual machine to your host machine.

Company Mentioned

Mention Thumbnail
featured image - How to Setup Ubuntu 20.04 LTS on a Vagrant Virtual Box
Achebe Okechukwu  HackerNoon profile picture


This article will teach you how to set up Ubuntu 20.04 LTS on a Vagrant virtual box. It will also show how to configure the Virtual box to suit your preferences. Once we’re done with installation and configuration, I will show you how to move the ip configuration file from your virtual machine to your host machine.


If you’ve read this far, thank you for taking the time. This is one of my first tutorials and I’d love to hear your thoughts in the comments.


Let’s get to it.


To begin, you need to go to the official vagrant website and download vagrant on your host or the local machine. Once you’re done with the download, the installation is pretty straightforward forward just follow the onscreen prompts. To confirm whether or not it has been installed, go to your terminal/cmd depending on the type of machine you’re using.


vagrant


You should see something like this:


achebe@okechukwus-MacBook-Pro ~ % vagrant Usage: vagrant [options] <command> [<args>]
-h, --help                       Print this help.
Common commands: 
autocomplete    manages autocomplete installation on host 
box             manages boxes: installation, removal, etc. 
cloud           manages everything related to Vagrant Cloud


Once you’re done with this, the next step is to setup the virtual box on your machine. For ease, it will make sense if all the installed resources are in one folder. So, we create a folder inside the existing one. like this:


achebe@okechukwus-MacBook-Pro ~ % cd Desktop 
achebe@okechukwus-MacBook-Pro Desktop % mkdir -p TestVagrant/Box/Ubuntu_20_04 
achebe@okechukwus-MacBook-Pro Desktop % cd TestVagrant/Box/Ubuntu_20_04


After creating the Ubuntu_20_04 folder inside the ‘Desktop/Testvagrant/Box’ folder, the next step is to install the vagrant ubuntu using the vagrant init command. like so:


vagrant init ubuntu/focal64


You should get this output:


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.


Next, start up your virtual machine with this command:

vagrant up


Once done, you’re set to use your virtual machine with ubuntu inside it.

To enter your machine, use this command:

vagrant ssh


You should get this output:


Welcome to Ubuntu 20.04.4 LTS (GNU/Linux 5.4.0-122-generic x86_64)

Documentation:  https://help.ubuntu.com
Management:     https://landscape.canonical.com
Support:        https://ubuntu.com/advantage

System information as of Wed Aug 10 23:19:29 UTC 2022

System load:  0.01              Processes:               118
Usage of /:   4.1% of 38.70GB   Users logged in:         0
Memory usage: 23%               IPv4 address for enp0s3: 10.0.2.15
Swap usage:   0%

0 updates can be applied immediately.


To make sure that your virtual machine is indeed running ubuntu, type this command:

hostnamectl'


I got this result on my own end:


vagrant@ubuntu-focal:~$ hostnamectl 
Static hostname: ubuntu-focal 
Icon name: computer-vm 
Chassis: vm 
Machine ID: 87a9bd2baeb54bcda24765b89147565a 
Boot ID: 3b9ba20e3d494ac89568bb487c447a7f 
Virtualization: oracle 
Operating System: Ubuntu 20.04.4 LTS 
Kernel: Linux 5.4.0-122-generic 
Architecture: x86-64


As it is, we’re done with the installation of Ubuntu on our virtual machine.


The next thing to do is to explore configuration. For instance, setting our private network set to dhcp.


To get the IP address of your virtual machine, use iconfig.


If you get this error…


vagrant@ubuntu-focal:~$ ifconfig
Command 'ifconfig' not found, but can be installed with:
apt install net-tools 
Please ask your administrator.


You can easily resolve it with the sudo command. like so:


sudo apt install net-tools


Then, run the iconfig' command again and you should get this:


vagrant@ubuntu-focal:~$ ifconfig 
enp0s3: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500 
inet 10.0.2.15  netmask 255.255.255.0  broadcast 10.0.2.255 
inet6 fe80::52:20ff:fe6d:cccd  prefixlen 64  scopeid 0x20<link> 
ether 02:52:20:6d:cc:cd  txqueuelen 1000  (Ethernet) 
RX packets 80714  bytes 112088617 (112.0 MB) 
RX errors 0  dropped 0  overruns 0  frame 0 
TX packets 11575  bytes 942445 (942.4 KB) 
TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536 
inet 127.0.0.1  netmask 255.0.0.0 
inet6 ::1  prefixlen 128  scopeid 0x10<host> 
loop  txqueuelen 1000  (Local Loopback) 
RX packets 49  bytes 4998 (4.9 KB) 
RX errors 0  dropped 0  overruns 0  frame 0 
TX packets 49  bytes 4998 (4.9 KB) 
TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0


To configure our vagrant virtual machine (VM) so that it uses dchp as private_network allowing us to give it our own ip, we will need to exit the VM with the exit command and halt our VM with vagrant halt.


exit

vagrant halt


Once you’ve exited the vagrant machine environment, the Ubuntu OS into our location machine folder where the vagrant is installed, a vagrantfile is located in this folder. It is in this vagrantfile that you add your configuration.


Add this line of code to your configuration.

config.vm.network "private_network", type: "dhcp"


This isn’t the end. We also need to make little configurations in the VirtualBox. This is assuming that you’ve installed VirtualBox and it’s working fine on your end.


Take the following steps to configure it

Select the installed virtual machine. Take note, vagrant automatically creates the VM in the VirtualBox during installation.


Select settings, choose network and then select Adapter 2 to add another network adapter. For the “Attached to” dropdown select “Host-only Adapter”. Then, for the “Name” dropdown select “vboxnet0”, then click OK.


To see what we just did, enter your VM with the vagrant ssh, then run the ifconfig command. You should notice some new changes. Like this:


vagrant@ubuntu-focal:~$ ifconfig 
enp0s3: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500 
inet 10.0.2.15  netmask 255.255.255.0  broadcast 10.0.2.255 
inet6 fe80::52:20ff:fe6d:cccd  prefixlen 64  scopeid 0x20<link> 
ether 02:52:20:6d:cc:cd  txqueuelen 1000  (Ethernet) 
RX packets 907  bytes 118268 (118.2 KB) 
RX errors 0  dropped 0  overruns 0  frame 0 
TX packets 696  bytes 126192 (126.1 KB) 
TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

enp0s8: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
inet 192.168.56.6  netmask 255.255.255.0  broadcast 192.168.56.255
inet6 fe80::a00:27ff:fec4:dd3d  prefixlen 64  scopeid 0x20<link>
ether 08:00:27:c4:dd:3d  txqueuelen 1000  (Ethernet)
RX packets 24  bytes 11082 (11.0 KB)
RX errors 0  dropped 0  overruns 0  frame 0
TX packets 16  bytes 2342 (2.3 KB)
TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
inet 127.0.0.1  netmask 255.0.0.0
inet6 ::1  prefixlen 128  scopeid 0x10<host>
loop  txqueuelen 1000  (Local Loopback)
RX packets 8  bytes 712 (712.0 B)
RX errors 0  dropped 0  overruns 0  frame 0
TX packets 8  bytes 712 (712.0 B)
TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0


enp0s8 shows your new ip address 192.168.56.6. Cool right?


The last to do is to transfer the output of ifconfig into a file, then transfer said file to our local machine - the host.


Run this command to write the output of ifconfig into the file that you’ll create.

ifconfig > ifconfig.txt


To send this file to your host machine securely, you need to install vagrant scp. For this installation, take your VM out of the folder where you have vagrant installed and run this command.

vagrant plugin install vagrant-scp


Once this is done, run this command to transfer the file from the VM to your host machine.

vagrant scp default:/home/vagrant/ifconfig.txt ~/Desktop


When you check your host desktop, you’ll see where the ifconfig.txt file is located.

If you are curious about what default I’m using, it’s the current name of the Virtual Machine.


Don’t believe me? Run this command in the vagrant folder.

vagrant status


Phew! This tutorial was a bit longer than I expected, but we were able to do what we set out to do.


If you have any questions or suggestions, please let me know it the comments.


Thank you so much for reading!



Lead image source

Also published here.