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.
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
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
Please note that the port number must be between 1024 and 65536.
Save the configuration file and exit with the command ":wq".
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
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
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.