{"id":1365,"date":"2026-09-03T11:37:19","date_gmt":"2026-09-03T06:07:19","guid":{"rendered":"https:\/\/cpanelfree.com\/blog\/how-to-configure-ufw-firewall-ubuntu\/"},"modified":"2026-09-03T12:32:44","modified_gmt":"2026-09-03T07:02:44","slug":"how-to-configure-ufw-firewall-ubuntu","status":"publish","type":"post","link":"https:\/\/cpanelfree.com\/blog\/how-to-configure-ufw-firewall-ubuntu\/","title":{"rendered":"How to Configure UFW Firewall on Ubuntu Server (Rules, Ports &amp; Best Practices)"},"content":{"rendered":"<div style=\"background-color: #f8fafc;border-left: 4px solid #10b981;padding: 20px;border-radius: 6px;margin-bottom: 25px\">\n<p style=\"margin: 0;font-size: 16px;color: #1e293b\">\n        <strong>Quick Answer:<\/strong> To secure an Ubuntu VPS using UFW (Uncomplicated Firewall), set the default policy to deny incoming traffic (<code>sudo ufw default deny incoming<\/code>), allow your SSH port (<code>sudo ufw allow 22\/tcp<\/code> or <code>sudo ufw limit ssh<\/code>), allow HTTP\/HTTPS (<code>sudo ufw allow 'Nginx Full'<\/code>), and enable the firewall with <code>sudo ufw enable<\/code>.\n    <\/p>\n<\/div>\n<h2>What is UFW and Why Every Linux VPS Needs It<\/h2>\n<p><strong>UFW (Uncomplicated Firewall)<\/strong> is the default frontend for managing netfilter\/iptables firewall rules in Ubuntu and Debian systems. By default, fresh Linux servers expose all bound service ports to the public internet, leaving internal services (like Redis on port 6379, MySQL on port 3306, and debug listeners) vulnerable to port scans and automated exploits.<\/p>\n<p>Enabling a stateful firewall ensures that only explicitly permitted web and administrative ports accept incoming TCP\/UDP connections.<\/p>\n<h2>Step 1: Setting Default Firewall Policies<\/h2>\n<p>Before allowing specific services, define the baseline inbound and outbound rules:<\/p>\n<pre style=\"background-color: #1e293b;color: #38bdf8;padding: 14px;border-radius: 6px;font-size: 13px\"># Deny all unsolicited incoming connections\nsudo ufw default deny incoming\n\n# Allow all outbound server connections\nsudo ufw default allow outgoing<\/pre>\n<h2>Step 2: Allowing SSH Access (Prevent Lockout!)<\/h2>\n<p><strong>Critical Warning:<\/strong> Never enable UFW without explicitly allowing your SSH management port first, otherwise your active session will be terminated upon activation.<\/p>\n<pre style=\"background-color: #1e293b;color: #38bdf8;padding: 14px;border-radius: 6px;font-size: 13px\"># Allow standard SSH port 22\nsudo ufw allow 22\/tcp\n\n# RECOMMENDED: Rate-limit SSH to block brute-force bots\nsudo ufw limit 22\/tcp\n\n# If you use a custom SSH port (e.g., 22022)\nsudo ufw allow 22022\/tcp<\/pre>\n<h2>Step 3: Opening Web Server &amp; Control Panel Ports<\/h2>\n<p>Open the standard networking ports required for web hosting, SSL encryption, and control panel management:<\/p>\n<pre style=\"background-color: #1e293b;color: #38bdf8;padding: 14px;border-radius: 6px;font-size: 13px\"># Open HTTP (Port 80) and HTTPS (Port 443)\nsudo ufw allow 80\/tcp\nsudo ufw allow 443\/tcp\n\n# Optional: Open Control Panel Web GUIs\n# CyberPanel Port 8090\nsudo ufw allow 8090\/tcp\n\n# aaPanel Port 7800 \/ 8888\nsudo ufw allow 7800\/tcp\n\n# FastPanel Port 8888\nsudo ufw allow 8888\/tcp<\/pre>\n<h2>Step 4: Enabling and Verifying UFW Status<\/h2>\n<p>Enable the firewall service and verify the active rule table:<\/p>\n<pre style=\"background-color: #1e293b;color: #38bdf8;padding: 14px;border-radius: 6px;font-size: 13px\"># Enable firewall on boot\nsudo ufw enable\n\n# Inspect numbered active rules\nsudo ufw status numbered<\/pre>\n<h2>Advanced UFW Management: IP Whitelisting &amp; Rule Deletion<\/h2>\n<p>To restrict database or admin panel access to your specific office static IP address:<\/p>\n<pre style=\"background-color: #1e293b;color: #38bdf8;padding: 14px;border-radius: 6px;font-size: 13px\"># Allow only your static IP to access MySQL on port 3306\nsudo ufw allow from 203.0.113.50 to any port 3306 proto tcp\n\n# Delete an obsolete rule by its rule number\nsudo ufw delete 4<\/pre>\n<h2>Advanced UFW Port Forwarding &amp; NAT Routing Rules<\/h2>\n<p>If your VPS acts as a gateway or Docker host, you can configure UFW to route traffic from public ports to private container networks using Network Address Translation (NAT). Edit <code>\/etc\/ufw\/before.rules<\/code> to add nat table forwarding:<\/p>\n<pre style=\"background-color: #1e293b;color: #38bdf8;padding: 14px;border-radius: 6px;font-size: 13px\"># NAT table rules for Docker container routing\n*nat\n:PREROUTING ACCEPT [0:0]\n:POSTROUTING ACCEPT [0:0]\n-A PREROUTING -p tcp --dport 8443 -j REDIRECT --to-ports 443\nCOMMIT<\/pre>\n<h2>Analyzing UFW Log Files for Port Scans and Intrusion Attempts<\/h2>\n<p>UFW logs all dropped packets directly to <code>\/var\/log\/ufw.log<\/code>. You can inspect this log in real time to identify rogue IP addresses scanning your server for unpatched vulnerabilities:<\/p>\n<pre style=\"background-color: #1e293b;color: #38bdf8;padding: 14px;border-radius: 6px;font-size: 13px\"># Monitor blocked connection attempts live\nsudo tail -f \/var\/log\/ufw.log | grep '[UFW BLOCK]'\n\n# Count top 10 attacker IPs attempting unauthorized connections\nsudo grep '[UFW BLOCK]' \/var\/log\/ufw.log | awk '{print $12}' | sort | uniq -c | sort -nr | head -n 10<\/pre>\n<h2>Comprehensive UFW Command Reference Cheat Sheet<\/h2>\n<p>Keep this production command reference handy when managing firewall rules on Ubuntu servers:<\/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\">Command<\/th>\n<th style=\"padding: 10px;border: 1px solid #334155\">Action &amp; Purpose<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr style=\"background-color: #f8fafc\">\n<td style=\"padding: 10px;border: 1px solid #cbd5e1\"><code>sudo ufw status verbose<\/code><\/td>\n<td style=\"padding: 10px;border: 1px solid #cbd5e1\">Display complete firewall status, default policies, and logging level<\/td>\n<\/tr>\n<tr>\n<td style=\"padding: 10px;border: 1px solid #cbd5e1\"><code>sudo ufw allow 22\/tcp<\/code><\/td>\n<td style=\"padding: 10px;border: 1px solid #cbd5e1\">Allow incoming TCP traffic on standard SSH port 22<\/td>\n<\/tr>\n<tr style=\"background-color: #f8fafc\">\n<td style=\"padding: 10px;border: 1px solid #cbd5e1\"><code>sudo ufw deny 3306\/tcp<\/code><\/td>\n<td style=\"padding: 10px;border: 1px solid #cbd5e1\">Explicitly block public internet access to MySQL database port<\/td>\n<\/tr>\n<tr>\n<td style=\"padding: 10px;border: 1px solid #cbd5e1\"><code>sudo ufw allow from 192.168.1.0\/24<\/code><\/td>\n<td style=\"padding: 10px;border: 1px solid #cbd5e1\">Allow all connections originating from a private subnet range<\/td>\n<\/tr>\n<tr style=\"background-color: #f8fafc\">\n<td style=\"padding: 10px;border: 1px solid #cbd5e1\"><code>sudo ufw reset<\/code><\/td>\n<td style=\"padding: 10px;border: 1px solid #cbd5e1\">Reset all UFW rules back to factory default disabled state<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h2>Protecting Internal Databases and Docker Services from Exposure<\/h2>\n<p>A frequent security misconfiguration occurs when installing Docker on Linux. By default, Docker modifies <code>iptables<\/code> directly and bypasses UFW rules, inadvertently exposing published container ports (such as Redis on port 6379 or MongoDB on port 27017) to the public internet.<\/p>\n<p>To ensure Docker respects your firewall restrictions, always bind container port publications to local loopback <code>127.0.0.1<\/code> (e.g. <code>-p 127.0.0.1:6379:6379<\/code>) or configure the Docker daemon with <code>\"iptables\": false<\/code> in <code>\/etc\/docker\/daemon.json<\/code>.<\/p>\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-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-change-ssh-port-linux-vps\/\" style=\"color: #0284c7;text-decoration: none;font-weight: 600\">How to Change Default SSH Port on Linux VPS<\/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\">Built-in Enterprise Security Without CLI Headaches<\/h3>\n<p style=\"color: #94a3b8;font-size: 14px;line-height: 1.6;max-width: 600px;margin: 0 auto 15px\">\n        Tired of configuring Linux firewalls and iptables? <strong>CpanelFree<\/strong> handles all enterprise DDoS mitigation, server hardening, and SSL certificates automatically at $0 cost.\n    <\/p>\n<p>    <a href=\"https:\/\/cpanelfree.com\/#plans\" style=\"display: inline-block;background-color: #10b981;color: #ffffff;padding: 10px 22px;border-radius: 6px;text-decoration: none;font-weight: bold;font-size: 14px\">Claim Free Hosting Account<\/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\">Does enabling UFW slow down server network throughput?<\/h4>\n<p style=\"margin: 0;color: #475569;font-size: 14px\">No. UFW translates rules directly into the Linux Linux kernel netfilter architecture, which processes millions of packets per second with virtually zero CPU overhead.<\/p>\n<\/div>\n<div style=\"border-bottom: 1px solid #e2e8f0;padding: 12px 0\">\n<h4 style=\"margin: 0 0 8px 0;color: #1e293b\">What happens if I accidentally lock myself out with UFW?<\/h4>\n<p style=\"margin: 0;color: #475569;font-size: 14px\">Log in via your cloud VPS provider&#8217;s web-based VNC \/ Out-of-Band Serial Console and run <code>sudo ufw disable<\/code> to regain access immediately.<\/p>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Quick Answer: To secure an Ubuntu VPS using UFW (Uncomplicated Firewall), set the default policy to deny incoming traffic (sudo ufw default deny incoming), allow your SSH port (sudo ufw allow 22\/tcp or sudo ufw limit ssh), allow HTTP\/HTTPS (sudo ufw allow &#8216;Nginx Full&#8217;), and enable the firewall with sudo ufw enable. What is UFW [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":1364,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[64],"tags":[],"class_list":["post-1365","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\/1365","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=1365"}],"version-history":[{"count":3,"href":"https:\/\/cpanelfree.com\/blog\/wp-json\/wp\/v2\/posts\/1365\/revisions"}],"predecessor-version":[{"id":1565,"href":"https:\/\/cpanelfree.com\/blog\/wp-json\/wp\/v2\/posts\/1365\/revisions\/1565"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/cpanelfree.com\/blog\/wp-json\/wp\/v2\/media\/1364"}],"wp:attachment":[{"href":"https:\/\/cpanelfree.com\/blog\/wp-json\/wp\/v2\/media?parent=1365"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cpanelfree.com\/blog\/wp-json\/wp\/v2\/categories?post=1365"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cpanelfree.com\/blog\/wp-json\/wp\/v2\/tags?post=1365"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}