SSH is the most secure cryptographic network communication to establish a local or remote connection between two machines. It is the widely used method to share data between remote servers. To connect via SSH service, SSH must be enabled on both the client and remote servers. It allows access to the remote terminal to execute the commands from the local server.
This guide will cover the steps to install and enable the SSH service in Ubuntu by utilizing these helping content:
- Installing and Enabling SSH Server on Ubuntu 22.04
- Optional: Disabling SSH Service on Ubuntu 22.04
How To Install and Enable SSH Server on Ubuntu 22.04?
You must follow these easy steps to install and enable SSH on Ubuntu 22.04.
Step 1: Update Default apt Packages List
Before installing the SSH server, it’s best practice to update the default packages in the system repository to the latest versions. To update the packages in the default repository, run this command:
$ sudo apt update
The packages in the system default repository are updated.
Step 2: Install OpenSSH Server
To connect via the SSH service, the OpenSSH server must be installed and enabled in client and remote systems. For installing the OpenSSH server in Ubuntu 22.04, execute this command:
$ sudo apt install openssh-server -y
The Open SSH server is installed in Ubuntu 22.04.
Step 3: Start and Enable SSH
To use the SSH service, you must start it. To start the SSH service, run this command:
$ sudo systemctl start ssh
After starting the SSH service; it must be enabled to use the SSH service. To enable SSH service in Ubuntu, execute the following command:
$ sudo systemctl enable ssh --now
The SSH is enabled.
Step 4: Verify the SSH Service Current Status
To verify that the SSH service is enabled and working in the system, run this command:
$ sudo systemctl status ssh
The output shows that the SSH service is currently active.
Step 5: Enable ufw Firewall
The ufw (uncomplicated firewall) controls the SSH traffic. To create the connection, the ufw should be enabled. To enable the ufw in Ubuntu, run the below-stated command:
$ sudo ufw enable
The ufw firewall is enabled.
Step 6: Enable ufw On Specific Port
Now, we must allow SSH traffic from a specific port. The SSH default port is 22. To allow SSH through port 22 and reload the service, use this command:
$ sudo ufw allow 22 && sudo ufw reload
SSH service will connect through port 22.
Step 7: Connect to Remote Machine via SSH
Now, we can connect to the local or remote server using the SSH service. The general syntax to connect via the SSH service is given below:
$ ssh <username>@<ip-address>
The SSH syntax details are as follows:
- ssh: It uses the SSH service.
- username: Put the desired destination server username.
- ip-address: Put the destination server IP address.
To connect the remote server via SSH having username “theo” and IP address “192.168.141.133”, execute the below command:
$ ssh email@example.com
The error-free output shows that the user is connected to a remote server, which can be verified from the terminal command prompt syntax. The terminal command prompt syntax is changed from “theo@mylinuxblog” to “theo@ubuntu”.
Optional: Disabling SSH Service on Ubuntu 22.04
Disabling the SSH will not allow the servers to connect via the SSH service. For disabling the SSH service in Ubuntu, use this command:
$ sudo systemctl disable ssh --now
The SSH is disabled.
To stop the SSH service, run the below-written command:
$ sudo systemctl stop ssh --now
$ sudo systemctl status ssh --now
To remove the OpenSSH server package in Ubuntu 22.04, execute the below-stated command:
$ sudo apt remove openssh-server
The OpenSSH server package is removed from the system.
The SSH service connects the two machines, which can be installed using the “sudo apt install openssh-server -y” command and can be enabled by running the “sudo systemctl enable ssh –now” command. The ufw firewall should be configured to allow the SSH traffic to connect to the SSH server. Moreover, the methods to disable, stop and remove the SSH service are also mentioned in this guide.