SSH Essentials Working with SSH Servers Clients and Keys

Get the latest tutorials on SysAdmin and open source topics.

We hope you find this tutorial helpful. In addition to guides like this one, we provide simple cloud infrastructure for developers.Learn more

SSH Essentials: Working with SSH Servers, Clients, and Keys

SSH is a secure protocol used as the primary means of connecting to Linux servers remotely. It provides a text-based interface by spawning a remote shell. After connecting, all commands you type in your local terminal are sent to the remote server and executed there.

In this cheat sheet-style guide, we will cover some common ways of connecting with SSH to achieve your objectives. This can be used as a quick reference when you need to know how to do connect to or configure your server in different ways.

Read the SSH Overview section first if you are unfamiliar with SSH in general or are just getting started.

Use whichever subsequent sections are applicable to what you are trying to achieve. Most sections are not predicated on any other, so you can use the examples below independently.

Use the Contents menu on the left side of this page (at wide page widths) or your browsers find function to locate the sections you need.

Copy and paste the command-line examples given, substituting the values in

The most common way of connecting to a remote Linux server is through SSH. SSH stands for Secure Shell and provides a safe and secure way of executing commands, making changes, and configuring services remotely. When you connect through SSH, you log in using an account that exists on the remote server.

When you connect through SSH, you will be dropped into a shell session, which is a text-based interface where you can interact with your server. For the duration of your SSH session, any commands that you type into your local terminal are sent through an encrypted SSH tunnel and executed on your server.

The SSH connection is implemented using a client-server model. This means that for an SSH connection to be established, the remote machine must be running a piece of software called an SSH daemon. This software listens for connections on a specific network port, authenticates connection requests, and spawns the appropriate environment if the user provides the correct credentials.

The users computer must have an SSH client. This is a piece of software that knows how to communicate using the SSH protocol and can be given information about the remote host to connect to, the username to use, and the credentials that should be passed to authenticate. The client can also specify certain details about the connection type they would like to establish.

Clients generally authenticate either using passwords (less secure and not recommended) or SSH keys, which are very secure.

Password logins are encrypted and are easy to understand for new users. However, automated bots and malicious users will often repeatedly try to authenticate to accounts that allow password-based logins, which can lead to security compromises. For this reason, we recommend always setting up SSH key-based authentication for most configurations.

SSH keys are a matching set of cryptographic keys which can be used for authentication. Each set contains a public and a private key. The public key can be shared freely without concern, while the private key must be vigilantly guarded and never exposed to anyone.

To authenticate using SSH keys, a user must have an SSH key pair on their local computer. On the remote server, the public key must be copied to a file within the users home directory at~/.ssh/authorized_keys. This file contains a list of public keys, one-per-line, that are authorized to log into this account.

When a client connects to the host, wishing to use SSH key authentication, it will inform the server of this intent and will tell the server which public key to use. The server then check itsauthorized_keysfile for the public key, generate a random string and encrypts it using the public key. This encrypted message can only be decrypted with the associated private key. The server will send this encrypted message to the client to test whether they actually have the associated private key.

Upon receipt of this message, the client will decrypt it using the private key and combine the random string that is revealed with a previously negotiated session ID. It then generates an MD5 hash of this value and transmits it back to the server. The server already had the original message and the session ID, so it can compare an MD5 hash generated by those values and determine that the client must have the private key.

Now that you know how SSH works, we can begin to discuss some examples to demonstrate different ways of working with SSH

This section will cover how to generate SSH keys on a client machine and distribute the public key to servers where they should be used. This is a good section to start with if you have not previously generated keys due to the increased security that it allows for future connections.

Generating a new SSH public and private key pair on your local computer is the first step towards authenticating with a remote server without a password. Unless there is a good reason not to, you should always authenticate using SSH keys.

A number of cryptographic algorithms can be used to generate SSH keys, including RSA, DSA, and ECDSA. RSA keys are generally preferred and are the default key type.

To generate an RSA key pair on your local computer, type:

This prompt allows you to choose the location to store your RSA private key. Press ENTER to leave this as the default, which will store them in the.sshhidden directory in your users home directory. Leaving the default location selected will allow your SSH client to find the keys automatically.

The next prompt allows you to enter a passphrase of an arbitrary length to secure your private key. By default, you will have to enter any passphrase you set here every time you use the private key, as an additional security measure. Feel free to press ENTER to leave this blank if you do not want a passphrase. Keep in mind though that this will allow anyone who gains control of your private key to login to your servers.

