How to safely test bash scripts with multipass

If you're looking for a quick and safe way to test bash scripts, check out the multipass virtual machine orchestrator.

How to safely test bash scripts with multipass If you're looking for a quick and safe way to test bash scripts, the multipass virtual machine orchestrator might be just what you need.

If you tend to cobble together your own Linux bash scripts for things like backups, security, or other data center needs, there might be times when you're hesitant to run that newly created script. What will it do? Might it erase crucial data? Could it possibly bring down that data center server?

Because of this, you might want an environment for which to test those scripts. That's where a tool like multipass comes in handy. Multipass is an open source tool that makes it easy to create, manage, and maintain virtual machines (VM). By creating a virtual machine, you sandbox those scripts such that running them cannot harm the host.

SEE: Hiring kit: Database administrator (Tech Pro Research)

I'm going to walk you through the process of installing and using multipass. The tool is installed via snap, so any Linux distribution that supports snap packages will work, so long as the operating system is installed on a machine that supports virtual environments. I'll demonstrate on Pop!_OS.

Installation

The first thing to do is install multipass. Open a terminal window and issue the command:

sudo snap install multipass --beta --classic

Once the command completes, you're ready to go.

Usage

The next step in this process is the creation of the virtual machine. Let's create one called test. This is done with the following command:

multipass launch --name test

Note: If the command refuses to launch, claiming it is not in your path, you might have to issue the command with the full path to the executable, like so:

/snap/bin/multipass --name test

When the command completes, you should see:

Launched: test

Log into your new virtual machine with the command:

multipass shell test

You should now find yourself in the VM with a completely different bash prompt (Figure A).

Figure A

Figure A: Our test vm shell is now ready.

You can now create your script within this virtual machine shell and run it without endangering your production machine. When you're finished testing, exit out of the virtual environment with the command:

exit

To delete the virtual environment, issue the command:

multipass delete test

A simple solution

You could certainly go the route of running your bash script testing on a full-blown virtual machine (such as those created by VirtualBox or VMware), but multipass is a much faster solution, especially when you don't need all of the bells and whistles of a complete virtual environment.

Also see

datacenterhero.jpg

Image: Jack Wallen