How to Change SSH Port in Linux

By default, SSH (Secure Shell) uses port 22 to establish connections. This is well-known and often targeted by brute-force attacks. Changing the default SSH port is a simple but effective SSH hardening step to secure your SSH server.

In this tutorial, we’ll explain how to change the SSH port on Ubuntu, Debian, and CentOS systems, step-by-step, so even beginners can follow.

Why Change the SSH Port?

Changing the SSH port can:

  • Reduce automated brute-force attack attempts.
  • Improve basic SSH security as part of your server hardening strategy.
  • Make your server less visible to malicious bots scanning for open port 22.

Steps to Change SSH Port in Linux

Step 1: Log in to the Server

Before making any changes, log in to your Linux server using your current SSH port (default is 22).
For Ubuntu, switch to the root user with:

sudo -i

Step 2: Edit the SSH Configuration File

The SSH configuration file is located at:

/etc/ssh/sshd_config

Open it with the vi editor (you can also use nano if preferred):

vi /etc/ssh/sshd_config

Inside the file:

  1. Search for the line containing Port by typing:
/Port
  1. Uncomment the line by removing the # at the beginning.
  2. Change the port number to a value between 1024 and 65535 (e.g., 15678).
    change ssh port
    Example:
    Port 15678

Step 3: Save and Exit

In vi, save changes and exit by typing:

:wq

save the configuration file and exit

Step 4: Restart the SSH Service

For the change to take effect, restart the SSH service.

  1. Ubuntu/Debian:
service sshd restart

or

systemctl restart ssh
  1. CentOS 7/8:
systemctl restart sshd

restart the ssh service

Step 5: Log in Using the New SSH Port

After changing the port, your current SSH session may disconnect. To log back in:

ssh -p 15678 username@your_server_ip

Additional SSH Security Tips

Changing the SSH port is a good start, but for stronger SSH hardening, you should also:

  • Disable root login (PermitRootLogin no in sshd_config).
  • Use SSH key authentication instead of passwords.
  • Configure a firewall to allow only the new SSH port.
Keywords:

change ssh port, change ssh port ubuntu, ssh service restart, ssh security, ssh hardening, how to change ssh port, change ssh port debian, change default ssh port, change ssh port linux, change ssh port ubuntu 22.04, change ssh port ubuntu 22.04, secure ssh server, change ssh port centos 7, change ssh port centos, ssh port configuration

Outline