Skip to main content
  1. Tutorials/

How To Use SSH to Connect to a Remote Server

Tutorials Linux Basics Networking Security System Tools Ubuntu
Introduction>

Introduction #

One essential tool to master as a system administrator is SSH.
SSH, or Secure Shell, is a protocol used to securely log onto remote systems. It is the most common way to access remote Linux servers.
In this guide, we will discuss how to use SSH to connect to a remote system.

Deploy your frontend applications from GitHub using DigitalOcean App Platform. Let DigitalOcean focus on scaling your app.

Core Syntax>

Core Syntax #

To connect to a remote system using SSH, we’ll use the ssh command.
If you are using Windows, you’ll need to install a version of OpenSSH in order to be able to ssh from a terminal. If you prefer to work in PowerShell, you can follow Microsoft’s documentation to add OpenSSH to PowerShell. If you would rather have a full Linux environment available, you can set up WSL, the Windows Subsystem for Linux, which will include ssh by default. Finally, as a lightweight third option, you can install Git for Windows, which provides a native Windows bash terminal environment that includes the ssh command. Each of these are well-supported and whichever you decide to use will come down to preference.
If you are using a Mac or Linux, you will already have the ssh command available in your terminal.
The most straightforward form of the command is:

ssh remote_host

The remote_host in this example is the IP address or domain name that you are trying to connect to.
This command assumes that your username on the remote system is the same as your username on your local system.
If your username is different on the remote system, you can specify it by using this syntax:

ssh remote_username@remote_host

Once you have connected to the server, you may be asked to verify your identity by providing a password. Later, we will cover how to generate keys to use instead of passwords.
To exit the ssh session and return back into your local shell session, type:

exit

How Does SSH Work?>

How Does SSH Work? #

SSH works by connecting a client program to an ssh server, called sshd.
In the previous section, ssh was the client program. The ssh server was already running on the remote_host that we specified.
On nearly all Linux environments, the sshd server should start automatically. If it is not running for any reason, you may need to temporarily access your server through a web-based console, or local serial console.
The process needed to start an ssh server depends on the distribution of Linux that you are using.
On Ubuntu, you can start the ssh server by typing:

sudo systemctl start ssh

That should start the sshd server and you can then log in remotely.

How To Configure SSH>

How To Configure SSH #

When you change the configuration of SSH, you are changing the settings of the sshd server.
In Ubuntu, the main sshd configuration file is located at /etc/ssh/sshd_config.
Back up the current version of this file before editing:

sudo cp /etc/ssh/sshd_config{,.bak}

Open it using nano or your favourite text editor:

sudo nano /etc/ssh/sshd_config

You will want to leave most of the options in this file alone. However, there are a few you may want to take a look at:
/etc/ssh/sshd_config

Port 22

The port declaration specifies which port the sshd server will listen on for connections. By default, this is 22. You should probably leave this setting alone, unless you have specific reasons to do otherwise. If you do change your port, we will show you how to connect to the new port later on.
/etc/ssh/sshd_config

HostKey /etc/ssh/ssh_host_rsa_key
HostKey /etc/ssh/ssh_host_dsa_key
HostKey /etc/ssh/ssh_host_ecdsa_key

The host keys declarations specify where to look for global host keys. We will discuss what a host key is later.
/etc/ssh/sshd_config

SyslogFacility AUTH
LogLevel INFO

These two items indicate the level of logging that should occur.
If you are having difficulties with SSH, increasing the amount of logging may be a good way to discover what the issue is.
/etc/ssh/sshd_config

LoginGraceTime 120
PermitRootLogin yes
StrictModes yes

These parameters specify some of the login information.
LoginGraceTime specifies how many seconds to keep the connection alive without successfully logging in.
It may be a good idea to set this time just a little bit higher than the amount of time it takes you to log in normally.
PermitRootLogin selects whether the root user is allowed to log in.
In most cases, this should be changed to no when you have created a user account that has access to elevated privileges (through su or sudo) and can log in through ssh, in order to minimize the risk of anyone gaining root access to your server.
strictModes is a safety guard that will refuse a login attempt if the authentication files are readable by everyone.
This prevents login attempts when the configuration files are not secure.
/etc/ssh/sshd_config

X11Forwarding yes
X11DisplayOffset 10

These parameters configure an ability called X11 Forwarding. This allows you to view a remote system’s graphical user interface (GUI) on the local system.
This option must be enabled on the server and given with the SSH client during connection with the -X option.
After making your changes, save and close the file. If you are using nano, press Ctrl+X, then when prompted, Y and then Enter.
If you changed any settings in /etc/ssh/sshd_config, make sure you reload your sshd server to implement your modifications:

