{"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-06-19T07:50:58","modified_gmt":"2026-06-19T07:50:58","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":"How to Secure SSH on Your VPS: Fail2ban, Key Authentication, and Firewall Setup"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">SSH is the primary access point for managing your Linux VPS \u2014 and it is also the most attacked service on the internet. Bots continuously scan for open SSH ports and attempt brute-force logins. Within hours of provisioning a new VPS with default SSH settings, you will see thousands of failed login attempts. This tutorial walks you through hardening SSH access on your VPS using proven security techniques. For VPS providers that include DDoS protection and managed firewalls, check the <a href=\"https:\/\/virtualserversvps.com\/#providers\">provider comparison table<\/a>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">1. SSH Key Authentication \u2014 Disable Password Logins<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">SSH keys use asymmetric cryptography (Ed25519 or 4096-bit RSA), which is practically uncrackable.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Generate an SSH Key Pair<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">On your local machine, generate a new key pair:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ssh-keygen -t ed25519 -a 100 -f ~\/.ssh\/vps_key<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Install the Public Key on Your VPS<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>ssh-copy-id -i ~\/.ssh\/vps_key.pub user@YOUR_VPS_IP<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Disable Password Authentication<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Edit <code>\/etc\/ssh\/sshd_config<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>PasswordAuthentication no\nPermitEmptyPasswords no\nPermitRootLogin no<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Restart SSH: <code>sudo systemctl restart sshd<\/code>. Keep your existing SSH session open while testing.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">2. Change the Default SSH Port<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Changing SSH from port 22 to a high-numbered port (e.g., 2222) eliminates 99% of automated attacks:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Port 2222<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Connect with: <code>ssh -p 2222 user@YOUR_VPS_IP<\/code>. Update your firewall rules.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">3. Install and Configure Fail2ban<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Fail2ban scans logs for repeated failed authentication attempts and bans the offending IP:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo apt update &amp;&amp; sudo apt install -y fail2ban<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Create <code>\/etc\/fail2ban\/jail.local<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>[DEFAULT]\nbantime = 3600\nfindtime = 600\nmaxretry = 5\n\n[sshd]\nenabled = true\nport = ssh\nlogpath = %(sshd_log)s\nbackend = %(sshd_backend)s<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Enable and start fail2ban:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo systemctl enable fail2ban\nsudo systemctl start fail2ban\nsudo fail2ban-client status sshd<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">4. Configure the Firewall (UFW)<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo ufw default deny incoming\nsudo ufw default allow outgoing\nsudo ufw allow 2222\/tcp comment 'SSH'\nsudo ufw allow 80\/tcp comment 'HTTP'\nsudo ufw allow 443\/tcp comment 'HTTPS'\nsudo ufw enable\nsudo ufw status verbose<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">5. Advanced SSH Hardening<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Add these to <code>\/etc\/ssh\/sshd_config<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Protocol 2\nClientAliveInterval 300\nClientAliveCountMax 0\nAllowUsers yourusername\nX11Forwarding no\nMaxAuthTries 3\nMaxSessions 2<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">6. Monitor SSH Access<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>last -10\nsudo journalctl -u sshd --since today | grep \"Failed password\"\nsudo tail -100 \/var\/log\/auth.log | grep -E \"(ssh|sshd)\"<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Summary Checklist<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>SSH key authentication configured, passwords disabled<\/li>\n<li>Root login disabled<\/li>\n<li>Default SSH port changed<\/li>\n<li>Fail2ban installed and monitoring sshd<\/li>\n<li>Firewall configured<\/li>\n<li>SSH idle timeout and session limits set<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">Securing SSH is one of the most impactful steps to protect your VPS. For VPS providers with additional security features, visit the <a href=\"https:\/\/virtualserversvps.com\/#providers\">provider comparison page<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>SSH is the primary access point for managing your Linux VPS \u2014 and it is also the most attacked service on the internet. Bots continuously scan for open SSH ports&#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":0,"footnotes":""},"categories":[4,1],"tags":[],"class_list":["post-462","post","type-post","status-publish","format-standard","hentry","category-security-compliance","category-vps-guides-tutorials"],"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>How to Secure SSH on Your VPS: Fail2ban, Key Authentication, and Firewall Setup - 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=\"How to Secure SSH on Your VPS: Fail2ban, Key Authentication, and Firewall Setup\" \/>\n<meta property=\"og:description\" content=\"How to Secure SSH on Your VPS: Fail2ban, Key Authentication, and Firewall Setup\" \/>\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 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=\"2 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\":\"How to Secure SSH on Your VPS: Fail2ban, Key Authentication, and Firewall Setup - Virtual Servers VPS Blog\",\"isPartOf\":{\"@id\":\"https:\/\/virtualserversvps.com\/blog\/#website\"},\"datePublished\":\"2026-06-19T07:50:58+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\":\"How to Secure SSH on Your VPS: Fail2ban, Key Authentication, and Firewall Setup\"}]},{\"@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":"How to Secure SSH on Your VPS: Fail2ban, Key Authentication, and Firewall Setup - 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":"How to Secure SSH on Your VPS: Fail2ban, Key Authentication, and Firewall Setup","og_description":"How to Secure SSH on Your VPS: Fail2ban, Key Authentication, and Firewall Setup","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","author":"Virtual-Servers-Vps-Editor","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Virtual-Servers-Vps-Editor","Est. reading time":"2 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":"How to Secure SSH on Your VPS: Fail2ban, Key Authentication, and Firewall Setup - Virtual Servers VPS Blog","isPartOf":{"@id":"https:\/\/virtualserversvps.com\/blog\/#website"},"datePublished":"2026-06-19T07:50:58+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":"How to Secure SSH on Your VPS: Fail2ban, Key Authentication, and Firewall Setup"}]},{"@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":1,"href":"https:\/\/virtualserversvps.com\/blog\/wp-json\/wp\/v2\/posts\/462\/revisions"}],"predecessor-version":[{"id":463,"href":"https:\/\/virtualserversvps.com\/blog\/wp-json\/wp\/v2\/posts\/462\/revisions\/463"}],"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}]}}