SSH is the Secure Shell, a powerful encrypted communication tool. This page contains some basic information about the use of SSH.
SSH client and server programs are included with almost all Linux distributions, Mac OS, and recent versions of Windows (later updates of 10 as well as 11). You can obtain SSH client and server software for most other platforms, including Android.
Here are some SSH clients (there are many others available):
Platform | Software | Comments |
---|---|---|
Linux | OpenSSH | Included in almost all Linux distributions |
OSX | OpenSSH | Included with OSX |
ChromeOS (Chromebook) | Terminal | The terminal provided with current versions of Chrome provides ssh capabilities. Use the 'Linux' menu option but instead of starting a local Linux vm/container, create a remote ssh session. |
Android | JuiceSSH | Free and premium versions available in the Google Play store. |
Windows | OpenSSH | Included with Windows 10 onward; may not be installed automatically on Windows 10. |
The most basic use of SSH is for a text-based connection used to enter commands and view command output, or execute a text-based application such a menu-driven system.
Some SSH clients have a graphical interface. For those used from the command line, the most basic form of invocation is:
ssh [[OPTIONS]] [user@]host command
Note that the ssh command name may vary, depending on the software used.
Where:
Useful options include:
Option | Description | Example |
---|---|---|
-C | Enable compression (gzip) to enhance performance | ssh -C user@host |
-X or -Y | Turn on X11 forwarding (it is recommended that -C also be used) - See X11 Tunnelling, below | ssh -X -C user@host |
-L or -R | Turns on tunnelling | (See Tunnelling section) |
-i identityfile | Specifies the private key file to be used (default: all private keys in ~/.ssh) | ssh -i ~/.ssh/id_dsa user@host |
-p portnumber | Connect to the TCP/IP port portnumber rather than the default SSH port 22. | ssh -p 2200 user@host |
ssh kim@winter
ssh -C kim@winter
df -h
on the remote system, and display the graphics on the local computer: ssh -XC kim@winter firefox
SSH is capable of carrying graphical traffic using the X Window System (X11) protocol. This means that if your client system is running the X Window System (most Unix/Linux systems, and any Mac or Windows system on which an X Server has been started), you can run remote graphical programs and display the output (windows) locally.
To enable this, add the -X
or -Y
options to your command line. The difference between -X and -Y is that (ideally) the -X option will prevent certain advanced types of access which are not required by most graphical programs and which may present a security vulnerability, such as taking a screenshot of your desktop.
ssh -XC kim@winter firefox
In addition to X11 tunnelling, SSH permits you to set up your own tunnels to carry arbitrary network traffic either from the client system to the server, or from the server to your client. This allows you to encrypt the traffic for security/privacy, and to circumvent firewalls.
For local, forward (client to server) tunnels, the syntax is:
ssh -L //listenport//://destinationhost//://destinationport// [[//OPTIONS//]] [//user//@]//host// [[//command//]]
Where:
localhost
, which denotes the SSH server, but it can also be the name of a system which the server can directly reach. Note that communication between the server and the destinationhost is not encrypted.For reverse (server to client) tunnels, the syntax is:
ssh -R //listenport//://destinationhost//://destinationport// [[//OPTIONS//]] [//user//@]//host// [[//command//]]
The arguments have the same meaning, but are executed one the opposite hosts, so the tunnel listens on the server and communicates to the destination through the client system.
ssh -L 8080:localhost:80 sam@winter
http://localhost:8080/
and be connected to the webserver (port 80) on the host winterssh -R 2000:spring:1234 sam@winter
ssh -L 3000:localhost:5900 sam@winter
SSH provides two mechanisms for file transfer: scp and sftp. You can use these from the command line using the OpenSSH software (installed by default in most MacOS, Windows, and Linux systems) or you can use a graphical tool that uses these protocols.
The following instructions are for using the OpenSSH version of scp/sftp from the command line:
The scp command functions like the normal Unix/Linux copy (cp) command, but accepts a hostname (and optional user ID) prepended to a filename for copying to/from remote systems:
scp [[//user//@]]//host//://sourcefilename// [[//user//@]]//host//://destinationfilename//
The options are the same as for the ssh command, except that to specify a port number, use
-P</code (capital) instead of <code>-p
(lowercase).
For example:
/etc/hosts
from this computer to the /tmp
directory on the computer ireland: scp /etc/hosts ireland:/tmp
test.tar
from the home directory of the user chris on the computer ireland to the current directory: scp chris@ireland:~/test.tar .
sftp provides an encrypted version of FTP. The command usage is:
sftp [[//user//@]]//host//
At which point you can use any standard FTP command.
sftp john.doe@matrix.senecac.on.ca
Many Linux graphical file managers, such as the GNOME Nautilus and KDE Konqueror programs, know how to manage files remotely using the ssh/scp/sftp protocols. For example, on GNOME, you can select the menu option Places>Connect to Server, enter the connection details, and view a file management window on a remote server. You can then rename, copy, move, and delete files using drag-and-drop operations (for example, you can copy a file by dragging it from your desktop to the remote window). You can also create bookmarks for rapid access to remote filesystems at a later date.
Although SSH can be used with passwords, a sysadmin may need to initiate hundreds of SSH connections a day, and typing passwords that often is tedious. SSH therefore permits authentication using public and private keys.
See Public Key Cryptography for an overview of how this technology works.
These instructions assume a Linux or Mac OS/X system running OpenSSH. Using OpenSSH on Windows will be similar except for file paths. Other SSH clients will have different procedures, and you'll need to check that the key produced is in OpenSSH format (you can convert key types using the ssh-keygen
command on a Linux system such as Matrix if needed).
ssh-keygen -ted25519
~/.ssh/id_ed25519
- your private key~/.ssh/id_ed25519.pub
- your public keyA note on key type and length: ssh-keygen can generate multiple types of keys, including rsa, dsa, ecdsa, and ed25519. Any of these types serves the same purpose, and rsa/dsa can be generated with varying key lengths. The longer the key, the more difficult it is to break the key by guessing it (trying successive values until the right key is found) - each bit added to the key doubles the number of possible key values. Key lengths of 1024-4096 bits are considered reasonably secure; as computers become faster, key lengths should be increased. Better yet, use the ed25519 eliptic curve option, which is considered the most secure format supported by the current OpenSSH implementation (8.4 as of the time of writing).
~/.ssh/id_rsa.pub
file to the remote system administrator. Do NOT send your private key!Automatically (preferred approach):
ssh-copy-id user@host
Manually:
~/.ssh
directory if it does not exist.~/.ssh
to 0700~/authorized_keys
~/authorized_keys
to 0600The GNU screen utility permits you to start a session, disconnect from it (accidentally or intentionally), and then reconnect from the same or another network connection. This is useful when you're using an unstable WiFi connection (e.g., in a coffee shop or mobile link) or want to start a long operation (such as a build or a test suite run) on campus, commute home while it's running, and then check on its status. Other tools such as tmux provide similar capabilities.
Note that this approach does not work with graphical applications (ssh -X), although software such as VNC can be used to provide disconnect/reconnect ability for graphical sessions.