Your submission was sent successfully! Close

You have successfully unsubscribed! Close

Ubuntu Core – how to enable aliases for your snaps commands

David Callé

on 28 January 2017

This article was last updated 6 years ago.

We are happy to announce that a new version of Ubuntu Core, based on snapd 2.21, has been released to the stable snaps channel yesterday.

As with any stable release, your Ubuntu Core devices will update and reboot automatically. If you are using snaps on the desktop, the release will reach you through a snapd package update on Ubuntu 16.04, 16.10 and 17.04.

This release comes with several improvements you can read about in the changelog, but let’s focus on a big feature that will help people who snap very large software, especially software that comes with many commands (such as OpenStack, ImageMagick, most databases…) and their users.

Introducing snap aliases

When you launch a snap from the command line, you need to use the name of the snap, then the name of a command it contains. In most cases, you don’t notice it, because snapd simplifies the process by collapsing <snap-name>.<command-name> into <command-name>, when both are the same. This way, you don’t need to type inkscape.inkscape, but simply inkscape and get a familiar software experience.

But when a snap contains multiple commands, with various names, things can become less familiar. If we take the PostgreSQL snap as an example, we can see it comes with many commands: initdb, createdb, etc. In this case, you have to run postgresql96.initdb, postgresql96.createdb, etc.

The alias feature of snapd lets snaps declare their own aliases, for users to manually enable after install or for snap stores to declare as “auto-aliases” that will be enabled upon install.

How to manually enable aliases

To have an overview of all available aliases on a system, you can use the snap aliases command. For this example, I’ve installed a modified version of the postgresql96 snap with aliases for all commands.

$ snap aliases
App                            Alias             Notes
postgresql96.clusterdb         clusterdb         -
postgresql96.createdb          createdb          -
postgresql96.createlang        createlang        -
postgresql96.createuser        createuser        -
postgresql96.dropdb            dropdb            -
postgresql96.droplang          droplang          -
postgresql96.dropuser          dropuser          -
postgresql96.ecpg              ecpg              -
postgresql96.initdb            initdb            -

As you can see, the snap now exposes aliases to snapd. They are not enabled yet, but it’s simply a matter of running snap alias <snap-name> <alias>. Note that you can pass multiple space-separated aliases to this command.

Let’s enable the createuser alias.

$ snap alias postgresql96 createuser
$ snap aliases | grep createuser
postgresql96.createuser        createuser        enabled

I can now ensure that there is indeed a createuser command coming from a snap:

$ which createuser

And of course, run it:

$ createuser --help
createuser creates a new PostgreSQL role.

To disable an alias you can use the snap unalias command.

How to declare aliases

Declaring a new alias in your snap is as easy as adding an aliases entry to your snapcraft.yaml apps keys. Let’s look at the createuser command in our snap:

       command: usr/bin/createuser
       plugs: [network]
       aliases: [createuser]

And build your snap as usual with snapcraft, snapcraft cleanbuild or let it flow through your CI.

See the full example for this snap in this pull request.

That’s it, heads-on to to make your own snap from scratch and give aliases a try!

Internet of Things

From home control to drones, robots and industrial systems, Ubuntu Core and Snaps provide robust security, app stores and reliable updates for all your IoT devices.

Newsletter signup

Select topics you're
interested in

In submitting this form, I confirm that I have read and agree to Canonical's Privacy Notice and Privacy Policy.

Related posts

Ubuntu Core as an immutable Linux Desktop base

Join us as we discuss the architecture of immutable operating systems and the role of Ubuntu Core in the future of the immutable Linux desktop.

Canonical announces Ubuntu Core compatible with SystemReady IR Systems, highlights OS security with PSA Certified Level 1 achievement

Ubuntu Core, Arm SystemReady and PSA Certified are transforming how the industry think about connected edge devices, security and device compatibility. [14...

Security in the smart home: considerations for device makers

When people think of home security they usually think of an alarm system with a keypad next to the door. These days, however, home security should have two...