<a id="ref-commands"></a>

# Common CLI commands

This guide lists CLI commands for common operations in MicroCloud.
This command list is not meant to be exhaustive, but it gives a general overview and serves as an entry point to working with MicroCloud through the CLI.

Most commands you use in MicroCloud are actually LXD client commands. Find detailed information about them in the LXD documentation, including [How-to guides](https://canonical.com/microcloud/docs/default/lxd/howto/#howtos) as well as the [man pages](https://canonical.com/microcloud/docs/default/lxd/reference/manpages/lxc/#lxc-md) for the **lxc** command. Each section of the command list below includes links to relevant pages in the LXD documentation.

## Create and manage instances

See [Instances](https://canonical.com/microcloud/docs/default/lxd/instances/#instances).

### Check available images

See [How to use remote images](https://canonical.com/microcloud/docs/default/lxd/howto/images_remote/#images-remote).

| List remotes   | **lxc remote list**            |
|----------------|--------------------------------|
| Switch remotes | **lxc remote switch <remote>** |
| List images    | **lxc image list [<remote>:]** |

### Create instances

See [How to create instances](https://canonical.com/microcloud/docs/default/lxd/howto/instances_create/#instances-create) and [How to access the console](https://canonical.com/microcloud/docs/default/lxd/howto/instances_console/#instances-console).

| Create a container (without starting it)             | **lxc init [<remote>:]<image> [<name>] [flags]**                      |
|------------------------------------------------------|-----------------------------------------------------------------------|
| Create and start a container                         | **lxc launch [<remote>:]<image> [<name>] [flags]**                    |
| Create a VM (without starting it)                    | **lxc init [<remote>:]<image> [<name>] --vm [flags]**                 |
| Create and start a VM and connect to its VGA console | **lxc launch [<remote>:]<image> [<name>] --vm --console=vga [flags]** |

### Manage instances

See [How to manage instances](https://canonical.com/microcloud/docs/default/lxd/howto/instances_manage/#instances-manage).

| List instances                            | **lxc list**                                      |
|-------------------------------------------|---------------------------------------------------|
| Show status information about an instance | **lxc info <instance>**                           |
| Start an instance                         | **lxc start <instance> [flags]**                  |
| Stop an instance                          | **lxc stop <instance> [flags]**                   |
| Force-stop an instance                    | **lxc stop <instance> --force**                   |
| Delete an instance                        | **lxc delete <instance> [--force|--interactive]** |
| Copy an instance                          | **lxc copy <instance> <new_name> [flags]**        |

## Access instances

See [How to run commands in an instance](https://canonical.com/microcloud/docs/default/lxd/instance-exec/#run-commands), [How to access the console](https://canonical.com/microcloud/docs/default/lxd/howto/instances_console/#instances-console), and [How to access files in an instance](https://canonical.com/microcloud/docs/default/lxd/howto/instances_access_files/#instances-access-files).

| Run a command inside an instance                           | **lxc exec <instance> -- <command>**                              |
|------------------------------------------------------------|-------------------------------------------------------------------|
| Get shell access to an instance (if **bash** is installed) | **lxc exec <instance> -- bash**                                   |
| Get console access to an instance                          | **lxc console <instance> [flags]**                                |
| Pull a file from an instance                               | **lxc file pull <instance>/<instance_filepath> <local_filepath>** |
| Push a file to an instance                                 | **lxc file pull <local_filepath> <instance>/<instance_filepath>** |

## Configure instances

See [How to configure instances](https://canonical.com/microcloud/docs/default/lxd/howto/instances_configure/#instances-configure), [How to use profiles](https://canonical.com/microcloud/docs/default/lxd/profiles/#profiles), and [Instance configuration](https://canonical.com/microcloud/docs/default/lxd/explanation/instance_config/#instance-config) (and sub-pages).

| Show the configuration of an instance                                                     | **lxc config show <instance>**                                                                                                                                                                                                                 |
|-------------------------------------------------------------------------------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| Show the configuration of an instance, including configurations inherited from a profile  | **lxc config show <instance> --expanded**                                                                                                                                                                                                      |
| Set some configuration options for an instance (this example limits memory and CPU usage) | **lxc config set <instance> limits.memory=8GiB limits.cpu=4**<br/><br/>#### TIP<br/>See [Instance options](https://canonical.com/microcloud/docs/default/lxd/reference/instance_options/#instance-options) for all available instance options. |
| Override some device options for an instance (this example sets the root disk size)       | **lxc config device override <instance> root size=10GiB**<br/><br/>#### TIP<br/>See [Devices](https://canonical.com/microcloud/docs/default/lxd/reference/devices/#devices) for the device options that are available for each device type.    |
| Edit the full configuration of an instance                                                | **lxc config edit <instance>**                                                                                                                                                                                                                 |
| Apply a profile to an instance                                                            | **lxc profile add <instance> <profile>**                                                                                                                                                                                                       |

### Use `cloud-init`

See [How to use cloud-init](https://canonical.com/microcloud/docs/default/lxd/cloud-init/#cloud-init).

For example, to import an SSH key:

1. Create a profile: **lxc profile create <profile>**
2. Run **lxc profile edit <profile>** and add the following configuration to the profile:
   ```none
   config:
     cloud-init.user-data: |
       #cloud-config
       ssh_authorized_keys:
         - <public_key>
   ```
3. Launch the instance using that profile (in addition to the `default` profile): **lxc launch <image> [<name>] --profile default --profile <profile>**

## Manage instance snapshots

See [Use snapshots for instance backup](https://canonical.com/microcloud/docs/default/lxd/howto/instances_backup/#instances-snapshots).

| Create a snapshot                  | **lxc snapshot <instance> [<snapshot_name>] [flags]**   |
|------------------------------------|---------------------------------------------------------|
| View information about a snapshot  | **lxc config show <instance>/<snapshot_name>**          |
| View all snapshots of an instance  | **lxc info <instance>**                                 |
| Restore a snapshot                 | **lxc restore <instance> <snapshot_name> [--stateful]** |
| Delete a snapshot                  | **lxc delete <instance>/<snapshot_name>**               |
| Create an instance from a snapshot | **lxc copy <instance>/<snapshot_name> <new_instance>**  |

## Configure storage

See [How to manage storage volumes](https://canonical.com/microcloud/docs/default/lxd/howto/storage_volumes/#howto-storage-volumes).

To create a storage pool, see [How to manage storage pools](https://canonical.com/microcloud/docs/default/lxd/howto/storage_pools/#howto-storage-pools).
However, keep in mind that for MicroCloud to be able to use the storage pool, it must be created for the cluster and not only for one machine.
Therefore, the following example commands use the `remote` storage pool that is automatically set up in MicroCloud.

| Create a custom storage volume of content type `filesystem` in the `remote` storage pool   | **lxc storage volume create remote <volume>**                       |
|--------------------------------------------------------------------------------------------|---------------------------------------------------------------------|
| Create a custom storage volume of content type `block` in the `remote` storage pool        | **lxc storage volume create remote <volume> --type=block**          |
| Attach a custom storage volume of content type `filesystem` to an instance                 | **lxc storage volume attach remote <volume> <instance> <location>** |
| Attach a custom storage volume of content type `block` to an instance                      | **lxc storage volume attach remote <volume> <instance>**            |

## Configure networking

See [Networking](https://canonical.com/microcloud/docs/default/lxd/networks/#networking) (and sub-pages).

| Create a network                              | **lxc network create <network> --type=<type> [options]**<br/><br/>#### TIP<br/>See [How to create a network](https://canonical.com/microcloud/docs/default/lxd/howto/network_create/#network-create) for detailed information.                                                                      |
|-----------------------------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| Attach an instance to a network               | **lxc network attach <network> <instance> [<device>] [<interface>]**<br/><br/>#### TIP<br/>See [Attach a network to an instance](https://canonical.com/microcloud/docs/default/lxd/howto/network_create/#network-attach) for detailed information.                                                  |
| Create and apply a network ACL to an instance | **lxc network acl rule add <ACL> ingress|egress [properties]**<br/><br/>**lxc network set <network> security.acls="<ACL>"**<br/><br/>#### TIP<br/>See [How to configure network ACLs](https://canonical.com/microcloud/docs/default/lxd/howto/network_acls/#network-acls) for detailed information. |
| Expose an instance on an external IP          | **lxc network forward <network> create <external_IP> target_address=<instance_IP>**<br/><br/>#### TIP<br/>See [How to configure network forwards](https://canonical.com/microcloud/docs/default/lxd/howto/network_forwards/#network-forwards) for detailed information.                             |

## Use projects

See [Instances grouping with projects](https://canonical.com/microcloud/docs/default/lxd/explanation/projects/#exp-projects) and [Projects](https://canonical.com/microcloud/docs/default/lxd/projects/#projects) (and sub-pages).

| Create a project    | **lxc project create <project> [--config <option>]**   |
|---------------------|--------------------------------------------------------|
| Configure a project | **lxc project set <project> <option>**                 |
| Switch to a project | **lxc project switch <project>**                       |

## Configure the LXD server

See [How to configure the LXD server](https://canonical.com/microcloud/docs/default/lxd/howto/server_configure/#server-configure).

| Configure server options   | **lxc config set <key> <value>**<br/><br/>#### TIP<br/>See [Server configuration](https://canonical.com/microcloud/docs/default/lxd/server/#server) for all available server options.   |
|----------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|

## Manage the MicroCloud cluster

See [How to manage instances in a cluster](https://canonical.com/microcloud/docs/default/lxd/howto/cluster_manage_instance/#cluster-manage-instance) and [Evacuate a cluster member](https://canonical.com/microcloud/docs/default/lxd/howto/cluster_manage/#cluster-evacuate).

| Inspect the cluster status for all services at once   | **microcloud service list**                                                                                                                                                                                                                                                                                                                                              |
|-------------------------------------------------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| Inspect the cluster status for each service           | **microcloud cluster list**<br/><br/>**lxc cluster list**<br/><br/>**microceph cluster list**<br/><br/>**microovn cluster list**                                                                                                                                                                                                                                         |
| Migrate an instance to a different cluster member     | **lxc move <instance> --target <member>**                                                                                                                                                                                                                                                                                                                                |
| Copy an instance from a different LXD server          | Add one of the MicroCloud cluster members as a remote on the different LXD server and copy or migrate the instance from that server.<br/><br/>**lxc copy <instance> <remote>**<br/><br/>#### TIP<br/>See [How to migrate LXD instances between servers](https://canonical.com/microcloud/docs/default/lxd/howto/instances_migrate/#howto-instances-migrate) for details. |
| Evacuate a cluster member                             | **lxc cluster evacuate <member>**                                                                                                                                                                                                                                                                                                                                        |
| Restore a cluster member                              | **lxc cluster restore <member>**                                                                                                                                                                                                                                                                                                                                         |
| Shut down a cluster member                            | See: [How to shut down a MicroCloud cluster member](https://canonical.com/microcloud/docs/default/how-to/member_shutdown/index.html.md#howto-member-shutdown).                                                                                                                                                                                                                                                  |
