{"id":1369,"date":"2026-09-03T11:37:30","date_gmt":"2026-09-03T06:07:30","guid":{"rendered":"https:\/\/cpanelfree.com\/blog\/how-to-change-ssh-port-linux-vps\/"},"modified":"2026-09-03T12:32:35","modified_gmt":"2026-09-03T07:02:35","slug":"how-to-change-ssh-port-linux-vps","status":"publish","type":"post","link":"https:\/\/cpanelfree.com\/blog\/how-to-change-ssh-port-linux-vps\/","title":{"rendered":"How to Change the Default SSH Port on Linux to Block 99% of Bot Scanners"},"content":{"rendered":"<div style=\"background-color: #f8fafc;border-left: 4px solid #14b8a6;padding: 20px;border-radius: 6px;margin-bottom: 25px\">\n<p style=\"margin: 0;font-size: 16px;color: #1e293b\">\n        <strong>Quick Answer:<\/strong> To change the default SSH port on a Linux VPS, select an unused high port (such as <code>22022<\/code>), allow the new port in UFW (<code>sudo ufw allow 22022\/tcp<\/code>), edit <code>\/etc\/ssh\/sshd_config<\/code> to set <code>Port 22022<\/code>, restart OpenSSH (<code>sudo systemctl restart ssh<\/code>), and verify connection in a separate terminal before closing your active session.\n    <\/p>\n<\/div>\n<h2>Why Changing Port 22 Drastically Cuts Server Noise<\/h2>\n<p>While security professionals rightly note that changing the SSH port is &#8220;security through obscurity&#8221; rather than a standalone cryptographic barrier, moving SSH off standard port 22 immediately eliminates over <strong>99% of automated credential stuffing bots<\/strong>. This keeps <code>\/var\/log\/auth.log<\/code> clean, conserves CPU cycles, and prevents authentication log bloat.<\/p>\n<h2>Step 1: Selecting a Safe Custom Port Number<\/h2>\n<p>Choose an unused port number between <strong>1024 and 65535<\/strong> that does not conflict with common web services (avoid 80, 443, 3306, 5432, 8080, 8090). A great standard choice is <code>22022<\/code> or <code>49152<\/code>.<\/p>\n<h2>Step 2: Opening the New Port in UFW &amp; Cloud Security Groups<\/h2>\n<p><strong>Warning:<\/strong> You must update your firewall rules BEFORE restarting SSH. If you restart SSH on a new port without updating your firewall, your server will drop the connection and lock you out.<\/p>\n<pre style=\"background-color: #1e293b;color: #38bdf8;padding: 14px;border-radius: 6px;font-size: 13px\"># Allow the new custom port in UFW\nsudo ufw allow 22022\/tcp\nsudo ufw status<\/pre>\n<p>If you are using AWS, Oracle Cloud, or Hetzner Cloud, also add an inbound security group rule allowing TCP traffic on port <code>22022<\/code> in your provider&#8217;s web console.<\/p>\n<h2>Step 3: Updating OpenSSH Daemon Configuration<\/h2>\n<pre style=\"background-color: #1e293b;color: #38bdf8;padding: 14px;border-radius: 6px;font-size: 13px\">sudo nano \/etc\/ssh\/sshd_config<\/pre>\n<p>Locate the line <code>#Port 22<\/code>, uncomment it, and update it to your new port:<\/p>\n<pre style=\"background-color: #1e293b;color: #38bdf8;padding: 14px;border-radius: 6px;font-size: 13px\"># OpenSSH Custom Port Setting\nPort 22022<\/pre>\n<h2>Step 4: Testing the Configuration and Restarting SSH<\/h2>\n<p>Validate the syntax of your configuration file before reloading the daemon:<\/p>\n<pre style=\"background-color: #1e293b;color: #38bdf8;padding: 14px;border-radius: 6px;font-size: 13px\"># Validate syntax for errors\nsudo sshd -t\n\n# If no errors returned, restart the SSH service\nsudo systemctl restart ssh<\/pre>\n<h2>Step 5: Verifying the Connection (Do Not Close Existing Window!)<\/h2>\n<p>Open a fresh terminal window on your local machine and test connecting on the new port with the <code>-p<\/code> flag:<\/p>\n<pre style=\"background-color: #1e293b;color: #38bdf8;padding: 14px;border-radius: 6px;font-size: 13px\">ssh -p 22022 username@your-server-ip<\/pre>\n<p>Once connected successfully, you can safely remove port 22 from your firewall (<code>sudo ufw delete allow 22\/tcp<\/code>).<\/p>\n<h2>Configuring SSH Client Shortcuts in ~\/.ssh\/config<\/h2>\n<p>Once your SSH port is changed from 22 to <code>22022<\/code>, typing the port argument manually during every terminal session is tedious. You can configure a client alias on your local Windows, macOS, or Linux machine by editing <code>~\/.ssh\/config<\/code>:<\/p>\n<pre style=\"background-color: #1e293b;color: #38bdf8;padding: 14px;border-radius: 6px;font-size: 13px\"># Local ~\/.ssh\/config profile\nHost myvps\n    HostName 203.0.113.50\n    Port 22022\n    User deployer\n    IdentityFile ~\/.ssh\/id_ed25519\n    ServerAliveInterval 60<\/pre>\n<p>Now, simply executing <code>ssh myvps<\/code> automatically routes your connection to port 22022 using your private key without typing IP addresses or port flags.<\/p>\n<h2>SELinux Considerations on RHEL, AlmaLinux &amp; Rocky Linux<\/h2>\n<p>If you are running enterprise RHEL-based distributions with SELinux in Enforcing mode, SELinux will block OpenSSH from binding to non-standard ports by default. Allow the custom port in SELinux policy before restarting the daemon:<\/p>\n<pre style=\"background-color: #1e293b;color: #38bdf8;padding: 14px;border-radius: 6px;font-size: 13px\"># Install SELinux management utilities\nsudo dnf install policycoreutils-python-utils -y\n\n# Add custom port 22022 to SSH port context\nsudo semanage port -a -t ssh_port_t -p tcp 22022\n\n# Verify SELinux SSH port assignments\nsudo semanage port -l | grep ssh<\/pre>\n<h2>Step-by-Step Port Change for Ubuntu, Debian, AlmaLinux &amp; CentOS<\/h2>\n<p>To ensure absolute compatibility across various Linux distributions, review the exact system requirements before altering your remote management port:<\/p>\n<table style=\"width: 100%;border-collapse: collapse;margin: 20px 0;font-size: 14px\">\n<thead>\n<tr style=\"background-color: #0f172a;color: #ffffff\">\n<th style=\"padding: 10px;border: 1px solid #334155\">Distribution<\/th>\n<th style=\"padding: 10px;border: 1px solid #334155\">Firewall Tool<\/th>\n<th style=\"padding: 10px;border: 1px solid #334155\">Service Daemon Name<\/th>\n<th style=\"padding: 10px;border: 1px solid #334155\">SELinux Enforcement<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr style=\"background-color: #f8fafc\">\n<td style=\"padding: 10px;border: 1px solid #cbd5e1;font-weight: bold\">Ubuntu 24.04 \/ 22.04<\/td>\n<td style=\"padding: 10px;border: 1px solid #cbd5e1\">UFW<\/td>\n<td style=\"padding: 10px;border: 1px solid #cbd5e1\"><code>ssh<\/code> or <code>ssh.service<\/code><\/td>\n<td style=\"padding: 10px;border: 1px solid #cbd5e1\">AppArmor (Default)<\/td>\n<\/tr>\n<tr>\n<td style=\"padding: 10px;border: 1px solid #cbd5e1;font-weight: bold\">Debian 12 \/ 11<\/td>\n<td style=\"padding: 10px;border: 1px solid #cbd5e1\">UFW \/ nftables<\/td>\n<td style=\"padding: 10px;border: 1px solid #cbd5e1\"><code>ssh.service<\/code><\/td>\n<td style=\"padding: 10px;border: 1px solid #cbd5e1\">AppArmor<\/td>\n<\/tr>\n<tr style=\"background-color: #f8fafc\">\n<td style=\"padding: 10px;border: 1px solid #cbd5e1;font-weight: bold\">AlmaLinux \/ Rocky Linux 9<\/td>\n<td style=\"padding: 10px;border: 1px solid #cbd5e1\">firewalld<\/td>\n<td style=\"padding: 10px;border: 1px solid #cbd5e1\"><code>sshd.service<\/code><\/td>\n<td style=\"padding: 10px;border: 1px solid #cbd5e1;color: #10b981\">SELinux Enforcing (Requires semanage)<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h2>Pairing Custom SSH Ports with Port Knocking &amp; WireGuard VPNs<\/h2>\n<p>For high-security environments, changing the SSH port can be augmented with <strong>Port Knocking<\/strong> (using <code>knockd<\/code>) or private network tunneling via <strong>WireGuard VPN<\/strong>. With WireGuard, you can close your SSH port to the public internet completely, allowing SSH connections only when authenticated to your private encrypted VPN mesh.<\/p>\n<h2>Automating Port Change with Ansible Configuration Management<\/h2>\n<p>If you manage multiple cloud VPS instances across different cloud providers, updating the SSH configuration manually on every single server is error-prone. You can automate custom SSH port deployment using Ansible playbooks, ensuring idempotent configuration without risk of server lockouts:<\/p>\n<pre style=\"background-color: #1e293b;color: #38bdf8;padding: 14px;border-radius: 6px;font-size: 13px\"># Sample Ansible task for automated custom SSH port rollout\n- name: Allow custom SSH port in UFW\n  ufw:\n    rule: allow\n    port: '22022'\n    proto: tcp\n\n- name: Update OpenSSH port directive\n  lineinfile:\n    path: \/etc\/ssh\/sshd_config\n    regexp: '^#?Port '\n    line: 'Port 22022'\n    validate: '\/usr\/sbin\/sshd -t -f %s'\n  notify: restart ssh<\/pre>\n<h2>Monitoring SSH Authentication Logs with Logwatch<\/h2>\n<p>To keep track of connection attempts and verify that bot traffic has ceased on port 22, install Logwatch to receive automated daily digest emails summarizing authentication successes and failures:<\/p>\n<pre style=\"background-color: #1e293b;color: #38bdf8;padding: 14px;border-radius: 6px;font-size: 13px\">sudo apt install logwatch -y\nsudo logwatch --detail High --service sshd --range today<\/pre>\n<div style=\"background-color: #f8fafc;border: 1px solid #e2e8f0;border-left: 4px solid #0ea5e9;padding: 20px;border-radius: 8px;margin: 30px 0\">\n<h3 style=\"margin-top: 0;color: #0f172a;font-size: 18px;display: flex;align-items: center\">\n        <span style=\"margin-right: 8px\">\ud83d\udd17<\/span> Recommended Related Technical Guides:<br \/>\n    <\/h3>\n<ul style=\"margin: 10px 0 0 0;padding-left: 20px;line-height: 1.8\">\n<li><a href=\"https:\/\/cpanelfree.com\/blog\/how-to-configure-ufw-firewall-ubuntu\/\" style=\"color: #0284c7;text-decoration: none;font-weight: 600\">How to Configure UFW Firewall on Ubuntu Server<\/a><\/li>\n<li><a href=\"https:\/\/cpanelfree.com\/blog\/how-to-install-configure-fail2ban-linux\/\" style=\"color: #0284c7;text-decoration: none;font-weight: 600\">How to Install and Configure Fail2ban on Linux<\/a><\/li>\n<li><a href=\"https:\/\/cpanelfree.com\/blog\/how-to-install-lets-encrypt-ssl-certbot-linux\/\" style=\"color: #0284c7;text-decoration: none;font-weight: 600\">How to Install Free Let&#8217;s Encrypt SSL on Linux<\/a><\/li>\n<li><a href=\"https:\/\/cpanelfree.com\/blog\/best-free-web-hosting-2026\/\" style=\"color: #0284c7;text-decoration: none;font-weight: 600\">Top 10 Best Free Web Hosting Services<\/a><\/li>\n<li><a href=\"https:\/\/cpanelfree.com\/#plans\" style=\"color: #0284c7;text-decoration: none;font-weight: 600\">Explore $0 Free cPanel Web Hosting Plans<\/a><\/li>\n<\/ul>\n<\/div>\n<div style=\"background: linear-gradient(135deg, #0f172a 0%, #1e293b 100%);border: 1px solid #334155;border-radius: 12px;padding: 25px;margin: 30px 0;text-align: center\">\n<h3 style=\"color: #38bdf8;margin-top: 0;font-size: 20px\">Deploy Web Apps with Zero Server Hardening Stress<\/h3>\n<p style=\"color: #94a3b8;font-size: 14px;line-height: 1.6;max-width: 600px;margin: 0 auto 15px\">\n        Skip SSH maintenance completely. <strong>CpanelFree<\/strong> delivers fully hardened, high-speed cPanel hosting with free SSL, MySQL databases, and email support at $0 cost forever.\n    <\/p>\n<p>    <a href=\"https:\/\/cpanelfree.com\/free-wordpress-hosting\" style=\"display: inline-block;background-color: #14b8a6;color: #ffffff;padding: 10px 22px;border-radius: 6px;text-decoration: none;font-weight: bold;font-size: 14px\">Launch Free Hosting<\/a>\n<\/div>\n<h2>Frequently Asked Questions<\/h2>\n<div style=\"border-bottom: 1px solid #e2e8f0;padding: 12px 0\">\n<h4 style=\"margin: 0 0 8px 0;color: #1e293b\">How can I avoid typing the -p 22022 port flag every time?<\/h4>\n<p style=\"margin: 0;color: #475569;font-size: 14px\">Add a shortcut to your local <code>~\/.ssh\/config<\/code> file specifying <code>Host myserver<\/code>, <code>HostName your-ip<\/code>, and <code>Port 22022<\/code>. You can then simply type <code>ssh myserver<\/code>.<\/p>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Quick Answer: To change the default SSH port on a Linux VPS, select an unused high port (such as 22022), allow the new port in UFW (sudo ufw allow 22022\/tcp), edit \/etc\/ssh\/sshd_config to set Port 22022, restart OpenSSH (sudo systemctl restart ssh), and verify connection in a separate terminal before closing your active session. Why [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":1368,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[64],"tags":[],"class_list":["post-1369","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-security"],"_links":{"self":[{"href":"https:\/\/cpanelfree.com\/blog\/wp-json\/wp\/v2\/posts\/1369","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/cpanelfree.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cpanelfree.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cpanelfree.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/cpanelfree.com\/blog\/wp-json\/wp\/v2\/comments?post=1369"}],"version-history":[{"count":4,"href":"https:\/\/cpanelfree.com\/blog\/wp-json\/wp\/v2\/posts\/1369\/revisions"}],"predecessor-version":[{"id":1563,"href":"https:\/\/cpanelfree.com\/blog\/wp-json\/wp\/v2\/posts\/1369\/revisions\/1563"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/cpanelfree.com\/blog\/wp-json\/wp\/v2\/media\/1368"}],"wp:attachment":[{"href":"https:\/\/cpanelfree.com\/blog\/wp-json\/wp\/v2\/media?parent=1369"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cpanelfree.com\/blog\/wp-json\/wp\/v2\/categories?post=1369"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cpanelfree.com\/blog\/wp-json\/wp\/v2\/tags?post=1369"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}