Chapter 4 SSH
4.1 What is SSH?
Secure Shell (SSH) is a cryptographic network protocol for accessing a computer over an unsecured network. It gives you secure access to a machine’s command-line. Secure Shell provides strong password authentication and public key authentication, as well as encrypted data communications between two computers connecting over an open network, such as the internet. However, all computers within the University of Liverpool Network are not accessible from the open internet for security reasons. Therefore, to access a machine at the University you do not only need to be connected to the internet, but also to the Virtual Private Network (VPN) that virtually brings you to the University of Liverpool Network.
4.2 Connecting to a remote host
The use of SSH to connect to a remote host is performed through the following command:
ssh <username>@<server.ip.address>
If you are connecting to a server for the first time, then you may receive the following warning:
The authenticity of host 'hostIPaddress' cannot be established.
DSA key fingerprint is 01:23:45:67:89:ab:cd:ef:ff:fe:dc:ba:98:76:54:32:10.
Are you sure you want to continue connecting (yes/no)?
Windows users need to install an SSH client in order to access a server remotely through SSH. There are several clients available, MobaXterm is the option we advise to employ as it provides a number of useful functions for remote computing in a single application. The free edition can be downloaded here. To establish a connection with your remote server you open mobaXterm and click on session and then SSH. The Remote Host is the machine you want to access which can be identified by an IP address. Here you can find a simple demo of the SSH client.
If this is your first time connecting to the server, or if the server has recently been reconfigured with a new key, then the above message is perfectly normal. You can proceed by typing yes and enter.
Once you have accessed the server the first thing to do is to change the temprorary password we assigned to your user.
user@host:~$ passwd
4.3 Unix Commands
Servers often run unix operating systems such as GNU/Linux. Unix commands are essential to perform operations from the terminal.
The following are the most frequently used commands:
4.3.1 List Files:
user@host:~$ ls -lh
In the above example two flags have been added to the ls command:
- -l List with long format, e.g., show file read/write/execute permissions.
- -h List files with readable file size, e.g. MB, GB, etc.
4.3.2 Make Directory:
A new directory can be created using the mkdir command:
user@host:~$ mkdir <new_directory_name>
4.3.3 Change your Working Directory:
To change your working directory use the cd (change directory) command, specifying your target directory or .. to move back:
user@host:~$ cd <target_directory>
user@host:~$ cd ..
4.3.4 Move:
The mv command command can be used to either rename or move files and folders:
user@host:~$ mv <current_filename> <new_filename> # This is to change a file name
user@host:~$ mv <filepath> <target_directory> # This is to move a file to a folder
4.3.5 Copy:
To copy files use cp. The -r flag can be added to recursively copy all files and subdirectories within a directory:
user@host:~$ cp <filename> <filecopy>
user@host:~$ cp −r <directory> <target>
4.3.6 Remove:
Files and folders and can be deleted using the rm (remove) command:
user@host:~$ rm <filepath> # To delete a file
user@host:~$ rm -r <directory> # To delete a folder and its content
Again, we add the -r (recursive) flag to remove a directory and all its contents (subdirectories and files).
Warning: rm is to be used with caution. There is no trash folder from which the files can be recovered. Upon using this command the files are deleted.
4.4 Command Line Editors
There exists a large number of command line editors that can be used to edit files directly within the terminal. To create a file from the terminal you can simply type the following command:
user@host:~$ > filename.txt
Below we provide instructions for using the nano editor. To open a file in nano run the following command inside the terminal:
user@host:~$ nano <filename>
Once the file opens you can move your cursor using the arrow keys, and edit content as in any text editor. You can save any changes that you have made to the file using Ctrl + O. To close the editor press Ctrl + X. Before the editor closes you will be asked if you want to save your changes. Type Y for Yes to save changes, and N for No if you want to close the editor without saving.
For an overview of nano shortcut keys you can press Ctrl + G, which will output the following list.
4.5 Practical
Use SSH to access one of the lab servers (the IP address of the server you have been assigned will be provided).
Change the current temporary password with a password of your choice.
Create a directory with your project name.
List files to check that the directory is created.
Change your working directory with the project directory.
Create a file, edit and save using nano.