How7o
  • Home
  • Tools
  • Prank Screens
  • Learn
  • Blog
  • Contact
Reading: How to Change the Default SSH Port on Linux
Share
How7oHow7o
Font ResizerAa
  • OS
Search
  • Home
  • Tools
  • Prank Screens
  • Learn
  • Blog
  • Contact
Follow US
© 2024–2026 How7o. All rights reserved.
How7o > Free Laravel, PHP, WordPress & Server Tutorials > Server Management > How to Change the Default SSH Port on Linux
Server Management

How to Change the Default SSH Port on Linux

how7o
By how7o
Last updated: May 22, 2026
7 Min Read
Change SSH port on Linux — firewall, SELinux, sshd_config, systemctl restart
SHARE

To change the default SSH port on Linux, edit /etc/ssh/sshd_config and set Port <newport>, open the new port in the firewall, register it with SELinux (on RHEL family), and restart sshd. This guide walks through all four steps without locking you out, and explains when each one is actually necessary.

Contents
  • TL;DR
  • Step 1 — Pick a port and verify it’s free
  • Step 2 — Open the port in the firewall (first!)
  • Step 3 — Tell SELinux (RHEL family only)
  • Step 4 — Edit sshd_config
  • Step 5 — Restart sshd
  • Step 6 — Test from a NEW terminal
  • Frequently asked questions
  • Related guides
  • References

Last verified: 2026-05-17 on AlmaLinux 9, Ubuntu 22.04, and Debian 12. Originally published 2023-03-04, rewritten and updated 2026-05-17.

TL;DR

# 1. Pick an unused port (1024-49151), e.g. 4920
# 2. Open it in the firewall
sudo firewall-cmd --add-port=4920/tcp --permanent && sudo firewall-cmd --reload   # RHEL family
sudo ufw allow 4920/tcp                                                            # Debian/Ubuntu

# 3. RHEL family: register the port with SELinux
sudo semanage port -a -t ssh_port_t -p tcp 4920

# 4. Edit sshd_config
sudo vi /etc/ssh/sshd_config
#  Change:  #Port 22
#  To:      Port 4920

# 5. Restart sshd
sudo systemctl restart sshd

# 6. Test from a NEW terminal (keep the old one open!)
ssh -p 4920 [email protected]

Step 1 — Pick a port and verify it’s free

Pick something between 1024 and 49151 that isn’t already in use. ss -tlnp lists bound TCP ports:

sudo ss -tlnp

For this guide I’ll use 4920; replace with your chosen number throughout.

Step 2 — Open the port in the firewall (first!)

Open the new port before changing sshd_config — that way the firewall is ready when sshd starts listening, and there’s no window where you’d be locked out.

# RHEL / AlmaLinux / Rocky / Fedora
sudo firewall-cmd --add-port=4920/tcp --permanent
sudo firewall-cmd --reload

# Debian / Ubuntu
sudo ufw allow 4920/tcp
Change SSH port on Linux — firewall, SELinux semanage, sshd_config, systemctl restart

Step 3 — Tell SELinux (RHEL family only)

Check if SELinux is enabled:

sestatus

If it says Current mode: enforcing, you need to register the new port. SELinux labels port 22 as ssh_port_t; other ports need the same label or sshd is blocked:

sudo semanage port -a -t ssh_port_t -p tcp 4920

# Verify it's in the list
sudo semanage port -l | grep ssh

If semanage isn’t installed, get it from policycoreutils-python-utils on AlmaLinux/Rocky/RHEL 8+: sudo dnf install policycoreutils-python-utils. Debian/Ubuntu don’t run SELinux by default — skip this step there.

Step 4 — Edit sshd_config

sudo vi /etc/ssh/sshd_config

Find the line:

#Port 22