sudo systemctl reload ssh

You should thoroughly test your changes to ensure that they operate in the way you expect.
It may be a good idea to have a few terminal sessions open while you are making changes. This will allow you to revert the configuration if necessary without locking yourself out.

How To Log Into SSH with Keys>

How To Log Into SSH with Keys #

While it is helpful to be able to log in to a remote system using passwords, it is faster and more secure to set up key-based authentication.

How Does Key-based Authentication Work?>

How Does Key-based Authentication Work? #

Key-based authentication works by creating a pair of keys: a private key and a public key.
The private key is located on the client machine and is secured and kept secret.
The public key can be given to anyone or placed on any server you wish to access.
When you attempt to connect using a key-pair, the server will use the public key to create a message for the client computer that can only be read with the private key.
The client computer then sends the appropriate response back to the server and the server will know that the client is legitimate.
This process is performed automatically after you configure your keys.

How To Create SSH Keys>

How To Create SSH Keys #

SSH keys should be generated on the computer you wish to log in from. This is usually your local machine.
Enter the following into the command line:

ssh-keygen -t rsa

You may be prompted to set a password on the key files themselves, but this is a fairly uncommon practice, and you should press enter through the prompts to accept the defaults. Your keys will be created at ~/.ssh/id_rsa.pub and ~/.ssh/id_rsa.
Change into the .ssh directory by typing:

cd ~/.ssh

Look at the permissions of the files:

ls -l

-rw-r--r-- 1 demo demo  807 Sep  9 22:15 authorized_keys
-rw------- 1 demo demo 1679 Sep  9 23:13 id_rsa
-rw-r--r-- 1 demo demo  396 Sep  9 23:13 id_rsa.pub

As you can see, the id_rsa file is readable and writable only to the owner. This helps to keep it secret.
The id_rsa.pub file, however, can be shared and has permissions appropriate for this activity.

How To Transfer Your Public Key to the Server>

How To Transfer Your Public Key to the Server #

If you currently have password-based access to a server, you can copy your public key to it by issuing this command:

ssh-copy-id remote_host

This will start an SSH session. After you enter your password, it will copy your public key to the server’s authorized keys file, which will allow you to log in without the password next time.

Client-Side Options>

Client-Side Options #

There are a number of optional flags that you can provide when connecting through SSH.
Some of these may be necessary to match the settings in the remote host’s sshd configuration.
For instance, if you changed the port number in your sshd configuration, you will need to match that port on the client-side by typing:

ssh -p port_number remote_host

Note: Changing your ssh port is a reasonable way of providing security through obscurity. If you are allowing ssh connections to a widely known server deployment on port 22 as normal, and you have password authentication enabled, you will likely be attacked by many automated login attempts. Exclusively using key-based authentication and running ssh on a nonstandard port is not the most complex security solution you can employ, but should reduce these to a minimum.

If you only want to execute a single command on a remote system, you can specify it after the host like so:

ssh remote_host command_to_run

You will connect to the remote machine, authenticate, and the command will be executed.
As we said before, if X11 forwarding is enabled on both computers, you can access that functionality by typing:

ssh -X remote_host

Providing you have the appropriate tools on your computer, GUI programs that you use on the remote system will now open their window on your local system.

Disabling Password Authentication>

Disabling Password Authentication #

If you have created SSH keys, you can enhance your server’s security by disabling password-only authentication. Apart from the console, the only way to log into your server will be through the private key that pairs with the public key you have installed on the server.

Warning: Before you proceed with this step, be sure you have installed a public key to your server. Otherwise, you will be locked out!

As root or user with sudo privileges, open the sshd configuration file:

sudo nano /etc/ssh/sshd_config

Locate the line that reads Password Authentication, and uncomment it by removing the leading #. You can then change its value to no:
/etc/ssh/sshd_config

PasswordAuthentication no

Two more settings that should not need to be modified (provided you have not modified this file before) are PubkeyAuthentication and ChallengeResponseAuthentication. They are set by default, and should read as follows:
/etc/ssh/sshd_config

PubkeyAuthentication yes
ChallengeResponseAuthentication no

After making your changes, save and close the file.
You can now reload the SSH daemon:

sudo systemctl reload ssh

Password authentication should now be disabled, and your server should be accessible only through SSH key authentication.

Conclusion>

Conclusion #

Learning your way around SSH will greatly benefit any of your future cloud computing endeavours. As you use the various options, you will discover more advanced functionality that can make your life easier. SSH has remained popular because it is secure, light-weight, and useful in diverse situations.
Next, you may want to learn about working with SFTP to perform command line file transfers.