If you choose to enter a passphrase, nothing will be displayed as you type. This is a security precaution.

Your identification has been saved in /root/.ssh/id_rsa. Your public key has been saved in /root/.ssh/id_rsa.pub. The key fingerprint is: 8c:e9:7c:fa:bf:c4:e5:9c:c9:b8:60:1f:fe:1c:d3:8a keys randomart image is: +–[ RSA 2048]—-+ + o S . o . * + o + = O . + = = + ….Eo+ +—————–+

This procedure has generated an RSA SSH key pair, located in the.sshhidden directory within your users home directory. These files are:

: The private key. DO NOT SHARE THIS FILE!

: The associated public key. This can be shared freely without consequence.

Generate an SSH Key Pair with a Larger Number of Bits

SSH keys are 2048 bits by default. This is generally considered to be good enough for security, but you can specify a greater number of bits for a more hardened key.

To do this, include the-bargument with the number of bits you would like. Most servers support keys with a length of at least 4096 bits. Longer keys may not be accepted for DDOS protection purposes:

If you had previously created a different key, you will be asked if you wish to overwrite your previous key:

If you choose yes, your previous key will be overwritten and you will no longer be able to log into servers using that key. Because of this, be sure to overwrite keys with caution.

If you have generated a passphrase for your private key and wish to change or remove it, you can do so easily.

Note: To change or remove the passphrase, you must know the original passphrase. If you have lost the passphrase to the key, there is no recourse and you will have to generate a new key pair.

To change or remove the passphrase, simply type:

You can type the location of the key you wish to modify or press ENTER to accept the default value:

Enter the old passphrase that you wish to change. You will then be prompted for a new passphrase:

Here, enter your new passphrase or press ENTER to remove the passphrase.

Each SSH key pair share a single cryptographic fingerprint which can be used to uniquely identify the keys. This can be useful in a variety of situations.

To find out the fingerprint of an SSH key, type:

You can press ENTER if that is the correct location of the key, else enter the revised location. You will be given a string which contains the bit-length of the key, the fingerprint, and account and host it was created for, and the algorithm used:

To copy your public key to a server, allowing you to authenticate without a password, a number of approaches can be taken.

If you currently have password-based SSH access configured to your server, and you have thessh-copy-idutility installed, this is a simple process. Thessh-copy-idtool is included in many Linux distributions OpenSSH packages, so it very likely may be installed by default.

If you have this option, you can easily transfer your public key by typing:

This will prompt you for the user accounts password on the remote system:

The authenticity of host 111.111.11.111 (111.111.11.111) cant be established. ECDSA key fingerprint is fd:fd:d4:f9:77:fe:73:84:e1:55:00:ad:d6:6d:22:fe. Are you sure you want to continue connecting (yes/no)? yes /usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed /usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed — if you are prompted now it is to install the new keys .11.111s password:

After typing in the password, the contents of your~/.ssh/id_rsa.pubkey will be appended to the end of the user accounts~/.ssh/authorized_keysfile:

Number of key(s) added: 1 Now try logging into the machine, with: ssh .11.111 and check to make sure that only the key(s) you wanted were added.

You can now log into that account without a password:

If you do not have thessh-copy-idutility available, but still have password-based SSH access to the remote server, you can copy the contents of your public key in a different way.

You can output the contents of the key and pipe it into thesshcommand. On the remote side, you can ensure that the~/.sshdirectory exists, and then append the piped contents into the~/.ssh/authorized_keysfile:

You will be asked to supply the password for the remote account:

The authenticity of host 111.111.11.111 (111.111.11.111) cant be established. ECDSA key fingerprint is fd:fd:d4:f9:77:fe:73:84:e1:55:00:ad:d6:6d:22:fe. Are you sure you want to continue connecting (yes/no)? yes .11.111s password:

After entering the password, your key will be copied, allowing you to log in without a password:

If you do not have password-based SSH access available, you will have to add your public key to the remote server manually.

On your local machine, you can find the contents of your public key file by typing:

ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAACAQCqql6MzstZYh1TmWWv11q5O3pISj2ZFl9HgH1JLknLLx44+tXfJ7mIrKNxOOwxIxvcBF8PXSYvobFYEZjGIVCEAjrUzLiIxbyCoxVyle7Q+bqgZ8SeeM8wzytsY+dVGcBxF6N4JS+zVk5eMcV385gG3Y6ON3EG112n6d+SMXY0OEBIcO6x+PnUSGHrSgpBgX7Ks1r7xqFa7heJLLt2 demo@test

