Working with units


Each node that Juju manages is referred to as a “unit”. Generally speaking, when using Juju you interact with the applications at the application level. There are times when working directly with units is useful though, particularly for debugging purposes. Juju provides a few different commands to make this easier.

The juju ssh command

The juju ssh command will connect you, via SSH, into a target unit. For example:

juju ssh mysql/3

This will start an SSH session on the 3rd mysql unit. This is useful for investigating things that happen on a unit, checking resources or viewing system logs.

It is possible to run commands via juju ssh, for example, juju ssh 1 uname -a will run the uname command on node one. This works for simple commands, however for more complex commands we recommend using juju run instead.

See also the juju help ssh command for more information.

The juju scp command

Copying files to and from units can be a common task depending on your workload, so Juju provides a juju scp command for copying files securely to and from units.


Copy a single file from machine 2 to the local machine:

juju scp 2:/var/log/syslog .

Copy 2 files from two MySQL units to the local backup/ directory, passing -v to scp as an extra argument:

juju scp -v mysql/0:/path/file1 mysql/1:/path/file2 backup/

Recursively copy the directory /var/log/mongodb/ on the first MongoDB server to the local directory remote-logs:

juju scp -r mongodb/0:/var/log/mongodb/ remote-logs/

Copy a local file to the second apache2 unit in the model “testing”:

juju scp -m testing foo.txt apache2/1:

Juju cannot transfer files between two remote units because it uses public key authentication exclusively and the native (OpenSSH) scp command disables agent forwarding by default. Either the destination or the source must be local (Juju client).

The juju run command

The run command can be used to inspect or do work on applications, units, or machines. Commands for applications or units are executed in a hook context. Charm authors can use this command to develop and debug scripts that run in hooks.

To run uname -a on every unit:

juju run "uname -a" --all

To run uptime on some units:

juju run "uptime" --machine=2
juju run "uptime" --application=mysql

When using the --application option with the run command the command will run on every application unit. When using the --machine option, the command is run as the root user on the remote machine.

Since v.2.5.2 an application leader can be targeted. To run hostname on the leader of application ‘ubuntu’:

juju run --unit ubuntu/leader hostname

When used in combination with certain applications you can script certain tasks. For instance, in the ‘hadoop’ charm you can initiate a terasort:

juju run --unit hadoop-master/0 "sudo -u hdfs /usr/lib/hadoop/"