Through a relay you run
SSH over srun gets to the compute node through the login node. There is another way around the hop, which turns the connection around. A compute node usually cannot accept connections from outside the cluster, but on many clusters it can make them, if only to download packages. So the job connects out to a machine that your laptop can also reach, a relay, and your laptop connects to the app through it.
The app listens on the compute node’s loopback, as on the previous page, so again nothing listens on the cluster network. All of your traffic goes through the relay, so whoever runs it is who you are trusting with it. On this page, that is you.
Whether a compute node may connect out at all depends on how your centre has set up its network: some clusters let it reach anywhere, some only the web, and some nothing outside the cluster. Their documentation may say; otherwise, try it from a shell on a compute node, e.g. curl -sI https://github.com for the web, or ssh me@my-server true for SSH to a server of yours.
The relay is your own server
If you have a server that both the cluster and your laptop can reach, such as a small cloud VM or a machine at your institution, SSH alone does it. The job runs ssh -R, which makes the app appear on the server’s own loopback, and from your laptop you ssh -L to the server, as you would to anything else.
On the cluster
#!/bin/bash
#SBATCH --job-name=own-relay
#SBATCH --nodes=1
#SBATCH --ntasks=1
#SBATCH --time=01:00:00
# Cluster side. Submit from this directory, naming your relay:
# RELAY_HOST=me@my-server sbatch launch.sh
# then run connect.sh on your laptop.
#
# The app listens on loopback. The job connects out to your relay with ssh -R,
# which makes the app appear on the relay's own loopback at RELAY_PORT.
#
# RELAY_HOST user@host of your relay (required)
# RELAY_SSH extra ssh options, e.g. "-p 443 -i ~/.ssh/id_relay"
# RELAY_PORT port the app appears on, on the relay's loopback (default 8888)
set -euo pipefail
RELAY_HOST="${RELAY_HOST:?set RELAY_HOST=user@host of your relay}"
RELAY_PORT="${RELAY_PORT:-8888}"
SESSION="$HOME/.interactive-apps/$SLURM_JOB_ID"
mkdir -p "$SESSION"
chmod 700 "$HOME/.interactive-apps" "$SESSION"
trap 'rm -rf "$SESSION"' EXIT
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
RELAY_HOST=$RELAY_HOST
RELAY_PORT=$RELAY_PORT
APP_TOKEN=$APP_TOKEN
INFO
mv "$SESSION/info.env.tmp" "$SESSION/info.env"
# --- your app, bound to 127.0.0.1:$APP_PORT, with $APP_TOKEN as its token ---
python3 "${DEMO_APP:-$SLURM_SUBMIT_DIR/../demo-app/demo_app.py}" --host 127.0.0.1 --port "$APP_PORT" &
# ----------------------------------------------------------------------------
# BatchMode: a job cannot answer a password or passphrase prompt, so the key
# must work unattended. accept-new: trust the relay's host key the first time.
# shellcheck disable=SC2086
ssh -N ${RELAY_SSH:-} \
-o BatchMode=yes -o StrictHostKeyChecking=accept-new \
-o ExitOnForwardFailure=yes -o ServerAliveInterval=30 \
-R "127.0.0.1:$RELAY_PORT:127.0.0.1:$APP_PORT" \
"$RELAY_HOST"Submit it with your server as RELAY_HOST:
RELAY_HOST=me@my-server sbatch launch.shThe job starts the app on a free port of its loopback, with a token, as on the previous page, and writes the details to ~/.interactive-apps/<job ID>. Then -R 127.0.0.1:8888:127.0.0.1:<app port> asks the server to listen on port 8888 of its loopback, and to pass every connection there back through this SSH session to the app. RELAY_PORT picks another port than 8888, and RELAY_SSH adds options to ssh, such as -p 443 or a key with -i.
BatchMode=yes is because a job cannot type a password or a passphrase, so ssh fails at once rather than wait for one. StrictHostKeyChecking=accept-new trusts the server’s host key the first time it is seen, and only that key after it. So before the first job, log in to the server once from the login node, and check the fingerprint it shows you against the server’s own, which puts the right key into your ~/.ssh/known_hosts on the cluster.1 ExitOnForwardFailure ends the job if the port on the server is taken, and ServerAliveInterval keeps the connection from timing out while nothing is going through it.
On your laptop
#!/bin/bash
# Laptop side. Once the job is running:
# LOGIN_HOST=<your login node> ./connect.sh <JOBID>
# then open the URL it prints. Ctrl-C to disconnect.
#
# It reads the relay, its port and the app's token from the login node, and
# tunnels to the relay. Set RELAY_HOST if your laptop knows the relay by
# another name than the job does.
set -euo pipefail
JOBID="${1:?usage: connect.sh JOBID}"
LOGIN_HOST="${LOGIN_HOST:?set LOGIN_HOST to the login node, as you would name it after ssh}"
LOCAL_PORT="${LOCAL_PORT:-8888}"
relay="${RELAY_HOST-}"
eval "$(ssh -o BatchMode=yes "$LOGIN_HOST" "cat ~/.interactive-apps/$JOBID/info.env")"
RELAY_HOST="${relay:-$RELAY_HOST}"
echo "Open http://localhost:$LOCAL_PORT/?token=$APP_TOKEN (Ctrl-C to disconnect)"
exec ssh -N -o ExitOnForwardFailure=yes -L "$LOCAL_PORT:127.0.0.1:$RELAY_PORT" "$RELAY_HOST"LOGIN_HOST=my-cluster ./connect.sh <job ID>reads the relay, its port and the token from the login node, and forwards port 8888 on your laptop to port 8888 on the server’s loopback, which is the app. RELAY_HOST overrides the relay’s name, if your laptop knows it by another name than the cluster does.
A key for the job, and what it may do
The job needs a key to log in to your server, and since it cannot type a passphrase, that key has none. Anyone who gets it, such as anyone who can read your home directory on the cluster, can log in to your server with it. So make a key for this alone, on the cluster,
ssh-keygen -t ed25519 -N '' -f ~/.ssh/id_relaysubmit with RELAY_SSH="-i ~/.ssh/id_relay", and on the server, restrict it to the one thing it is for, by putting options in front of it in ~/.ssh/authorized_keys:
restrict,port-forwarding,permitlisten="127.0.0.1:8888",command="/bin/false" ssh-ed25519 AAAA... id_relay
restrict turns off everything a key can do by default, then port-forwarding turns forwarding back on, and permitlisten allows only port 8888 on the server’s loopback, which is what the job asks for. command replaces any command the key asks to run with /bin/false, so it cannot get a shell either. With all four, someone who steals the key can open that one port on your server, and nothing else.
What it means for security
Everything that crosses a network is inside SSH: from the compute node to the server, and from the server to your laptop. Nobody between them can read it, and no third party is involved.
But the two SSH sessions meet on the server, and there, on its loopback, the traffic is plain HTTP again. So whoever has root on the server can read it. That is you, and whoever hosts it for you, e.g. your cloud provider. And every other user of the server can connect to the port, like other users’ jobs on a compute node, which is why the token stays on. So use a server that nobody else logs in to.
The obvious relay is the cluster’s own login node, which every compute node can reach. On many clusters that does not work: the compute nodes may not be allowed to SSH to the login nodes, or the login nodes may not accept keys that you added yourself. And where it works, the port it opens is on the login node’s loopback, where every other user logged in to that login node can connect to it.
Limitations
You need the server, always on, and reachable from the compute nodes. If your cluster only lets the compute nodes out to the web, the server’s sshd has to listen on port 443 too.2 And a port on the server can only be held by one job at a time, so two jobs at once need two RELAY_PORTs, and two permitlistens for the key.
The next page is the same idea with somebody else’s relay, for when you don’t have a server.