How to forward a port with Slurm `srun` without opening another `ssh` on my computer?
Feb 1, 10:34pm

To forward a port using Slurm's srun command without opening another SSH connection from your local machine, you can use Slurm's built-in port forwarding feature. This is especially useful when you want to access a service running on a compute node (e.g., a Jupyter notebook, a web server, or a database) directly from your local browser or application.


Method: Use srun --tunnel (or --forward-port in newer versions)

1. Basic Syntax

bash
srun --tunnel=LOCAL_PORT:REMOTE_PORT --pty /bin/bash
  • LOCAL_PORT: The port on your local machine (or login node) that you want to forward.
  • REMOTE_PORT: The port on the compute node where the service is running.

2. Example: Forwarding a Jupyter Notebook

Suppose you want to forward a Jupyter notebook running on port 8888 on a compute node to your local machine's port 8888:

bash
srun --tunnel=8888:8888 --pty /bin/bash
  • Once the job starts, launch Jupyter on the compute node:
    bash
    jupyter notebook --no-browser --port=8888
  • On your local machine, open a browser and go to:
    text
    http://localhost:8888

3. Notes

  • The --tunnel option is available in Slurm 20.11 and later. If you're using an older version, check if your cluster supports it or use alternative methods (like SSH tunneling through the login node).
  • If you need to forward multiple ports, you can specify multiple --tunnel options:
    bash
    srun --tunnel=8888:8888 --tunnel=8000:8000 --pty /bin/bash

4. Alternative: Use SSH Tunneling via the Login Node

If --tunnel is not available, you can forward the port through the login node:

bash
ssh -L LOCAL_PORT:COMPUTE_NODE:REMOTE_PORT USER@LOGIN_NODE
  • Replace COMPUTE_NODE with the hostname of the compute node (you can find this in your Slurm job output).

Why This Works

  • srun --tunnel sets up the port forwarding directly through Slurm, so you don’t need to manually SSH into the compute node.
  • The port is forwarded from the compute node to the login node, and from there to your local machine (if you’re connected via SSH to the login node).

Feb 1, 10:34pm

This is a shared conversation and may include unverified content that doesn't reflect Mistral AI's views. Login to resume chatting.