Enabling SSH on an Ubuntu server is a fundamental task for any system administrator or developer managing remote infrastructure. Secure Shell provides a robust and encrypted method to access your machine, eliminating the need for physical presence or insecure alternatives like Telnet. This guide walks through the precise steps required to install, configure, and secure your SSH service, ensuring you maintain reliable and safe access to your Ubuntu environment.
Understanding the SSH Ecosystem on Ubuntu
Before diving into the configuration, it is essential to understand the components involved in the process. The server-side component is the `openssh-server` package, which listens for incoming connection requests. The client-side, `openssh-client`, is typically pre-installed on other Ubuntu machines or laptops you might use to initiate the connection. The configuration file, located at `/etc/ssh/sshd_config`, is the central control panel for how your SSH daemon behaves. Grasping this architecture helps in troubleshooting and customizing the setup beyond the basic installation.
Installing the OpenSSH Server Package
The first concrete step to allow SSH is to install the server package on your Ubuntu machine. The OpenSSH server is not installed by default on minimal server installations or cloud images. You will need to update your local package index to ensure you get the latest version available from the repositories. Following the update, you can install the server component using the standard package manager.
Step-by-step installation commands
To install the server, open your terminal and execute the following commands with superuser privileges. First, update the package list to refresh the repository information:
sudo apt update
After the index is updated, install the OpenSSH server package. The system will download the necessary files and set up the service automatically:
sudo apt install openssh-server
Managing the SSH Service
Once the installation is complete, the SSH service should start automatically. However, you need to verify that it is running correctly and ensure it will start on boot. Ubuntu uses `systemd` to manage services, providing a consistent interface to control processes. You do not need to manually start the daemon if the installation finished without errors, but checking its status is a critical validation step.
Verifying service status and enabling boot start
Use the following command to check if the SSH daemon is active and listening on port 22:
sudo systemctl status ssh
If the output indicates the service is "active (running)", you are successfully allowing SSH connections. To ensure the service starts automatically after a server reboot, enable it with the command:
sudo systemctl enable ssh
Configuring the Firewall for SSH Access
With the server running, you must ensure the network firewall allows traffic on the default SSH port, which is 22. Ubuntu often uses `UFW` (Uncomplicated Firewall) as its frontend, providing a user-friendly way to manage rules. By default, enabling UFW might block all incoming traffic, including your SSH connections. You must create an exception before you lock yourself out of the server.
Allowing SSH through UFW
First, check the application profiles recognized by UFW:
sudo ufw app list
You should see an "OpenSSH" profile. To allow SSH connections, apply this profile:
sudo ufw allow OpenSSH
After configuring the rule, you can enable the firewall if it was previously disabled. The connection will remain intact because the rule was applied before enabling the strict policy.