You can copy this value, and manually paste it into the appropriate location on the remote server. You will have to log into the remote server through other means (like the DigitalOcean web console).

On the remote server, create the~/.sshdirectory if it does not already exist:

Afterwards, you can create or append the~/.ssh/authorized_keysfile by typing:

You should now be able to log into the remote server without a password.

The following section will cover some of the basics about how to connect to a server with SSH.

To connect to a remote server and open a shell session there, you can use thesshcommand.

The simplest form assumes that your username on your local machine is the same as that on the remote server. If this is true, you can connect using:

If your username is different on the remoter server, you need to pass the remote users name like this:

Your first time connecting to a new host, you will see a message that looks like this:

The authenticity of host 111.111.11.111 (111.111.11.111) cant be established. ECDSA key fingerprint is fd:fd:d4:f9:77:fe:73:84:e1:55:00:ad:d6:6d:22:fe. Are you sure you want to continue connecting (yes/no)? yes

Type yes to accept the authenticity of the remote host.

If you are using password authentication, you will be prompted for the password for the remote account here. If you are using SSH keys, you will be prompted for your private keys passphrase if one is set, otherwise you will be logged in automatically.

To run a single command on a remote server instead of spawning a shell session, you can add the command after the connection information, like this:

This will connect to the remote host, authenticate with your credentials, and execute the command you specified. The connection will immediately close afterwards.

By default the SSH daemon on a server runs on port 22. Your SSH client will assume that this is the case when trying to connect. If your SSH server is listening on a non-standard port (this is demonstrated in a later section), you will have to specify the new port number when connecting with your client.

You can do this by specifying the port number with the-poption:

To avoid having to do this every time you log into your remote server, you can create or edit a configuration file in the~/.sshdirectory within the home directory of your local computer.

Edit or create the file now by typing:

In here, you can set host-specific configuration options. To specify your new port, use a format like this:

This will allow you to log in without specifying the specific port number on the command line.

If you have an passphrase on your private SSH key, you will be prompted to enter the passphrase every time you use it to connect to a remote host.

To avoid having to repeatedly do this, you can run an SSH agent. This small utility stores your private key after you have entered the passphrase for the first time. It will be available for the duration of your terminal session, allowing you to connect in the future without re-entering the passphrase.

This is also important if you need to forward your SSH credentials (shown below).

To start the SSH Agent, type the following into your local terminal session:

This will start the agent program and place it into the background. Now, you need to add your private key to the agent, so that it can manage your key:

You will have to enter your passphrase (if one is set). Afterwards, your identity file is added to the agent, allowing you to use your key to sign in without having re-enter the passphrase again.

If you wish to be able to connect without a password to one server from within another server, you will need to forward your SSH key information. This will allow you to authenticate to another server through the server you are connected to, using the credentials on your local computer.

To start, you must have your SSH agent started and your SSH key added to the agent (see above). After this is done, you need to connect to your first server using the-Aoption. This forwards your credentials to the server for this session:

From here, you can SSH into any other host that your SSH key is authorized to access. You will connect as if your private SSH key were located on this server.

This section contains some common server-side configuration options that can shape the way that your server responds and what types of connections are allowed.

If you have SSH keys configured, tested, and working properly, it is probably a good idea to disable password authentication. This will prevent any user from signing in with SSH using a password.

To do this, connect to your remote server and open the/etc/ssh/sshd_configfile with root or sudo privileges:

Inside of the file, search for thePasswordAuthenticationdirective. If it is commented out, uncomment it. Set it to no to disable password logins:

After you have made the change, save and close the file. To implement the changes, you should restart the SSH service.

Now, all accounts on the system will be unable to login with SSH using passwords.

Some administrators suggest that you change the default port that SSH runs on. This can help decrease the number of authentication attempts your server is subjected to from automated bots.

To change the port that the SSH daemon listens on, you will have to log into your remote server. Open thesshd_configfile on the remote system with root privileges, either by logging in with that user or by usingsudo:

Once you are inside, you can change the port that SSH runs on by finding thePort 22specification and modifying it to reflect the port you wish to use. For instance, to change the port to 4444, put this in your file:

Save and close the file when you are finished. To implement the changes, you must restart the SSH daemon.

After the daemon restarts, you will need to authenticate by specifying the port number (demonstrated in an earlier section).

To explicitly limit the user accounts who are able to login through SSH, you can take a few different approaches, each of which involve editing the SSH daemon config file.

