SSH

SSH / Connecting to Remote Machines

SSH/SCP

Often in scientific computing one needs to access a computer remotely. This may be because the other machine is a computing cluster, which no one has direct access to, or simply because there is some information on a different machine that one doesn’t have physical access to at the time.  We will refer to your machine as the local machine or local host and the other computer  as the remote machine or remote host.  In order to login to a remote machine you need to have ssh installed on your machine. This is automatic on Linux or Mac OS X computers, Windows users will need to download an ssh client.

To log into the remote host use:

ssh -X username@remotehost

this will then prompt you for your password. If the username on the local machine is that same as on the remote one then you don’t need to include your username. If you want to exit the remote machine and return to your local machine just type

exit

If you want to copy files from the remote machine or to the remote machine you use scp:

scp file remotehost:directory/
scp remotehost:directory/filename . 

ssh config

In order to make your life much easier you can edit your ssh config file to store the username and host address. This file can be found in the directory .ssh/ with filename config on you local machine.  You can create it if it isn’t there. Here is an example of what you might have in your .ssh/config file

Host ctp
     HostName ctp.citytech.cuny.edu
     User ari
     ServerAliveInterval 300

This creates the host alias ctp, which will be set to ctp.citytech.cuny.edu and it sets the username to ari. The last line sets the time before autologout which can help if your connection hangs up while you are waiting for something to finish. If you want to add another machine just leave a blank line and then new entries. Once this is set up you only need to type

ssh ctp

to login to the remote machine.

private/public key pairing

You can also set up a public/private key pair to avoid having to enter you password each time. This is much better security so it is advised that you do it.  To do this you need to create a key and then copy it to the remote machine. This is done with two commands:

ssh-keygen -t rsa
ssh-copy-id -i hostname

ssh-copy-id is not installed usually on Mac OS X. In that case you can use homebrew to install it.  If you are using Mac OS X and you want the private key to be stored in your keychain there are a few more steps you need to take.  First you need to add the key to the OS X keychain:

ssh-add -K .ssh/id_rsa

if you used the default file name for the key. Then you need to add a few more options to your ssh config file. In order to make options work for all hosts use host *

Host *
     #next 3 are to get Mac OSX to save passpharse in keychain
     UseKeychain yes
     AddKeysToAgent yes
     IdentityFile ~/.ssh/id_rsa
     #These are other options that you may want to set for all hosts
     ForwardX11 yes
     Compression yes
     ForwardX11Timeout 48h
     ServerAliveInterval 300
     ServerAliveCountMax 120

Comments are closed.