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.
srun --tunnel (or --forward-port in newer versions)bashsrun --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.Suppose you want to forward a Jupyter notebook running on port 8888 on a compute node to your local machine's port 8888:
bashsrun --tunnel=8888:8888 --pty /bin/bash
bashjupyter notebook --no-browser --port=8888
texthttp://localhost:8888
--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).--tunnel options:
bashsrun --tunnel=8888:8888 --tunnel=8000:8000 --pty /bin/bash
If --tunnel is not available, you can forward the port through the login node:
bashssh -L LOCAL_PORT:COMPUTE_NODE:REMOTE_PORT USER@LOGIN_NODE
COMPUTE_NODE with the hostname of the compute node (you can find this in your Slurm job output).srun --tunnel sets up the port forwarding directly through Slurm, so you don’t need to manually SSH into the compute node.