User Tools

Site Tools


coding:ssh

Connect to a remote desktop (or cluster) via ssh

For connections to desktops inside of the University of Illinois network, VPN needs to be enabled and working. Follow the instructions here.

Once that is working, a connection to the desktop can be established in the terminal via ssh:

ssh <username>@<computer-name>.matse.illinois.edu 

There will be a prompt to add this computer to the 'list of known hosts', hit enter or type 'yes'. Then you need to provide your username's password. You can follow these instructions to set up ssh-keys such that you don't have to type the password every time when you connect.

If you look closely, the terminal prompt should change to the new computer: <username>@<computer-name>:~$. Now you can use this terminal like any other terminal, except for no GUI. You can try 'x-forwarding', see here but it might be unstable/glitchy.

Copy data from/to the remote computer

Use either scp or rsync.

For example, if you are logged in or sitting in front of a computer with hostname cat and your username is nova, and you want to transfer files to a remote computer with hostname dog and your username on that machine is marley:

nova@cat:~$ rsync -avr <path-to-files/folders> marley@dog:<destination-directory> 

where the options '-avr' are a combination of:

  • -v : verbose
  • -r : copies data recursively (but don’t preserve timestamps and permission while transferring data
  • -a : archive mode, archive mode allows copying files recursively and it also preserves symbolic links, file permissions, user & group ownerships and timestamps

The same works if you want to copy files from dog to cat:

nova@cat:~$ rsync -avr marley@dog:<path-to-files/folders> <destination-directory>

You will be asked to type your password for the user 'marley' in either case.

Rsync works if your computer has a fixed ip-address, so in most use-cases it is going to be more convenient to execute the 'rsync' command on your laptop (e.g. laptop=cat in the above example) if a laptop is involved. If both computers have a fixed ip-address, 'rsync' works in both ways.

Run a simulation or other long code over ssh

Usually, all processes are killed if the terminal they were executed from is closed/killed. This can be disadvantageous when a simulation or other long code is executed over ssh because a disconnect in vpn/ssh can accidentally kill the running process/simulation, or closing the terminal of the local computer will kill the running process. To avoid this, there are multiple ways of detaching or disowning the running process so that the terminal can be safely closed or disconnected.

Option 1: nohup

nohup long-running-command &

Output which would normally go to the terminal, e.g. stdout goes into a file called nohup.log. Read more about nohup here.

Option 2: bg + disown

./long-running-command &> output.log
Ctrl+Z
bg
disown -h

This puts already running tasks/processes into the “background”. Execute the process, then hit Ctrl+Z to suspend the task, then execute bg to put your most recent suspended task to background, allowing it to continue running. Disown will keep the process running after you log out. The -h flag prevents hangup. It is useful to re-direct the stdout and/or stderr to a file for this: ./long-running-command &> output.log, otherwise output is lost. Read more here.

WARNING : If you are disowning a bash script starting with mpirun, CTRL-Z will stop it. Put sleep 30 at the beginning and do the steps above in 30 seconds.

Option 3: screen

Ssh into your remote computer. Type screen. You can name different sessions by using screen -S <name>. Then start the process you want. Press Ctrl-A then Ctrl-D. This will “detach” your screen session but leave your processes running. You can now log out of the remote computer. If you want to come back later, log on again and type screen -r This will “resume” your screen session, and you can see the output of your process. Read more here and here.

coding/ssh.txt · Last modified: 2021/12/15 19:00 by bargun2

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki