SSH in default ubuntu user run snapcraft: command not found

This is not a question but more like a finding.
When I use JNLP to connect Jenkins slave and use default user “ubuntu”, I have to manually

echo $PATH: /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

export PATH=“$PATH:/snap/bin”

So that to get snapcraft works.

But when I do this locally

lxc oem-iot-bionic-1 bash

and

sudo su - ubuntu

/snap/bin is default included in the $PATH

echo $PATH: /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin

I am using images:ubuntu/bionic/cloud image.

It is easy to make the mistake. Does anyone know why such differences?

I don’t know what JNLP does exactly, but it looks like you are dropped into sh instead of bash. What does echo $SHELL say? If you’re running bash it’ll say bash. When using sh, the variable is empty.

sh doesn’t include /snap/bin in it’s PATH, bash does. That’s the only difference.

There are several ways to get a shell into a LXD container. Ideally, you would want to get a login shell so that your shell reads by default any configuration files that are meant to be read.

You can run lxc exec to execute a command in a container, and a byproduct of that is you can you run bash to get a shell. The close that the LXD lxc client has to get a shell, is the lxc shell built-in alias.
If you want to get a non-root shell into a container, you would need to write the full command yourself. And this is because this ubuntu non-root account is only available in the ubuntu: and images:ubuntu/.../cloud container images.

Personally, I use lxc ubuntu mycontainer, per https://blog.simos.info/using-command-aliases-in-lxd-to-exec-a-shell/

To answer your question, the snapd package installs this file, /etc/profile.d/apps-bin-path.sh which configured your $PATH to include /snap/bin for you. A login shell will source this file and setup successfully the $PATH for snaps.

If you were to add snaps to your $PATH manually, you can just run the following,

source /etc/profile.d/apps-bin-path.sh
1 Like

I tried when Jenkins use JNLP connect to slaves and it prints out

/bin/bash

I tried and it really works. I have learned a lot from your explain. That is really helpful. Thanks for sharing.