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.
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:
- Open the terminal and type the following command to edit the SSH server configuration file:
This command opens the
sudo nano /etc/ssh/sshd_config
sshd_configfile in the nano text editor. The
sudocommand is used to execute the command with root privileges.
- 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.
- Remove the
#symbol to uncomment the line, and change
22to your desired port number. For example, if you want to change the port to
1234, the line should look like this:
- After making the change, save the file by pressing
Ctrl + X, then
Yto confirm, and finally
Enterto exit the editor.
- 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
systemctlcommand 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:
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
1234 with your chosen port number. This command tells UFW to allow incoming connections on the specified port.
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.
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.
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.
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.
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.
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.
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.