On your remote server, open this file now with root or sudo privileges:

The first method of specifying the accounts that are allowed to login is using theAllowUsersdirective. Search for theAllowUsersdirective in the file. If one does not exist, create it anywhere. After the directive, list the user accounts that should be allowed to login through SSH:

Save and close the file. Restart the daemon to implement your changes.

If you are more comfortable with group management, you can use theAllowGroupsdirective instead. If this is the case, just add a single group that should be allowed SSH access (we will create this group and add members momentarily):

Now, you can create a system group (without a home directory) matching the group you specified by typing:

Make sure that you add whatever user accounts you need to this group. This can be done by typing:

Now, restart the SSH daemon to implement your changes.

It is often advisable to completely disable root login through SSH after you have set up an SSH user account that hassudoprivileges.

To do this, open the SSH daemon configuration file with root or sudo on your remote server.

Inside, search for a directive calledPermitRootLogin. If it is commented, uncomment it. Change the value to no:

Save and close the file. To implement your changes, restart the SSH daemon.

There are some cases where you might want to disable root access generally, but enable it in order to allow certain applications to run correctly. An example of this might be a backup routine.

This can be accomplished through the root usersauthorized_keysfile, which contains SSH keys that are authorized to use the account.

Add the key from your local computer that you wish to use for this process (we recommend creating a new key for each automatic process) to the root usersauthorized_keysfile on the server. We will demonstrate with thessh-copy-idcommand here, but you can use any of the methods of copying keys we discuss in other sections:

Now, log into the remote server. We will need to adjust the entry in theauthorized_keysfile, so open it with root or sudo access:

At the beginning of the line with the key you uploaded, add acommand=listing that defines the command that this key is valid for. This should include the full path to the executable, plus any arguments:

Save and close the file when you are finished.

Now, open thesshd_configfile with root or sudo privileges:

Find the directivePermitRootLogin, and change the value toforced-commands-only. This will only allow SSH key logins to use root when a command has been specified for the key:

Save and close the file. Restart the SSH daemon to implement your changes.

The SSH daemon can be configured to automatically forward the display of X applications on the server to the client machine. For this to function correctly, the client must have an X windows system configured and enabled.

To enable this functionality, log into your remote server and edit thesshd_configfile as root or with sudo privileges:

Search for theX11Forwardingdirective. If it is commented out, uncomment it. Create it if necessary and set the value to yes:

Save and close the file. Restart your SSH daemon to implement these changes.

To connect to the server and forward an applications display, you have to pass the-Xoption from the client upon connection:

Graphical applications started on the server through this session should be displayed on the local computer. The performance might be a bit slow, but it is very helpful in a pinch.

In the next section, well focus on some adjustments that you can make on the client side of the connection.

On your local computer, you can define individual configurations for some or all of the servers you connect to. These can be stored in the~/.ssh/configfile, which is read by your SSH client each time it is called.

Create or open this file in your text editor on your local computer:

Inside, you can define individual configuration options by introducing each with aHostkeyword, followed by an alias. Beneath this and indented, you can define any of the directives found in thessh_configman page:

You could then connect toon port 4444 using the username demo by simply typing:

You can also use wildcards to match more than one host. Keep in mind that later matches can override earlier ones. Because of this, you should put your most general matches at the top. For instance, you could default all connections to not allow X forwarding, with an override forexample.comby having this in your file:

Save and close the file when you are finished.

If you find yourself being disconnected from SSH sessions before you are ready, it is possible that your connection is timing out.

You can configure your client to send a packet to the server every so often in order to avoid this situation:

On your local computer, you can configure this for every connection by editing your~/.ssh/configfile. Open it now:

If one does not already exist, at the top of the file, define a section that will match all hosts. Set theServerAliveIntervalto 120 to send a packet to the server every two minutes. This should be enough to notify the server not to close the connection:

Save and close the file when you are finished.

By default, whenever you connect to a new server, you will be shown the remote SSH daemons host key fingerprint.

The authenticity of host 111.111.11.111 (111.111.11.111) cant be established. ECDSA key fingerprint is fd:fd:d4:f9:77:fe:73:84:e1:55:00:ad:d6:6d:22:fe. Are you sure you want to continue connecting (yes/no)? yes

This is configured so that you can verify the authenticity of the host you are attempting to connect to and spot instances where a malicious user may be trying to masquerade as the remote host.

