`multipass exec` command

See also: multipass exec and shells, How to use instance command aliases

The multipass exec command executes the given commands inside the instance. The first argument is the instance to run the commands on, -- optionally separates the multipass options from the rest - the command to run itself:

$ multipass exec primary -- uname -r
4.15.0-48-generic

You can pipe standard input and output to/from the command:

$ multipass exec primary -- lsb_release -a | grep ^Codename:
No LSB modules are available.
Codename:       bionic

The -- separator is required if you want to pass options to the command being run. Options to the exec command itself must be specified before --.

It is possible to specify on which instance directory the command must be executed. For that, there are three options. The first one is --working-directory <dir>, which tells Multipass that the command must be executed in the folder <dir>. For example,

$ multipass exec arriving-pipefish --working-directory /home -- ls -a
.  ..  ubuntu

The ls -la command showed the contents of the /home directory, because it was executed from there.

The second option to specify the working directory is to look through the mounted folders first. In case we are executing the alias on the host from a directory which is mounted on the instance, the command will be executed on the instance from there. If the working directory is not mounted on the instance, the command will be executed on the default directory on the instance. This is the default behavior and no parameter must be specified for this mapping to happen.

The third option is to directly execute the command in the default directory in the instance (usually, it is /home/ubuntu. The parameter to force this behaviour is --no-map-working-directory.


The full multipass help exec output explains the available options:

$ multipass help exec
Usage: multipass exec [options] <name> [--] <command>
Run a command on an instance

Options:
  -h, --help                      Displays help on commandline options
  -v, --verbose                   Increase logging verbosity. Repeat the 'v' in
                                  the short option for more detail. Maximum
                                  verbosity is obtained with 4 (or more) v's,
                                  i.e. -vvvv.
  -d, --working-directory <dir>   Change to <dir> before execution
  -n, --no-map-working-directory  Do not map the host execution path to a
                                  mounted path

Arguments:
  name                            Name of instance to execute the command on
  command                         Command to execute on the instance

Last updated 2 months ago.