Getting Started with SSH in the labs

ssh is a secure shell that allows us to access remote machines from the terminal. With correct VPN configuration and access this will allow us to access the University network and your linux home directories from your local machine.

Please not to use this you will need to be connected to the University network. This means either using the remote desktop solutions (VMWare or Leostreams / NoMachine) or VPN access. Whist this will mainly discuss connecting from a users remote desktop to the University network, it is also relevant to connecting between machines on the same network in particular copying files between the linux and windows machines.

Depending upon your installation of Windows you may need to install OpenSHH to do this follow the instructions here

On a Mac an ssh client is installed and enabled by default.

Basic ssh

At it’s simples ssh allows user@hostname to connect to a machine, in the case of the University user is going to be your student id used to log into the machines physically.

This is shown in the following example.

Note when connecting to a machine for the first time you are prompted to continue, you must type “yes” to do so. This will then add the machine to a file called ~/.ssh/known_hosts. This is used as extra security to avoid ip spoofing and other “man in the middle” attacks.

You will also note that I had to use the fully qualified domain name (student.bournemouth.ac.uk) this is because the VPN doesn’t add this to the standard search path in the DNS (again for security).

To disconnect from the remote machine we can use the CTRL + d key combination to send the logout command (which can also by typed to exit).

The next time you ssh into the same machine you will not be asked to continue, also note the last time you logged in is also reported.

Another thing to note is that if your username on the local machine is the same as the remote machine the username part may be omitted from the shh command.

We can also execute a command on the remote machine rather than logging in. The next example show the execution of the command ls ~/Desktop on the remote machine.

Using ssh-keygen

ssh-keygen allows us to generate a public / private key pair to use in ssh sessions which will negate the use of passwords. This same key can also be used when connecting to GitHub

ssh-keygen -t rsa -b 4096

Now we have generated our key we can copy it to the remote host using ssh-copy-id as all the linux machines in the University use the same home directory and credentials we only need to do this once.

CanonicalizeHostname

It becomes quite tedious to keep typing in the full hostname so we can add the following to our .ssh/config file

CanonicalizeHostname yes
CanonicalDomains student.bournemouth.ac.uk 
CanonicalizeMaxDots 0
CanonicalizeFallbackLocal no

This instructs ssh to search in the student.bournemouth.ac.uk domain if a full name is not presented so we can now just use the machine names.

remote commands revisited

Some times the command may need to use the terminal display (for example for an interactive session) if this is the case we need to use the -t flag with ssh as follows.

Using scp

The scp “secure copy” allows us to copy files too and from remote machines. This works the same as the normal copy command.

At it’s simplest we can use scp [file(s)] user@host:/path/ so for example to copy the file main.cpp to my home directory on the remote machine I would use the following.

scp main.cpp w11901:/home/jmacey/

Note the use of the : after the machine name to separate the machine from the path.

To copy the contents of a directory we need to use the -r (recurse flag) as follows

scp -r SomeFiles w11901:/home/jmacey/

To copy in the other direction we just use the hostname / path first as follows

scp -r w11901:/home/jmacey/SomeFiles ./

Note if using a more modern shell such as the fish shell tab completion will work over scp.

You will notice that scp does a verbatim copy to / from the host and makes no attempt to synchronize the files or folders. To do the we can use the rsync command.

Note that the Windows machines in the University have an ssh / scp client installed but not a server. Therefore it is possible to copy from Windows -> Linux and Windows <- Linux. It is not however possible to either ssh or scp from the Linux machines to the windows hosts.

Using rsync over ssh

The rsync command is used to synchronize file system across the same or remote machines. Whist this is standard for Linux machines / WSL the windows terminal itself does not support it. There are however 3rd party tools which implement the same thing. rsync has many options but at it’s simplest level we use

rsync [options] SRC... [DEST]

Where [DEST] can be either a local or remote machine.

For this example we will consider the following scenario.

  1. Most of our work is being done on the remote machine in a Folder called MyProject
  2. We will occasionally work at home so need to do a by-directional synch of projects by date / time
  3. We need to try and keep both directories up to date.

To start with I now have a maya project folder with a single scene in it on my linux machine at the University as show.

We will now rsync this to our local machine (note I’m using WSL for this)

First I use the –dry-run flag to see what files will be copied across. This is very useful for testing rsync calls as it will not copy / overwrite any files.

rsync -avx --dry-run w11901:/home/jmacey/MyProject ./

After that I use the same command without the –dry-run flag to copy the files.

flag long flag purpose
-a –archive archive mode
-v –verbose verbose mode
-x –one-file-system don’t cross filesystem boundaries

Archive mode actually switches on a load of other modes which are pertinent to our backing up strategy. In particular all of these flags are enabled by -a

-r, --recursive recurse into directories
-l, --links copy symlinks as symlinks
-p, --perms preserve permissions
-t, --times preserve modification times
-g, --group preserve group
-D same as --devices --specials
--specials preserve special files

Now we have a copy of the project on our local machine I will add a new scene file to the scenes directory.

We can now use

rsync -avx --dry-run MyProject/ w11901:/home/jmacey/MyProject/

To see what needs to be changed. You will notice that it now sees that the remote machine is missing the scene2.ma file and will copy that across. Note that MyProject/ is important we need to say we want to sync the contents of the directory.

In the following example I sync across a new file from the host, then ssh into the remote machine and use touch to simulate scene1.ma changing. Running the rsync command again will notice this change and copy the file back from the remote machine.

There are many other ways to use rsync depending upon the scenario. There are loads of good examples here and here

Using SSH and VS Code

It is possible to allow the Visual Studio code editor to work via ssh. This requires installing the “Remote - SSH” extension and some setup which is documented here This can be very useful for simple console based development (especially due to the keyboard lag found on the NoMachine system).

There is also an excellent post on Remote Development using SSH

But you should really be using vi/vim

Alternatively you can ssh into the machine and use the vim editor, this does have a bit of a learning curve but is worth the effort as vi / vim is installed on virtually all linux machines.

To exit vim I suggest reading this valuable article but for a more in depth tutorial on vim look here

And of cause just to start an editor war we also have ed nano and emacs installed in the labs.

Further reading / References

Practical rsync commands

Editor Wars

The revenge of VIM

Next
Previous

Related