SSH over srun
The previous page ended on a solution specific to Slurm, that is easy to deploy with minimal setup, and works the same whether it is JupyterLab or RStudio: run an SSH server on the compute node for the length of your job, and reach it through Slurm rather than through the network.
The idea
You cannot SSH to a compute node, but you can already run programs there with srun, and srun connects the program’s input and output to wherever you ran it. That is all a shell on a compute node is. So two more pieces are needed, and SSH has both:
sshd, the SSH server, has an inetd mode,sshd -i, in which it speaks SSH on its input and output, instead of listening on a port. Sosrun ... sshd -i, run from the login node, is an SSH server on the compute node that only you can reach, since only you can runsrunin your own job.ssh, the client, can use a command as its connection instead of the network, with theProxyCommandoption. Make that commandssh <login node> srun ... sshd -i, and thesshon your laptop is talking to thesshdon the compute node.
Then it is an ordinary SSH session, and SSH does what it always does: it checks who you are, encrypts everything, and forwards a port on your laptop with -L. The app listens on the compute node’s loopback address, 127.0.0.1, which other nodes cannot connect to at all.
So nothing listens on the cluster network, and nothing in plain HTTP travels between nodes. SSH checks the server’s key itself, so there is no certificate to make and no browser warning. And the app does not have to speak HTTPS, so RStudio is no harder than JupyterLab.
The scripts
It is two scripts: one you submit on the cluster, and one you run on your laptop. The SSH server needs keys and a configuration of its own, which the first one makes for every job, and the second one fetches.
On the cluster
#!/bin/bash
#SBATCH --job-name=srun-ssh
#SBATCH --nodes=1
#SBATCH --ntasks=1
#SBATCH --time=01:00:00
# Cluster side. Submit from this directory:
# sbatch launch.sh
# then run connect.sh on your laptop.
#
# Starts the app on the compute node's loopback, where nothing else on the
# cluster network can reach it, and prepares a throwaway sshd for connect.sh to
# run over the srun pipe.
set -euo pipefail
SESSION="$HOME/.interactive-apps/$SLURM_JOB_ID"
mkdir -p "$SESSION"
chmod 700 "$HOME/.interactive-apps" "$SESSION"
# The keys only open this job, so they are useless once it ends. Tidy them up
# when the app exits; after an scancel the directory may be left behind, and
# can simply be deleted.
trap 'rm -rf "$SESSION"' EXIT
# sshd refuses to run -i unless started by absolute path.
SSHD="${SSHD:-$(command -v sshd || echo /usr/sbin/sshd)}"
# A host key and a client key made for this job alone, so nothing here touches
# your own ~/.ssh.
ssh-keygen -q -t ed25519 -N '' -f "$SESSION/host_key"
ssh-keygen -q -t ed25519 -N '' -f "$SESSION/client_key"
cp "$SESSION/client_key.pub" "$SESSION/authorized_keys"
cat >"$SESSION/sshd_config" <<CONF
HostKey $SESSION/host_key
AuthorizedKeysFile $SESSION/authorized_keys
PasswordAuthentication no
KbdInteractiveAuthentication no
PubkeyAuthentication yes
AllowTcpForwarding yes
X11Forwarding no
StrictModes no
CONF
"$SSHD" -t -f "$SESSION/sshd_config"
# Any free port will do, since only this node's loopback can see it.
APP_PORT="$(python3 -c 'import socket; s = socket.socket(); s.bind(("127.0.0.1", 0)); print(s.getsockname()[1])')"
# A token for the app. Nothing else on the cluster network can reach the node's
# loopback, but every other job on the same node can.
APP_TOKEN="$(python3 -c 'import secrets; print(secrets.token_hex(24))')"
export APP_TOKEN
# Written aside and moved into place, so connect.sh never reads half of it.
cat >"$SESSION/info.env.tmp" <<INFO
JOBID=$SLURM_JOB_ID
NODE=$(hostname)
CLUSTER_USER=$(id -un)
APP_PORT=$APP_PORT
APP_TOKEN=$APP_TOKEN
SSHD=$SSHD
SESSION=$SESSION
INFO
mv "$SESSION/info.env.tmp" "$SESSION/info.env"
echo "app on $(hostname) 127.0.0.1:$APP_PORT; now run on your laptop: ./connect.sh $SLURM_JOB_ID"
# --- your app, bound to 127.0.0.1:$APP_PORT, with $APP_TOKEN as its token ---
# e.g. JUPYTER_TOKEN="$APP_TOKEN" jupyter lab --no-browser --ip=127.0.0.1 --port="$APP_PORT"
python3 "${DEMO_APP:-$SLURM_SUBMIT_DIR/../demo-app/demo_app.py}" --host 127.0.0.1 --port "$APP_PORT"Everything for the job goes in ~/.interactive-apps/<job ID>, which only you can read. Your home directory is shared between the login and compute nodes on nearly every cluster, so that is how the laptop side gets what it needs, through the login node, without reading the job’s output.
The job makes two pairs of keys for itself: a host key, which is how your laptop knows it is talking to this job’s sshd and not to someone pretending to be it, and a client key, which is how this sshd knows it is you. Both are made fresh for every job, and the server lets in only the client key, so nothing here touches your own ~/.ssh, and the keys are useless once the job ends.1 The configuration turns off passwords, and StrictModes no lets sshd use keys in your home directory, which it would otherwise refuse to do unless your home directory’s permissions are as strict as it likes them. sshd -t checks the configuration before anything else starts, so a mistake shows up in the job’s output rather than as a failed connection later.
SSHD is the full path to sshd, which it needs to run at all.2 Any sshd will do, as it runs as you and needs no root: the system’s own in /usr/sbin, or one you installed. Then the job picks a free port for the app, and a token, which the next section is about, and writes all of it to info.env.
The last line is the app. Here it is a small stand-in for JupyterLab that says which node it is running on; replace it with yours.
On your laptop
#!/bin/bash
# Laptop side. After `sbatch launch.sh` on the cluster:
# LOGIN_HOST=<your login node> ./connect.sh <JOBID>
# then open the URL it prints. Ctrl-C to disconnect.
#
# LOGIN_HOST is whatever you type after `ssh` to reach the login node, e.g. an
# alias from ~/.ssh/config. Leave it empty to run this on the login node itself.
set -euo pipefail
JOBID="${1:?usage: connect.sh JOBID}"
LOGIN_HOST="${LOGIN_HOST-}"
LOCAL_PORT="${LOCAL_PORT:-8888}"
# on_login COMMAND: run a command on the login node.
on_login() {
if [[ -n $LOGIN_HOST ]]; then ssh -o BatchMode=yes "$LOGIN_HOST" "$1"; else bash -c "$1"; fi
}
# Copy the session's details and keys here; they are deleted on exit.
TMP="$(mktemp -d)"
trap 'rm -rf "$TMP"' EXIT
on_login "cat ~/.interactive-apps/$JOBID/info.env" >"$TMP/info.env"
# shellcheck disable=SC1091
source "$TMP/info.env"
on_login "cat $SESSION/client_key" >"$TMP/client_key"
chmod 600 "$TMP/client_key"
# Trust exactly the host key the job generated, and nothing else. It is filed
# under a name of its own, so nothing in your ~/.ssh/config for the node applies.
echo "srun-ssh-$JOBID $(on_login "cat $SESSION/host_key.pub")" >"$TMP/known_hosts"
# The transport to the node: srun starts sshd there, in inetd mode (-i), which
# speaks SSH on stdin/stdout. --unbuffered is required, or the handshake stalls.
PROXY="srun --jobid=$JOBID --overlap --unbuffered --ntasks=1 $SSHD -i -f $SESSION/sshd_config"
if [[ -n $LOGIN_HOST ]]; then PROXY="ssh -o BatchMode=yes $LOGIN_HOST '$PROXY'"; fi
echo "Open http://localhost:$LOCAL_PORT/?token=$APP_TOKEN (Ctrl-C to disconnect)"
exec ssh -N \
-o ProxyCommand="$PROXY" \
-o HostKeyAlias="srun-ssh-$JOBID" -o UserKnownHostsFile="$TMP/known_hosts" -o StrictHostKeyChecking=yes \
-o IdentitiesOnly=yes -i "$TMP/client_key" \
-o ExitOnForwardFailure=yes -o ServerAliveInterval=30 \
-L "$LOCAL_PORT:127.0.0.1:$APP_PORT" \
"$CLUSTER_USER@$NODE"Once the job is running, run it with the job ID, and LOGIN_HOST set to whatever you type after ssh to reach the login node, e.g. an alias from your ~/.ssh/config:
LOGIN_HOST=my-cluster ./connect.sh <job ID>and open the URL it prints. Ctrl-C disconnects, and scancel <job ID> on the login node ends the job.
It copies info.env and the client key from the login node into a temporary directory, which it deletes when it exits. It trusts exactly the host key the job made, and nothing else, with StrictHostKeyChecking=yes: if anything else answers, ssh stops rather than asking you. HostKeyAlias files that key under a name of its own, so that nothing in your ~/.ssh/config for that node gets in the way.
The proxy command is the heart of it: ssh to the login node, and from there srun into the job, to start sshd -i with the job’s configuration. Each option of srun matters. --jobid and --overlap are as in getting into a running job; --ntasks=1 starts one sshd, not one per task of the job; and --unbuffered makes srun pass on every byte as it comes. Without it, srun holds on to the output until it has a buffer full, the SSH handshake stalls, and nothing says why.
The rest are the options for the session itself: ExitOnForwardFailure so that it stops if your local port is taken, rather than carry on without the forward, ServerAliveInterval to keep it from timing out while you are reading, and -L, which forwards port 8888 on your laptop to the app’s port on the compute node’s loopback. LOCAL_PORT picks another local port.
JupyterLab and RStudio
For JupyterLab, the last line of launch.sh becomes
JUPYTER_TOKEN="$APP_TOKEN" jupyter lab --no-browser --ip=127.0.0.1 --port="$APP_PORT"--ip=127.0.0.1 is what makes it listen on loopback only, and --port is the port connect.sh forwards to. The token is the one the job made, so the URL connect.sh prints opens it.
For RStudio Server, take the rserver command from Rocker’s recipe for running it in a Slurm job with Apptainer, keep its password, and add --www-address=127.0.0.1 --www-port="$APP_PORT", for the same reasons. Then you open http://localhost:8888 and sign in with that password. No proxy is needed in front of it, since the plain HTTP never leaves the compute node: between your laptop and the node, it is inside SSH.
The same goes for anything else with a web interface, such as TensorBoard or a Dask dashboard: make it listen on 127.0.0.1 and on $APP_PORT.
Why the token still matters
Loopback is private to the node, but not to you. Every process on that node can connect to 127.0.0.1, including other users’ jobs, if your cluster shares nodes between jobs. So nobody can reach the app from another node, or read its traffic, but someone with a job on the same node can still connect to its port, and then the app’s own token or password is the only thing in the way. That is why launch.sh makes a strong token for every job and gives it to the app, rather than turning it off.3
You get a shell too
The same connection, without -N and -L, is an ordinary SSH login to the compute node, inside your job’s allocation. So connect.sh with its last line changed to
exec ssh \
-o ProxyCommand="$PROXY" \
-o HostKeyAlias="srun-ssh-$JOBID" -o UserKnownHostsFile="$TMP/known_hosts" -o StrictHostKeyChecking=yes \
-o IdentitiesOnly=yes -i "$TMP/client_key" \
"$CLUSTER_USER@$NODE"gives you a shell there, which you can use for anything you would use ssh for, such as scp or rsync with the same options. Slurm’s environment variables, such as SLURM_JOB_ID, are not set in it, since sshd starts every session with a clean environment.
Limitations
It needs an sshd on the compute node that you can run. Nearly every Linux system has one in /usr/sbin, since it is how you log in, but a compute node is not always a full system. If it has none, install OpenSSH yourself, e.g. from conda-forge, as the page on tools for yourself shows, and set SSHD to it when you submit, as in SSHD=/path/to/sshd sbatch launch.sh.
connect.sh logs in to the login node five times: four to fetch the details and keys, and one to carry the session. With an SSH key, you won’t notice. But if your cluster asks for a password or a second factor every time you log in, it will fail, since it cannot type them for you. SSH can share one login between connections, which fixes that: put this in your ~/.ssh/config on your laptop,
Host my-cluster
ControlMaster auto
ControlPath ~/.ssh/cm-%C
ControlPersist 10m
where my-cluster is your LOGIN_HOST, then log in once with ssh my-cluster as usual, and connect.sh reuses that login for 10 minutes after the last connection has closed.
There is one more hop than in the usual recipe, and two SSH sessions, one inside the other. In practice, I don’t think you’d notice the difference for JupyterLab or RStudio.
And it relies on your cluster letting the login node srun into your job, which is what Slurm does by default. The next page is about a different way around the hop, for when you want the job to be reachable from somewhere other than the login node: the job connects out to a relay.
Footnotes
After a
scancel, the job may not get to delete them, and the directory can simply be deleted by hand.↩︎sshdrefuses to start unless it is started by its absolute path.↩︎If you ask for the whole node, with
#SBATCH --exclusive, nobody else’s job runs on it, and then nobody else can reach the port at all. But that is a whole node charged to your project, for one app.↩︎