Ubuntu Core – how to enable aliases for your snaps commands

David Callé

on 28 January 2017

Tags: core , Ubuntu Core

This article was last updated 7 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
/snap/bin/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:

[...]
apps:
  [...]
  createuser:
       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 tutorials.ubuntu.com 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

Get the latest Ubuntu news and updates in your inbox.

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

Related posts

EdgeIQ and Ubuntu Core; bringing security and scalability to device management 

Today, EdgeIQ and Canonical announced the release of the EdgeIQ Coda snap and official support of Ubuntu Core on the EdgeIQ Symphony platform. EdgeIQ Symphony...

Getting started with Azure IoT Operations on Ubuntu

Introduction With the recent announcement of the release of Azure IoT Operations, Microsoft has provided its customers with a unified data plane offering...

Meet Canonical at SPS 2024

SPS (Smart Production Solutions) 2024 is almost here! With over 1,200 national and international exhibitors, SPS is the main gathering of industrial...