Secure Shell (SSH) is the standard method for remotely managing Linux servers. By default, the Ubuntu SSH daemon (sshd) is relatively secure, but it is configured for maximum compatibility rather than maximum security. If your server is exposed to the public internet, it will be subjected to automated brute-force attacks within minutes of going live. Hardening your SSH configuration is an essential first step in securing any Ubuntu deployment.
Step 1: Disable Root Login
The most common target for brute-force attacks is the root user, as every Linux system has one. You should never log in directly as root; instead, log in as a standard user and use sudo to escalate privileges.
- Open the SSH configuration file using a text editor (requires sudo privileges):
sudo nano /etc/ssh/sshd_config - Locate the line that says
PermitRootLogin yes(orprohibit-password). - Change it to:
PermitRootLogin no
Step 2: Disable Password Authentication
Passwords can be guessed, stolen, or brute-forced. SSH Keys (using RSA, Ed25519, etc.) are cryptographically secure and practically impossible to crack. Before disabling passwords, ensure you have successfully generated an SSH key pair on your client machine and added the public key to the server’s ~/.ssh/authorized_keys file.
- In the same
sshd_configfile, find the linePasswordAuthentication yes(it may be commented out with a#). - Uncomment it and change it to:
PasswordAuthentication no
Step 3: Change the Default SSH Port (Optional but Recommended)
While changing the default port (22) is technically “security through obscurity,” it dramatically reduces the volume of log noise generated by automated botnets scanning the internet for open SSH servers.
- Find the line
#Port 22in the configuration file. - Uncomment it and change it to a high, unused port number, for example:
Port 22444 - Crucial Step: Before restarting SSH, ensure you update your firewall (e.g., UFW) to allow traffic on the new port (
sudo ufw allow 22444/tcp), or you will lock yourself out of the server.
Step 4: Apply the Changes
Save the file and exit the text editor (in nano, press Ctrl+X, then Y, then Enter). To apply the new security rules, restart the SSH service:
sudo systemctl restart ssh
Warning: Keep your current SSH session open. Open a new terminal window and attempt to log in using your SSH key (and the new port, if you changed it). If you made a configuration error, your current session will remain active, allowing you to fix the issue without being permanently locked out.