{"id":462,"date":"2026-06-19T07:50:58","date_gmt":"2026-06-19T07:50:58","guid":{"rendered":"https:\/\/virtualserversvps.com\/blog\/?p=462"},"modified":"2026-08-03T22:14:10","modified_gmt":"2026-08-03T22:14:10","slug":"how-to-secure-ssh-on-your-vps-fail2ban-key-authentication-and-firewall-setup","status":"publish","type":"post","link":"https:\/\/virtualserversvps.com\/blog\/how-to-secure-ssh-on-your-vps-fail2ban-key-authentication-and-firewall-setup\/","title":{"rendered":"SSH Hardening Best Practices: Key-Only Authentication and a Locked-Down sshd on Your VPS"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">SSH is the most attacked service on the internet. Within hours of provisioning a VPS with default settings, authentication logs show thousands of failed login attempts from botnets scanning port 22 and spraying common passwords. The defaults are exactly what attackers want: password authentication enabled, root allowed to log in directly, and no rate limiting. Hardening SSH is a one-time task that takes about fifteen minutes and eliminates entire classes of attacks before they reach your application. This guide covers the order of operations that matters.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The steps below assume a fresh VPS with a root login and an IP you control from a fixed location. If you have not chosen a provider yet, <a href=\"https:\/\/virtualserversvps.com\/#providers\">compare plans side by side<\/a> \u2014 hosts that include native DDoS filtering and a dedicated IPv4 address make SSH hardening noticeably more effective, because attackers cannot hide your entry point behind a shared NAT.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Step 1: Move to Key-Based Authentication<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Ed25519 keys are fast, short, and considered secure. Generate one on your local machine and copy it to the server:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ssh-keygen -t ed25519 -a 100 -f ~\/.ssh\/vps_ed25519\nssh-copy-id -i ~\/.ssh\/vps_ed25519.pub root@YOUR_SERVER_IP<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Verify that the key works end to end (<code>ssh -i ~\/.ssh\/vps_ed25519 root@YOUR_SERVER_IP<\/code>) before disabling passwords. Keep the private key on a machine with disk encryption, and consider adding a passphrase plus <code>ssh-agent<\/code> so the key is not usable if the laptop is stolen. Add <code>IdentitiesOnly yes<\/code> to <code>~\/.ssh\/config<\/code> for the host so ssh does not try every key in your agent and trip <code>MaxAuthTries<\/code> on the server.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Step 2: Lock Down sshd_config<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Edit <code>\/etc\/ssh\/sshd_config<\/code> and apply these settings, then create a non-root user for daily work:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>PermitRootLogin no\nPasswordAuthentication no\nKbdInteractiveAuthentication no\nPubkeyAuthentication yes\nMaxAuthTries 3\nMaxSessions 4\nLoginGraceTime 20\nAllowUsers deploy\n# optional: restrict to a single port and listen address\n# Port 2222\n# ListenAddress 10.0.0.5<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Create the user with <code>adduser deploy<\/code>, add your public key to <code>\/home\/deploy\/.ssh\/authorized_keys<\/code>, and grant sudo with <code>usermod -aG sudo deploy<\/code>. Then lock down the file permissions that OpenSSH is strict about: <code>chmod 700 \/home\/deploy\/.ssh<\/code> and <code>chmod 600 \/home\/deploy\/.ssh\/authorized_keys<\/code> \u2014 if the key file is world-readable or the directory is group-writable, sshd silently ignores it. Finally run <code>sshd -t<\/code> to validate the config, and reload with <code>systemctl reload ssh<\/code>. Always keep a second terminal session open when testing \u2014 if you lock yourself out, you can still revert from the other session.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Step 3: Rate-Limit SSH at the Firewall<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Key-only authentication already stops password brute force, but rate limiting also throttles connection floods that can exhaust <code>sshd<\/code> child processes. You do not need a separate daemon for this \u2014 nftables (the default firewall on Debian 12 and Ubuntu 22.04+) can limit new SSH connections natively:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>table inet filter {\n    chain input {\n        type filter hook input priority filter; policy drop;\n        ct state established,related accept\n        tcp dport 22 ct state new limit rate 4\/minute burst 8 accept\n        tcp dport 22 ct state new drop\n        tcp dport 80,443 accept\n    }\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">This accepts at most four new SSH connections per minute with a burst of eight \u2014 plenty for legitimate use, brutal for scanners. The same table can also drop invalid packets (<code>ct state invalid drop<\/code>) and log rejected attempts to a dedicated chain. If you prefer a ready-made tool, Fail2ban wraps this idea with per-IP banning, but the native rules above need no extra service and survive reboots via <code>nft list ruleset &gt; \/etc\/nftables.conf<\/code>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Step 4: Optional Two-Factor Authentication<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">For root access to production servers, add a second factor with the PAM Google Authenticator module: install <code>libpam-google-authenticator<\/code>, run <code>google-authenticator<\/code> as the user, and add <code>auth required pam_google_authenticator.so<\/code> to <code>\/etc\/pam.d\/sshd<\/code> together with <code>AuthenticationMethods publickey,keyboard-interactive<\/code> in <code>sshd_config<\/code>. The result is key-plus-TOTP authentication \u2014 even a stolen private key is useless without the phone.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Step 5: Watch the Authentication Log<\/h2>\n\n\n<!-- wp:list\">\n<ul><li>Follow login activity in real time: <code>journalctl -u ssh -f<\/code> (Debian\/Ubuntu) or <code>tail -f \/var\/log\/secure<\/code> (RHEL family).<\/li><li>Audit successful logins weekly: <code>journalctl -u ssh | grep \"Accepted publickey\"<\/code>.<\/li><li>Check for unexpected key changes: <code>sha256sum \/etc\/ssh\/ssh_host_ed25519_key.pub<\/code> and compare against your records \u2014 a changed host key signals a compromised or replaced server.<\/li><li>Watch failed auth counts over time; after this hardening, they should drop to near zero because scanners give up on rate-limited, key-only endpoints.<\/li><\/ul>\n<!-- \/wp:post-content -->\n\n<!-- wp:paragraph -->\n<p>Harden SSH once, on every server, before anything else touches the network. The full sequence \u2014 keys, locked-down sshd, native rate limiting, optional 2FA, and log monitoring \u2014 takes minutes and removes the most common entry point attackers use. If you are evaluating hosts for a new deployment, <a href=\"https:\/\/virtualserversvps.com\/#providers\">our comparison table<\/a> covers which providers include DDoS protection and managed firewalls on top of the base specs.<\/p>\n<!-- \/wp:paragraph -->\n\n<!-- wp:paragraph -->\n<p>Ready to put this into practice? <a href=\"https:\/\/interserver.net\/vps?id=1067805&amp;sid=virtualserversvps\" target=\"_blank\" rel=\"noreferrer noopener sponsored\">InterServer&#8217;s VPS plans<\/a> give you full root access with a dedicated IP from the first month, so you can apply exactly this hardening without platform restrictions.<\/p>\n<!-- \/wp:paragraph -->\n","protected":false},"excerpt":{"rendered":"<p>SSH is the most attacked service on the internet. Within hours of provisioning a VPS with default settings, authentication logs show thousands of failed login attempts from botnets scanning port&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":2,"footnotes":""},"categories":[4],"tags":[],"class_list":["post-462","post","type-post","status-publish","format-standard","hentry","category-security-compliance"],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v26.1 (Yoast SEO v26.1) - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>SSH Hardening Best Practices: Key-Only Authentication and a Locked-Down sshd on Your VPS - Virtual Servers VPS Blog<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/virtualserversvps.com\/blog\/how-to-secure-ssh-on-your-vps-fail2ban-key-authentication-and-firewall-setup\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"SSH Hardening Best Practices: Key-Only Authentication and a Locked-Down sshd on Your VPS\" \/>\n<meta property=\"og:description\" content=\"SSH Hardening Best Practices: Key-Only Authentication and a Locked-Down sshd on Your VPS\" \/>\n<meta property=\"og:url\" content=\"https:\/\/virtualserversvps.com\/blog\/how-to-secure-ssh-on-your-vps-fail2ban-key-authentication-and-firewall-setup\/\" \/>\n<meta property=\"og:site_name\" content=\"Virtual Servers VPS Blog\" \/>\n<meta property=\"article:published_time\" content=\"2026-06-19T07:50:58+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2026-08-03T22:14:10+00:00\" \/>\n<meta name=\"author\" content=\"Virtual-Servers-Vps-Editor\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Virtual-Servers-Vps-Editor\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/virtualserversvps.com\/blog\/how-to-secure-ssh-on-your-vps-fail2ban-key-authentication-and-firewall-setup\/\",\"url\":\"https:\/\/virtualserversvps.com\/blog\/how-to-secure-ssh-on-your-vps-fail2ban-key-authentication-and-firewall-setup\/\",\"name\":\"SSH Hardening Best Practices: Key-Only Authentication and a Locked-Down sshd on Your VPS - Virtual Servers VPS Blog\",\"isPartOf\":{\"@id\":\"https:\/\/virtualserversvps.com\/blog\/#website\"},\"datePublished\":\"2026-06-19T07:50:58+00:00\",\"dateModified\":\"2026-08-03T22:14:10+00:00\",\"author\":{\"@id\":\"https:\/\/virtualserversvps.com\/blog\/#\/schema\/person\/82a299a8284a66ff49f97c74684724a0\"},\"breadcrumb\":{\"@id\":\"https:\/\/virtualserversvps.com\/blog\/how-to-secure-ssh-on-your-vps-fail2ban-key-authentication-and-firewall-setup\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/virtualserversvps.com\/blog\/how-to-secure-ssh-on-your-vps-fail2ban-key-authentication-and-firewall-setup\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/virtualserversvps.com\/blog\/how-to-secure-ssh-on-your-vps-fail2ban-key-authentication-and-firewall-setup\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/virtualserversvps.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"SSH Hardening Best Practices: Key-Only Authentication and a Locked-Down sshd on Your VPS\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/virtualserversvps.com\/blog\/#website\",\"url\":\"https:\/\/virtualserversvps.com\/blog\/\",\"name\":\"Virtual Servers VPS Blog\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/virtualserversvps.com\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/virtualserversvps.com\/blog\/#\/schema\/person\/82a299a8284a66ff49f97c74684724a0\",\"name\":\"Virtual-Servers-Vps-Editor\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/virtualserversvps.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/d820b15f1cd028e97610d9adf536df7be5cb6423869967037d468d5355fa003f?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/d820b15f1cd028e97610d9adf536df7be5cb6423869967037d468d5355fa003f?s=96&d=mm&r=g\",\"caption\":\"Virtual-Servers-Vps-Editor\"},\"sameAs\":[\"https:\/\/virtualserversvps.com\/blog\"],\"url\":\"https:\/\/virtualserversvps.com\/blog\/author\/virtualserversvps\/\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"SSH Hardening Best Practices: Key-Only Authentication and a Locked-Down sshd on Your VPS - Virtual Servers VPS Blog","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/virtualserversvps.com\/blog\/how-to-secure-ssh-on-your-vps-fail2ban-key-authentication-and-firewall-setup\/","og_locale":"en_US","og_type":"article","og_title":"SSH Hardening Best Practices: Key-Only Authentication and a Locked-Down sshd on Your VPS","og_description":"SSH Hardening Best Practices: Key-Only Authentication and a Locked-Down sshd on Your VPS","og_url":"https:\/\/virtualserversvps.com\/blog\/how-to-secure-ssh-on-your-vps-fail2ban-key-authentication-and-firewall-setup\/","og_site_name":"Virtual Servers VPS Blog","article_published_time":"2026-06-19T07:50:58+00:00","article_modified_time":"2026-08-03T22:14:10+00:00","author":"Virtual-Servers-Vps-Editor","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Virtual-Servers-Vps-Editor","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/virtualserversvps.com\/blog\/how-to-secure-ssh-on-your-vps-fail2ban-key-authentication-and-firewall-setup\/","url":"https:\/\/virtualserversvps.com\/blog\/how-to-secure-ssh-on-your-vps-fail2ban-key-authentication-and-firewall-setup\/","name":"SSH Hardening Best Practices: Key-Only Authentication and a Locked-Down sshd on Your VPS - Virtual Servers VPS Blog","isPartOf":{"@id":"https:\/\/virtualserversvps.com\/blog\/#website"},"datePublished":"2026-06-19T07:50:58+00:00","dateModified":"2026-08-03T22:14:10+00:00","author":{"@id":"https:\/\/virtualserversvps.com\/blog\/#\/schema\/person\/82a299a8284a66ff49f97c74684724a0"},"breadcrumb":{"@id":"https:\/\/virtualserversvps.com\/blog\/how-to-secure-ssh-on-your-vps-fail2ban-key-authentication-and-firewall-setup\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/virtualserversvps.com\/blog\/how-to-secure-ssh-on-your-vps-fail2ban-key-authentication-and-firewall-setup\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/virtualserversvps.com\/blog\/how-to-secure-ssh-on-your-vps-fail2ban-key-authentication-and-firewall-setup\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/virtualserversvps.com\/blog\/"},{"@type":"ListItem","position":2,"name":"SSH Hardening Best Practices: Key-Only Authentication and a Locked-Down sshd on Your VPS"}]},{"@type":"WebSite","@id":"https:\/\/virtualserversvps.com\/blog\/#website","url":"https:\/\/virtualserversvps.com\/blog\/","name":"Virtual Servers VPS Blog","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/virtualserversvps.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/virtualserversvps.com\/blog\/#\/schema\/person\/82a299a8284a66ff49f97c74684724a0","name":"Virtual-Servers-Vps-Editor","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/virtualserversvps.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/d820b15f1cd028e97610d9adf536df7be5cb6423869967037d468d5355fa003f?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/d820b15f1cd028e97610d9adf536df7be5cb6423869967037d468d5355fa003f?s=96&d=mm&r=g","caption":"Virtual-Servers-Vps-Editor"},"sameAs":["https:\/\/virtualserversvps.com\/blog"],"url":"https:\/\/virtualserversvps.com\/blog\/author\/virtualserversvps\/"}]}},"_links":{"self":[{"href":"https:\/\/virtualserversvps.com\/blog\/wp-json\/wp\/v2\/posts\/462","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/virtualserversvps.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/virtualserversvps.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/virtualserversvps.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/virtualserversvps.com\/blog\/wp-json\/wp\/v2\/comments?post=462"}],"version-history":[{"count":2,"href":"https:\/\/virtualserversvps.com\/blog\/wp-json\/wp\/v2\/posts\/462\/revisions"}],"predecessor-version":[{"id":790,"href":"https:\/\/virtualserversvps.com\/blog\/wp-json\/wp\/v2\/posts\/462\/revisions\/790"}],"wp:attachment":[{"href":"https:\/\/virtualserversvps.com\/blog\/wp-json\/wp\/v2\/media?parent=462"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/virtualserversvps.com\/blog\/wp-json\/wp\/v2\/categories?post=462"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/virtualserversvps.com\/blog\/wp-json\/wp\/v2\/tags?post=462"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}