How to Change SSH Server Default Port on Ubuntu

This brief tutorial shows students and new users how to change SSH default port on Ubuntu 20.04 | 18.04 LTS servers.

Changing your SSH Server default port number from 22 to something else is one way to secure your server. This might not be the only way to protect your SSH server from authorized login attempts, but it can help in slowing down attacks against the default port.

Students who want to learn how to change SSH server default port # from 22 to something else can follow the steps below.

As we mentioned above, this isn’t the only way to protect your server, but doing it will add another layer of security to your servers. It’s no secret that SSH servers communicate over port 22. It’s a known port and everyone including hackers know it.

Also port scanners try known ports when scanning for vulnerabilities so changing it adds some layer of protection.

Step 1: Logging to SSH

With SSH port being default, all you have to do is type ssh root@server_ip_address

ssh root@server_ip_address

You don’t need to specify a port that’s because the SSH client is built with instruction to always communicate over port 22.  When you change the port, you’ll need to to specify the new port number everytime you want to connect.

For example, if you change your SSH port to 10021, this is how you’ll always connect to the server.

ssh root@server_ip_address -p10021

Step 2: Changing SSH port number

Now that you know, let’s go and change the SSH server default port # from 22 to 10021. To do that, logon to the SSH server as usual.

Then open SSH default configuration file and make the below change.

sudo nano /etc/ssh/sshd_config

Put a # before current for Port 22

# What ports, IPs and protocols we listen for
# Port 22

Then open port # 10021

# What ports, IPs and protocols we listen for
Port 10021

Save the file and you’re done.

Step 3: Restart SSH service

After changing the port number, the final step is to restart the server so it picks up the change. To do that, run the commands below.

sudo service sshd restart

That’s it! This his how you change SSH port number.

Remember to always include the new port when connection to your server.

ssh root@server_ip_address -p10021

If you change the port above, you may also need to adjust Ubuntu firewall to allow traffic on the new port. Run the commands below to allow the port above.

sudo ufw allow 10021


This post showed you how to change SSH default port number. If you find any error above, please use the comment form below to report.

You may also like the post below:

One Reply to “How to Change SSH Server Default Port on Ubuntu”

  1. Hi
    Good description, thanks.
    Maybe good to add as first step:
    Adjust firewall to allow new port

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.