Software & AppsOperating SystemLinux

Fixing SSH Default Port Not Changing in Ubuntu 22.10

Ubuntu 16

SSH, or Secure Shell, is a protocol used to securely log onto remote systems. It is the most common method used to administer Linux servers. By default, the SSH server listens on port 22. However, for security reasons, you might want to change this port to a different one. This article will guide you through the process of changing the default SSH port in Ubuntu 22.10, and how to troubleshoot if the port is not changing as expected.

Quick Answer

To fix the SSH default port not changing in Ubuntu 22.10, you need to edit the SSH server configuration file, uncomment the line specifying the default port, and change it to your desired port number. After saving the file and restarting the SSH service, you can verify the change by checking the listening ports. If the SSH port is still not changing, further troubleshooting may be required.

Understanding SSH and Its Default Port

SSH is a secure protocol used for remote login to another computer over a network. It provides a secure channel over an unsecured network in a client-server architecture. By default, SSH listens on port 22. However, changing the default port can add an extra layer of security to your system by reducing the risk of automated attacks.

Changing the SSH Default Port

To change the default SSH port, you need to edit the SSH server configuration file. Here is a step-by-step guide on how to do it:

  1. Open the terminal and type the following command to edit the SSH server configuration file:
    sudo nano /etc/ssh/sshd_config
    This command opens the sshd_config file in the nano text editor. The sudo command is used to execute the command with root privileges.
  2. In the opened file, find the line that starts with #Port 22. This line specifies the default port that SSH listens on. The # symbol at the beginning of the line means that the line is commented out, and thus ignored.
  3. Remove the # symbol to uncomment the line, and change 22 to your desired port number. For example, if you want to change the port to 1234, the line should look like this:
    Port 1234
  4. After making the change, save the file by pressing Ctrl + X, then Y to confirm, and finally Enter to exit the editor.
  5. Now, you need to restart the SSH service for the changes to take effect. You can do this by running the following command:
    sudo systemctl restart ssh
    The systemctl command is used to control the systemd system and service manager. Here, it is used to restart the SSH service.

Verifying the Change

After changing the SSH port and restarting the service, you should verify that the SSH service is now listening on the new port. You can do this by checking the listening ports with the following command:

ss -tlpn

This command displays all listening sockets along with the process that opened the socket. Look for a line that shows your new port number. If you see the new port listed, it means the SSH service is now listening on the specified port.

Adjusting Firewall Settings

If you have a firewall enabled, you need to allow incoming connections on the new port. If you are using UFW, you can do this with the following command:

sudo ufw allow 1234

Replace 1234 with your chosen port number. This command tells UFW to allow incoming connections on the specified port.

Troubleshooting

If you have followed the steps above and the SSH port is still not changing, there might be other configurations or services interfering with the SSH port change. In such cases, it is recommended to consult the official Ubuntu documentation or seek assistance from the Ubuntu community forums for further troubleshooting.

Conclusion

Changing the default SSH port in Ubuntu 22.10 is a straightforward process that can help improve the security of your system. However, it’s important to remember that changing the port is not a substitute for other security measures, such as using strong passwords and keeping your system updated. Always ensure that you follow best practices for securing your SSH server.

How can I check the current SSH port in Ubuntu 22.10?

To check the current SSH port in Ubuntu 22.10, you can use the following command: grep Port /etc/ssh/sshd_config. This will display the line that specifies the SSH port, including any comments.

Can I use any port number for SSH?

In theory, you can use any available port number for SSH. However, it is recommended to choose a port number above 1024 to avoid conflicts with well-known services. Additionally, make sure the chosen port is not used by any other application on your system.

How do I connect to SSH using a non-default port?

To connect to SSH using a non-default port, you need to specify the port number when connecting. For example, if you changed the SSH port to 1234, the command to connect would be: ssh username@hostname -p 1234. Replace username with your username and hostname with the IP address or hostname of the remote server.

What should I do if I get a “Connection refused” error after changing the SSH port?

If you get a "Connection refused" error after changing the SSH port, it means that the SSH service is not listening on the specified port or the port is blocked by a firewall. Double-check the SSH configuration file to ensure the port is correctly set, and make sure your firewall allows incoming connections on the new port.

How can I revert the SSH port back to the default port?

To revert the SSH port back to the default port (port 22), you need to edit the sshd_config file and change the port number back to 22. Save the file, restart the SSH service, and adjust the firewall settings if necessary.

Leave a Comment

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