SSH

From Alliance Doc
Revision as of 17:25, 29 March 2016 by Cgeroux (talk | contribs)
Jump to navigation Jump to search

TODO: will need to properly organize this page and perhaps add filler text between the transcluded pages and also decided what pages should be transcluded here and which should not, at the moment the below is just a list of possible pages which could be relevant. It may even be desirable to edit the transcluded pages (i.e. reduce header sizes) to allow for better transclusion.

Other languages:

<translate>

Creating an SSH session (Click for larger image)
Connected to a remote host (Click for larger image)
Enabling X11 Forwarding(Click for larger image)
Specifying a private key (Click for larger image)

Connecting with MobaXterm works in basically the same way as PuTTY (see Connecting with PuTTY) however, there is more functionality combined into MobaXterm than PuTTY. MobaXterm has a built-in SFTP client to transfer files as well as a built-in X11 server to allow you to run graphical programs remotely without the need to install a third-party X11 server. If you have already been using PuTTY and have saved sessions, MobaXterm will use these saved sessions so that you do not have to re-enter the settings.

To connect to a machine which you have not previously connected to using MobaXterm or PuTTY go to Sessions->New session, select an "SSH" session, type in the remote host address and your USERNAME (note you may need to check the "Specify username" check box). Then click "OK". MobaXterm will then save that session information you just entered for future connections, and also open an SSH connection to the specified host, which will then request your password. Once your password is entered successfully you will now have a terminal you can type commands at as well as an SFTP client in the left pane which you can use to view files on the remote machine as well as transfer files to and from the remote machine by dragging and dropping files.

X11 Forwarding

To enable X11 forwarding to allow the use of graphical applications from the host machine:

  1. Ensure that X11 forwarding is enabled for a particular session by right clicking on the session and select "Edit Session". In the session settings window, select "Advanced SSH settings" and ensure that the "X11-Forwarding" checkbox is checked.
  2. Ensure that the Icon for the "X server" in the top right corner of the main window is green. If it isn't green that means that you do not currently have an X server running. To start, click on the red "X" icon.
  3. Test that X11 forwarding is working by opening the session by double-clicking the session on the "Sessions" pane on the left and entering your password. Then run a simple GUI-based program to test, such as typing the command xclock. If you see a popup window with a clock, X11 forwarding should be working.

Using a Key Pair

Right-click on the session in the left "Sessions" pane and select "Edit Session". In the session settings window, select "Advanced SSH settings" and check the "Use private key" checkbox. You can then click on the icon at the right of the text box to browse the file system and select a private key file to use. To create a key pair, see Generating SSH keys in Windows. </translate>

Other languages:

<translate>

Enter hostname or IP address (Click for larger image)
Specify username to use when connecting; this is optional as one can type it when connecting (Click for larger image)
Enable X11 forwarding (Click for larger image)
Specifying an SSH key (Click for larger image)

Start up PuTTY and enter the host name or IP address of the machine you wish to connect to. You may also save a collection of settings by entering a session name in the Save Sessions text box and clicking the Save button. You can set the username to use when logging into a particular host under the Connection->Data section in the Auto-login username text box to saving typing the username when connecting.

X11 forwarding

If working with graphical-based programs, X11 forwarding should be enabled. To do this, go to Connection->SSH->X11 and check the Enable X11 forwarding checkbox. To use X11 forwarding one must install an X window server such as Xming or, for the recent versions of Windows, VcXsrv. The X window server should be actually started prior to connecting with SSH. Test that X11 forwarding is working by opening a PuTTY session and running a simple GUI-based program, such as typing the command xclock. If you see a popup window with a clock, X11 forwarding should be working.

Using a key pair

To set the private key putty uses when connecting to a machine go to Connection->SSH->Auth and clicking the Browse button to find the private key file to use. Putty uses files with a .ppk suffix, which are generated using PuTTYGen (see Generating SSH keys in Windows for instructions on how to create such a key). In newer versions of Putty, you need to click the "+" sign next to Auth and then select Credentials to be able to browse for the Private key file for authentication. Note that the additional fields in that newer interface, i.e. Certificate to use and Plugin to provide authentication response, should be left blank. </translate> Generating ssh keys in Windows Using ssh keys in Linux Ssh keys

Other languages:

<translate> This article is aimed at Windows and Mac users who do not have or have very little experience in UNIX environments. It should give you the necessary basics to access the compute servers and being quickly able to use them.

Connections to the servers use the SSH protocol, in text mode. You do not use a graphical interface (GUI) but a console. Note that Windows executables do not run on our servers without using an emulator.

There is a self-paced course available on this topic from SHARCNET: Introduction to the Shell

Obtaining help

Generally, UNIX commands are documented in the reference manuals that are available on the servers. To access those from a terminal:

Question.png
[name@server ~]$ man command

man uses less (see the section Viewing and editing files), and you must press q to exit this program.

By convention, the executables themselves contain some help on how to use them. Generally, you invoke this help using the command line argument -h or --help, or in certain cases, -help. For example,

Question.png
[name@server ~]$ ls --help

Orienting yourself on a system

Following your connection, you are directed to your $HOME directory (the UNIX word for folder) for your user account. When your account is created, your $HOME only contains a few hidden configuration files that start with a period (.), and nothing else.

On a Linux system, you are strongly discouraged to create files or directories that contain names with spaces or special characters, including accents.

Listing directory contents

To list all files in a directory in a terminal, use the ls (list) command:

Question.png
[name@server ~]$ ls

To include hidden files:

Question.png
[name@server ~]$ ls -a

