
{"id":46964,"date":"2024-07-29T18:00:43","date_gmt":"2024-07-29T18:00:43","guid":{"rendered":"https:\/\/ubuntuhandbook.org\/?p=46964"},"modified":"2025-09-25T10:32:34","modified_gmt":"2025-09-25T10:32:34","slug":"enable-disable-configure-firewall-ubuntu","status":"publish","type":"post","link":"https:\/\/ubuntuhandbook.org\/index.php\/2024\/07\/enable-disable-configure-firewall-ubuntu\/","title":{"rendered":"Enable, Disable, Configure Firewall in Ubuntu 24.04 [Beginner&#8217;s Guide]"},"content":{"rendered":"<p><a href=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/07\/gufw-logo.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"alignleft size-thumbnail wp-image-46965\" src=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/07\/gufw-logo-250x250.webp\" alt=\"\" width=\"250\" height=\"250\" srcset=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/07\/gufw-logo-250x250.webp 250w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/07\/gufw-logo-300x300.webp 300w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/07\/gufw-logo-700x700.webp 700w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/07\/gufw-logo-768x768.webp 768w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/07\/gufw-logo.webp 1200w\" sizes=\"auto, (max-width: 250px) 100vw, 250px\" \/><\/a><\/p>\n<p>This is a beginner&#8217;s guide shows you how to enable, disable, and configure firewall in Ubuntu using UFW.<\/p>\n<p>Firewall is a network security system that monitors incoming and outgoing network traffic, and decides whether to allow or block specific traffic based on pre-defined security rules.<\/p>\n<p>Linux Kernel has the <b>Netfilter subsystem<\/b>, which is implemented as a packet filter and firewall. <b>Iptables<\/b> (and <b>nftables<\/b>, the successor of iptables) is the user-level command line tool to configure the firewall by adding\/removing netfilter rules.<\/p>\n<p>Iptables (and nftables) is much more flexible but really hard for beginners. <b>UFW<\/b> (Uncomplicated Firewall), the user-friendly front-end for iptables, is which I&#8217;m going to talk about below.<\/p>\n<div id=\"attachment_46967\" style=\"width: 650px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/07\/security-firewall.webp\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-46967\" class=\"size-full wp-image-46967\" src=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/07\/security-firewall.webp\" alt=\"\" width=\"640\" height=\"400\" srcset=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/07\/security-firewall.webp 640w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/07\/security-firewall-300x188.webp 300w\" sizes=\"auto, (max-width: 640px) 100vw, 640px\" \/><\/a><p id=\"caption-attachment-46967\" class=\"wp-caption-text\">Image by Pete Linforth from Pixabay<\/p><\/div>\n<p><!--more--><\/p>\n<h3>Enable Firewall in Ubuntu<\/h3>\n<p>UFW is usually pre-installed in both Ubuntu Desktop and Server, though NOT enabled by default.<\/p>\n<p>Just in case, you may open terminal (Ctrl+Alt+T) and run command to install it:<\/p>\n<pre>sudo apt install ufw<\/pre>\n<p>As mentioned, the firewall is usually not enabled by default. <b>To check its status<\/b>, use command:<\/p>\n<pre>sudo ufw status<\/pre>\n<p>It will show you either &#8220;<em>Status: in active<\/em>&#8221; or <em>&#8220;Status: active&#8221; along with user added rule<\/em>s.<\/p>\n<p><b>NOTE: It supports adding rules before ufw enabled. For remote server, run <code>sudo ufw allow ssh<\/code> to whitelist ssh first, or you&#8217;ll lost SSH connection. If non-default SSH port is in use, for example port 1234, then use <code>sudo ufw allow 1234\/tcp<\/code> command instead.<\/b><\/p>\n<p><b>To enable the firewall<\/b>, just run command:<\/p>\n<pre>sudo ufw enable<\/pre>\n<p>It should output &#8220;<i>Firewall is active and enabled on system startup<\/i>&#8221; if command&#8217;s done successfully.<\/p>\n<p><a href=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/07\/enable-firewall.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-46966\" src=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/07\/enable-firewall.webp\" alt=\"\" width=\"546\" height=\"245\" srcset=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/07\/enable-firewall.webp 546w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/07\/enable-firewall-300x135.webp 300w\" sizes=\"auto, (max-width: 546px) 100vw, 546px\" \/><\/a><\/p>\n<h3>Configure Firewall using UFW<\/h3>\n<h4>1. Check status &amp; added rules<\/h4>\n<p>As mentioned above, you may check the firewall status by running the command below:<\/p>\n<pre>sudo ufw status<\/pre>\n<p>It will show you if the firewall is activated or not. If yes, it also shows all the user added rules.<\/p>\n<p><b>However, to check user added rules even when firewall is in-activated<\/b>, this command may be helpful:<\/p>\n<pre>sudo ufw show added<\/pre>\n<p><a href=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/07\/ufw-added.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-46968\" src=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/07\/ufw-added.webp\" alt=\"\" width=\"540\" height=\"245\" srcset=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/07\/ufw-added.webp 540w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/07\/ufw-added-300x136.webp 300w\" sizes=\"auto, (max-width: 540px) 100vw, 540px\" \/><\/a><\/p>\n<h4>2. Configure UFW default policy<\/h4>\n<p>The default firewall policy <b>allows any outgoing traffic<\/b>, meaning from the Ubuntu PC\/Server you can access any website, use <code>apt<\/code>, <code>wget<\/code>, etc commands to install\/download something in your system.<\/p>\n<p>However, <b>incoming<\/b> by default is disabled. You need to add your own rules to allow outside systems to connect to your machine. All routing and forwarding are also disabled, which is good default if you are not using your machine as a router.<\/p>\n<p><b>To check default policy<\/b>, use command:<\/p>\n<pre>sudo ufw status verbose<\/pre>\n<p><a href=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/07\/ufw-defaults.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-46969\" src=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/07\/ufw-defaults.webp\" alt=\"\" width=\"544\" height=\"244\" srcset=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/07\/ufw-defaults.webp 544w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/07\/ufw-defaults-300x135.webp 300w\" sizes=\"auto, (max-width: 544px) 100vw, 544px\" \/><\/a><\/p>\n<p>If you want to change the default policy, for example deny outgoing, use command:<\/p>\n<pre>sudo ufw default deny outgoing<\/pre>\n<p>After that, if you want to access outside systems, then you can either re-allow all outgoing traffic via:<\/p>\n<pre>sudo ufw default allow outgoing<\/pre>\n<p>Or, manually add outgoing rules for certain ports. For example, <b>add firewall rules below to allow using apt command to install something:<\/b><\/p>\n<pre>sudo ufw allow out 53\/udp<\/pre>\n<pre>sudo ufw allow out 80\/tcp<\/pre>\n<p>When done, you may <b>delete the rules<\/b>, so all outgoing denied again:<\/p>\n<pre>sudo ufw delete allow out 53\/udp<\/pre>\n<pre>sudo ufw delete allow out 80\/tcp<\/pre>\n<h4>3. Add UFW Rules<\/h4>\n<p>As commands above mentioned, you may use <code>ufw allow<\/code> command to allow incoming (and\/or outgoing) traffic to specific port, and use <code>ufw deny<\/code> command to deny traffics.<\/p>\n<p>For example, <b>allow incoming to port 80<\/b> (both tcp &amp; udp) from any where, use command:<\/p>\n<pre>sudo ufw allow 80<\/pre>\n<p>Or, allow incoming to port 53 only for udp, use command:<\/p>\n<pre>sudo ufw allow 53\/udp<\/pre>\n<p>To be more specific, you can tell from where the traffic is allowed to certain port on current machine. For example, the command below set the firewall to allow remote IP ranging from 192.168.0.0 to 192.168.0.255 to tcp port 22 in this host.<\/p>\n<pre>sudo ufw allow from 192.168.0.0\/24 to any port 22 proto tcp<\/pre>\n<p>The &#8220;<b>any<\/b>&#8221; in last command means any network interfaces in local host. To specify certain IP in this host, 192.168.0.100 for example, then the last command can be:<\/p>\n<pre>sudo ufw allow from 192.168.0.0\/24 to 192.168.0.100 port 22 proto tcp<\/pre>\n<p>As mentioned above, you can also use <b>service name in UFW command<\/b> to allow (or deny) certain traffics. For example:<\/p>\n<pre>sudo ufw allow smtp<\/pre>\n<p>This command will allow the SMTP port <b>25<\/b>, even when the service is not installed. However, <b>it only and will always sets the service&#8217;s default port (e.g, 22 for ssh)<\/b> even when a custom one is in use.<\/p>\n<h4>4. Remove UFW filewall rules<\/h4>\n<p><b>To delete ufw rules<\/b>, just add <code>delete<\/code> operation between <code>ufw<\/code> and <code>allow<\/code> (or <code>deny<\/code>) in the last commands you run.<\/p>\n<ul>\n<li>First, list added rules via command:\n<pre>sudo ufw show added<\/pre>\n<\/li>\n<li>Then, delete a rule. For example, undo <code>allow 53\/udp<\/code>:\n<pre>sudo ufw delete allow 53\/udp<\/pre>\n<\/li>\n<\/ul>\n<p>If ufw is in activated status, you may <strong>list all added rules with line numbers<\/strong>, then delete via desired number:<\/p>\n<ul>\n<li>First, show ufw status as well as added rules with numbers:\n<pre>sudo ufw status numbered<\/pre>\n<\/li>\n<li>Then, delete the first rule via:\n<pre>sudo ufw delete 1<\/pre>\n<\/li>\n<\/ul>\n<p><b>NOTE: After deleted a rule, the numbers of the following rules change automatically. If you need to delete multiple rules, either run <code>ufw status numbered<\/code> and delete commands multiple times, or use the command below instead:<\/b><\/p>\n<pre>for rules in 5 3 2; do sudo ufw delete $rules; done<\/pre>\n<p>Replace the numbers <code>5 3 2<\/code> with the desired ones you want to delete, and they must be list in reverse order so to delete from the highest number downwards.<\/p>\n<p>If you don&#8217;t want to answer &#8220;<code>Proceed with operation (y|n)<\/code>&#8221; for each delete action, then use the command below instead:<\/p>\n<pre>for rules in 8 7 5 3 2; do yes|sudo ufw delete $rules --force; done<\/pre>\n<p><a href=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/07\/ufw-delete.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-46970\" src=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/07\/ufw-delete.webp\" alt=\"\" width=\"533\" height=\"465\" srcset=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/07\/ufw-delete.webp 533w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/07\/ufw-delete-300x262.webp 300w\" sizes=\"auto, (max-width: 533px) 100vw, 533px\" \/><\/a><\/p>\n<h4>5. Disable IPv6<\/h4>\n<p>The ufw firewall rules by default apply to both IPv6 and IPv4.<\/p>\n<p>If you want to <strong>exclude IPv6 for all firewall rules<\/strong>, then edit the <code>\/etc\/default\/ufw<\/code> file via command:<\/p>\n<pre>sudo nano \/etc\/default\/ufw<\/pre>\n<p>Then, then change <code>IPV6=yes<\/code> to <code>IPV6=no<\/code>. Finally, press Ctrl+S then Ctrl+X to exit. Also, run <code>sudo ufw reload<\/code>\u00a0to apply changes.<\/p>\n<p>Or, specify the IPv4 address in your rules. For example:<\/p>\n<pre>ufw allow to 0.0.0.0\/0 port 80 proto tcp<\/pre>\n<p>This rule will allow traffic from anywhere to port 80\/tcp on this host through all the ipv4 interfaces.<\/p>\n<h3>Disable Firewall<\/h3>\n<p><b>To disable firewall<\/b>, simply run:<\/p>\n<pre>sudo ufw disable<\/pre>\n<p>It will show &#8220;<i>Firewall stopped and disabled on system startup<\/i>&#8220;. However, all user added rules are still left there though NOT function.<\/p>\n<p>For choice, you may <b>reset ufw<\/b> via command:<\/p>\n<pre>sudo ufw reset<\/pre>\n<p>Which will disable firewall (if enabled), then reset all rules to the installed defaults.<\/p>","protected":false},"excerpt":{"rendered":"<p>This is a beginner&#8217;s guide shows you how to enable, disable, and configure firewall in Ubuntu using UFW. Firewall is a network security system that monitors incoming and outgoing network traffic, and decides whether to allow or block specific traffic based on pre-defined security rules. Linux Kernel has the Netfilter subsystem, which is implemented as [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":46965,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9],"tags":[2179],"class_list":["post-46964","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-howtos","tag-firewall"],"_links":{"self":[{"href":"https:\/\/ubuntuhandbook.org\/index.php\/wp-json\/wp\/v2\/posts\/46964","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ubuntuhandbook.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ubuntuhandbook.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ubuntuhandbook.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ubuntuhandbook.org\/index.php\/wp-json\/wp\/v2\/comments?post=46964"}],"version-history":[{"count":0,"href":"https:\/\/ubuntuhandbook.org\/index.php\/wp-json\/wp\/v2\/posts\/46964\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/ubuntuhandbook.org\/index.php\/wp-json\/wp\/v2\/media\/46965"}],"wp:attachment":[{"href":"https:\/\/ubuntuhandbook.org\/index.php\/wp-json\/wp\/v2\/media?parent=46964"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ubuntuhandbook.org\/index.php\/wp-json\/wp\/v2\/categories?post=46964"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ubuntuhandbook.org\/index.php\/wp-json\/wp\/v2\/tags?post=46964"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}