---
title: "How To Set Up the UFW Firewall on Linux"
description: "Learn how to set up the UWF firewall on Linux and configure firewall rules to either allow or block connections on your network in this tutorial!"
canonical: "https://adamtheautomator.com/ufw-firewall/"
---

# How To Set Up the UFW Firewall on Linux

> Learn how to set up the UWF firewall on Linux and configure firewall rules to either allow or block connections on your network in this tutorial!

Source: https://adamtheautomator.com/ufw-firewall/

---

ATA Learning

Tap to hide

[

ATA Learning

](/)

*   [Home](/)
*   [Tutorials](/tutorials/)
*   [Instructors](/author/)
*   [Advertising](/advertising/)
*   [Recommended Resources](/resources/)
*   [About Adam](/about-adam/)

Search for:  

*   [](https://twitter.com/adbertram)
*   [](https://github.com/Adam-the-Automator)
*   [](https://www.linkedin.com/company/adam-the-automator-llc)
*   [](/feed/)

![How To Set Up the UFW Firewall on Linux](https://adamtheautomator.com/wp-content/uploads/2021/12/How-To-Set-Up-the-UFW-Firewall-on-Linux.jpg)

# How To Set Up the UFW Firewall on Linux

[![](https://secure.gravatar.com/avatar/2788bb1a3f735603f81eca51d68daec56a9d97e805a10268fb2c20afcc76b81b?s=192&d=mm&r=g)Nicholas Xuan Nguyen](https://adamtheautomator.com/author/nicholas-xuan-nguyen/)29 December 20217 min. read

Categories: [IT Ops](/category/it-ops/)

Tags:[Command Line](/tag/command-line/)[Linux](/tag/linux/)

Table of Contents

*   [Prerequisites](#prerequisites)
*   [Installing UFW and Enabling IPv6 Connection](#installing-ufw-and-enabling-ipv6-connection)
*   [Configuring Default Policies for Firewall Rules](#configuring-default-policies-for-firewall-rules)
*   [Allowing SSH Connections on the UFW Firewall](#allowing-ssh-connections-on-the-ufw-firewall)
*   [Allowing HTTP and HTTPS Connections](#allowing-http-and-https-connections)
*   [Allowing Connections from Specific Port Range and IP Address](#allowing-connections-from-specific-port-range-and-ip-address)
*   [Allowing Traffic from a Specific Network Interface](#allowing-traffic-from-a-specific-network-interface)
*   [Deleting UFW Firewall Rules](#deleting-ufw-firewall-rules)
*   [Resetting the UFW Firewall](#resetting-the-ufw-firewall)
*   [Conclusion](#conclusion)

Without a firewall, there are no rules or restrictions on your network traffic and that leads to a number of negative consequences. Linux system comes with a default firewall configuration tool, which is Uncomplicated Firewall (UFW). But how do you set up a UFW firewall? Sit back and relax, this tutorial has got you covered!

In this tutorial, you’ll learn how to configure UFW and set up a firewall on your Linux system to secure your network and ward-off malicious acts.

Ready? Read on to get started!

## Prerequisites

This tutorial will be a hands-on demonstration. If you’d like to follow along, be sure you have the following:

*   An Ubuntu machine – This tutorial uses Ubuntu 20.04 LTS, but other Linux distributions will work.

Related:[How to Install Ubuntu 20.04 \[Step-by-Step\]](https://adamtheautomator.com/install-ubuntu/)

*   [Root](https://linuxize.com/post/how-to-enable-and-disable-root-user-account-in-ubuntu/) privileges to your machine.

## **Installing UFW and Enabling IPv6 Connection**

Even though UFW comes packaged with your Ubuntu system, UFW is not installed by default. Install UFW first with the `apt` package manager and configure it to allow connections over IPv6.

Related:[How to use the Ansible apt Module to Manage Linux Packages](https://adamtheautomator.com/ansible-apt/)

1\. Open your terminal and run the `apt update` command below to update your local package index. The command accepts all prompts (`-y`) during the update for less user intervention.

```bash
sudo apt update -y
```

![Updating the System Package](https://adamtheautomator.com/wp-content/uploads/2021/12/image-248.png)

Updating the System Package

2\. Next, run the below command to install UFW (`install uwf`) on your system while accepting all prompts (`-y`) during the installation.

```powershell
sudo apt install ufw -y
```

![Installing UFW on Ubuntu](https://adamtheautomator.com/wp-content/uploads/2021/12/image-249.png)

Installing UFW on Ubuntu

3\. Open the UFW configuration file (_/etc/default/ufw_) with your favorite text editor. UFW supports IPv6, but you need to make sure that the firewall is configured to accept connections over IPv6.

> _If you only have IPv4 enabled, you’re still leaving yourself open to IPv6 attacks._

4\. Scroll down to the **IPV6** variable and set the value to **yes**, as shown below, then save the changes and exit the editor

![Enabling IPV6 in the UFW Configuration File](https://adamtheautomator.com/wp-content/uploads/2021/12/image-250.png)

Enabling IPV6 in the UFW Configuration File

5\. Finally, run the command below to disable and re-enable UFW. The command restarts the UFW service so the changes can take effect.

After the command completes, your firewall can now write both IPv4 and IPv6 firewall rule sets.

```bash
sudo ufw disable && sudo ufw enable
```

## Configuring Default Policies for Firewall Rules

If you’re just getting started with UFW, it’s recommended to set up a default policy for your rules. The default policies are applied to a chain that doesn’t have any specific rules defined yet.

Set up UFW to deny all incoming connections and allow all outgoing connections. As a result, anyone trying to reach your machine from the outside world is denied, while you can still freely connect to any website or server.

Run the `ufw` command below to `deny` all `incoming` connections by `default`.

```bash
sudo ufw default deny incoming
```

![Denying Incoming Network Traffics](https://adamtheautomator.com/wp-content/uploads/2021/12/image-252.png)

Denying Incoming Network Traffics

Now run the following command to `allow` all `outgoing` connections by `default`.

```bash
sudo ufw default allow outgoing
```

![Allowing Outgoing Network Traffics](https://adamtheautomator.com/wp-content/uploads/2021/12/image-253.png)

Allowing Outgoing Network Traffics

## Allowing SSH Connections on the UFW Firewall

You’ve just set up default policies on your UFW firewall to deny all incoming traffic, and the “allow all-deny all” rule is a good setting for a regular user. But what if you’re running a server? You’ll need to allow specific traffic in and out. Allowing SSH connection on your UFW firewall will do the trick to allow specific traffic in and out.

Related:[A Windows Guy in a Linux World: Setting up SSH in Linux](https://adamtheautomator.com/ssh-command-in-linux/)

You’ll set up an SSH server that allows incoming SSH connections on port 22. But why port 22 and not any other port? On Unix-like systems, the SSH daemon listens on port 22 by default, so it’s a good practice to use the default SSH port to make your life a bit easier.

1\. Run the below commands to install the [OpenSSH](https://www.openssh.com/) server (`install openssh-server`) on your system and start an OpenSSH server (`start ssh`).

```bash
sudo apt install openssh-server -y
sudo systemctl start ssh
```

2\. Now run the command below to allow incoming SSH connections. Without specifying port 22 will be enough as UFW knows what port is for SSH.

```bash
sudo ufw allow ssh
```

![Allowing SSH connection ](https://adamtheautomator.com/wp-content/uploads/2021/12/image-254.png)

Allowing SSH connection

> _The /etc/services file contains a list of all available services on your system. Open the file on your text editor, scroll down to **ssh** and see the port number (**22**) is part of the service description, as shown below._

![Previewing the /etc/services file ](https://adamtheautomator.com/wp-content/uploads/2021/12/image-255.png)

Previewing the _/etc/services_ file

But perhaps you prefer to specify the port number (`22`) to allow for SSH. If so, run the following command instead.

```bash
sudo ufw allow 22
```

3\. Now run the below command to enable UFW.

```bash
sudo ufw enable
```

Type **Y** in the confirmation prompt, as shown below, and press Enter to continue running the command. UFW will now start filtering packets on your system.

![Enabling UFW](https://adamtheautomator.com/wp-content/uploads/2021/12/image-256.png)

Enabling UFW

4\. Finally, run either of the below commands to check the status of your UFW firewall.

```bash
## Displays more detailed information, such as the interface and 
## the packet's current progress
sudo ufw status verbose
## Shows each rule with a number and the corresponding allow or deny status 
## The numbered mode is useful when you are trying to delete a rule set here and there
sudo ufw status numbered
```

If you run the command with the `verbose` option, you’ll see an output similar to the one below:

*   **Status: active** – Indicates the firewall is currently running.
*   **Logging: on (low)** – Indicates that UFW is logging all packets being processed by the firewall.
*   **Default: deny (incoming), allow (outgoing), disabled (routed)** – Indicates that the default policy is to deny all incoming connections and allow all outgoing connections.
*   **New profiles: skip** – Indicates the firewall is currently using the default set of rules.

![Checking verbose UFW firewall status ](https://adamtheautomator.com/wp-content/uploads/2021/12/image-257.png)

Checking verbose UFW firewall status

If you run the command with the `numbered` option instead, you’ll see the output below. You can see a list of numbered rules and their corresponding **ALLOW** or **DENY** status.

![Viewing the UW Firewall status in a numbered list](https://adamtheautomator.com/wp-content/uploads/2021/12/image-258.png)

Viewing the UW Firewall status in a numbered list

## **Allowing HTTP and HTTPS Connections**

At this point, you’ve only allowed SSH connections on your UFW firewall, but that limits your server’s capabilities. Allow other types of connections, such as HTTP or HTTPS, and add more rules to the UFW firewall.

Run either of the following commands to allow incoming HTTP connections.

```bash
## HTTP connection uses port 80 (not secure)
sudo ufw allow 80
sudo ufw allow http
```

![Allowing HTTP connections](https://adamtheautomator.com/wp-content/uploads/2021/12/image-259.png)

Allowing HTTP connections

Now, run either of the commands below to allow incoming HTTPS connections.

```bash
sudo ufw allow https
## HTTP connection uses port 443 (secure)
sudo ufw allow 443
```

![Allowing incoming HTTPS connections. ](https://adamtheautomator.com/wp-content/uploads/2021/12/image-260.png)

Allowing incoming HTTPS connections.

## Allowing Connections from Specific Port Range and IP Address

Some applications use multiple ports in order to provide their services. And perhaps you have a range of ports to open or you need to allow connection from a specific IP address. In that case, add more UFW firewall rules.

Run the commands below to allow incoming connections on ports 5001 to 5009. You always should specify the protocol (`tcp` or `udp`) after the port range that the rules apply to because not all ports are used by both protocols.

For example, commonly used TCP ports include 80 (HTTP) and 443 (HTTPS). But common UDP ports include 53 (DNS) and 67/68 (DHCP).

```bash
sudo ufw allow 5001:5010/tcp
sudo ufw allow 5001:5010/udp
```

![Allowing traffic on 5001:5010 port range](https://adamtheautomator.com/wp-content/uploads/2021/12/image-261.png)

Allowing traffic on 5001:5010 port range

Run the below command instead if you prefer to allow SSH connections from a specific IP address. The command allows SSH connections (`port 22`) only from the `192.168.1.2` IP address.

```bash
sudo ufw allow from 192.168.1.2 to any port 22
```

![Allowing SSH Connections from Specific IP Address](https://adamtheautomator.com/wp-content/uploads/2021/12/image-262.png)

Allowing SSH Connections from Specific IP Address

## Allowing Traffic from a Specific Network Interface

UFW also lets you allow traffic on a specific network interface only, such as [eth0](https://unix.stackexchange.com/questions/82919/what-does-the-eth0-interface-name-mean-in-linux) is the first Ethernet interface and [wlan0](https://answer-all.com/common-questions/what-is-wlan0-on-my-network/) is the first Wi-Fi interface.

Run either of the commands below to allow HTTP connections only on the `eth0` and `wlan0` interfaces.

```bash
## Allow HTTP connection only on the eth0 interface
sudo ufw allow in on eth0 to any port 80
## Allow HTTP connection only on the wlan0 interface
sudo ufw allow in on wlan0 to any port 80
```

![Allowing traffic on a specific interface](https://adamtheautomator.com/wp-content/uploads/2021/12/image-263.png)

Allowing traffic on a specific interface

## Deleting UFW Firewall Rules

Perhaps some UFW firewall rules don’t serve any purpose anymore. In that case, you might want to remove some of the rules from UFW. But first, you must know either the number or the name of the rule to delete.

1\. Run the below command to get a numbered list of the rules added to UFW.

```bash
sudo ufw status numbered
```

Note the rule’s number or name in the output, like the one below.

![Previewing all the rules](https://adamtheautomator.com/wp-content/uploads/2021/12/image-264.png)

Previewing all the rules

2\. Next, run the command below to `delete` rule number `4`, which is the `5001:5010/tcp` port range.

```bash
sudo ufw delete 4
```

![Deleting a Rule by Rule Number](https://adamtheautomator.com/wp-content/uploads/2021/12/image-265.png)

Deleting a Rule by Rule Number

3\. Run the below command to `delete` a rule by its actual name with the `allow` status. In this example, you would delete the `http` rule by running the following command.

```bash
sudo ufw delete allow http
```

![Deleting a Rule by Rule Name (http)](https://adamtheautomator.com/wp-content/uploads/2021/12/image-266.png)

Deleting a Rule by Rule Name (http)

4\. Now run the following command to `delete` a rule by specifying a port number (`443`) with the `allow` status.

```bash
sudo ufw delete allow 443
```

![Deleting a Rule by Port Number (443)](https://adamtheautomator.com/wp-content/uploads/2021/12/image-267.png)

Deleting a Rule by Port Number (443)

5\. Finally, re-run the following command as you did in step one to list all rules.

```bash
sudo ufw status numbered
```

As show can see below, the rules for the `5001:5010/tcp` port range, the `http`, and the `443` port are now gone.

![Checking the firewall rules](https://adamtheautomator.com/wp-content/uploads/2021/12/image-268.png)

Checking the firewall rules

## Resetting the UFW Firewall

There might be times when you need to reset UFW to its defaults, such as after configuring a large set of rules. An update may change your configuration, requiring you to re-configure UFW and possibly start over from scratch.

Run the `ufw reset` command below to reset all of your firewall rules to their default settings. This command disables UFW and deletes all of your current firewall rules.

```bash
sudo ufw reset
```

Type ‘Y’ and press Enter to continue resetting your UFW firewall.

![Resetting UFW](https://adamtheautomator.com/wp-content/uploads/2021/12/image-269.png)

Resetting UFW

After the reset is complete, you will have a fresh installation of UFW fully disabled, and even your default policies are gone.

Now run the below command to re-enable UFW start configuring your firewall rules from scratch.

```bash
sudo ufw enable
```

If you decide you don’t want to use UFW anymore, then there’s no need to re-enable it. Or run the command below to ensure UFW is disabled.

```bash
sudo ufw disable
```

![Disabling UFW firewall](https://adamtheautomator.com/wp-content/uploads/2021/12/image-270.png)

Disabling UFW firewall

## Conclusion

Throughout this tutorial, you’ve realized that setting up a firewall is not too daunting when using UFW. You should now have a good understanding of how to set up and implement your own rules with UFW on Ubuntu.

Now, why not build on this newfound knowledge by learning more about [UFW and Docker Security on a Linux machine](https://dev.to/andylim0221/ufw-and-docker-security-on-linux-machine-oc3)?

Share this article

[Share on X](https://twitter.com/intent/tweet?url=https%3A%2F%2Fadamtheautomator.com%2Fufw-firewall%2F&text=How%20To%20Set%20Up%20the%20UFW%20Firewall%20on%20Linux)[Share on Facebook](https://www.facebook.com/sharer/sharer.php?u=https%3A%2F%2Fadamtheautomator.com%2Fufw-firewall%2F)[Share on LinkedIn](https://www.linkedin.com/sharing/share-offsite/?url=https%3A%2F%2Fadamtheautomator.com%2Fufw-firewall%2F)

## Related Posts

![](https://adamtheautomator.com/wp-content/uploads/2022/09/Practical-Linux-Unix-Tee-Commands-for-the-Linux-Admin.jpg)

### [Master Unix tee Commands for Real-World Linux Admin Tasks](/unix-tee/)

Simplify Linux output management and streamline your workflow with the “Unix tee” command. This tutorial guides you through its practical applications.

![](https://adamtheautomator.com/wp-content/uploads/2022/09/Useful-Linux-Dig-Examples-for-the-Network-Admin.jpg)

### [Linux Dig for Network Admins: Practical Guide with Examples](/linux-dig/)

Become a network troubleshooting expert! This comprehensive Linux Dig guide empowers you with essential DNS query commands for immediate results.

![](https://adamtheautomator.com/wp-content/uploads/2021/12/install-phpmyadmin.jpg)

### [Step-by-Step Tutorial to Install phpMyAdmin Securely on Linux](/install-phpmyadmin/)

Discover how to securely install phpMyAdmin on Linux with our concise guide. Learn essential steps for a safe and efficient setup, tailored for Linux users.

## Categories

*   [IT Ops](/category/it-ops/)
*   [Cloud](/category/cloud/)
*   [DevOps](/category/devops/)
*   [Home Ops](/category/home-ops/)
*   [Information Security](/category/infosec/)
*   [Software Development](/category/software-development/)

## Site

*   [Home](/)
*   [Tutorials](/tutorials/)
*   [Instructors](/author/)
*   [Advertising](/advertising/)
*   [Recommended Resources](/resources/)
*   [About Adam](/about-adam/)

Copyright 2026© ATA Learning | [Privacy Policy](/privacy/)
