How To Install and Enable SSH on Ubuntu

Security is paramount in all Linux environments when connecting a client to a server. SSH, or Secure Shell, is the go-to method for ensuring secure connections. It empowers Linux users to remotely access and control servers from anywhere with robustness and effectiveness. 

This guide demonstrates how to enable SSH on Ubuntu distribution. In addition, we will explore how to enable SSH on Ubuntu. Let’s dive into the details about SSH!

What Is SSH?

SSH, or Secure Shell, is a client-server technology commonly used for securely logging into a remote Linux/Unix server and executing commands. The SSH daemon (sshd) runs on TCP port 22 on Ubuntu Linux. It replaces older, insecure protocols like rlogin, rsh, telnet, FTP, and others by encrypting all communication and providing various authentication methods to prevent attacks. SSH also ensures that sensitive information remains encrypted during transmission, making it safe to transfer data and run commands remotely.

Prerequisites (How To Enable SSH on Ubuntu?)

Before proceeding with the steps to install and enable SSH on your Ubuntu system, make sure you meet the following requirements:

  • You must have permission to access the remote computer you want to connect with.
  • Ensure you have an active internet connection to download the necessary packages.
  • There should be at least one other system accessible in your network, such as another computer on your local area network, a remote server connected via the internet, or a locally running virtual machine.
  • Access to the Terminal application with sudo or root privileges.

How To Install SSH on Ubuntu? (Step-By-Step Guide)

To install SSH on Ubuntu, follow the below steps:

Step 1: Update and Upgrade the Apt Packages List

First, Open the terminal application. It is recommended that the apt packages list be updated before installing any new application. Therefore, execute the following command to update your system packages on Ubuntu:

$ sudo apt-get update

After updating the packages list, upgrade installed packages to their latest versions by executing the ‘apt upgrade’ command:

$ sudo apt-get upgrade

Step 2: Install SSH on Ubuntu

To use SSH on your system, you need to install the ‘OpenSSH-client’ package on your machine. Therefore, you can install SSH on Ubuntu by running the below command:

$ sudo apt-get install openssh-client

how to install and enable ssh on ubuntu

Step 3: Install SSH Server

After installing the SSH client, the next step is to allow incoming connections for SSH sessions. To do this, install the OpenSSH server package on your Ubuntu system by executing the following command:

$ sudo apt install openssh-server

Once all steps are completed, you can use the SSH command to connect with a remote system. To do this, execute the following command inside the client terminal:

$ ssh user@server-ip

How To Enable SSH on Ubuntu?

To activate or enable SSH on Ubuntu, execute the following command:

$ sudo systemctl enable ssh

Next, to ensure access through the firewall, enable UFW and allow SSH traffic on port 22:

$ sudo ufw allow ssh

With these steps completed, congratulations! You've successfully set up and enabled SSH on your Ubuntu server, allowing you to securely connect using the SSH client.

How To Manage SSH Service on Ubuntu?

Like other applications, you can start, stop, and restart SSH service on your Ubuntu system. To manage the SSH server service on Ubuntu, you can use the systemctl command with various options:

To start the SSH service, use:

$ sudo systemctl start ssh

This command initiates the SSH server, enabling it to accept incoming connections.

To stop the SSH service, use:

$ sudo systemctl stop ssh

This command halts the SSH server, terminating all existing connections and preventing new ones.

To restart the SSH service, use:

$ sudo systemctl restart ssh

This above command stops and again starts the SSH server, effectively refreshing its configuration and connections.

To check the current status of the SSH service, use:

$ sudo systemctl status ssh

how to install and enable ssh on ubuntu

This command provides detailed information about the SSH service, including whether it's running, any errors encountered, and the current configuration status.

How To Connect to a Remote Server Using SSH?

Once SSH is enabled on your Ubuntu system, you can log into a remote system using the Terminal. Open the Terminal using the keyboard shortcut "CTRL+ALT+T" and enter the following command:

$ ssh username@public_IP 

Replace "username" with your actual username and "public_IP" with the IP address of the Ubuntu system where SSH is installed.

If you're unsure of the IP address, you can find it quickly using the command:

$ ip a

Look for the public IP address displayed in the output.

After entering the required information, you'll be logged into the server, allowing you to manage it securely from your workstation.

How to Configure a Firewall to the SSH Port on Ubuntu

UFW is a command-line tool to manage firewall settings on Linux systems, particularly Ubuntu. It simplifies the process of configuring firewall rules by providing an easy-to-use interface. With UFW, users can define rules to allow or block traffic to specific ports and control incoming and outgoing connections without needing in-depth networking knowledge. To configure your firewall and allow traffic through a specific port, such as port 2244, you can use the UFW firewall configuration tool. Here's the syntax to allow traffic on port 2244:

$ sudo ufw allow 2244/tcp

This command permits TCP traffic from any source to any destination on port 22.

It's important to note that if you're using the default SSH port, which is 22, there's no need to change the port number. The default SSH port, 22, is the standard port used for SSH connections. If you haven't changed the default port, you can connect to your SSH server without specifying a port number. Here, Port 2244 is used as an example and is mentioned in the SSH configuration section.

How To Disable and Remove the SSH Server on Ubuntu?

You may not need the SSH server anymore or may want to remove or disable it for some reason. The following commands will help you with that:

To temporarily stop or disable SSH on Ubuntu, execute the below command:

$ sudo systemctl stop ssh

After rebooting your system, you can easily disable SSH using this command:

$ sudo systemctl disable ssh
To remove SSH from Ubuntu:
$ sudo apt-get remove opnessh-server

Ensure that the firewall rule that opened TCP port 22 for SSH has been removed using the following syntax:

$ sudo ufw delete allow ssh

Conclusion

In this article, we demonstrated how to enable SSH on Ubuntu distribution. We have also provided a step-by-step installation guide to install SSH on Ubuntu and configure firewall settings. 

BlueVPS offers a dedicated environment that is simple, scalable, and customizable, with unlimited traffic. If you are using a Linux VPS server, you can follow these steps to secure it with SSH. After following the above instructions, you can securely administer your machine's command prompt as an administrator using the SSH protocol. However, implement appropriate security measures to prevent unauthorized access to your system.