To sort results by date (from newest to oldest) instead of alphabetically:

Question.png
[name@server ~]$ ls -t

And, to obtain detailed information on all files (permissions, owner, group, size and last modification date):

Question.png
[name@server ~]$ ls -l

The option -h gives the file sizes in human readable format.

You can combine options, for example:

Question.png
[name@server ~]$ ls -alth

Navigating the filesystem

To move about in the filesystem, use the cd command (change directory).

So, to change to my_directory, type:

Question.png
[name@server ~]$ cd my_directory

To change to the parent folder, type:

Question.png
[name@server ~]$ cd ..

And, to move back to your home directory ($HOME):

Question.png
[name@server ~]$ cd

Creating and removing directories

To create (make) a directory, use the mkdir command:

Question.png
[name@server ~]$ mkdir my_directory

To remove a directory, use the rmdir command:

Question.png
[name@server ~]$ rmdir my_directory

Deleting a directory like this only works if it is empty.

Deleting files

You can remove files using the rm command:

Question.png
[name@server ~]$ rm my_file

You can also recursively remove a directory:

Question.png
[name@server ~]$ rm -r my_directory

The (potentially dangerous!) -f option can be useful to bypass confirmation prompts and to continue the operation after an error.

Copying and renaming files or directories

To copy a file use the cp command:

Question.png
[name@server ~]$ cp source_file destination_file

To recursively copy a directory:

Question.png
[name@server ~]$ cp -R source_directory destination_directory

To rename a file or a folder (directory), use the mv command (move):

Question.png
[name@server ~]$ mv source_file destination_file

This command also applies to directories. You should then replace source_file with source_directory and destination_file with destionation_directory.

File permissions

UNIX systems support 3 types of permissions : read (r), write (w) and execute (x). For files, a file should be readable to be read, writable to be modified, and executable to be run (if it's a binary executable or a script). For a directory, read permissions are necessary to list its contents, write permissions enable modification (adding or removing a file) and execute permissions enable changing to it.

Permissions apply to 3 different classes of users, the owner (u), the group (g), and all others or the world (o). To know which permissions are associated to files and subdirectories of the current directory, use the following command:

Question.png
[name@server ~]$ ls -la

The 10 characters at the beginning of each line show the permissions. The first character indicates the file type :

  • -: a normal file
  • d: a directory
  • l: a symbolic link

Then, from left to right, this command shows read, write and execute permissions of the owner, the group and other users. Here are some examples :

  • drwxrwxrwx: a world-readable and world-writable directory
  • drwxr-xr-x: a directory that can be listed by everybody, but only the owner can add or remove files
  • -rwxr-xr-x: a world-readable and world-executable file that can only be changed by its owner
  • -rw-r--r--: a world-readable file that can only be changed by its owner.
  • -rw-rw----: a file that can be read and changed by its owner and by its group
  • -rw-------: a file that can only be read and changed by its owner
  • drwx--x--x: a directory that can only be listed or modified by its owner, but all others can still pass it on their way to a deeper subdirectory
  • drwx-wx-wx: a directory that everybody can enter and modify but where only the owner can list its contents

Important note: to be able to read or write in a directory, you need to have execute permissions (x) set in all parent directories, all the way up to the filesystem's root (/). So if your home directory has drwx------ permissions and contains a subdirectory with drwxr-xr-x permissions, other users cannot read the contents of this subdirectory because they do not have access (by the executable bit) to its parent directory.

After listing the permissions, ls -la command gives a number, followed by the file owner's name, the file group's name, its size, last modification date, and name.

The chmod command allows you to change file permissions. The simple way to use it is to specify which permissions you wish to add or remove to which type of user. To do this, you specify the list of users (u for the owner, g for the group, o for other users, a for all three), followed by a + to add permissions or - to remove permissions, which is then followed by a list of permissions to modify (r for read, w for write, x for execute). Non-specified permissions are not affected. Here are a few examples:

  • Prevent group members and all others to read or modify the file secret.txt:
    Question.png
    [name@server ~]$ chmod go-rwx secret.txt
    
  • Allow everybody to read the file public.txt:
    Question.png
    [name@server ~]$ chmod a+r public.txt
    
  • Make the file script.sh executable:
    Question.png
    [name@server ~]$ chmod a+x script.sh
    
  • Allow group members to read and write in the directory shared:
    Question.png
    [name@server ~]$ chmod g+rwx shared
    
  • Prevent other users from reading or modifying your home directory:
    Question.png
    [name@server ~]$ chmod go-rw ~
    

Viewing and editing files

Viewing a file

To view a file read-only, use the less command:

Question.png
[name@server ~]$ less file_to_view

You can then use the arrow keys or the mouse wheel to navigate the document. You can search for something in the document by typing /what_to_search_for. You can quit less by pressing the q key.

Comparing two files

The diff command allows you to compare two files:

Question.png
[name@server ~]$ diff file1 file2

The -y option shows both files side by side.

Searching within a file

The grep command allows you to look for a given expression in one file:

Question.png
[name@server ~]$ grep 'tata' file1

... or in multiple files:

Question.png
[name@server ~]$ grep 'tata' fil*

Note that, in Linux, the * wildcard matches zero or more characters. The ? wildcard matches exactly one character.

The text to be searched for can also be variable. For example, to look for the text number 10 or number 11, etc. with any number between 10 and 29, the following command can be used:

Question.png
[name@server ~]$ grep 'number [1-2][0-9]' file

A regular expression must be used for the search text. To learn more, see this guide to regular expressions.

</translate>