Home Vagrant How To Reset Vagrant Virtual Machine To Original State

How To Reset Vagrant Virtual Machine To Original State

By sk
Published: Updated: 4.2K views

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.

Display username and hostname in Oracle Linux VM
Display username and hostname in Oracle Linux VM

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
Display username and hostname in Oracle Linux
Display username and hostname in Oracle Linux

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
Verify if a package is installed in Oracle Linux
Verify if a package is installed in Oracle Linux

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...
Reset vagrant virtual machine to its original state
Reset vagrant virtual machine to its original state

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.

Log in to Oracle Linux vagrant virtual machine
Log in to Oracle Linux vagrant 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.

You May Also Like

Leave a Comment

* By using this form you agree with the storage and handling of your data by this website.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

This website uses cookies to improve your experience. By using this site, we will assume that you're OK with it. Accept Read More