How to Use SSH to Connect to a Remote Server in Linux

  Use code KB4KDO0L9 to receive a 10% recurring discount on any server.

ssh letters with arrows pointing to windows logo and linux penguin

Just as we use remote controls for our devices that are a few feet away, remote logins allow people to work from anywhere by accessing a remote server. Nowadays, we can hardly imagine a world where we can’t control computers from remote locations. The two most used protocols to establish a connection with a remote machine are Remote Desktop Protocol (RDP) for Windows-based machines, or Secure Shell (SHH) for Linux-based machines. To establish a remote connection, these two protocols use client and server applications. Once you establish a connection with a remote machine, you can do anything you would if you were sitting in front of the machine, for instance: transfer files, access and manage other computers, and so on.

Requirements for SSH

However, there are some necessary conditions to fulfill to use an ssh command: you need to have a network connection, and the remote computer needs to be turned on all of the time; you need to install and enable the client and server applications; you need the name or the IP address of the remote machine you are trying to connect to; you need to have the required permission to access the remote server, and the remote connection needs to be allowed by the firewall settings.

Terminology

Before we jump into the SSH program, it is essential to clear up the terminology used in the IT community related to this issue.

Remote: A remote computer is a computer that is in a remote location, and you’re not physically using it.   

Local: A local machine is a computer you’re currently using and can be accessed without a network. The local port is the port number on the local computer. Unlike the local port, the remote port is the port number on the remote computer.

 Some other terms you might want to look into before going into SSH are: ssh config file, ssh command line options, remote command, authentication agent connection forwarding, local forwarded ports, public key file format, transport layer, debug logs, securely publish, ssh client program, per user configuration file, remote computer, generic message exchange authentication, cipher specification, transport layer protocol, print debugging messages, and how to do ssh from windows to Linux.

What is SSH?

SSH, also known as Secure Socket Shell, is a protocol that gives users a secure way to access a computer. SSH is used by anyone who needs to remotely manage a computer in a secure manner, as well as system and network administrators. Secure Shell SSH provides public key authentication and password authentication, and encrypted data communications between the two computers connected over an open network. Secure shell protocol connections are encrypted using symmetric encryption, asymmetric encryption, and hashing.

Each SSH has a key pair. The ssh key pair includes a public and private key. Ssh keygen is a crucial part, since ssh keygen is a tool for creating new authentication key pairs. To provide a public key, each user account in your system must generate a public key if they don’t already have one. One of the ssh keys, the private key, is a secret, and it is stored on the computer you use to connect. The host key is used as one of the authentication methods. The ssh protocol provides strong encryption, authentication protocol methods like public key authentication, and a secure connection. The ssh protocol uses ssh utilities for managing applications and systems remotely, logging in to another computer, moving files from one computer to another, and executing commands. SSH tunneling or port forwarding creates an encrypted SSH key connection between a server and a client through which service ports can be relayed. Port forwarding can be used for going through firewalls. Port forwarding can also be used for adding encryption to the legacy application. Some people use port forwarding for opening backdoors into the internal network.

ssh text with arrows going through

History of SSH

SSH’s first version emerged in 1995. It was designed by a researcher at the Helsinki University of Technology named Tatu Ylönen.

Over the years, some flaws were found in the initial version of SSH, called SSH-1, which is why this version is now considered to be not safe to use and deprecated. The current version of SSH, called SSH-2, uses a stronger integrity check with ssh authentication codes and a Diffie-Hellman key exchange to improve security.

SSH Linux Based Machines

One of the most attractive features of Linux is the ability to use a computer without entering commands into the keyboard and to do that on any computer in the world. Although establishing an SSH connection is a daily task for many Linux users, it can be a little intimidating for anyone who hasn’t tried it yet. Mac OS allows you to easily and quickly connect to a server with its built-in SSH client called Terminal. Read more if you want to find out How to SSH on a Mac. There are different types of ssh clients. With Putty, you can create a virtual private network. At ServerMania, we offer Linux Dedicated Servers.

How to Enable an SSH Connection

To enable an SSH connection, we first need to know how SSH works. To establish an SSH connection, you need a client machine and an agreeing server-side component. SSH enables a secure connection between these two. The application you install on the computer and connect to another computer is called an SSH client.

The client uses the remote host information to start the connection. It establishes the encrypted connection if the provided credentials or the remote hosts are verified. To enable a connection, you need to ensure that both the client computer and a server component are installed on the local machine and the remote machine. OpenSSH is an open-source SSH too, and installing it is relatively easy.

SSH Command Examples and Options

