Hexazn – Tips & Tricks | Hexazn Blog

How to Change SSH Port from 22 to 3965 on Ubuntu 20.04 LTS

SSH (Secure Shell) is a network protocol that allows administrators to securely access and manage remote Linux servers. By default, SSH operates on port 22.

Changing the default SSH port to a custom port, such as 3965, can help reduce automated scanning and login attempts targeting the default port. However, changing the port alone does not guarantee server security. Using SSH keys, disabling unnecessary password authentication, and keeping your server updated are also important.

In this tutorial, we will explain how to change the SSH port from 22 to 3965 on Ubuntu 20.04 LTS without losing access to your server.

Prerequisites

Before proceeding, make sure you have:

  • A VPS or dedicated server running Ubuntu 20.04 LTS.
  • Root or sudo privileges.
  • An active SSH connection.
  • Access to your VPS provider’s firewall or security group settings, if applicable.
  • Console or VNC access for emergency recovery.

Important: Do not close your existing SSH connection until you have successfully connected using the new port.

Step 1: Connect to Your Ubuntu Server

First, connect to your server using the default SSH port (22).

Open your terminal and execute:

ssh root@YOUR_SERVER_IP -p 22

Replace YOUR_SERVER_IP with your server’s actual public IP address.

If you use a non-root account, replace root with your username.

Step 2: Edit the SSH Configuration File

Ubuntu stores its primary SSH server configuration in:

/etc/ssh/sshd_config

Open the configuration file using the Nano text editor:

sudo nano /etc/ssh/sshd_config

Locate the following line:

#Port 22

Remove the # symbol and change the port number to 3965:

Port 3965

Save the file by pressing:

  • Ctrl + O to save.
  • Enter to confirm.
  • Ctrl + X to exit.

Note: Check for other active Port directives in /etc/ssh/sshd_config and any included configuration files. These can affect which ports SSH listens on.

Step 3: Configure the Ubuntu Firewall

Before restarting SSH, make sure the new port is allowed through the firewall.

Check whether UFW (Uncomplicated Firewall) is enabled:

sudo ufw status

If UFW is active, allow incoming TCP connections on port 3965:

sudo ufw allow 3965/tcp

Verify the firewall rules:

sudo ufw status

You should see a rule similar to:

3965/tcp    ALLOW    Anywhere

Do not remove the existing port 22 rule yet.

If your VPS provider uses an external firewall or security group, make sure TCP port 3965 is also allowed there.

Step 4: Validate the SSH Configuration

Before restarting SSH, check the configuration for syntax errors:

sudo sshd -t

If the command produces no output and exits successfully, the configuration syntax is valid.

You can also inspect the effective SSH port configuration:

sudo sshd -T | grep '^port '

The expected output is:

port 3965

If the output still shows port 22, review the configuration for additional port directives or conflicting settings.

Step 5: Restart the SSH Service

After validating the configuration, restart the SSH service:

sudo systemctl restart ssh

Check whether the service is running:

sudo systemctl status ssh

The service should display:

Active: active (running)

Now verify that SSH is listening on port 3965:

sudo ss -tlnp | grep ':3965'

If SSH is correctly configured, you should see a listening socket associated with sshd.

Step 6: Test SSH Connectivity Using Port 3965

Keep your original SSH session open.

Open a second terminal and attempt to connect using the new port:

ssh root@YOUR_SERVER_IP -p 3965

Replace the server IP and username as appropriate.

If you can successfully log in, SSH is accessible through the new port.

If the connection fails, verify:

  • The SSH service is running.
  • Port 3965 is configured correctly.
  • UFW allows TCP port 3965.
  • The provider’s external firewall allows the connection.
  • The server is listening on the correct network interface.

Do not remove access through port 22 until the new connection has been tested successfully.

Step 7: Remove the Old SSH Firewall Rule

Once you have confirmed that SSH works on port 3965, you can remove the previous port 22 allowance from UFW.

Run:

sudo ufw delete allow 22/tcp

Verify the updated firewall rules:

sudo ufw status

You should see port 3965 allowed. Confirm that no other rule still permits SSH access through port 22 if your intention is to close that port.

Important: This step changes the UFW firewall rule. It does not independently disable SSH listening on port 22. Your SSH configuration must also be correct.

Step 8: Verify the Final Configuration

Run the following commands to perform a final verification.

Check SSH service status:

sudo systemctl is-active ssh

Expected output:

active

Check the SSH listening port:

sudo ss -tlnp | grep sshd

Verify your firewall:

sudo ufw status

Finally, connect again from a separate terminal:

ssh root@YOUR_SERVER_IP -p 3965

If the connection succeeds, your SSH port migration is complete.

Troubleshooting Common SSH Connection Issues

Connection Refused

A Connection refused error may indicate that SSH is not listening on the requested port.

Check:

sudo systemctl status ssh

And:

sudo ss -tlnp | grep ':3965'

Connection Timed Out

A timeout commonly indicates a networking or firewall issue.

Check the UFW configuration:

sudo ufw status

Also check whether the VPS provider’s external firewall permits port 3965.

SSH Service Fails to Restart

Validate the SSH configuration:

sudo sshd -t

Review recent SSH service logs:

sudo journalctl -u ssh -n 50 --no-pager

Correct any configuration errors before attempting another restart.

Unable to Access the Server

If you lose SSH access, use your hosting provider’s VNC, serial console, or web console to access the server.

You can then inspect the SSH configuration and firewall rules and restore access.

Additional SSH Security Recommendations

Changing the SSH port helps reduce noise from basic automated scans, but it is not a replacement for proper security controls.

For better protection, consider:

  • SSH key authentication: Use cryptographic keys instead of passwords where possible.
  • Disable root SSH login: Use a non-root sudo account after verifying that it works.
  • Fail2Ban: Help block repeated failed authentication attempts.
  • Firewall restrictions: Allow SSH connections only from trusted IP addresses where practical.
  • Regular security updates: Keep OpenSSH and the operating system patched.

Ubuntu 20.04 support note: Standard security maintenance for Ubuntu 20.04 ended in May 2025. As of November 2026, servers still using Ubuntu 20.04 should have appropriate extended security coverage, such as Ubuntu Pro/ESM, or be migrated to a currently supported Ubuntu LTS release.

Conclusion

Changing the SSH port from 22 to 3965 on Ubuntu 20.04 is a straightforward server administration task.

The most important part of this process is ensuring that the new port is permitted through all relevant firewalls before restarting SSH and testing the new connection.

After completing the steps above, your Ubuntu server should accept SSH connections on port 3965 instead of the default port 22.

Always maintain an active SSH session or console recovery option while modifying remote server access settings.


Article Category: Linux Server Administration
Operating System: Ubuntu 20.04 LTS
Difficulty Level: Beginner
Topic: SSH Configuration & Server Security

Suggested SEO Title: How to Change SSH Port on Ubuntu 20.04 (22 to 3965)

Meta Description: Learn how to change the default SSH port from 22 to 3965 on Ubuntu 20.04 LTS with step-by-step commands, UFW firewall configuration, verification, and troubleshooting.

Suggested URL Slug: change-ssh-port-ubuntu-20-04

Leave a Comment

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

Scroll to Top