# How To Configure Firewall with UFW on Ubuntu 20.04 LTS

[https://www.cyberciti.biz/faq/how-to-configure-firewall-with-ufw-on-ubuntu-20-04-lts/](https://www.cyberciti.biz/faq/how-to-configure-firewall-with-ufw-on-ubuntu-20-04-lts/)

How do I set up and configure firewall with UFW on Ubuntu 20.04 LTS server?  
<span id="bkmrk-"></span>  
UFW is an acronym for an uncomplicated firewall. Securing a network with an uncomplicated firewall is super easy and highly recommended. This page explains how to set up and secure your Ubuntu 20.04 LTS server with ufw.

<div class="my_postbox toc" id="bkmrk-tutorial-requirement"><table class="tutorialrequirements"><thead><tr><th colspan="2">Tutorial requirements</th></tr></thead><tbody><tr><td width="30%">Requirements</td><td width="70%">Ubuntu Linux 20.04 LTS</td></tr><tr><td><span title="Indicates whether the root account requires for administrative purposes to complete this tutorial">Root privileges</span></td><td>[Yes](https://www.cyberciti.biz/faq/how-can-i-log-in-as-root/ "See how to login as root user")</td></tr><tr><td><a name="tutorial_difficulty_level"></a><span title="The relative difficulty of completing this tutorial task">Difficulty level</span></td><td>[Easy](https://www.cyberciti.biz/faq/tag/easy/ "See all Easy Linux / Unix System Administrator Tutorials")</td></tr><tr><td>Category</td><td>Firewall</td></tr><tr><td><span title="This tutorial has prerequisites. Please make sure you have the required tools/apps/developer tools available before proceeding">Prerequisites</span></td><td>ufw command</td></tr><tr><td><a name="tutorial_est_reading_time"></a><span title="Estimated reading time for this tutorial page">Est. reading time</span></td><td>7 minutes</td></tr><tr><td colspan="2">Table of contents ↓

<div class="toc_white no_bullets" id="bkmrk-1%C2%A0set-up-ufw-policy-"><a name="TOC"></a>- [<span class="toc_number toc_depth_1">1 </span>Set up ufw policy](https://www.cyberciti.biz/faq/how-to-configure-firewall-with-ufw-on-ubuntu-20-04-lts/#Set_up_ufw_policy "Set up ufw policy")
- [<span class="toc_number toc_depth_1">2 </span>Open SSH port](https://www.cyberciti.biz/faq/how-to-configure-firewall-with-ufw-on-ubuntu-20-04-lts/#Open_SSH_port "Open SSH port")
- [<span class="toc_number toc_depth_1">3 </span>Turn on ufw firewall](https://www.cyberciti.biz/faq/how-to-configure-firewall-with-ufw-on-ubuntu-20-04-lts/#Turn_on_ufw_firewall "Turn on ufw firewall")
- [<span class="toc_number toc_depth_1">4 </span>Open ports with ufw](https://www.cyberciti.biz/faq/how-to-configure-firewall-with-ufw-on-ubuntu-20-04-lts/#Open_ports_with_ufw "Open ports with ufw")
- [<span class="toc_number toc_depth_1">5 </span>Block ports with ufw](https://www.cyberciti.biz/faq/how-to-configure-firewall-with-ufw-on-ubuntu-20-04-lts/#Block_ports_with_ufw "Block ports with ufw")
- [<span class="toc_number toc_depth_1">6 </span>Get ufw firewall status](https://www.cyberciti.biz/faq/how-to-configure-firewall-with-ufw-on-ubuntu-20-04-lts/#Get_ufw_firewall_status "Get ufw firewall status")
- [<span class="toc_number toc_depth_1">7 </span>Delete ufw firewall rules](https://www.cyberciti.biz/faq/how-to-configure-firewall-with-ufw-on-ubuntu-20-04-lts/#Delete_ufw_firewall_rules "Delete ufw firewall rules")
- [<span class="toc_number toc_depth_1">8 </span>Firewall management commands](https://www.cyberciti.biz/faq/how-to-configure-firewall-with-ufw-on-ubuntu-20-04-lts/#Firewall_management_commands "Firewall management commands")
- [<span class="toc_number toc_depth_1">9 </span>IP Masquerading](https://www.cyberciti.biz/faq/how-to-configure-firewall-with-ufw-on-ubuntu-20-04-lts/#IP_Masquerading "IP Masquerading")
- [<span class="toc_number toc_depth_1">10 </span>Egress filtering](https://www.cyberciti.biz/faq/how-to-configure-firewall-with-ufw-on-ubuntu-20-04-lts/#Egress_filtering "Egress filtering")
- [<span class="toc_number toc_depth_1">11 </span>Conclusion](https://www.cyberciti.biz/faq/how-to-configure-firewall-with-ufw-on-ubuntu-20-04-lts/#Conclusion "Conclusion")

</div></td></tr></tbody></table>

</div><div class="card" id="bkmrk-nixcraft%3A-privacy-fi"><div class="card-body"><div class="card-title">nixCraft: Privacy First, Reader Supported</div><div class="card-text">- **nixCraft is a one-person operation**. I create all the content myself, with no help from AI or ML. I keep the content accurate and up-to-date.
- **Your privacy is my top priority**. I don’t track you, show you ads, or spam you with emails. Just pure content in the true spirit of Linux and FLOSS.
- **Fast and clean browsing experience**. nixCraft is designed to be fast and easy to use. You won’t have to deal with pop-ups, ads, cookie banners, or other distractions.
- **Support independent content creators**. nixCraft is a labor of love, and it’s only possible thanks to the support of our readers. If you enjoy the content, please support us on Patreon or share this page on social media or your blog. Every bit helps.

</div></div></div><div class="card" id="bkmrk-join%C2%A0patreon%C2%A0%E2%9E%94"><div class="card-body"><div class="card-text"><center>[Join **Patreon** ➔](https://www.patreon.com/nixcraft)</center></div></div></div>## <a name="Set_up_ufw_policy"></a>Step 1 – Set Up default UFW policies

To view status of ufw, type:  
`<span class="normaluserprompt" title="The shell prompt usually ends in a $ sign and is not part of the command for the nonprivileged user.">$ </span>sudo ufw status`  
Sample outputs:

```
Status: inactive
```

The default policy firewall works out great for both the servers and desktop. It is always a good policy to closes all ports on the server and open only required ports one by one. Let us block all incoming connection and only allow outgoing connections from the Ubuntu 20.04 LTS box:  
`<span class="normaluserprompt" title="The shell prompt usually ends in a $ sign and is not part of the command for the nonprivileged user.">$ </span>sudo ufw default allow outgoing<span class="normaluserprompt" title="The shell prompt usually ends in a $ sign and is not part of the command for the nonprivileged user.">$ </span>sudo ufw default deny incoming`

### Enabling IPv6 support

Make sure the directive <kbd>IPV6=yes</kbd> do exists in <kbd>**/etc/default/ufw**</kbd> file. For instance:  
`<span class="normaluserprompt" title="The shell prompt usually ends in a $ sign and is not part of the command for the nonprivileged user.">$ </span>cat /etc/default/ufw`

## <a name="Open_SSH_port"></a>Step 2 – Open SSH TCP port 22 connections

The next logical step is to allow incoming SSH ports. We can easily open SSH TCP port 22 using UFW as follows:  
`<span class="normaluserprompt" title="The shell prompt usually ends in a $ sign and is not part of the command for the nonprivileged user.">$ </span>sudo ufw allow ssh`  
If you are running ssh on TCP port 2222 or TCP port 2323, enter:  
`<span class="normaluserprompt" title="The shell prompt usually ends in a $ sign and is not part of the command for the nonprivileged user.">$ </span>sudo ufw allow 2222/tcp<span class="normaluserprompt" title="The shell prompt usually ends in a $ sign and is not part of the command for the nonprivileged user.">$ </span>sudo ufw allow 2323/tcp`  
Some sysadmins have a static IP address (such as 202.54.2.5) at home or office location. In that case, only allow ssh access from the static IP address such as 202.54.2.5 to Ubuntu server IP address 172.24.13.45:  
`<span class="normaluserprompt" title="The shell prompt usually ends in a $ sign and is not part of the command for the nonprivileged user.">$ </span>sudo ufw allow proto tcp from 202.54.2.5 to 172.24.13.45 port 22`  
But how do I find out my static IP 202.54.2.5 on Ubuntu server itself? Try the [w command](https://www.cyberciti.biz/faq/unix-linux-w-command-examples-syntax-usage-2/ "Linux / Unix: w Command Examples") or lastlog command:  
`<span class="normaluserprompt" title="The shell prompt usually ends in a $ sign and is not part of the command for the nonprivileged user.">$ </span>w<span class="normaluserprompt" title="The shell prompt usually ends in a $ sign and is not part of the command for the nonprivileged user.">$ </span>lastlog -u {YOUR_ADMIN_LOGIN_NAME_HERE}<span class="normaluserprompt" title="The shell prompt usually ends in a $ sign and is not part of the command for the nonprivileged user.">$ </span>lastlog -u vivek`  
And here is what I see:

```
Username         Port     From             Latest
vivek            pts/0    202.54.2.5       Thu Sep 29 15:19:21 +0000 2022
```

Next, let us limit ssh port, run:  
`<span class="normaluserprompt" title="The shell prompt usually ends in a $ sign and is not part of the command for the nonprivileged user.">$ </span>sudo ufw limit ssh`  
See “[How to limit SSH (TCP port 22) connections with ufw on Ubuntu Linux](https://www.cyberciti.biz/faq/howto-limiting-ssh-connections-with-ufw-on-ubuntu-debian/)” for more information.

## <a name="Turn_on_ufw_firewall"></a>Step 3 – Turn on firewall

Now we got basic configuration enabled. In other words, the firewall will drop all incoming traffic except for ssh TCP port 22. Let us true it on the UFW, enter:  
`<span class="normaluserprompt" title="The shell prompt usually ends in a $ sign and is not part of the command for the nonprivileged user.">$ </span>sudo ufw enable`  
![Ubuntu Configure Firewall with UFW]()  
Remember, once UFW enabled, it runs across system reboots too. We can verify that easily as follows using the systemctl command:  
`<span class="normaluserprompt" title="The shell prompt usually ends in a $ sign and is not part of the command for the nonprivileged user.">$ </span>sudo systemctl status ufw.service`  
![How to Configure Firewall with UFW on Ubuntu 20.04]()

### Want to disable the UFW based firewall? Try

If you need to stop the firewall and disable on system startup, eenter:  
`<span class="normaluserprompt" title="The shell prompt usually ends in a $ sign and is not part of the command for the nonprivileged user.">$ </span>sudo ufw disable`  
Sample outputs:

```
Firewall stopped and disabled on system startup
```

## <a name="Open_ports_with_ufw"></a>Step 4 – Open specific incoming connections/ports

Let us add more rules. Say you want to open ports and allow IP address with ufw. The syntax is as follows to open TCP port 80 and 443:  
`<span class="normaluserprompt" title="The shell prompt usually ends in a $ sign and is not part of the command for the nonprivileged user.">$ </span>sudo ufw allow 80/tcp comment 'accept Apache'<span class="normaluserprompt" title="The shell prompt usually ends in a $ sign and is not part of the command for the nonprivileged user.">$ </span>sudo ufw allow 443/tcp comment 'accept HTTPS connections'`  
Open UDP/1194 (OpenVPN) server:  
`<span class="normaluserprompt" title="The shell prompt usually ends in a $ sign and is not part of the command for the nonprivileged user.">$ </span>sudo ufw allow 1194/udp comment 'OpenVPN server'`

### Allow port ranges via ufw

We can allow port ranges too say, tcp and udp 3000 to 4000:  
`<span class="normaluserprompt" title="The shell prompt usually ends in a $ sign and is not part of the command for the nonprivileged user.">$ </span>sudo ufw allow 3000:4000/tcp<span class="normaluserprompt" title="The shell prompt usually ends in a $ sign and is not part of the command for the nonprivileged user.">$ </span>sudo ufw allow 3000:4000/udp`  
In this example, you want to allow ALL connections from an IP address called 104.22.10.214, enter:  
`<span class="normaluserprompt" title="The shell prompt usually ends in a $ sign and is not part of the command for the nonprivileged user.">$ </span>sudo ufw allow from 104.22.10.214`  
Let us allow connections from an IP address called 104.22.11.213 to our port 25, enter:  
`<span class="normaluserprompt" title="The shell prompt usually ends in a $ sign and is not part of the command for the nonprivileged user.">$ </span>sudo ufw allow from 104.22.11.213 to any port 25 proto tcp`  
We can set dest IP 222.222.222.222 for port 25 too:  
`<span class="normaluserprompt" title="The shell prompt usually ends in a $ sign and is not part of the command for the nonprivileged user.">$ </span>sudo ufw allow from 104.22.11.213 to 222.222.222.222 port 25 proto tcp`

### Allow connection on specific interface

Open port 22 for wg0 interface only:  
`<span class="normaluserprompt" title="The shell prompt usually ends in a $ sign and is not part of the command for the nonprivileged user.">$ </span>sudo ufw allow in on wg0 to any port 22`  
Say you want to allow connection for TCP port 3306 on lxdbr0 interface from 10.105.28.22, then add:  
`<span class="normaluserprompt" title="The shell prompt usually ends in a $ sign and is not part of the command for the nonprivileged user.">$ </span>sudo ufw allow in on lxdbr0 from 10.105.28.22 to any port 3306 proto tcp`  
Let us add sub/net instead of single IP address:  
`<span class="normaluserprompt" title="The shell prompt usually ends in a $ sign and is not part of the command for the nonprivileged user.">$ </span>sudo ufw allow in on lxdbr0 from 10.105.28.0/24 to any port 3306 proto tcp`

## <a name="Block_ports_with_ufw"></a>Step 5 – Block and deny incoming connections/ports

Do you want to close ports and block certain IP addresses? The syntax is as follows to deny access. In other words, simply ignoring access to port 25:  
`<span class="normaluserprompt" title="The shell prompt usually ends in a $ sign and is not part of the command for the nonprivileged user.">$ </span>sudo ufw deny 25/tcp`  
Make sure we deny all connections from an IP address called 203.5.1.43, enter:  
`<span class="normaluserprompt" title="The shell prompt usually ends in a $ sign and is not part of the command for the nonprivileged user.">$ </span>sudo ufw deny from 203.5.1.43`  
Deny all connections from an IP/subnet called 103.13.42.13/29, enter:  
`<span class="normaluserprompt" title="The shell prompt usually ends in a $ sign and is not part of the command for the nonprivileged user.">$ </span>sudo ufw deny from 103.13.42.13/29`  
Want to deny access to 1.1.1.2 (say bad guys IP) on port 22? Try:  
`<span class="normaluserprompt" title="The shell prompt usually ends in a $ sign and is not part of the command for the nonprivileged user.">$ </span>sudo ufw deny from 1.1.1.2 to any port 22 proto tcp`

## <a name="Get_ufw_firewall_status"></a>Step 6 – Verify status of UFW

Use the status command as follows:  
`<span class="normaluserprompt" title="The shell prompt usually ends in a $ sign and is not part of the command for the nonprivileged user.">$ </span>sudo ufw status`

```
Status: active
 
To                         Action      From
--                         ------      ----
22/tcp                     ALLOW       Anywhere                  
80/tcp                     ALLOW       Anywhere                   # accept Apache
443/tcp                    ALLOW       Anywhere                   # accept HTTPS connections
1194/udp                   ALLOW       Anywhere                   # OpenVPN server
3000:4000/tcp              ALLOW       Anywhere                  
3000:4000/udp              ALLOW       Anywhere                  
Anywhere                   ALLOW       104.22.10.214             
25/tcp                     ALLOW       104.22.11.213             
222.222.222.222 25/tcp     ALLOW       104.22.11.213             
Anywhere                   DENY        203.5.1.43                
Anywhere                   DENY        103.13.42.8/29            
22/tcp                     DENY        1.1.1.2                   
22/tcp (v6)                ALLOW       Anywhere (v6)             
80/tcp (v6)                ALLOW       Anywhere (v6)              # accept Apache
443/tcp (v6)               ALLOW       Anywhere (v6)              # accept HTTPS connections
1194/udp (v6)              ALLOW       Anywhere (v6)              # OpenVPN server
3000:4000/tcp (v6)         ALLOW       Anywhere (v6)             
3000:4000/udp (v6)         ALLOW       Anywhere (v6)
```

Want verbose outputs? Try:  
`<span class="normaluserprompt" title="The shell prompt usually ends in a $ sign and is not part of the command for the nonprivileged user.">$ </span>sudo ufw status verbose`

## <a name="Delete_ufw_firewall_rules"></a>Ubuntu 20.04 LTS UFW delete rules

So far we learned how to add, deny, and list the firewall rules. It is time to delete unwanted rules. The syntax is as follows to list all of the current rules in a numbered list format:  
`<span class="normaluserprompt" title="The shell prompt usually ends in a $ sign and is not part of the command for the nonprivileged user.">$ </span>sudo ufw status numbered`

```
Status: active
 
     To                         Action      From
     --                         ------      ----
[ 1] 22/tcp                     ALLOW IN    Anywhere                  
[ 2] 80/tcp                     ALLOW IN    Anywhere                   # accept Apache
[ 3] 443/tcp                    ALLOW IN    Anywhere                   # accept HTTPS connections
[ 4] 1194/udp                   ALLOW IN    Anywhere                   # OpenVPN server
[ 5] 3000:4000/tcp              ALLOW IN    Anywhere                  
[ 6] 3000:4000/udp              ALLOW IN    Anywhere
```

To delete 6th rule type the command:  
`<span class="normaluserprompt" title="The shell prompt usually ends in a $ sign and is not part of the command for the nonprivileged user.">$ </span>sudo ufw delete 6<span class="normaluserprompt" title="The shell prompt usually ends in a $ sign and is not part of the command for the nonprivileged user.">$ </span>sudo ufw status numbered`