If you're wondering how to SSH connect to remote computers, we’re going to look at some of the basic SSH commands:

  • ls – show directory contents;
  • cd – change directory;
  • touch – create a new file;
  • mkdir – create a new folder;
  • cat – show contents of a file;

pwd – show current directory;

ssh on keyboard

How to Install an OpenSSH Client

Because many Linux ssh distributions already have an SSH client, you need to make sure that an SSH client isn’t already installed. You can easily establish remote connections with SSH. To check if an OpenSSH client is available on your system that is Linux-based, the SSH Linux command is CTRL+ALT+T on your keyboard or search for “terminal”. Then you type in ssh and press Enter in the terminal. In case the OpenSSH client is installed, you will get the following response:

username@host:~$ ssh 
usage: ssh [-1246AaCfGgKkMNnqsTtVvXxYy] [-b bind_address]
[-c cipher_spec]
[-D [bind_address:]port] [-E log_file] [-e escape_char]
[-F configfile] [-I pkcs11] [-i identity_file]
[-J [user@]host[:port]] [-L address] [-l login_name] [-m
mac_spec] [-O ctl_cmd] [-o option]
[-p port] [-Q query_option] [-R address] [-S ctl_path]
[-W host:port] [-w local_tun[:remote_tun]]
[user@]hostname [command]
username@host:~$

If you don’t receive this message, it means that you will need to install the openSSH client. To install OpenSSH client on your computer, run the following ssh command:

sudo apt-get install openssh-client

Then, when asked, type in your superuser password and hit Enter. Now, you are able to SSH into any machine if you have the needed privileges to gain ssh access to it. Verbose mode provides additional detail about what software and drivers the coputer is loading during startup.

How to Install an OpenSSH Server

A machine needs to have the server-side part of the SSH software in order to accept SSH connection. When started, the OPEN SSH server reads a configuration named /etc/ssh/sshd_config. The /etc/ssh/sshd_config file allows you to set options that modify the operation of the daemon.

You can try to connect to the local host, if you want to check if OpenSSH server is available on the remote host that needs to accept SSH connections, more specifically, on its Ubuntu system. You can do this by opening the terminal on the server machine and pressing CTRL+ALT+T on your keyboard or searching for “terminal”. Then, you type in ssh localhost and press enter. The response for the remote system without the SSH server will look like this:

username@host:~$ ssh localhost 
ssh: connect to host localhost port 22: Connection refused username@host:~$

If you don’t have an OpenSSH server installed, you will need to install it by running the following command:

sudo apt-get install openssh-server ii.

Then, when asked, you will type in your superuser password and press Enter and Y to allow the installation to continue. You can check if the SSH server is running on the machine by typing in:

sudo service ssh status

If the SSH service is running properly, the response should look like this:

username@host:-$ sudo service ssh status
ssh.service - OpenBSD Secure Shell server
Loaded: loaded (/lib/systemd/system/ssh.service; enabled; vendor preset: enab
Active: active (running) since Fr 2018-03-12 10:53:44 CET; 1min 22s ago Process:
1174 ExecReload=/bin/kill -HUP $MAINPID (code=exited, status=0/SUCCES
Main PID: 3165 (sshd)

There is also another way to check if you installed the OpenSSH server properly, and that is by running the locahost ssh command in your terminal prompt again. The response should look like this:

username@host:~$ ssh localhost 
The authenticity of host 'localhost (127.0.0.1)' can't be established. ECDSA key fingerprint is SHA256:9jqmhko9Yo1EQAS1QeNy9xKceHFG5F8W6kp7EX9U3Rs. Are you sure you want to continue connecting (yes/no)? yes Warning: Permanently added 'localhost' (ECDSA) to the list of known hosts.
username@host:~$

Then, to continue, you just need to enter yes or y, and you’ve successfully set up your server to accept a SSH connection.

Connecting to an SSH Server

We will use the ssh command in order to connect to a remote machine using SSH.

To connect to a remote machine, the most basic form of this ssh command is:

ssh remote_host

The remote_host is an example of the domain name or the IP address that you want to connect to. You can use this command if the username on your local system is the same as the username on the remote user. You can use the following basic syntax for the ssh command if the remote machine has a different username:

ssh remote_username@remote_host

You might be asked to provide a password to verify your identity once you’re connected to the server. To return to your local shell session and exit the ssh session, all you need to do is type:

exit


Complete Digital Server Solutions For All

Bare Metal

Dedicated Servers

A single tenant, physical server allowing you full access to its resources.

Professional-grade

Hybrid Servers

Virtualized server platform hosted on enterprise-grade physical servers.

Affordable & Secure

Cloud Servers

High-performance and highly-available infrastructure, delivering a reliable, secure, and scalable solution.