As you may already, Vagrant is an open source application to build and maintain virtual software development environments. You can quickly spin up a virtual machine using a pre-configured Vagrant box and copy the same Vagrant box to other systems to deploy a exact replica of the current development environment. Once you're done exploring your development environment, you can simply reset the Vagrant machine back to it's original default state. So you don't have to download the same Vagrant box over and over.
Reset Vagrant Virtual Machine To Original State
For the purpose of this guide, I downloaded Oracle Linux 8 Vagrant box and created a VM using that box. The default username and hostname of this VM is vagrant
and localhost
respectively.
Let us change these values. I have changed the username and hostname as described in this guide.
Now my current username is ostechnix
and hostname is ol8
.
$ whoami ostechnix
$ hostname ol8
Let us install any software, for example nano
editor.
$ sudo dnf install nano
Verify if nano
is installed or not using command:
$ dnf list nano
Do whatever you want to do in the Virtual machine.
After you explored the virtual environment and if you don't want it anymore, simply wipe it and re-create a clean virtual environment.
Before you reset the Vagrant machine to it's default state, backup your work and then shutdown the VM:
# poweroff
You can also use the following command from the hostname system to shutdown the vagrant virtual machine:
$ vagrant halt
Verify if the VM is running or not:
$ vagrant status
Sample output:
Current machine states: default shutoff (libvirt) The Libvirt domain is not running. Run `vagrant up` to start it.
The VM is powered off.
Now we will reset the Vagrant box to original state using commands:
$ vagrant destroy
Sample output:
$ vagrant destroy default: Are you sure you want to destroy the 'default' VM? [y/N] y ==> default: Removing domain...
The destroy
command will stop the running Virtual machine (if it is already running) and destroy it along with all resources that were created during the virtual machine creation process. Now your host system should be left at a clean state, as if you never created the guest machine in the first place.
Please note that the above command will not destroy the vagrant box that have been downloaded while creating the VM. For your information, all vagrant boxes are stored in ~/.vagrant.d/boxes/
location in your system. Let us verify it with ls
command:
$ ls ~/.vagrant.d/boxes/ fedora33 oraclelinux-VAGRANTSLASH-7 Fedora33 oraclelinux-VAGRANTSLASH-8
Yes, all boxes are available. We only deleted the VM that we created using the vagrant box.
After stopping and destroying the VM, run the following command to create a new fresh virtual machine:
$ vagrant up --provider=libvirt
Here, I am using the vagrant box with libvirt/kvm
provider.
If you've downloaded the Vagrant box for Oracle Virtualbox, you don't have to specify the --provider=libvirt
option. Just use this command instead:
$ vagrant up
Done! The vagrant machine has been reset to it's default original state. All customizations and installed software in the Virtual machine will be gone. You should now have a fresh virtual machine.
As you see in the above example, I didn't delete the Oracle Linux 8 Vagrant box and re-download it. I simply reset the Vagrant VM to it's original state when it is downloaded from the official site.
Hope this helps.