Change SSH port
Description
In this guide, you will learn how to change the default SSH port on your VPS.
Changing the default SSH port can reduce automated scans and unwanted login attempts against port 22. It should be considered an additional security measure, not a replacement for SSH keys, MFA, or Fail2Ban.
We are using the Linux operating system Ubuntu 24.04 and the firewall tool UFW.
What you’ll learn
Step by step guide
1
# Step 1: Open firewall to allow the new SSH port2
sudo ufw allow 23657/tcp comment 'SSH altered'3
4
# Step 2: Configure a new port for SSH5
sudo nano /etc/ssh/sshd_config6
# Change or add:7
#Port 228
# to e. g.:9
#Port 2365710
11
# Step 3: Test the SSH configuration for errors (no output means no errors)12
sudo sshd -t13
14
# Step 4: Disable systemd socket activation15
sudo systemctl disable --now ssh.socket16
17
# Step 5: Restart SSH service to apply changes18
sudo systemctl enable --now ssh19
sudo systemctl restart ssh20
sudo ss -tlnp | grep ssh # should show the new port 23657 21
22
# Step 6: Verify that the new SSH port works using a new terminal session23
echo "DO NOT CLOSE THE CURRENT TERMINAL SESSION UNTIL YOU HAVE VERIFIED THAT THE NEW SSH PORT WORKS!"24
# ssh -p 23657 user@your_server_ip25
26
# Step 7: Delete the old SSH port from the firewall rules27
sudo ufw delete allow 22/tcp