{"id":28668,"date":"2026-02-27T12:45:57","date_gmt":"2026-02-27T11:45:57","guid":{"rendered":"https:\/\/contabo.com\/blog\/?p=28668"},"modified":"2026-04-01T09:07:45","modified_gmt":"2026-04-01T07:07:45","slug":"how-to-change-ssh-port-on-linux-server","status":"publish","type":"post","link":"https:\/\/contabo.com\/blog\/how-to-change-ssh-port-on-linux-server\/","title":{"rendered":"How to Change the SSH Port on Linux"},"content":{"rendered":"\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1200\" height=\"630\" src=\"https:\/\/contabo.com\/blog\/wp-content\/uploads\/2026\/03\/blog-head_how2-change-the-ssh-port-on-a-linux-server.webp\" alt=\"Change SSH Port on a Linux Server\" class=\"wp-image-28873\" srcset=\"https:\/\/contabo.com\/blog\/wp-content\/uploads\/2026\/03\/blog-head_how2-change-the-ssh-port-on-a-linux-server.webp 1200w, https:\/\/contabo.com\/blog\/wp-content\/uploads\/2026\/03\/blog-head_how2-change-the-ssh-port-on-a-linux-server-600x315.webp 600w, https:\/\/contabo.com\/blog\/wp-content\/uploads\/2026\/03\/blog-head_how2-change-the-ssh-port-on-a-linux-server-768x403.webp 768w\" sizes=\"auto, (max-width: 1200px) 100vw, 1200px\" \/><\/figure>\n\n\n\n<p>Every bot on the internet knows that SSH lives on port 22. It&#8217;s the first door they knock on. Within minutes of spinning up a fresh Linux server, you&#8217;ll see login attempts from IPs you&#8217;ve never heard of, hammering away at the default SSH port number with dictionary attacks. The logs fill up fast.<\/p>\n\n\n\n<p>Changing the default SSH port won&#8217;t make your server invincible. Let&#8217;s be clear about that. But it kills the vast majority of automated brute force noise, and that alone is worth the two minutes this takes. Think of it as moving your front door to a side street that script kiddies don&#8217;t bother walking down.<\/p>\n\n\n\n<p>This guide walks you through every step: picking a new port, editing sshd_config, updating your firewall rules, restarting the service, and confirming it all works. Nothing fancy. Just the commands you need on a Linux VPS.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-choosing-a-new-ssh-port-number\">Choosing a New SSH Port Number<\/h2>\n\n\n\n<p>Before you touch any config files, you need to decide what port to move SSH to. Pick the wrong one and you&#8217;ll collide with another service. Pick something obvious and you&#8217;ve wasted your time.<\/p>\n\n\n\n<p>Here&#8217;s the quick reference for common ssh ports and well known ports you absolutely must avoid:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><td><strong>Port<\/strong><\/td><td><strong>Service<\/strong><\/td><td><strong>Protocol<\/strong><\/td><\/tr><tr><td>20\/21<\/td><td>FTP<\/td><td>TCP<\/td><\/tr><tr><td>22<\/td><td>SSH<\/td><td>TCP<\/td><\/tr><tr><td>23<\/td><td>Telnet<\/td><td>TCP<\/td><\/tr><tr><td>25<\/td><td>SMTP<\/td><td>TCP<\/td><\/tr><tr><td>53<\/td><td>DNS<\/td><td>TCP\/UDP<\/td><\/tr><tr><td>80<\/td><td>HTTP<\/td><td>TCP<\/td><\/tr><tr><td>110<\/td><td>POP3<\/td><td>TCP<\/td><\/tr><tr><td>143<\/td><td>IMAP<\/td><td>TCP<\/td><\/tr><tr><td>443<\/td><td>HTTPS<\/td><td>TCP<\/td><\/tr><tr><td>3306<\/td><td>MySQL<\/td><td>TCP<\/td><\/tr><tr><td>8080<\/td><td>HTTP alt<\/td><td>TCP<\/td><\/tr><tr><td>8443<\/td><td>HTTPS alt<\/td><td>TCP<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>The well known ports range covers 0 through 1023. These belong to privileged services, and your OS expects them to stay that way. The registered ports range, 1024 through 49151, houses things like databases, application servers, and other software that&#8217;s already claimed its turf.<\/p>\n\n\n\n<p>Your best bet is the dynamic or private ports range: <strong>49152 to 65535<\/strong>. Nothing&#8217;s pre-assigned there. Pick something random within that range, something like 51832 or 62419, and you&#8217;re unlikely to hit a conflict. Avoid &#8220;clever&#8221; choices like 2222 or 2200. Those are the second place attackers check after 22.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-how-to-change-the-default-ssh-port\">How to Change the Default SSH Port<\/h2>\n\n\n\n<p>You&#8217;ve got your port number. Now let&#8217;s actually change it. The process is the same whether you&#8217;re on Ubuntu, Debian, CentOS, or any other mainstream Linux distro. Five steps, and you should leave your current SSH session open the entire time. If something goes wrong, that open session is your lifeline.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-connect-to-your-server-via-ssh\">Connect to Your Server via SSH<\/h3>\n\n\n\n<p>Open a terminal. On macOS or Linux, it&#8217;s built in. On Windows, grab PuTTY or use the built-in OpenSSH client if you&#8217;re on Windows 10+. Run the SSH login command:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ssh username@server_ip<\/code><\/pre>\n\n\n\n<p>Enter your password when prompted. If you&#8217;ve already set up SSH key authentication (and you should), it&#8217;ll log you straight in. Either way, get a root shell or make sure you can run <code>sudo<\/code>. You&#8217;ll need elevated privileges for everything that follows.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-edit-the-sshd-config-file\">Edit the sshd_config File<\/h3>\n\n\n\n<p>The SSH daemon&#8217;s behavior is controlled by a single configuration file. Open it:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo nano \/etc\/ssh\/sshd_config<\/code><\/pre>\n\n\n\n<p>Scroll until you find the line that reads <strong><code>#Port 22<\/code><\/strong>. The hash means it&#8217;s commented out, and SSH is using the default port 22 implicitly. Remove the <code><strong>#<\/strong> <\/code>and change <code><strong>22<\/strong> <\/code>to your chosen port number:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Port 51832<\/code><\/pre>\n\n\n\n<p>That&#8217;s it for the SSH configuration file. Save and exit nano with <strong>Ctrl+X<\/strong>, then <strong>Y<\/strong>, then <strong>Enter<\/strong>. On some newer Ubuntu systems (22.10+), SSH may use a socket-based configuration instead. If your sshd_config change doesn&#8217;t stick, check <strong><code>\/etc\/ssh\/sshd_config.d\/<\/code><\/strong> for override files or look into <strong><code>systemd socket activation<\/code><\/strong> settings.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-update-firewall-rules-for-the-new-port\">Update Firewall Rules for the New Port<\/h3>\n\n\n\n<p>Here&#8217;s where people lock themselves out. You&#8217;ve told SSH to listen on a new port, but your firewall is still only allowing traffic on 22. If you restart SSH now, you&#8217;re done. Locked out. Game over.<\/p>\n\n\n\n<p>If you&#8217;re running UFW (the standard on Ubuntu and Debian), allow your new port first:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo ufw allow 51832\/tcp<\/code><\/pre>\n\n\n\n<p>Reload to apply:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo ufw reload<\/code><\/pre>\n\n\n\n<p>Verify the UFW firewall rules are in place:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo ufw status<\/code><\/pre>\n\n\n\n<p>You should see your new port listed as ALLOW. If you&#8217;re using <strong>firewalld<\/strong> (common on CentOS\/RHEL), the equivalent is:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo firewall-cmd --permanent --add-port=51832\/tcp &amp;&amp; sudo firewall-cmd --reload<\/code><\/pre>\n\n\n\n<p>If you&#8217;ve never configured any firewall rules at all, and <strong><code>ufw status<\/code><\/strong> shows &#8220;inactive&#8221;, you can skip this step. But you should probably fix that soon. Running a server without a firewall is asking for trouble.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-restart-the-ssh-service\">Restart the SSH Service<\/h3>\n\n\n\n<p>Now apply the new configuration by restarting the SSH daemon. On any modern distro using <code>systemd<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo systemctl restart sshd<\/code><\/pre>\n\n\n\n<p>On older systems still running SysVinit:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo service ssh restart<\/code><\/pre>\n\n\n\n<p>Check that the restart went cleanly:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo systemctl status sshd<\/code><\/pre>\n\n\n\n<p>You want to see &#8220;active (running)&#8221; with no errors. If it failed, you&#8217;ve got a typo in <code>sshd_config<\/code>. Go back and fix it. Your current session is still alive, so you can still make changes.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-verify-the-new-ssh-port-is-active\">Verify the New SSH Port Is Active<\/h3>\n\n\n\n<p>Don&#8217;t just trust that the restart worked. Verify. There are two things to check: the port is open, and you can actually connect through it.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"h-check-the-port-with-ss-or-netstat\">Check the Port with ss or netstat<\/h4>\n\n\n\n<p>The <code>ss <\/code>command is the modern replacement for netstat on Linux. Run:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ss -tuln | grep 51832<\/code><\/pre>\n\n\n\n<p>You should see a LISTEN entry on your port. If you prefer the older netstat approach:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>netstat -tuln | grep 51832<\/code><\/pre>\n\n\n\n<p>Same output, different tool. If nothing shows up, SSH isn&#8217;t listening on that port. Go back to the <code>sshd_config<\/code> file and double-check your edit.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"h-log-in-using-the-new-ssh-port\">Log In Using the New SSH Port<\/h4>\n\n\n\n<p>Open a <strong>new<\/strong> terminal window. Do not close your existing session. Test the connection using the <strong><code>-<\/code><\/strong><code><strong>p<\/strong> <\/code>flag:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ssh -p 51832 username@server_ip<\/code><\/pre>\n\n\n\n<p>If you get a shell, you&#8217;re good. The SSH port change worked. If the connection hangs or gets refused, something&#8217;s wrong with your firewall rules or the sshd_config edit. Your old session is still open, so go troubleshoot.<\/p>\n\n\n\n<p>Once you&#8217;ve confirmed the new port works, you can optionally remove the old port 22 rule from your firewall:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo ufw deny 22\/tcp &amp;&amp; sudo ufw reload<\/code><\/pre>\n\n\n\n<p>Only do this after you&#8217;re 100% sure the new port is working. There&#8217;s no undo if you lock yourself out.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-ssh-port-change-faq\">SSH Port Change FAQ<\/h2>\n\n\n\n<div class=\"schema-faq wp-block-yoast-faq-block\"><div class=\"schema-faq-section\" id=\"faq-question-1773928138223\"><strong class=\"schema-faq-question\">Why Change the Default SSH Port?<\/strong> <p class=\"schema-faq-answer\">Automated SSH brute force attacks target port 22 by default. Thousands of bots scan entire IP ranges looking for that open door. Moving to a non-standard port doesn&#8217;t stop a determined attacker who runs a full port scan, but it eliminates the drive-by noise. Your auth logs get quieter. Your fail2ban stops working overtime. It&#8217;s basic security hygiene paired with proper measures like SSH key authentication and disabling root login.<\/p> <\/div> <div class=\"schema-faq-section\" id=\"faq-question-1773928148211\"><strong class=\"schema-faq-question\">Which Port Is Best for SSH?<\/strong> <p class=\"schema-faq-answer\">Anything in the 49152 to 65535 range that you can remember. Avoid &#8220;creative&#8221; ports like 2222, 8022, or 443 (which collides with HTTPS). The ssh port number you pick doesn&#8217;t matter much as long as it&#8217;s not a well known port, not used by another service on your box, and not something an attacker would guess on their second try. Random is good. Write it down somewhere safe.<\/p> <\/div> <div class=\"schema-faq-section\" id=\"faq-question-1773928156645\"><strong class=\"schema-faq-question\">Does Changing SSH Port Affect Performance?<\/strong> <p class=\"schema-faq-answer\">No. Zero impact. You&#8217;re changing which numbered door the SSH daemon answers on, not how it processes connections. The encryption, the key exchange, the data transfer, all of it works identically regardless of the port. Your Linux server performance stays exactly the same. The only thing that changes is the entry point, and the fact that your server stops showing up in lazy port scans targeting the default SSH port.<\/p> <\/div> <\/div>\n","protected":false},"excerpt":{"rendered":"<p>Every bot on the internet knows that SSH lives on port 22. It&#8217;s the first door they knock on. Within minutes of spinning up a fresh Linux server, you&#8217;ll see login attempts from IPs you&#8217;ve never heard of, hammering away at the default SSH port number with dictionary attacks. The logs fill up fast. Changing [&hellip;]<\/p>\n","protected":false},"author":44,"featured_media":28873,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"_uag_custom_page_level_css":"","site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"set","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[18],"tags":[],"ppma_author":[3402],"class_list":["post-28668","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-tutorials"],"uagb_featured_image_src":{"full":["https:\/\/contabo.com\/blog\/wp-content\/uploads\/2026\/03\/blog-head_how2-change-the-ssh-port-on-a-linux-server.webp",1200,630,false],"thumbnail":["https:\/\/contabo.com\/blog\/wp-content\/uploads\/2026\/03\/blog-head_how2-change-the-ssh-port-on-a-linux-server-150x150.webp",150,150,true],"medium":["https:\/\/contabo.com\/blog\/wp-content\/uploads\/2026\/03\/blog-head_how2-change-the-ssh-port-on-a-linux-server-600x315.webp",600,315,true],"medium_large":["https:\/\/contabo.com\/blog\/wp-content\/uploads\/2026\/03\/blog-head_how2-change-the-ssh-port-on-a-linux-server-768x403.webp",768,403,true],"large":["https:\/\/contabo.com\/blog\/wp-content\/uploads\/2026\/03\/blog-head_how2-change-the-ssh-port-on-a-linux-server.webp",1200,630,false],"1536x1536":["https:\/\/contabo.com\/blog\/wp-content\/uploads\/2026\/03\/blog-head_how2-change-the-ssh-port-on-a-linux-server.webp",1200,630,false],"2048x2048":["https:\/\/contabo.com\/blog\/wp-content\/uploads\/2026\/03\/blog-head_how2-change-the-ssh-port-on-a-linux-server.webp",1200,630,false]},"uagb_author_info":{"display_name":"Milan Ivanovic","author_link":"https:\/\/contabo.com\/blog\/author\/milan\/"},"uagb_comment_info":0,"uagb_excerpt":"Every bot on the internet knows that SSH lives on port 22. It&#8217;s the first door they knock on. Within minutes of spinning up a fresh Linux server, you&#8217;ll see login attempts from IPs you&#8217;ve never heard of, hammering away at the default SSH port number with dictionary attacks. The logs fill up fast. Changing&hellip;","authors":[{"term_id":3402,"user_id":0,"is_guest":1,"slug":"contabro","display_name":"ContaBro","avatar_url":"https:\/\/secure.gravatar.com\/avatar\/?s=96&d=mm&r=g","0":null,"1":"","2":"","3":"","4":"","5":"","6":"","7":"","8":""}],"_links":{"self":[{"href":"https:\/\/contabo.com\/blog\/wp-json\/wp\/v2\/posts\/28668","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/contabo.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/contabo.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/contabo.com\/blog\/wp-json\/wp\/v2\/users\/44"}],"replies":[{"embeddable":true,"href":"https:\/\/contabo.com\/blog\/wp-json\/wp\/v2\/comments?post=28668"}],"version-history":[{"count":13,"href":"https:\/\/contabo.com\/blog\/wp-json\/wp\/v2\/posts\/28668\/revisions"}],"predecessor-version":[{"id":29627,"href":"https:\/\/contabo.com\/blog\/wp-json\/wp\/v2\/posts\/28668\/revisions\/29627"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/contabo.com\/blog\/wp-json\/wp\/v2\/media\/28873"}],"wp:attachment":[{"href":"https:\/\/contabo.com\/blog\/wp-json\/wp\/v2\/media?parent=28668"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/contabo.com\/blog\/wp-json\/wp\/v2\/categories?post=28668"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/contabo.com\/blog\/wp-json\/wp\/v2\/tags?post=28668"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/contabo.com\/blog\/wp-json\/wp\/v2\/ppma_author?post=28668"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}