{"id":202,"date":"2025-07-28T16:06:17","date_gmt":"2025-07-28T16:06:17","guid":{"rendered":"https:\/\/hosting.international\/blog\/?p=202"},"modified":"2026-04-14T17:13:50","modified_gmt":"2026-04-14T17:13:50","slug":"securing-your-server-a-beginners-guide-to-firewall-setup-ufw-firewalld","status":"publish","type":"post","link":"https:\/\/hosting.international\/blog\/securing-your-server-a-beginners-guide-to-firewall-setup-ufw-firewalld\/","title":{"rendered":"Securing Your Server: A Beginner&#8217;s Guide to Firewall Setup (UFW &amp; Firewalld)"},"content":{"rendered":"\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"777\" height=\"441\" src=\"https:\/\/hosting.international\/blog\/wp-content\/uploads\/2025\/07\/image-12.png\" alt=\"\" class=\"wp-image-203\" srcset=\"https:\/\/hosting.international\/blog\/wp-content\/uploads\/2025\/07\/image-12.png 777w, https:\/\/hosting.international\/blog\/wp-content\/uploads\/2025\/07\/image-12-300x170.png 300w, https:\/\/hosting.international\/blog\/wp-content\/uploads\/2025\/07\/image-12-768x436.png 768w\" sizes=\"auto, (max-width: 777px) 100vw, 777px\" \/><\/figure>\n\n\n\n<p>In the vast and interconnected world of the internet, your server is a valuable asset, and unfortunately, a potential target. Just like you wouldn&#8217;t leave your home unlocked, you shouldn&#8217;t leave your server unprotected. The very first and most critical step in comprehensive server security is setting up a robust firewall.<\/p>\n\n\n\n<p>A firewall acts as your server&#8217;s digital bouncer, controlling all incoming and outgoing network traffic. It meticulously filters data packets based on a set of defined rules, allowing legitimate connections while blocking suspicious or unauthorized access. This guide will walk you through the essential steps to configure a basic firewall using UFW (Uncomplicated Firewall) for Ubuntu\/Debian-based systems and Firewalld for CentOS\/RHEL.<\/p>\n\n\n\n<p>Why a Firewall is Non-Negotiable for Server Security Implementing a firewall setup is foundational for any VPS security or dedicated server security. Here\u2019s why it\u2019s absolutely essential:<\/p>\n\n\n\n<p><strong>First Line of Defense<\/strong>: A firewall is your primary barrier against unauthorized access attempts, brute-force attacks, and malicious probes targeting your server.<\/p>\n\n\n\n<p><strong>Reduces Attack Surface<\/strong>: By closing all unused ports, a firewall significantly reduces the number of entry points attackers can exploit, minimizing your server&#8217;s vulnerability.<\/p>\n\n\n\n<p><strong>Controls Network Traffic<\/strong>: You gain fine-grained control over both inbound and outbound traffic, deciding exactly which services can communicate with the outside world and vice versa.<\/p>\n\n\n\n<p><strong>Protects Data<\/strong>: By preventing unauthorized access, firewalls play a crucial role in safeguarding your sensitive data and preventing breaches.<\/p>\n\n\n\n<p><strong>Essential for Control<\/strong>: On a VPS or dedicated server, you have direct control and responsibility for your system protection. A firewall gives you the tools to manage this effectively.<\/p>\n\n\n\n<p>Understanding Basic Firewall Concepts Before diving into commands, let&#8217;s clarify a few concepts:<\/p>\n\n\n\n<p><strong>Default Policies<\/strong>: Most firewalls operate on a &#8220;default deny&#8221; incoming policy. This means, by default, all incoming connections are blocked unless explicitly allowed by a rule. Outgoing connections are often allowed by default.<\/p>\n\n\n\n<p><strong>Rules<\/strong>: These are specific instructions that tell the firewall whether to ALLOW or DENY traffic based on criteria like source IP address, destination IP address, protocol (TCP\/UDP), and ports.<\/p>\n\n\n\n<p><strong>Ports<\/strong>: Think of ports as numbered &#8220;doors&#8221; on your server. Each service (e.g., web server, SSH, FTP) uses a specific port (or range of ports) to communicate. For instance, HTTP uses port 80, and HTTPS uses port 443.<\/p>\n\n\n\n<p>Basic Firewall Setup with UFW (for Ubuntu\/Debian) UFW (Uncomplicated Firewall) is a user-friendly frontend for iptables, making firewall management straightforward on Ubuntu and Debian systems.<\/p>\n\n\n\n<p><strong>Check UFW Status (and Install if Needed)<\/strong>: UFW is usually pre-installed on Ubuntu.<\/p>\n\n\n\n<p><code>sudo ufw status verbose<\/code> If not installed, run:<\/p>\n\n\n\n<p><code>sudo apt update<\/code> <code>sudo apt install ufw<\/code> <strong>Set Default Policies<\/strong>: It&#8217;s best practice to deny all incoming connections and allow all outgoing connections by default.<\/p>\n\n\n\n<p><code>sudo ufw default deny incoming<\/code> <code>sudo ufw default allow outgoing<\/code> <strong>Allow Essential Services (CRITICAL FIRST STEP: SSH!)<\/strong> Before enabling UFW, you must allow SSH access, otherwise, you will lock yourself out of your server!<\/p>\n\n\n\n<p>SSH (Port 22):<\/p>\n\n\n\n<p><code>sudo ufw allow ssh<\/code> <code># OR by port number: sudo ufw allow 22\/tcp<\/code> HTTP (Web Server &#8211; for unencrypted traffic, Port 80):<\/p>\n\n\n\n<p><code>sudo ufw allow http<\/code> <code># OR by port number: sudo ufw allow 80\/tcp<\/code> HTTPS (Secure Web Server &#8211; for encrypted traffic, Port 443):<\/p>\n\n\n\n<p><code>sudo ufw allow https<\/code> <code># OR by port number: sudo ufw allow 443\/tcp<\/code> Other Common Services (if needed, use with caution):<\/p>\n\n\n\n<p><code>FTP (Ports 20, 21): sudo ufw allow ftp<\/code><\/p>\n\n\n\n<p><code>MySQL (Port 3306): sudo ufw allow mysql<\/code> (only if accessed externally, generally not recommended)<\/p>\n\n\n\n<p><strong>Enable UFW<\/strong>: After allowing SSH, you can safely enable the firewall.<\/p>\n\n\n\n<p><code>sudo ufw enable<\/code> You will see a warning that enabling it may disrupt existing SSH connections. Type <code>y<\/code> and press Enter.<\/p>\n\n\n\n<p><strong>Check UFW Status<\/strong>: Verify your rules are active.<\/p>\n\n\n\n<p><code>sudo ufw status verbose<\/code> <strong>Deleting Rules<\/strong>: If you need to remove a rule:<\/p>\n\n\n\n<p><code>sudo ufw delete allow 80\/tcp<\/code> <code># OR by rule number (from 'sudo ufw status numbered'): sudo ufw delete 3<\/code> <strong>Disabling\/Resetting UFW (Use with Extreme Caution)<\/strong>:<\/p>\n\n\n\n<p>To disable UFW (removes firewall protection): <code>sudo ufw disable<\/code><\/p>\n\n\n\n<p>To reset UFW to its default state (deletes all custom rules): <code>sudo ufw reset<\/code><\/p>\n\n\n\n<p>Basic Firewall Setup with Firewalld (for CentOS\/RHEL) Firewalld is a dynamic firewall management tool common on CentOS, Fedora, and RHEL. It uses &#8220;zones&#8221; to manage rules based on the trust level of network connections.<\/p>\n\n\n\n<p><strong>Check Firewalld Status (and Install if Needed)<\/strong>:<\/p>\n\n\n\n<p><code>sudo systemctl status firewalld<\/code> If not installed or not running:<\/p>\n\n\n\n<p><code>sudo yum install firewalld<\/code> # or <code>sudo dnf install firewalld<\/code> <code>sudo systemctl start firewalld<\/code> <code>sudo systemctl enable firewalld<\/code> <strong>Check Current Rules<\/strong>: List all active rules in the default (usually public) zone:<\/p>\n\n\n\n<p><code>sudo firewall-cmd --list-all<\/code> <strong>Allow Essential Services (using service names or ports)<\/strong>: Firewalld allows you to open ports by specifying service names (e.g., ssh, http, https) which automatically handle standard port numbers. Changes using <code>--permanent<\/code> will persist after reboot.<\/p>\n\n\n\n<p>SSH:<\/p>\n\n\n\n<p><code>sudo firewall-cmd --permanent --add-service=ssh<\/code> HTTP (Web Server &#8211; Port 80):<\/p>\n\n\n\n<p><code>sudo firewall-cmd --permanent --add-service=http<\/code> HTTPS (Secure Web Server &#8211; Port 443):<\/p>\n\n\n\n<p><code>sudo firewall-cmd --permanent --add-service=https<\/code> Alternatively, by Port Number:<\/p>\n\n\n\n<p><code>sudo firewall-cmd --permanent --add-port=80\/tcp<\/code> <code>sudo firewall-cmd --permanent --add-port=443\/tcp<\/code> <strong>Reload Firewalld<\/strong>: After making <code>--permanent<\/code> changes, you must reload Firewalld for them to take effect immediately.<\/p>\n\n\n\n<p><code>sudo firewall-cmd --reload<\/code> <strong>Deleting Rules<\/strong>: To remove a service or port:<\/p>\n\n\n\n<p><code>sudo firewall-cmd --permanent --remove-service=http<\/code> <code># OR: sudo firewall-cmd --permanent --remove-port=80\/tcp<\/code> Remember to <code>sudo firewall-cmd --reload<\/code> after removal.<\/p>\n\n\n\n<p><strong>Disabling Firewalld (Use with Extreme Caution)<\/strong>:<\/p>\n\n\n\n<p><code>sudo systemctl stop firewalld<\/code> <code>sudo systemctl disable firewalld<\/code> Key Considerations and Best Practices for Server Security <strong>Allow SSH First!<\/strong> This cannot be stressed enough. Always add and verify your SSH rule before enabling a firewall or reloading its configuration. Losing SSH access means losing control of your server.<\/p>\n\n\n\n<p><strong>Principle of Least Privilege<\/strong>: Only open ports that are absolutely necessary for your server&#8217;s functions. Every open port is a potential point of entry for attackers.<\/p>\n\n\n\n<p><strong>Regularly Review Rules<\/strong>: As your server&#8217;s services or applications change, review and update your firewall rules accordingly. Remove any rules that are no longer needed.<\/p>\n\n\n\n<p><strong>Combine with Other Security Measures<\/strong>: A firewall is a crucial layer, but it&#8217;s not the only one. Combine it with strong, unique passwords, regular system updates, intrusion detection systems (like Fail2Ban), and robust website security practices.<\/p>\n\n\n\n<p><strong>Backup Your Server<\/strong>: Before making any major changes to your firewall configuration, ensure you have a recent website backup strategy in place. This provides a safety net in case of misconfigurations.<\/p>\n\n\n\n<p>Conclusion Setting up a firewall is a fundamental step in securing your VPS or dedicated server. By diligently configuring UFW or Firewalld, you create a vital barrier against unauthorized access and significantly reduce your server&#8217;s attack surface. Empower yourself with these essential tools and take control of your server security to protect your digital assets. It&#8217;s a proactive measure that gives you peace of mind and ensures the stability of your online presence.<\/p>\n\n\n\n<p><em>Read more:<\/em><\/p>\n\n\n\n<p><a href=\"https:\/\/hosting.international\/blog\/what-is-virtualization-and-why-is-it-important-for-your-hosting-kvm-openvz-and-more\/\" data-type=\"link\" data-id=\"https:\/\/hosting.international\/blog\/what-is-virtualization-and-why-is-it-important-for-your-hosting-kvm-openvz-and-more\/\">What is Virtualization, and Why Is It Important for Your Hosting? (KVM, OpenVZ, and More)<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In the vast and interconnected world of the internet, your server is a valuable asset, and unfortunately, a potential target. Just like you wouldn&#8217;t leave your home unlocked, you shouldn&#8217;t leave your server unprotected. The very first and most critical step in comprehensive server security is setting up a robust firewall. A firewall acts as [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12,32],"tags":[34],"class_list":["post-202","post","type-post","status-publish","format-standard","hentry","category-hosting-articles","category-knowledge-base","tag-a-beginners-guide-to-firewall-setup"],"_links":{"self":[{"href":"https:\/\/hosting.international\/blog\/wp-json\/wp\/v2\/posts\/202","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/hosting.international\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/hosting.international\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/hosting.international\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/hosting.international\/blog\/wp-json\/wp\/v2\/comments?post=202"}],"version-history":[{"count":2,"href":"https:\/\/hosting.international\/blog\/wp-json\/wp\/v2\/posts\/202\/revisions"}],"predecessor-version":[{"id":298,"href":"https:\/\/hosting.international\/blog\/wp-json\/wp\/v2\/posts\/202\/revisions\/298"}],"wp:attachment":[{"href":"https:\/\/hosting.international\/blog\/wp-json\/wp\/v2\/media?parent=202"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/hosting.international\/blog\/wp-json\/wp\/v2\/categories?post=202"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/hosting.international\/blog\/wp-json\/wp\/v2\/tags?post=202"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}