Your submission was sent successfully! Close

Thank you for signing up for our newsletter!
In these regular emails you will find the latest updates from Canonical and upcoming events where you can meet our team.Close

Thank you for contacting our team. We will be in touch shortly.Close

  1. Blog
  2. Article

David Callé
on 28 January 2017

Ubuntu Core – how to enable aliases for your snaps commands


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!

Related posts


Canonical
20 March 2024

Canonical’s Ubuntu Core receives Microsoft Azure IoT Edge Tier 1 supported platform status

Canonical announcements Canonical News

London, 20 March 2024. Canonical has announced that Ubuntu Core, its operating system optimised for the Internet of Things (IoT) and edge, has received Microsoft Azure IoT Edge Tier 1 supported platform status from Microsoft.  This collaboration brings computation, storage, and artificial intelligence (AI) capabilities in the cloud closer ...


lizzieepton
5 March 2024

Create an Ubuntu Core image with Landscape Client included

Internet of Things Ubuntu Pro

Canonical recently released the Landscape Client snap which, along with the new snap management features in the Landscape web portal, allows for device management of Ubuntu Core devices. In this blog we will look at how this can be deployed at scale by building a custom Ubuntu Core image that includes the Landscape Client snap ...


lizzieepton
13 February 2024

Simplify IoT device management: How to add Ubuntu Core devices to Landscape

Internet of Things Article

Landscape has been a member of the Canonical product list for almost as long as Canonical has existed. Landscape allows administrators to manage their desktop and server instances from a single centralised portal. With the latest release of Landscape Server (23.10), we’ve introduced the ability to manage snap packages from Landscape – and ...