How to change SSH port on your server
To change the SSH port on Ubuntu, you'll need to modify the SSH daemon configuration file. Here are the steps to do this:
-
Open the SSH Daemon Configuration File: Open the SSH daemon configuration file (
sshd_config
) in a text editor. You will typically need elevated privileges to edit this file. You can use a command-line text editor likenano
orvim
. For example: -
Locate the 'Port' Directive: Look for the line that starts with
Port
. This line specifies the port on which the SSH server listens. The default is usually22
. You can change it to any unused port, for example: -
Save and Exit: Save the changes and exit the text editor. For
nano
, you can typically do this by pressingCtrl + X
, thenY
to confirm the changes, and finallyEnter
to exit. -
Restart the SSH Service: After modifying the configuration file, you need to restart the SSH service for the changes to take effect. Use the following command:
Alternatively, you can use the following command on more recent Ubuntu versions:
-
Verify the Changes: You can verify that SSH is now listening on the new port by running:
Replace<new_port>
with the port number you specified in the configuration file. -
Update Firewall Rules using
iptables
in Linux: If you are usingiptables
as firewall manager, make sure to update the rules to allow traffic on the new SSH port.
2222
with your new SSH port.
After adding a rule, you may need to save the changes to make them persistent across reboots. The method for saving rules can vary depending on your Linux distribution.
For Ubuntu, you can use the iptables-save and iptables-restore commands:
This command saves the current iptables rules to a file, and you can restore them with:Remember that changing the SSH port can enhance security by making it less predictable, but it's also essential to update your firewall rules and remember the new port when connecting.