SSH Tunnel Setup (Advanced)
On KAYA
-
Create a slurm file to open a port on the compute node.
File:
tunnel.slurm#!/bin/bash -l #SBATCH --job-name=tunnel #SBATCH --partition=gpu #SBATCH --ntasks=1 #SBATCH --cpus-per-task=16 #SBATCH --mem=16G #SBATCH --gres=gpu:p100:1 #SBATCH --export=ALL #SBATCH --mail-type=ALL #SBATCH [email protected] #SBATCH --time=3-00:00:00 # module load Anaconda3/2024.06 cuda/12.4 module list nvcc --version nvidia-smi /usr/sbin/sshd -D -p 2222 -f /dev/null -h ${HOME}/.ssh/id_ecdsa -
Add module load commands to
.bashrcfile as each Jupyter cell will run a new bash session so modules need to be loaded again and again.module load Anaconda3/2024.06 cuda/12.4 -
Check if KAYA has
id_ecdsafile in~/.ssh/directory. If not, generate one usingssh-keygencommand.ssh-keygen -t ecdsa -b 521 -f ${HOME}/.ssh/id_ecdsa -
Add public key to the
~/.ssh/authorized_keysfile.cat ~/.ssh/id_ecdsa.pub >> ~/.ssh/authorized_keys -
Change the permissions of
.ssh.chmod 700 ~/.ssh chmod 600 ~/.ssh/authorized_keys -
Submit the slurm file.
sbatch tunnel.sh
On Local Machine
-
Open your ssh config file and add the following lines.
File:
~/.ssh/configHost login-node HostName kaya.hpc.uwa.edu.au User nchoong PreferredAuthentications publickey IdentityFile "~/.ssh/id_rsa" ServerAliveInterval 240 ServerAliveCountMax 2 Host compute-node HostName localhost Port 2222 User nchoong IdentityFile "~/.ssh/id_rsa" -
Check if you have
id_rsafile in~/.ssh/directory. If not, generate one usingssh-keygencommand.ssh-keygen -t rsa -b 4096 -f ~/.ssh/id_rsa -
Add public key to KAYA.
ssh-copy-id -i ~/.ssh/id_rsa.pub [email protected] -
Change the permissions of the
.ssh.chmod 700 ~/.ssh chmod 600 ~/.ssh/authorized_keys -
Create a bash script to open a tunnel to the compute node.
File:
connect_compute_node.sh#!/bin/bash # Kill any existing SSH tunnel lsof -ti:2222 | xargs kill -9 ps aux | grep "ssh -L 2222" | grep -v grep | awk '{print $2}' | xargs kill # Get the compute node name dynamically compute_node=$(ssh login-node squeue --me --name=tunnel --states=R -h -O NodeList | xargs) # Check if the compute node was found if [ -z "$compute_node" ]; then echo "No compute node found for the current job." exit 1 fi echo "Connecting to compute node: $compute_node" # Establish SSH tunnel ssh -L 2222:"$compute_node":2222 login-node # add -fN to the ssh command to run it in the background without shell # ssh -L 2222:"$compute_node":2222 login-node -fN -
Change the permissions of the script.
chmod +x connect_compute_node.sh -
Or you can create a function in your
.bashrcfile.function connect_compute_node() { # Kill any existing SSH tunnel lsof -ti:2222 | xargs kill -9 ps aux | grep "ssh -L 2222" | grep -v grep | awk '{print $2}' | xargs kill # Get the compute node name dynamically compute_node=$(ssh login-node squeue --me --name=tunnel --states=R -h -O NodeList | xargs) # Check if the compute node was found if [ -z "$compute_node" ]; then echo "No compute node found for the current job." return 1 fi echo "Connecting to compute node: $compute_node" # Establish SSH tunnel ssh -L 2222:"$compute_node":2222 login-node } -
Run the script or function.
./connect_compute_node.shor
connect_compute_node -
Then open VSCode or another terminal and run the following command to connect to the compute node.
ssh compute-nodeor
ssh -p 2222 nchoong@localhost