In certain circumstances, you may wish to disable this feature.Note: This can be a big security risk, so make sure you know what you are doing if you set your system up like this.

To make the change, the open the~/.ssh/configfile on your local computer:

If one does not already exist, at the top of the file, define a section that will match all hosts. Set theStrictHostKeyCheckingdirective to no to add new hosts automatically to theknown_hostsfile. Set theUserKnownHostsFileto/dev/nullto not warn on new or changed hosts:

You can enable the checking on a case-by-case basis by reversing those options for other hosts. The default forStrictHostKeyCheckingis ask:

Host * StrictHostKeyChecking no UserKnownHostsFile /dev/null Host testhost HostName

StrictHostKeyChecking ask UserKnownHostsFile /home/

Multiplexing SSH Over a Single TCP Connection

There are situations where establishing a new TCP connection can take longer than you would like. If you are making multiple connections to the same machine, you can take advantage of multiplexing.

SSH multiplexing re-uses the same TCP connection for multiple SSH sessions. This removes some of the work necessary to establish a new session, possibly speeding things up. Limiting the number of connections may also be helpful for other reasons.

To set up multiplexing, you can manually set up the connections, or you can configure your client to automatically use multiplexing when available. We will demonstrate the second option here.

To configure multiplexing, edit your SSH clients configuration file on your local machine:

If you do not already have a wildcard host definition at the top of the file, add one now (asHost *). We will be setting theControlMaster,ControlPath, andControlPersistvalues to establish our multiplexing configuration.

TheControlMastershould be set to auto in able to automatically allow multiplexing if possible. TheControlPathwill establish the path to control socket. The first session will create this socket and subsequent sessions will be able to find it because it is labeled by username, host, and port.

Setting theControlPersistoption to 1 will allow the initial master connection to be backgrounded. The 1 specifies that the TCP connection should automatically terminate one second after the last SSH session is closed:

Save and close the file when you are finished. Now, we need to actually create the directory we specified in the control path:

Now, any sessions that are established with the same machine will attempt to use the existing socket and TCP connection. When the last session exists, the connection will be torn down after one second.

If for some reason you need to bypass the multiplexing configuration temporarily, you can do so by passing the-Sflag with none:

Tunneling other traffic through a secure SSH tunnel is an excellent way to work around restrictive firewall settings. It is also a great way to encrypt otherwise unencrypted network traffic.

SSH connections can be used to tunnel traffic from ports on the local host to ports on a remote host.

A local connection is a way of accessing a network location from your local computer through your remote host. First, an SSH connection is established to your remote host. On the remote server, a connection is made to an external (or internal) network address provided by the user and traffic to this location is tunneled to your local computer on a specified port.

This is often used to tunnel to a less restricted networking environment by bypassing a firewall. Another common use is to access a localhost-only web interface from a remote location.

To establish a local tunnel to your remote server, you need to use the-Lparameter when connecting and you must supply three pieces of additional information:

The local port where you wish to access the tunneled connection.

The host that you want your remote host to connect to.

The port that you want your remote host to connect on.

These are given, in the order above (separated by colons), as arguments to the-Lflag. We will also use the-fflag, which causes SSH to go into the background before executing and the-Nflag, which does not open a shell or execute a program on the remote side.

For instance, to connect toexample.comon port 80 on your remote host, making the connection available on your local machine on port 8888, you could type:

Now, if you point your local web browser to127.0.0.1:8888, you should see whatever content is atexample.comon port 80.

A more general guide to the syntax is:

Since the connection is in the background, you will have to find its PID to kill it. You can do so by searching for the port you forwarded:

You can then kill the process by targeting the PID, which is the number in the second column of the line that matches your SSH command:

Another option is to start the connectionwithoutthe-fflag. This will keep the connection in the foreground, preventing you from using the terminal window for the duration of the forwarding. The benefit of this is that you can easily kill the tunnel by typing CTRL-C.

SSH connections can be used to tunnel traffic from ports on the local host to ports on a remote host.

In a remote tunnel, a connection is made to a remote host. During the creation of the tunnel, aremoteport is specified. This port, on the remote host, will then be tunneled to a host and port combination that is connected to from the local computer. This will allow the remote computer to access a host through your local computer.

This can be useful if you need to allow access to an internal network that is locked down to external connections. If the firewall allows connectionsoutof the network, this will allow you to connect out to a remote machine and tunnel traffic from that machine to a location on the internal network.

To establish a remote tunnel to your remote server, you need to use the-Rparameter when connecting and you must supp