Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

Change SSH Port: Steps, Risks & Better Options

· 6 min read · Home Tech

In this article
  1. Why Change the SSH Port?
  2. How SSH Port Configuration Works
  3. Step-by-Step: Change the SSH Port
  4. How to Choose a Port Number
  5. Common Mistakes and Troubleshooting
  6. Does Changing the Port Improve Security?
  7. Alternatives to Changing the Port
  8. Frequently Asked Questions

Yes, you can change the SSH port from the default 22 to any unused port by editing the Port directive in the SSH server configuration file (/etc/ssh/sshd_config) and restarting the service. This article explains exactly how to do it on Linux and macOS, how to choose a safe port, why it does not meaningfully improve security, and what to do instead.

Why Change the SSH Port?

The default SSH port is 22, which is assigned by the Internet Assigned Numbers Authority (IANA) for the SSH protocol. Because it is well-known, automated bots continuously scan the entire internet for any host listening on port 22 and attempt to log in with common usernames and passwords. Changing the port reduces the volume of these automated login attempts because most scanners only target port 22. However, it does not stop a determined attacker who can scan all 65,535 ports in under a minute. The real benefit is less log noise and fewer brute-force attempts, not stronger security.

How SSH Port Configuration Works

The SSH server (sshd) reads its settings from /etc/ssh/sshd_config on most Linux distributions and macOS. The Port directive tells sshd which port to listen on. You can specify multiple ports by using several Port lines, but it is common to use only one. The client connects with ssh -p <port> user@host or by setting the Port option in the client configuration file (~/.ssh/config).

Step-by-Step: Change the SSH Port

  1. Open a terminal and connect to your server via SSH (still on port 22).
  2. Back up the configuration file: sudo cp /etc/ssh/sshd_config /etc/ssh/sshd_config.bak
  3. Edit the file with a text editor: sudo nano /etc/ssh/sshd_config (or use vim).
  4. Find the line that says #Port 22 (the hash means it is commented out). Remove the hash and change 22 to your chosen port, e.g., Port 2222.
  5. Save the file and exit the editor.
  6. Test the configuration before restarting: sudo sshd -t. If it returns no errors, you are safe to continue.
  7. Restart the SSH service: sudo systemctl restart sshd (on systemd systems) or sudo service ssh restart (on older init systems).
  8. Open a new terminal and test the connection: ssh -p 2222 user@host. If it works, keep that terminal open and close the old one.
  9. If you have a firewall (ufw, firewalld, iptables), allow the new port and remove the rule for port 22 if you no longer want it open.

If you are using a cloud provider (AWS, Azure, GCP), you must also update the security group or network firewall rules to allow inbound traffic on the new port. Otherwise, the connection will time out.

How to Choose a Port Number

Port numbers range from 1 to 65535. Ports below 1024 are privileged and require root to bind, which is fine because sshd runs as root initially. However, many low ports are already assigned to other services (HTTP=80, HTTPS=443, MySQL=3306). Choose a port that is not commonly used by other software on your system. Common choices include 2222, 2200, or a random high port like 48213. Avoid using port 22 on a different protocol (like 2222) because some scanners also check that. The most important thing is to avoid a port that conflicts with an existing service on your server. You can check with sudo netstat -tulpn | grep <port> to see if it is already in use.

Common Mistakes and Troubleshooting

  • Forgetting to update the firewall: After changing the port, you must allow the new port in your firewall. If you do not, you will be locked out. Always test in a second terminal before closing the first.
  • Editing the wrong file: On some systems, the SSH configuration is split into files under /etc/ssh/sshd_config.d/. If you edit the main file but a drop-in file overrides the port, your change may not take effect.
  • Not restarting the service: The change only takes effect after sshd is restarted. A syntax error can prevent the service from starting, so always run sshd -t first.
  • Using a port already in use: If the port is occupied, sshd will fail to start. Check with ss -tulpn or netstat.
  • Client not specifying the port: After the change, you must use -p <port> or update your client config. Otherwise, the client tries port 22 and fails.

Does Changing the Port Improve Security?

Changing the SSH port is often called "security by obscurity". It reduces automated attacks that target only port 22, but it does not protect against a targeted attacker who scans all ports. The real security measures are:

  • Use key-based authentication: Disable password login (PasswordAuthentication no) and use SSH keys. This is the single most effective change.
  • Disable root login: Set PermitRootLogin no so attackers cannot try to log in as root directly.
  • Use a firewall: Restrict SSH access to specific IP addresses if possible.
  • Keep SSH updated: Regularly update OpenSSH to get security patches.
  • Consider fail2ban: This tool automatically bans IPs that show malicious behavior, such as repeated failed login attempts.

Changing the port is a minor inconvenience for attackers but can be a useful part of a defense-in-depth strategy. It is not a substitute for proper authentication.

Alternatives to Changing the Port

If your goal is to reduce automated attacks, you have several alternatives that are more effective than changing the port:

  • Use SSH keys: This is the strongest recommendation. With password authentication disabled, even if an attacker finds the port, they cannot easily log in.
  • Use a VPN: Put SSH behind a VPN (like WireGuard or OpenVPN) so it is not exposed to the internet at all. Only VPN clients can reach the SSH port.
  • Use port knocking: The SSH port stays closed until a specific sequence of connection attempts is made. This is more complex but hides the service.
  • Use an SSH jump host: Expose only one server to the internet and have it forward SSH to internal servers.

Frequently Asked Questions

Can I use any port number?

Yes, any unused TCP port from 1 to 65535. However, ports below 1024 require root privileges to bind, which sshd already has. Just avoid ports already used by other services on your system.

Will changing the SSH port break my existing connections?

Only if you restart sshd while a connection is active. The restart kills existing sessions. To avoid this, you can use systemctl reload sshd instead of restart, which applies the new port without dropping active connections (though the old port may stop listening immediately).

How do I connect to a non-standard SSH port?

Use the -p option: ssh -p 2222 user@host. Or add a Host block in ~/.ssh/config with Port 2222 to avoid typing it every time.

Is changing the SSH port enough to prevent hacking?

No. It reduces automated scans, but a targeted attacker can find the port. Always use key authentication and disable password login for real security.

What if I get locked out after changing the port?

If you changed the port and cannot connect, check that the firewall allows the new port. If you have physical or console access (like a VPS web console), you can log in locally and revert the change. If you do not have console access, you may need to contact your hosting provider.

Does changing the port affect SCP or SFTP?

Yes, SCP and SFTP use the same SSH connection. You need to specify the port for those commands too, e.g., scp -P 2222 file user@host: (note the capital P for scp, while ssh uses lowercase p).