How to use instance command aliases

See also: Alias, Instance

[since version 1.8.0]

This document demonstrates how to create, list, execute, and remove aliases for commands running inside an instance.

Contents:

Create an alias

See also: alias

To create an alias that runs a command on a given instance, use the command alias. The code below uses this command to create an alias lscc that will run the command ls inside an instance crazy-cat:

$ multipass alias crazy-cat:ls lscc

After running this command, the alias lscc is defined as running the command ls on the instance crazy-cat. If the alias name lscc is omitted, the alias name defaults to the name of the command to run (ls in this case).

Working directory mapping

By default since version 1.10.0, in case the host folder on which executing an alias is mounted on the instance, the working directory on the instance is changed to the mounted directory. This behavior can be avoided when defining the alias using the parameter --no-map-working-directory. For instance:

$ multipass alias crazy-cat:pwd pwdcc --no-map-working-directory

List defined aliases

See also: aliases

To see the list of aliases defined so far, use the aliases command:

$ multipass aliases
Alias  Instance   Command  Working directory
lscc   crazy-cat  ls       map
pwdcc  crazy-cat  pwd      default

The column Working directory, present since Multipass 1.10.0, tells us on which directory of the host the alias will be executed. The value default means that the alias will be executed in the instance default working directory (normally, /home/ubuntu). The value map means that, in case the host directory from which the user calls the alias is mounted on the instance, the alias will be executed on the mounted directory on the instance. In versions prior to 1.10.0, the execution directory will be always default; starting in Multipass 1.10.0 the value will be default only if the --no-map-working-directory argument is present at alias creation.

Execute an alias

There are two ways to execute the alias.

multipass <alias>

The first way of executing an alias is

$ multipass lscc

This shells into the instance crazy-cat, executes ls and returns to the host command-line, as if it was an exec command.

Arguments are also supported, provided you separate any options with --:

$ multipass lscc -- -l

<alias>

The second way of running an alias is a two-step process:

Add Multipass alias script folder to system path

First, the Multipass alias script folder to the system path. The instructions to do so are displayed the first time one creates an alias, and vary for each platform. For instance,

$ multipass alias crazy-cat:ls lscc
You'll need to add this to your shell configuration (.bashrc, .zshrc or so) for
aliases to work without prefixing with `multipass`:

PATH="$PATH:/home/user/snap/multipass/common/bin"

Linux
Expand to see the instructions for Linux

In Linux, the shell configuration file must be modified. In most Linux distributions, the shell used by default is bash, which can be configured via the file .bashrc in the users home directory. Any text editor can be used for this, for example doing

$ nano ~/.bashrc

Once editing the file, the path can be modified by appending at its end a line such as

export PATH="$PATH:/home/user/snap/multipass/common/bin"

(remember to replace the correct folder by the one given in the output of the Multipass command above and to restart the shell).

In case of using zsh as shell, the file to modify is .zshrc instead of .bashrc; the procedure is the same.

MacOS
Expand to see the instructions for MacOS

In MacOS, the most commonly used shell is zsh. The procedure for adding a folder to the system path is the same as in Linux, described above.

Windows
Expand to see the instructions for Windows

For Windows, however, it is a bit more involved. To make the change permanent, use PowerShell to store the old system path, add the alias folder to it, and store the new path.

$old_path = (Get-ItemProperty -Path 'Registry::HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session Manager\Environment' -Name PATH).path
$new_path = “$old_path;C:\Users\<user>\AppData\Local\Multipass\bin”
Set-ItemProperty -Path 'Registry::HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session Manager\Environment' -Name PATH -Value $new_path

Don’t forget to restart your terminal. The folder is now permanently added to your path, Multipass can now execute aliases just invoking their name.

Execute the alias

Once you’ve added the alias folder to the system path, you can execute it directly (without mentioning multipass) as below:

$ lscc

This command (given that the path was already added to the system’s path) is equivalent to multipass lscc. Arguments are also supported, without the need for --:

$ lscc -l

Remove an alias

See also: unalias

Finally, to remove the alias lscc, issue:

$ multipass unalias lscc

Multipass 1.10.0 accepts many arguments to the unalias command, specifying more than one alias to remove. For example, removing at one aliases lscc and pwdcc is accomplished with:

$ multipass unalias lscc pwdcc

Multipass 1.10.0 also introduces the --all argument, aimed to remove all the defined alias at once. Its usage is:

$ multipass unalias --all

An alias is also removed when the instance for which it was defined is purged. This means that multipass delete crazy-cat --purge will also remove the alias lscc and pwdcc.


Last updated 3 months ago.