Logo

How to Secure SSH by Changing the Port and Password on CentOS 8, AlmaLinux 8/9, Rocky 8, and Ubuntu 22.04

How to Secure SSH by Changing the Port and Password on CentOS 8, AlmaLinux 8/9, Rocky 8, and Ubuntu 22.04

SSH (Secure Shell) is a vital service for managing remote servers, but the default settings can make your system vulnerable to brute force attacks. This guide explains how to change the SSH port and secure it with a strong password on CentOS 8, AlmaLinux 8/9, Rocky Linux 8, and Ubuntu 22.04.


Step 1: Prerequisites

 

  • install putty https://www.putty.org/
  • Root access to the server or a user with sudo privileges.
  • Backup your current SSH configuration:
     
    sudo cp /etc/ssh/sshd_config /etc/ssh/sshd_config.bak

Step 2: Change SSH Port

  1. Edit the SSH configuration file:

    sudo nano /etc/ssh/sshd_config
  2. Look for the following line and change the port from 22 to a non-standard port (e.g., 2222):

    #Port 22

    Uncomment the line and set your desired port:

    Port 2222
  3. Save and exit the editor (Ctrl + O, then Enter, and Ctrl + X).

  4. Restart the SSH service:

    sudo systemctl restart sshd
  5. Test the new SSH port before logging out:

     
    ssh -p 2222 user@your-server-ip

Step 3: Secure SSH with a Strong Password

  1. Set a Strong Password for Your User:

    Use the following command to change the password for your user:

    sudo passwd your-username
    Save and exit the editor, then restart the SSH service:
 
sudo systemctl restart sshd

 

  Video in Youtube