1. Overview
Securing SSH access helps protect your Linux server from unauthorized access and automated attacks.
This guide explains how to secure the SSH service on Ubuntu and AlmaLinux, including operating system-specific commands where required.
2. Change the Default SSH Port
Changing the default SSH port (22) can help reduce automated scanning and attack attempts.
Step 1: Edit the SSH Configuration
Open the SSH configuration file:
sudo nano /etc/ssh/sshd_configLocate the following line:
Port 22Change it to a different port number, for example:
Port 24357Note: Choose a port number between 1024 and 65535.
Step 2: Restart the SSH Service
Ubuntu
sudo systemctl restart sshAlmaLinux
sudo systemctl restart sshd3. Update Firewall Rules
Allow the new SSH port through the firewall before restarting the SSH service.
Ubuntu (UFW)
sudo ufw allow 24357/tcp
sudo ufw enableAlmaLinux (firewalld)
sudo firewall-cmd –permanent –add-port=24357/tcp
sudo firewall-cmd –reload4. Disable Root Login
Disabling direct root login provides an additional layer of security.
Open the SSH configuration file:
sudo nano /etc/ssh/sshd_configAdd or update the following setting:
PermitRootLogin noRestart the SSH service after saving the changes.
5. Use SSH Key Authentication
SSH key authentication is more secure than password-based authentication.
Option A: Use the SSH Key Pair Generated During VM Creation
When creating the Virtual Machine, select the option to generate a new SSH key pair.
A .pem file (for example, MYSSHKey.pem) will be downloaded to your local computer.
Set the correct file permission:
chmod 400 MYSSHKey.pemNavigate to the directory containing the key:
cd Downloads/Connect to the server using the SSH key:
ssh -i MYSSHKey.pem root@your-server-ip -p 24357Note: No password is required when using the .pem file provided during VM creation.
Option B: Generate an SSH Key Pair on Your Local Machine
Generate a new SSH key pair:
ssh-keygen -t rsa -b 4096The keys are created in the following locations:
Public key: ~/.ssh/id_rsa.pub
Private key: ~/.ssh/id_rsa
Note: Do not share your private key.
Copy the Public Key to the Server
Run:
ssh-copy-id -p 24357 root@your-server-ipEnter the user’s password when prompted.
Alternatively, copy the contents of:
~/.ssh/id_rsa.pubto:
~/.ssh/authorized_keyson the remote server.
Set the Correct Permissions
chmod 700 ~/.ssh
chmod 600 ~/.ssh/authorized_keysDisable Password Authentication
Edit the SSH configuration file:
sudo nano /etc/ssh/sshd_configUpdate the following settings:
PasswordAuthentication no
ChallengeResponseAuthentication no
UsePAM noRestart the SSH service after saving the configuration.
6. Install and Configure Fail2Ban
Fail2Ban helps protect the server from brute-force login attempts.
Install Fail2Ban
Ubuntu
sudo apt install fail2banAlmaLinux
sudo dnf install epel-release -y
sudo dnf install fail2ban -yEnable and Start the Service
sudo systemctl enable fail2ban –nowConfigure Fail2Ban (Optional)
Create or edit the following configuration file:
/etc/fail2ban/jail.localEnable the SSH jail as required.