{"id":15668,"date":"2023-08-18T11:30:10","date_gmt":"2023-08-18T11:30:10","guid":{"rendered":"https:\/\/www.hostinger.com\/tutorials\/?p=15668"},"modified":"2025-07-09T12:04:43","modified_gmt":"2025-07-09T12:04:43","slug":"how-to-configure-firewall-on-ubuntu-using-ufw","status":"publish","type":"post","link":"\/tutorials\/how-to-configure-firewall-on-ubuntu-using-ufw","title":{"rendered":"How to Configure Your Ubuntu Firewall with UFW and Set Firewall Rules"},"content":{"rendered":"<p>When configured improperly, your Linux virtual private server (VPS) is vulnerable to cyber attacks. Even if your hosting provider installs robust security features, you must apply additional measures to ensure its safety.<\/p><p>An effective way to secure your Linux VPS is by enabling Uncomplicated Firewall in Ubuntu. This feature helps filter malicious incoming and outgoing traffic on your server.<\/p><p>In this article, we will explain how to use UFW for Ubuntu firewall configuration and create custom rules. This UFW setup guide will also explore its importance and best practices.<\/p><div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><a href=\"https:\/\/assets.hostinger.com\/content\/tutorials\/pdf\/Linux-Commands-Cheat-Sheet.pdf\" target=\"_blank\" rel=\"noopener\"><img decoding=\"async\" width=\"2048\" height=\"566\" src=\"https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2022\/11\/Linux-cheat-sheet.png\/public\" alt=\"\" class=\"wp-image-69262\" srcset=\"https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2022\/11\/Linux-cheat-sheet.png\/w=2048,fit=scale-down 2048w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2022\/11\/Linux-cheat-sheet.png\/w=300,fit=scale-down 300w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2022\/11\/Linux-cheat-sheet.png\/w=1024,fit=scale-down 1024w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2022\/11\/Linux-cheat-sheet.png\/w=150,fit=scale-down 150w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2022\/11\/Linux-cheat-sheet.png\/w=768,fit=scale-down 768w\" sizes=\"(max-width: 2048px) 100vw, 2048px\" \/><\/a><\/figure><\/div><p>\n\n\n\n\n\n<\/p><h2 class=\"wp-block-heading\" id=\"h-understanding-the-importance-of-the-ubuntu-firewall\">Understanding the Importance of the Ubuntu Firewall<\/h2><p>A firewall is a security system monitoring your server&rsquo;s outgoing and incoming connections. It uses a set of predetermined rules to allow or block these connections.<\/p><p>This tool is essential since a computer connected to the internet can receive and process requests from any source, including cyber attackers. Here are the benefits of having firewall protection:<\/p><ul class=\"wp-block-list\">\n<li><strong>Cyber threat protection<\/strong>. A firewall blocks malicious connections that cyber criminals use to launch a hacking attack or inject malware.<\/li>\n\n\n\n<li><strong>Traffic filtering<\/strong>. VPS users can set a custom rule to only allow specific connections to reach the server and block the others.<\/li>\n\n\n\n<li><strong>Access control<\/strong>. A system administrator can use a firewall to block outgoing traffic to prevent users on the server from accessing potentially malicious websites.<\/li>\n\n\n\n<li><strong>Data collection<\/strong>. A firewall tracks system events, which you can analyze to set more suitable rules.<\/li>\n<\/ul><p>UFW is an application for Linux firewall configuration. It is pre-installed on Ubuntu 22.04 LTS and Debian 10 or later but disabled by default.<\/p><p>Uncomplicated Firewall (UFW) utilizes a command-line interface (CLI) and uses <a href=\"\/tutorials\/iptables-tutorial\">iptables<\/a> for configuration. Iptables is Linux&rsquo;s built-in firewall containing tables &ndash; storage for rules determining how to filter traffic.<\/p><p>It also has a graphical user interface called GUFW, which users can install on their desktop environment. Meanwhile, UFW command line usage is for a remote server like VPS.<\/p><h2 class=\"wp-block-heading\" id=\"h-how-to-set-up-ubuntu-firewall-on-different-ubuntu-versions\">How to Set Up Ubuntu Firewall on Different Ubuntu Versions<\/h2><p>Before configuring firewall rules in Ubuntu, we must enable UFW. Although we will use<strong> Ubuntu 22.04 <\/strong>for this tutorial, the <a href=\"\/tutorials\/linux-commands\">Linux commands<\/a> should also work for the older or later versions.<\/p><p>UFW configuration on a remote server requires SSH connection using Terminal. Here&rsquo;s how to do so on a Ubuntu system via Hostinger:<\/p><ol class=\"wp-block-list\">\n<li>Open <strong>hPanel <\/strong>&rarr; <strong>VPS<\/strong>.<\/li>\n\n\n\n<li>Select the relevant VPS.<\/li>\n\n\n\n<li>In the <strong>SSH access<\/strong> tab, copy the <strong>Terminal <\/strong>command similar to the following:<\/li>\n<\/ol><pre class=\"wp-block-preformatted\">ssh username@server-ip<\/pre><ol start=\"4\" class=\"wp-block-list\">\n<li>Open <strong>Terminal <\/strong>and paste the command. Press <strong>Enter<\/strong>.<\/li>\n\n\n\n<li>Enter your <strong>SSH root password<\/strong>.<\/li>\n<\/ol><div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><a href=\"https:\/\/www.hostinger.com\/tutorials\/wp-content\/uploads\/sites\/2\/2017\/01\/hostinger-vps-overview-screen-v2.png\"><img decoding=\"async\" width=\"1810\" height=\"664\" src=\"https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2017\/01\/hostinger-vps-overview-screen-v2.png\/public\" alt=\"SSH access credentials in hPanel VPS overview menu\" class=\"wp-image-125759\" srcset=\"https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2017\/01\/hostinger-vps-overview-screen-v2.png\/w=1810,fit=scale-down 1810w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2017\/01\/hostinger-vps-overview-screen-v2.png\/w=300,fit=scale-down 300w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2017\/01\/hostinger-vps-overview-screen-v2.png\/w=1024,fit=scale-down 1024w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2017\/01\/hostinger-vps-overview-screen-v2.png\/w=150,fit=scale-down 150w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2017\/01\/hostinger-vps-overview-screen-v2.png\/w=768,fit=scale-down 768w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2017\/01\/hostinger-vps-overview-screen-v2.png\/w=1536,fit=scale-down 1536w\" sizes=\"(max-width: 1810px) 100vw, 1810px\" \/><\/a><\/figure><\/div><p>If you connect using SSH applications like <a href=\"\/tutorials\/how-to-use-putty-ssh\">PuTTY<\/a>, paste the server <strong>IP address <\/strong>into the designated field and use port <strong>22 <\/strong>to connect. On the CLI, enter the <strong>root username and password<\/strong>. For security reasons, note that the password won&rsquo;t show when you type it.<\/p><p>\n\n\n<div class=\"protip\">\n                    <h4 class=\"title\">Pro Tip<\/h4>\n                    <p>Hostinger&rsquo;s users can use the Browser Terminal feature to run shell commands directly from their web browser without Terminal or SSH applications.<\/p>\n                <\/div>\n\n\n\n<\/p><p>Once connected, follow these steps to configure UFW on Ubuntu 22.04:<\/p><ol class=\"wp-block-list\">\n<li>Activate UFW with the following command:<\/li>\n<\/ol><pre class=\"wp-block-preformatted\">sudo ufw enable<\/pre><ol start=\"2\" class=\"wp-block-list\">\n<li>If you receive a <strong>UFW command not found <\/strong>error, it means the firewall isn&rsquo;t installed on your system. To install it, use the command below:<\/li>\n<\/ol><pre class=\"wp-block-preformatted\">sudo apt-get install ufw<\/pre><ol start=\"3\" class=\"wp-block-list\">\n<li>To confirm that the installation is successful, use this command to check the firewall status:<\/li>\n<\/ol><pre class=\"wp-block-preformatted\">sudo ufw status<\/pre><div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><a href=\"\/tutorials\/wp-content\/uploads\/sites\/2\/2023\/08\/PuTTY-SSH-client-showing-the-UFW-status.png\"><img decoding=\"async\" width=\"842\" height=\"46\" src=\"https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2023\/08\/PuTTY-SSH-client-showing-the-UFW-status.png\/public\" alt=\"PuTTY SSH client showing the UFW status\" class=\"wp-image-91897\" srcset=\"https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2023\/08\/PuTTY-SSH-client-showing-the-UFW-status.png\/w=842,fit=scale-down 842w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2023\/08\/PuTTY-SSH-client-showing-the-UFW-status.png\/w=300,fit=scale-down 300w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2023\/08\/PuTTY-SSH-client-showing-the-UFW-status.png\/w=150,fit=scale-down 150w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2023\/08\/PuTTY-SSH-client-showing-the-UFW-status.png\/w=768,fit=scale-down 768w\" sizes=\"(max-width: 842px) 100vw, 842px\" \/><\/a><\/figure><\/div><p>The default UFW policies block all incoming connections and allow outgoing connections, which are sufficient for most users. However, you must establish custom rules if you host network services or applications.<\/p><figure class=\"wp-block-image size-large\"><a class=\"hgr-tutorials-cta hgr-tutorials-cta-vps-hosting\" href=\"\/vps-hosting\" target=\"_blank\" rel=\"noreferrer noopener\"><img decoding=\"async\" width=\"1024\" height=\"300\" src=\"https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2023\/02\/VPS-hosting-banner.png\/public\" alt=\"\" class=\"wp-image-77934\" srcset=\"https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2023\/02\/VPS-hosting-banner.png\/w=1024,fit=scale-down 1024w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2023\/02\/VPS-hosting-banner.png\/w=300,fit=scale-down 300w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2023\/02\/VPS-hosting-banner.png\/w=150,fit=scale-down 150w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2023\/02\/VPS-hosting-banner.png\/w=768,fit=scale-down 768w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/a><\/figure><h2 class=\"wp-block-heading\" id=\"h-how-to-set-firewall-rules-on-ubuntu-with-ufw\">How to Set Firewall Rules on Ubuntu With UFW<\/h2><p>A firewall rule is an instruction to determine how your system treats connections &ndash; accepted or denied. In this section, we will explain how to create custom UFW rules based on ports, IP addresses, and services.<\/p><h3 class=\"wp-block-heading\" id=\"h-opening-and-closing-ports-with-ufw\">Opening and Closing Ports with UFW<\/h3><p>Ports are connection interfaces an application uses to establish a connection with a server.<\/p><p>Opening unused ports creates security vulnerabilities, as cyber criminals may use them to access your server. It makes closing and opening a port in Linux VPS essential to improve its security.<\/p><p>To minimize risks, users can open and close ports with UFW to control which applications are allowed to connect to their computer. Here are the command syntaxes:<\/p><pre class=\"wp-block-preformatted\">sudo ufw allow port\/protocol\n\nsudo ufw deny port\/protocol<\/pre><p>Replace <strong>port <\/strong>with the port number and <strong>protocol <\/strong>with<strong> <\/strong><a href=\"https:\/\/www.avast.com\/c-tcp-vs-udp-difference\" target=\"_blank\" rel=\"noopener\">TCP or UDP<\/a>. To find out the values for your application, refer to <a href=\"https:\/\/www.iana.org\/assignments\/service-names-port-numbers\/service-names-port-numbers.xhtml\" target=\"_blank\" rel=\"noopener\">IANA&rsquo;s list of port number registries<\/a>.<\/p><p>Use <strong>allow<\/strong> to change the default incoming policy. For instance, run the following command to enable the port for SSH connections:<\/p><pre class=\"wp-block-preformatted\">sudo ufw allow 22\/tcp<\/pre><p>To change the default policy for outgoing traffic, use the <strong>deny <\/strong>option. For example, enter the command below to block the connection from your server to a MySQL database:<\/p><pre class=\"wp-block-preformatted\">sudo ufw deny 56\/tcp<\/pre><p>You can also open or close port ranges with a single command. Here&rsquo;s how the basic syntax looks:<\/p><pre class=\"wp-block-preformatted\">sudo ufw allow\/deny starting_port:ending_port\/protocol<\/pre><p>For instance, here&rsquo;s the command to deny access from port 300 to 310 UDP:<\/p><pre class=\"wp-block-preformatted\">sudo ufw deny 300:310\/UDP<\/pre><h3 class=\"wp-block-heading\" id=\"h-working-with-services-on-ubuntu-firewall\">Working With Services on Ubuntu Firewall<\/h3><p>UFW lets administrators manage network services on Ubuntu systems by opening and closing ports. Instead of specifying the port, they can enter the service name.<\/p><p>For example, HTTP transmissions use port 80, while HTTPS connections require port 443. If you want to enable HTTP connections, run the following command:<\/p><pre class=\"wp-block-preformatted\">sudo ufw allow http<\/pre><p>The command will automatically open the HTTP port, namely 80. Similarly, enabling HTTPS connections means opening port 443.<\/p><h3 class=\"wp-block-heading\" id=\"h-denying-or-allowing-ip-address-connections\">Denying or Allowing IP Address Connections<\/h3><p>UFW also lets users deny access to a specific IP address. To do so, execute the following command:<\/p><pre class=\"wp-block-preformatted\">sudo ufw deny from ipaddress<\/pre><p>Replace <strong>deny <\/strong>with <strong>allow<\/strong> if you want to enable access from the specified IP address, like the following:<\/p><pre class=\"wp-block-preformatted\">sudo ufw allow from 192.168.1.3<\/pre><p>You can also create a rule that applies to a specific network interface. For instance, this rule syntax allows an IP address to connect to a specific port only:<\/p><pre class=\"wp-block-preformatted\">sudo ufw allow from ipaddress to any port portnumber<\/pre><p>Here&rsquo;s an example command that allows an IP address to connect to your server only when using port 44:<\/p><pre class=\"wp-block-preformatted\">sudo ufw allow from 192.168.1.3 to any port 44<\/pre><p>Alternatively, use this command to prevent the IP address from connecting to your server when using the specified port:<\/p><pre class=\"wp-block-preformatted\">sudo ufw deny from 192.168.1.3 to any port 44<\/pre><h3 class=\"wp-block-heading\" id=\"h-deleting-rules-on-ubuntu-firewall\">Deleting Rules on Ubuntu Firewall<\/h3><p>To delete firewall rules in Ubuntu, use the <strong>delete<\/strong> command. Here&rsquo;s the syntax:<\/p><pre class=\"wp-block-preformatted\">sudo ufw delete rule_number<\/pre><p>Since deleting rules requires their number label, list them using this command:<\/p><pre class=\"wp-block-preformatted\">sudo ufw status numbered<\/pre><div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><a href=\"\/tutorials\/wp-content\/uploads\/sites\/2\/2023\/08\/PuTTY-SSH-client-showing-a-list-of-UFW-rules.png\"><img decoding=\"async\" width=\"842\" height=\"119\" src=\"https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2023\/08\/PuTTY-SSH-client-showing-a-list-of-UFW-rules.png\/public\" alt=\"PuTTY SSH client showing a list of UFW rules\" class=\"wp-image-91898\" srcset=\"https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2023\/08\/PuTTY-SSH-client-showing-a-list-of-UFW-rules.png\/w=842,fit=scale-down 842w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2023\/08\/PuTTY-SSH-client-showing-a-list-of-UFW-rules.png\/w=300,fit=scale-down 300w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2023\/08\/PuTTY-SSH-client-showing-a-list-of-UFW-rules.png\/w=150,fit=scale-down 150w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2023\/08\/PuTTY-SSH-client-showing-a-list-of-UFW-rules.png\/w=768,fit=scale-down 768w\" sizes=\"(max-width: 842px) 100vw, 842px\" \/><\/a><\/figure><\/div><p>Now, replace the placeholder<strong> <\/strong>with the appropriate rule number. For example, here&rsquo;s a command that will delete rule number<strong> four<\/strong>.<\/p><pre class=\"wp-block-preformatted\">sudo ufw delete 4<\/pre><p>Alternatively, you can reset the firewall configuration to the default rules. To do so, set <strong>incoming <\/strong>and <strong>outgoing <\/strong>to <strong>default<\/strong> using these commands:<\/p><pre class=\"wp-block-preformatted\">sudo ufw default deny incoming\n\nsudo ufw default allow outgoing<\/pre><p>To delete UFW rules entirely and start over, use the <strong>reset <\/strong>command:<\/p><pre class=\"wp-block-preformatted\">sudo ufw reset<\/pre><p>If you want to learn more commands, browse the UFW manual by running the following:<\/p><pre class=\"wp-block-preformatted\">sudo ufw -help<\/pre><p>Hostinger&rsquo;s <a href=\"\/vps-hosting\">VPS solutions<\/a> have a built-in firewall management feature accessible via hPanel. It provides a graphical user interface to help users easily configure their firewall rules. Select your VPS and navigate to the <strong>Firewall<\/strong> section:<\/p><div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><a href=\"https:\/\/www.hostinger.com\/tutorials\/wp-content\/uploads\/sites\/2\/2016\/09\/hpanel-vps-firewall-sidebar.png\"><img decoding=\"async\" width=\"394\" height=\"826\" src=\"https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2016\/09\/hpanel-vps-firewall-sidebar.png\/public\" alt=\"The Firewall button on hPanel VPS dashboard\" class=\"wp-image-90696\" srcset=\"https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2016\/09\/hpanel-vps-firewall-sidebar.png\/w=394,fit=scale-down 394w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2016\/09\/hpanel-vps-firewall-sidebar.png\/w=143,fit=scale-down 143w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2016\/09\/hpanel-vps-firewall-sidebar.png\/w=72,fit=scale-down 72w\" sizes=\"(max-width: 394px) 100vw, 394px\" \/><\/a><\/figure><\/div><p>Then, select the <strong>Create firewall configuration<\/strong> button and give your configuration a name:<\/p><div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><a href=\"https:\/\/www.hostinger.com\/tutorials\/wp-content\/uploads\/sites\/2\/2016\/09\/hpanel-vps-firewall-createnew.png\"><img decoding=\"async\" width=\"928\" height=\"338\" src=\"https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2016\/09\/hpanel-vps-firewall-createnew.png\/public\" alt=\"The process of creating a new VPS Firewall configuration on hPanel\" class=\"wp-image-90697\" srcset=\"https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2016\/09\/hpanel-vps-firewall-createnew.png\/w=928,fit=scale-down 928w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2016\/09\/hpanel-vps-firewall-createnew.png\/w=300,fit=scale-down 300w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2016\/09\/hpanel-vps-firewall-createnew.png\/w=150,fit=scale-down 150w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2016\/09\/hpanel-vps-firewall-createnew.png\/w=768,fit=scale-down 768w\" sizes=\"(max-width: 928px) 100vw, 928px\" \/><\/a><\/figure><\/div><p>Lastly, hit <strong>Edit<\/strong> and add any preferred firewall rules:<\/p><div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><a href=\"https:\/\/www.hostinger.com\/tutorials\/wp-content\/uploads\/sites\/2\/2016\/09\/hpanel-vps-firewall-edit-highlighted.png\"><img decoding=\"async\" width=\"1024\" height=\"375\" src=\"https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2016\/09\/hpanel-vps-firewall-edit-highlighted.png\/public\" alt=\"The Firewall page on hPanel. The Edit button is highlighted\" class=\"wp-image-90698\" srcset=\"https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2016\/09\/hpanel-vps-firewall-edit-highlighted.png\/w=1024,fit=scale-down 1024w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2016\/09\/hpanel-vps-firewall-edit-highlighted.png\/w=300,fit=scale-down 300w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2016\/09\/hpanel-vps-firewall-edit-highlighted.png\/w=150,fit=scale-down 150w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2016\/09\/hpanel-vps-firewall-edit-highlighted.png\/w=768,fit=scale-down 768w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/a><\/figure><\/div><p>Alternatively, Hostinger VPS users can ask <strong>Kodee AI assistant<\/strong> to manage their server&rsquo;s firewall. For example, they can create a new firewall rule by simply asking, &ldquo;Create and activate a new firewall rule on my VPS that allows connection to port 12345 from any IP address.&rdquo;<\/p><h2 class=\"wp-block-heading\" id=\"h-ufw-best-practices\">UFW Best Practices<\/h2><p>In this section, we will explain Ubuntu firewall best practices to help you utilize the program effectively to improve your VPS security.<\/p><p><strong>Enable UFW Logging<\/strong><\/p><p>UFW logging allows system administrators to review all incoming or outgoing <a href=\"https:\/\/www.cloudflare.com\/learning\/network-layer\/what-is-a-packet\/\" target=\"_blank\" rel=\"noopener\">packets<\/a> stored in iptables. It provides detailed information about the packets, including their source, destination, and transmission protocol.<\/p><p>This feature helps users troubleshoot connection issues and identify potential security threats. However, it is disabled by default. To check its status, use the following command:<\/p><pre class=\"wp-block-preformatted\">sudo ufw status verbose<\/pre><p>To enable it, run this command:<\/p><pre class=\"wp-block-preformatted\">sudo ufw logging on<\/pre><p>Use the same command with an <strong>off <\/strong>value to disable UFW logging. You can run different commands to view UFW logs, but the easiest way is to use <strong>less<\/strong>. Here&rsquo;s the command:<\/p><pre class=\"wp-block-preformatted\">sudo less \/var\/log\/ufw*<\/pre><p>The command above assumes the default directory for UFW logs. If you store the firewall logs in another folder, change the file path accordingly.<\/p><p>\n\n\n<div class=\"protip\">\n                    <h4 class=\"title\">Pro Tip<\/h4>\n                    <p>In addition to checking the logging feature, use <strong>sudo ufw status verbose<\/strong> to list your rules.<\/p>\n                <\/div>\n\n\n\n<\/p><p><strong>Use UFW With IPv6<\/strong><\/p><p>As IPv4 IP addresses run out, IPv6 becomes more important in modern networking. UFW lets you filter IPv6 to improve your <a href=\"\/tutorials\/vps-security\">VPS security<\/a>, but this feature is disabled by default.<\/p><p>To enable it, change the UFW configuration file using a text editor like <strong>Nano<\/strong> with this command:<\/p><pre class=\"wp-block-preformatted\">sudo nano \/etc\/default\/ufw<\/pre><div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><a href=\"\/tutorials\/wp-content\/uploads\/sites\/2\/2023\/08\/The-IPv6-status-in-UFW-configuration-file.png\"><img decoding=\"async\" width=\"943\" height=\"191\" src=\"https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2023\/08\/The-IPv6-status-in-UFW-configuration-file.png\/public\" alt=\"The IPv6 status in UFW configuration file\" class=\"wp-image-91899\" srcset=\"https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2023\/08\/The-IPv6-status-in-UFW-configuration-file.png\/w=943,fit=scale-down 943w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2023\/08\/The-IPv6-status-in-UFW-configuration-file.png\/w=300,fit=scale-down 300w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2023\/08\/The-IPv6-status-in-UFW-configuration-file.png\/w=150,fit=scale-down 150w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2023\/08\/The-IPv6-status-in-UFW-configuration-file.png\/w=768,fit=scale-down 768w\" sizes=\"(max-width: 943px) 100vw, 943px\" \/><\/a><\/figure><\/div><p>Find <strong>IPv6 <\/strong>and change the value from <strong>no <\/strong>to <strong>yes<\/strong>. Press <strong>Ctrl + X <\/strong>to quit Nano and press <strong>Y <\/strong>to confirm the changes. To disable it, change the value back to <strong>no<\/strong>.<\/p><p><strong>Check UFW Compatibility With Docker<\/strong><\/p><p><a href=\"\/tutorials\/what-is-docker\">Docker<\/a> may cause conflicts with UFW as it can modify the firewall rules in iptables. UFW won&rsquo;t prevent an application in the Docker container from connecting to a blocked port, regardless of the rules.<\/p><p>Since UFW doesn&rsquo;t show that the port is open, this incompatibility exposes your server to security risks. There are different ways to match UFW and Docker&rsquo;s rules, but using the <a href=\"https:\/\/github.com\/chaifeng\/ufw-docker\" target=\"_blank\" rel=\"noopener\"><strong>ufw-docker <\/strong>utility<\/a> is the easiest.<\/p><p>Open Terminal or your SSH client and follow these steps to install and use the tool:<\/p><ol class=\"wp-block-list\">\n<li>Enter these commands to download the script from the repository:<\/li>\n<\/ol><pre class=\"wp-block-preformatted\">sudo wget -O \/usr\/local\/bin\/ufw-docker \\ https:\/\/github.com\/chaifeng\/ufw-docker\/raw\/master\/ufw-docker\nsudo chmod +x \/usr\/local\/bin\/ufw-docker\n<\/pre><ol start=\"2\" class=\"wp-block-list\">\n<li>Install the utility using this command:<\/li>\n<\/ol><pre class=\"wp-block-preformatted\">sudo install ufw-docker<\/pre><ol start=\"3\" class=\"wp-block-list\">\n<li>Restart UFW with the command below:<\/li>\n<\/ol><pre class=\"wp-block-preformatted\">sudo systemctl restart ufw<\/pre><ol start=\"4\" class=\"wp-block-list\">\n<li>Run the <strong>ufw-docker <\/strong>command to open a port for a Docker container, like the following:<\/li>\n<\/ol><pre class=\"wp-block-preformatted\">sudo ufw-docker allow httpd 80<\/pre><p><strong>Set Up UFW Application Profiles<\/strong><\/p><p>Several applications require multiple network interfaces, which can be tedious to allow individually. UFW lets you easily set a rule based on the application&rsquo;s ports with one command:<\/p><pre class=\"wp-block-preformatted\">sudo ufw allow app_name<\/pre><p>Replace <strong>app_name <\/strong>with your application profile. For example, here&rsquo;s the command to allow incoming SSH connections:<\/p><pre class=\"wp-block-preformatted\">sudo ufw allow ssh<\/pre><p>You can also allow ports from a source IP address to a specific destination using this command:<\/p><pre class=\"wp-block-preformatted\">sudo ufw allow from source_IP to destination_IP app app_name<\/pre><p>UFW has pre-configured application profiles by default, which you can obtain using this command:<\/p><pre class=\"wp-block-preformatted\">sudo ufw app list<\/pre><p>You may also create custom application profiles. To do so, connect to your server via Terminal or an SSH client and follow these steps:<\/p><ol class=\"wp-block-list\">\n<li>Run the <a href=\"\/tutorials\/linux-touch-command\"><strong>touch<\/strong> command<\/a> to create an empty file in the UFW application folder:<\/li>\n<\/ol><pre class=\"wp-block-preformatted\">sudo touch \/etc\/ufw\/applications.d\/file_name<\/pre><ol start=\"2\" class=\"wp-block-list\">\n<li>Open the file using <strong>Nano<\/strong> with this command:<\/li>\n<\/ol><pre class=\"wp-block-preformatted\">sudo nano \/etc\/ufw\/applications.d\/file_name<\/pre><ol start=\"3\" class=\"wp-block-list\">\n<li>Enter the following snippet to set your application profile. Replace the placeholders with the appropriate values:<\/li>\n<\/ol><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">[app_name]\ntitle=application_title\ndescription=application_description\nports=ports\/protocol|ports\/protocol|ports\/protocol<\/pre><ol start=\"4\" class=\"wp-block-list\">\n<li>Press <strong>Ctrl + X <\/strong>to close <strong>Nano <\/strong>and <strong>Y <\/strong>to confirm the changes.<\/li>\n\n\n\n<li>Update the application definitions in UFW using these commands:<\/li>\n<\/ol><pre class=\"wp-block-preformatted\">sudo ufw app update appname\nsudo ufw app info appname<\/pre><p>You can specify multiple ports with the same protocol using commas in the configuration code. For port ranges, use a colon instead of a dash. See the following example:<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">ports=80,443,8140\/tcp|800:1500\/udp<\/pre><p><strong>Follow Other Security Practices Beyond Firewall<\/strong><\/p><p>Enabling UFW is one of many important Ubuntu security measures. Since there are a variety of cyber threats, system administrators must fortify firewalls with other security practices.<\/p><p>Here are several additional security practices you should implement in your VPS hosting environment:<\/p><ul class=\"wp-block-list\">\n<li><strong>Update regularly<\/strong>. Older software packages have vulnerabilities that expose your VPS to cyber threats. To minimize the risk, regularly update your VPS software and enable <a href=\"\/tutorials\/cron-job\">cron jobs<\/a> to automate the process.<\/li>\n\n\n\n<li><strong>Change SSH ports<\/strong>. SSH connections use port 22 by default, which hackers often exploit to access your server. <a href=\"\/tutorials\/how-to-change-ssh-port-vps\">Changing the SSH port<\/a> helps improve your VPS security.<\/li>\n\n\n\n<li><strong>Use SSH keys<\/strong>. <a href=\"\/tutorials\/ssh\/how-to-set-up-ssh-keys\">SSH keys<\/a> are additional security credentials for connecting to your VPS. They are longer and more complex than passwords, making them more secure.<\/li>\n\n\n\n<li><strong>Disable root login<\/strong>. A root user can modify any aspect of your system, making them a common target for hacking. To secure your VPS, disable root login and create alternative user roles with superuser privileges.<\/li>\n\n\n\n<li><strong>Install IDS\/IPS tool<\/strong>. Intrusion detection and prevention systems let you quickly determine the origin of malicious network traffic, which you can block using UFW. To learn more about how to install the tool, check out our tutorial on <a href=\"\/tutorials\/how-to-install-suricata-on-ubuntu\">setting up Suricata on Ubuntu<\/a>.&nbsp;<\/li>\n<\/ul><h2 class=\"wp-block-heading\" id=\"h-conclusion\">Conclusion<\/h2><p>A VPS system is vulnerable to cyber threats when configured improperly. An efficient way to improve your Ubuntu server security is to install Uncomplicated Firewall (UFW) that filters outgoing and incoming connections.<\/p><p>UFW is installed by default, but users must manually enable it via Terminal or an SSH client like PuTTY. Once enabled, edit the default policies to filter connections based on ports, IP addresses, or services.<\/p><p>In addition, follow the firewall&rsquo;s best practices to improve your Ubuntu network security. For example, enable logging to track traffic in detail, check UFW compatibility with Docker, set up an applications profile, and fortify the firewall with other security practices like using SSH keys.<\/p><p>\n\n\n<div class=\"protip\">\n                    <h4 class=\"title\">Discover More About How to Protect Your Linux<\/h4>\n                    <p><a href=\"\/tutorials\/how-to-set-up-a-linux-vpn-server-with-openvpn\/\">How to Configure OpenVPN on VPS<\/a><br>\n<a href=\"\/tutorials\/fail2ban-configuration\">How to Configure Fail2Ban on CentOS<\/a><br>\n<a href=\"\/tutorials\/how-to-install-clamav-centos7\">How to Install ClamAV on CentOS<\/a><\/p>\n                <\/div>\n\n\n\n<\/p><h2 class=\"wp-block-heading\" id=\"h-ubuntu-firewall-faq\">Ubuntu Firewall FAQ<\/h2><p>In this section, we will answer several commonly asked questions about setting up UFW in Ubuntu.<\/p><div class=\"schema-faq wp-block-yoast-faq-block\"><div class=\"schema-faq-section\" id=\"faq-question-1692356593841\"><h3 class=\"schema-faq-question\">Is UFW Pre-installed in Ubuntu 22.04 and Newer Versions?<\/h3> <p class=\"schema-faq-answer\">Yes, UFW was introduced in Ubuntu 8.04 LTS, and it comes pre-installed in later versions, including 22.04. However, it is disabled on the default settings.&nbsp;<br><br>To enable it, enter the <strong>sudo ufw enable <\/strong>command. Other UFW features are also disabled by default, such as IPv6 support and logging.<\/p> <\/div> <div class=\"schema-faq-section\" id=\"faq-question-1692356608454\"><h3 class=\"schema-faq-question\">What Are the Basic UFW Commands to Know When Setting up a Firewall on Ubuntu?<\/h3> <p class=\"schema-faq-answer\">UFW has various commands for different purposes. For example, the <strong>sudo ufw status <\/strong>command checks whether the firewall is active.<br><br>Other important commands include <strong>sudo ufw deny <\/strong>and <strong>sudo ufw allow<\/strong>,<strong> <\/strong>which alter the default settings for incoming and outgoing connections. Enter <strong>sudo ufw -help <\/strong>to list all commands.<\/p> <\/div> <div class=\"schema-faq-section\" id=\"faq-question-1692356628573\"><h3 class=\"schema-faq-question\">Are the UFW Commands the Same Across Ubuntu Versions?<\/h3> <p class=\"schema-faq-answer\">UFW commands are mostly the same regardless of the Ubuntu version. However, if you use versions older than 8.04 TLS, you can&rsquo;t run them.<\/p> <\/div> <div class=\"schema-faq-section\" id=\"faq-question-1692356641869\"><h3 class=\"schema-faq-question\">What Is the Difference Between UFW and GUFW?<\/h3> <p class=\"schema-faq-answer\">UFW is a command-line version of Ubuntu&rsquo;s firewall, while GUFW has a graphical user interface. Both work similarly to configure your system&rsquo;s firewall.<br><br>GUFW is easier to use for beginners as it has a visual interface. However, UFW is more convenient for a remote server as it uses SSH.<\/p> <\/div> <\/div>\n","protected":false},"excerpt":{"rendered":"<p>When configured improperly, your Linux virtual private server (VPS) is vulnerable to cyber attacks. Even if your hosting provider installs [&#8230;]<\/p>\n<p><a class=\"btn btn-secondary understrap-read-more-link\" href=\"\/tutorials\/how-to-configure-firewall-on-ubuntu-using-ufw\">Read More&#8230;<\/a><\/p>\n","protected":false},"author":337,"featured_media":87036,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"rank_math_title":"How to Configure Ubuntu Firewall and Set UFW Rules in %currentyear%","rank_math_description":"Ubuntu Firewall is a UFW (Uncomplicated Firewall). Learn how to configure it, manage services, and protect your server from threats.","rank_math_focus_keyword":"ubuntu firewall","footnotes":""},"categories":[22648,22644],"tags":[],"class_list":["post-15668","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-managing-monitoring-and-security","category-vps"],"hreflangs":[{"locale":"en-US","link":"https:\/\/www.hostinger.com\/tutorials\/how-to-configure-firewall-on-ubuntu-using-ufw","default":0},{"locale":"fr-FR","link":"https:\/\/www.hostinger.com\/fr\/tutoriels\/comment-configurer-pare-feu-ufw","default":0},{"locale":"es-ES","link":"https:\/\/www.hostinger.com\/es\/tutoriales\/como-configurar-firewall-ubuntu","default":0},{"locale":"id-ID","link":"https:\/\/www.hostinger.com\/id\/tutorial\/firewall-ubuntu","default":0},{"locale":"en-UK","link":"https:\/\/www.hostinger.com\/uk\/tutorials\/how-to-configure-firewall-on-ubuntu-using-ufw","default":0},{"locale":"en-MY","link":"https:\/\/www.hostinger.com\/my\/tutorials\/how-to-configure-a-firewall-on-ubuntu-with-ufw","default":0},{"locale":"en-PH","link":"https:\/\/www.hostinger.com\/ph\/tutorials\/how-to-configure-a-firewall-on-ubuntu-with-ufw","default":0},{"locale":"es-MX","link":"https:\/\/www.hostinger.com\/mx\/tutoriales\/como-configurar-firewall-ubuntu","default":0},{"locale":"es-CO","link":"https:\/\/www.hostinger.com\/co\/tutoriales\/como-configurar-firewall-ubuntu","default":0},{"locale":"es-AR","link":"https:\/\/www.hostinger.com\/ar\/tutoriales\/como-configurar-firewall-ubuntu","default":0},{"locale":"en-IN","link":"https:\/\/www.hostinger.com\/in\/tutorials\/how-to-configure-firewall-on-ubuntu-using-ufw","default":0},{"locale":"en-CA","link":"https:\/\/www.hostinger.com\/ca\/tutorials\/how-to-configure-firewall-on-ubuntu-using-ufw","default":0},{"locale":"en-AU","link":"https:\/\/www.hostinger.com\/au\/tutorials\/how-to-configure-firewall-on-ubuntu-using-ufw","default":0},{"locale":"en-NG","link":"https:\/\/www.hostinger.com\/ng\/tutorials\/how-to-configure-firewall-on-ubuntu-using-ufw","default":0}],"_links":{"self":[{"href":"https:\/\/www.hostinger.com\/tutorials\/wp-json\/wp\/v2\/posts\/15668","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.hostinger.com\/tutorials\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.hostinger.com\/tutorials\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.hostinger.com\/tutorials\/wp-json\/wp\/v2\/users\/337"}],"replies":[{"embeddable":true,"href":"https:\/\/www.hostinger.com\/tutorials\/wp-json\/wp\/v2\/comments?post=15668"}],"version-history":[{"count":38,"href":"https:\/\/www.hostinger.com\/tutorials\/wp-json\/wp\/v2\/posts\/15668\/revisions"}],"predecessor-version":[{"id":131128,"href":"https:\/\/www.hostinger.com\/tutorials\/wp-json\/wp\/v2\/posts\/15668\/revisions\/131128"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.hostinger.com\/tutorials\/wp-json\/wp\/v2\/media\/87036"}],"wp:attachment":[{"href":"https:\/\/www.hostinger.com\/tutorials\/wp-json\/wp\/v2\/media?parent=15668"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.hostinger.com\/tutorials\/wp-json\/wp\/v2\/categories?post=15668"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.hostinger.com\/tutorials\/wp-json\/wp\/v2\/tags?post=15668"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}