Useful lxc command aliases

Login Alias
Description: Allows for “login” shell with optional user argument for a more normal login shell with readable /dev/std(out|err) and a proper PTS.

Creation: lxc alias add login 'exec @ARGS@ --mode interactive -- bash -xc {exec,login,-p,-f,$USER}'
Improved for systems without bash (trailing space after -f is required!):
lxc alias add login 'exec @ARGS@ --mode interactive -- /bin/sh -xac $@${USER:-root} - exec /bin/login -p -f '
non-priv shell: lxc login container --env USER=ubuntu
root: lxc login container

As mentioned below, the tricky bit seems to be working past the automatic single quoting of alias parameters and lack of a strait forward way to create a parameter that contains spaces. The trick above should be useful for other aliases as well.


Thanks, I am stealing it.

I have been trying to find such an alias that can be used in all types of containers.

The alpine container does not have bash but has sh.
The following returns immediately and you do not get a shell. But if you run the command exec login -p -f root in the container, you get a shell.

$ lxc exec alpine --mode interactive -- sh -c exec login -p -f root

Improved for alpine:

lxc alias add login 'exec @ARGS@ --mode interactive -- /bin/sh -xac $@${USER:-root} - exec /bin/login -p -f '
Warning: That trailing space after -f is apparently needed!
Then you can
root: lxc login aplineContainer
operator: lxc login alpineContainer --env USER=operator

There are some weird quoting issues that prevent you from getting a properly quoted string with spaces in it to pass through an alias unmolested, and the individual parameters get single quoted, but you can work around it by letting the shell expand the command string from positional arguments.

1 Like

Thanks again!

Here is what I would be suggesting to users.

Something missing in LXD is a simple command that gives you shell access to a LXD container, under the non-root account of the container. The Ubuntu containers from the ubuntu: repository have the ubuntu account (created by cloud-init instructions as the container is first started).

I used to suggest to lxc exec mycontainer -- sudo --user ubuntu --login. This can be aliased, with an alias named ubuntu so that a user can just run lxc ubuntu mycontainer and get a non-root shell.


Managing aliases in LXD

You can list, add, rename and remove LXD aliases.

Listing LXD aliases

Run the lxc alias list command to list all aliases. In the following, there are no aliases at all (default).

$ lxc alias list

Adding a LXD alias

Let’s create an alias, called list. There is already a lxc list subcommand, therefore we will be hiding the official subcommand. But why do this? First, lxc list produces by default a very wide table so we are going to drop some of the columns. Second, we want to show here how to remove an alias, so we will remove the list alias anyway.

$ lxc alias add list 'list -c ns46'
$ lxc alias list
| ALIAS | TARGET        |
| list  | list -c ns46  |

Now, when you run lxc list, you will get just four columns, n for the Name of the container, s for the State of the container (RUNNING, _STOPPED or something else), and 46 for both the IPv4 and IPv6 IP addresses of the container.

Renaming a LXD alias

Frankly, list is not a good choice for the name of an alias because it masks (hides) the proper lxc list subcommand. Let’s rename the alias to something else.

$ lxc alias rename list ll
$ lxc alias list
| ALIAS | TARGET        |
| ll    | list -c ns46  |

Now you can run the following command to list the containers in a table of four only columns.

$ lxc ll

Removing a LXD alias

We have now decided to remove the ll alias. If you love it though, you may keep it! Here is the command anyway.

$ lxc alias remove ll

To get a non-root shell to a LXD container (Ubuntu container from ubuntu: repository)

Create the following alias, named ubuntu:

lxc alias add ubuntu 'exec @ARGS@ --mode interactive -- /bin/sh -xac $@ubuntu - exec /bin/login -p -f '

Run as:

lxc ubuntu mycontainer

To get a shell to a LXD container (default: root, else specify with $USER)

Create the following alias, named ubuntu:

lxc alias add login 'exec @ARGS@ --mode interactive -- /bin/sh -xac $@${USER:-root} - exec /bin/login -p -f '

Run as (to get a root shell by default):

lxc login mycontainer

Run as (to get a specific $USER shell):

lxc login mycontainer --env USER=ubuntu
1 Like

This works great, and I’d already been using this after reading it from several of your fantastic tutorials.

This one doesn’t work for me. I get the following output (from an ubuntu-minimal:bionic container, which might explain it?)

seffyroff@celery:~$ lxc alias add ubuntu 'exec @ARGS@ --mode interactive -- /bin/sh -xac ubuntu - exec /bin/login -p -f '
seffyroff@celery:~$ lxc ubuntu lxdui
+ ubuntu
-: 1: -: ubuntu: not found

Thanks for the catch. Indeed, there was a typo (two characters were missing).
Try to copy that line again to recreate the alias (obviously, remove the old alias first). I verified that it now works.

1 Like

Thanks for the fix. I doubt i’d have figured that out myself - it’s a level of cli syntax witchcraft I’ve yet to reach :slight_smile:

I’m at a point where I’m routinely spinning up and down large numbers of containers. the lxc stop --all command is great in this case, but I’d love a similar arg for delete. Perhaps it can be accomplished with an alias? Or a shell iterator like one might have used in Docker when it was cool (docker rm ($docker ps -a)) or something like that…

Anyone have something in their toolbelt they’d care to share?

Figured it out!
lxc delete $(lxc list --format csv -c n)

Does the delete of all containers, on the local host. Simply alias that with something not too easily mis-typable :wink:

lxc alias add delall 'delete $(lxc list --format csv -c n)'

Except that doesn’t work. Some clever way to nest commands is required?

Could you explain what the different components of this command do? In particular, what are the arguments @ARGS@, $@ubuntu and the single dash?