Uncomment it (remove the #) and change the number:

Port 4920

Tip — keep both for one cycle: add a second Port 22 line below the new one for a single restart, so sshd listens on both. Verify the new port works, then come back and remove the Port 22 line. This is the safest pattern for production servers where a lockout is unacceptable.

Step 5 — Restart sshd

sudo systemctl restart sshd

If systemctl restart sshd errors with “job failed”, run sshd -t to syntax-check the config. Errors there usually mean a typo in the port line.

Step 6 — Test from a NEW terminal

Keep your existing SSH session open. Open a separate terminal and try the new port:

ssh -p 4920 [email protected]

Logged in? Good. Now you can close the old session and remove the old port from the firewall:

# RHEL family
sudo firewall-cmd --remove-service=ssh --permanent
sudo firewall-cmd --reload

# Debian/Ubuntu
sudo ufw delete allow 22/tcp

Frequently asked questions

Does changing the SSH port actually improve security?

Marginally. Moving SSH off port 22 stops untargeted scanners and login-brute-force bots that only try the default — your log noise drops dramatically. But a targeted attacker who already knows your IP just runs nmap first and finds the new port in seconds. Real security wins come from disabling password auth (key-only logins), fail2ban, and locking AllowUsers in sshd_config. Port change is a noise-reduction technique, not a security control.

What’s the safest way to test the new port without locking myself out?

Keep the old session open while you test. After systemctl restart sshd, open a new terminal and run ssh -p <newport> user@host. If it works, you’re fine. If it doesn’t, you still have the original session to revert. Never close the original until you’ve successfully logged in with the new port.

Which port number should I pick?

Anything between 1024 and 49151 that isn’t already in use. Above 49151 is the ephemeral / dynamic port range — outgoing connections may try to use it, which can cause conflicts. ss -tlnp lists currently bound ports on the server. Some sysadmins pick a memorable number (like 22022 or 2222); the only requirement is that it’s not already taken and not a well-known service.

Do I need to update firewalld / ufw?

Yes — if a firewall is enabled, the new port needs to be allowed before you’ll be able to connect. RHEL family: firewall-cmd --add-port=4920/tcp --permanent && firewall-cmd --reload. Debian/Ubuntu: sudo ufw allow 4920/tcp. Add the new rule before restarting sshd if you can — it avoids the ‘service running but port closed’ window.

What’s SELinux doing here and when do I need semanage?

SELinux enforces a policy that labels each port with a type. By default, only port 22 is labeled ssh_port_t — try to start sshd on a different port and SELinux blocks it with a permission denied. semanage port -a -t ssh_port_t -p tcp 4920 adds the new port to the policy. This applies to RHEL/AlmaLinux/Rocky/Fedora; Debian/Ubuntu don’t run SELinux by default, so skip this step there.

Related guides

  • How to Add and Delete Users on a Linux Server from the Terminal
  • Change the SSH Welcome Message on an Ubuntu VPS
  • Fix Nginx “recv() failed (104: Connection reset by peer)” with FastCGI

References

OpenSSH sshd_config reference: man.openbsd.org/sshd_config. RHEL SELinux + sshd customization: access.redhat.com/solutions/15093.

TAGGED:BashconfigurationSecuritySSH

Sign Up For Daily Newsletter

Be keep up! Get the latest breaking news delivered straight to your inbox.
[mc4wp_form]
By signing up, you agree to our Terms of Use and acknowledge the data practices in our Privacy Policy. You may unsubscribe at any time.
Share This Article
Facebook Copy Link Print
Previous Article DataTables default sort order — order: [[1, 'desc']] config How to Change the Default Sort Order in DataTables
Next Article JavaScript check HTTP referrer — document.referrer How to Check the HTTP Referrer with JavaScript
Leave a Comment

Leave a Reply Cancel reply

You must be logged in to post a comment.

FacebookLike
XFollow
PinterestPin
InstagramFollow
Most Popular
Run Laravel queue workers with Supervisor
How to Run Laravel Queue Workers in Production with Supervisor
May 23, 2026
Nginx as a reverse proxy for a Node.js app on Ubuntu
How to Set Up Nginx as a Reverse Proxy for Node.js on Ubuntu
May 23, 2026
Install and configure Redis on Ubuntu for Laravel and WordPress
How to Install and Configure Redis on Ubuntu (for Laravel & WordPress)
May 23, 2026
Harden a fresh Ubuntu VPS with UFW, Fail2Ban, and SSH key auth
How to Harden a Fresh Ubuntu VPS: UFW + Fail2Ban + SSH Key Auth
May 23, 2026
Set up Let's Encrypt SSL with Certbot on Ubuntu
How to Set Up Let’s Encrypt SSL with Certbot on Ubuntu (Apache & Nginx)
May 23, 2026

You Might Also Like

Transfer Discourse to a new server
Server Management

How to Transfer Discourse to a New Server on AlmaLinux (Backup + Restore, Step-by-Step)

9 Min Read
JavaScript format number with decimals — toFixed, Math.floor, and Intl.NumberFormat
Web Development

How to Format a Number with Decimals in JavaScript

5 Min Read
Install Node.js on Ubuntu — terminal with NodeSource setup_22.x curl command and Node.js hexagon icon
Web Development

How to Install Node.js on Ubuntu (22.04 & 24.04): Step-by-Step

11 Min Read
Installing Docker on AlmaLinux guide
Server Management

Install Docker on AlmaLinux: Step-by-Step (Docker CE + Compose)

4 Min Read
How7o

We provide tips, tricks, and advice for improving websites and doing better search.

Tools

  • Age Calculator
  • Word Counter
  • Image Upscaler
  • Password Generator
  • QR Code Generator
  • See all tools→

Pranks

  • Fake Blue Screen Prank
  • Hacker Typer
  • Fake iMessage Generator
  • Windows XP Crash Prank
  • Windows 11 Update Prank
  • See all prank screens →

Company

  • About Us
  • Blog
  • Contact
  • Privacy Policy
  • Terms of Service
  • Sitemap
© 2024–2026 How7o. All rights reserved.
Welcome Back!

Sign in to your account

Username or Email Address
Password

Lost your password?