How to Change SSH port for Ubuntu

Learn how to change SSH listening port on Ubuntu 20.04/22.04/24.04 in our simple step-by-step guide.

Introduction

On Ubuntu server, port 22 is used to establish an SSH connection by default. You can strengthen the server security by modifying the SSH port to prevent brute force attacks. This article introduces the 6 steps on how to change the SSH port for Ubuntu.

1. Log in to the server.

2. Edit the SSH configuration file.

3. Save the configuration file and exit.

4. Restart the SSH service.

5. Log in to the server with the new port.

6 Steps to Change SSH Port on Ubuntu

Step 1. Log in to the server

For how to log in to the Linux server, please refer to How to SSH to a Linux Server. If the system is Ubuntu OS, please switch to root user with the command "sudo-i".

# sudo -i

Step 2. Edit the SSH configuration file

The SSH configuration file is sshd_config. You can use "vi" to edit the configuration. First, enter the command below

# vi /etc/ssh/sshd_config

Next, type "/Port" to search for the and uncomment the "#". Then, change the port to a larger number such as 15678.

Please note that the port number must be between 1024 and 65536.

Edit the SSH configuration file

Step 3. Save the configuration file and exit

Save the configuration file and exit with the command ":wq".

Save the configuration file

Step 4. Adjusting Firewall Settings

If the system has UFW (Uncomplicated Firewall) enabled, you need to allow the new SSH port. First check the ufw status, If it is inactive, you do not need to do anything, proceed to the next step.

Generally, the server ufw is turned off by default. If ufw is Active, open the SSH port you just set up.

# ufw status
Status: active
# ufw allow 15678/tcp

Step 5. Restart the SSH service

Restart the SSH service for the configuration to take effect.

# on ubuntu 20.04 or 22.04
service sshd restart

# or on ubuntu 24.04
systemctl daemon-reload
systemctl restart ssh.socket

Step 6. Test the connection with the new port

After you change the SSH port, the session breaks. You need to log back in to the server using the new port.

ssh administrator@server_ip -p 15678

If you use putty, please change the port as shown below.

putty connection setting