---
title: How to Set Up a Basic Firewall with Uncomplicated Firewall (UFW)
description: A firewall is a fundamental tool for securing your server, as it controls incoming and outgoing network traffic based on predetermined rules. For Linux servers, UFW (Uncomplicated Firewall) is a popular choice due to its simplicity and ease of use,…
url: "https://truehost.com/support/knowledge-base/how-to-set-up-a-basic-firewall-with-uncomplicated-firewall-ufw/"
updated: "2024-10-29"
category: Cloud servers in Kenya
---

# How to Set Up a Basic Firewall with Uncomplicated Firewall (UFW)

A firewall is a fundamental tool for securing your server, as it controls incoming and outgoing network traffic based on predetermined rules. For Linux servers, UFW (Uncomplicated Firewall) is a popular choice due to its simplicity and ease of use, even for those new to Linux system administration

Originally developed for Ubuntu, UFW has since been adopted by other Debian-based systems and even some non-Debian-based distributions, thanks to its efficiency and straightforward command structure.

### Supported Operating Systems

- **Ubuntu** (all LTS and newer versions)

- **Debian** (and many of its derivatives)

- **Linux Mint**

- **Kali Linux**

- **Raspberry Pi OS** (Raspbian)

On **Red Hat-based** systems like **CentOS**, **RHEL**, and **Fedora**, UFW can be installed but is less common, as these distributions typically use **firewalld** as their primary firewall tool.

### How to Set Up UFW on Your Linux Server

In this article, we will use a server that has Ubuntu 22.04 installed

##### **Step 1: Update your server**

- First, start by updating apckages on your server. Use the command below to do that.

```
sudo apt update
```

![](https://truehost.com/support/wp-content/uploads/2024/10/image-592.png)

##### **Step 2: Install UFW**

- Run the command below to install
- This command will install UFW and any required dependencies.

```
sudo apt install ufw
```

![](https://truehost.com/support/wp-content/uploads/2024/10/image-594-1024x458.png)

##### **Step 2: Set Up Default Rules**

- These rules help determine the overall security level of your firewall.
- It’s generally best practice to **deny all incoming connections** and **allow all outgoing connections** by default. This ensures that only the traffic you explicitly allow can access your server.

**Deny Incoming Connections:**

```
sudo ufw default deny incoming
```

**Allow Outgoing Connections:**

```
sudo ufw default allow outgoing
```

![](https://truehost.com/support/wp-content/uploads/2024/10/image-595-1024x202.png)

- These default policies establish a secure baseline by only allowing connections to services you specify.

##### **Step 3: Allow Specific Ports and Services**

- After setting up default rules, you’ll need to allow connections to essential services.

**Allow SSH (Port 22)** – SSH allows remote access to your server, so allowing this port is critical if you want to maintain access.

```
sudo ufw allow ssh
```

![](https://truehost.com/support/wp-content/uploads/2024/10/image-596.png)

Alternatively, if SSH is running on a different port, replace `ssh` with the port number, for example:

```
sudo ufw allow 2222
```

![](https://truehost.com/support/wp-content/uploads/2024/10/image-597.png)

Allow HTTP and HTTPS – If your server hosts a website, you’ll need to allow HTTP (port 80) and HTTPS (port 443) traffic.

```
sudo ufw allow http
sudo ufw allow https
```

![](https://truehost.com/support/wp-content/uploads/2024/10/image-598.png)

**Allow Other Services** – You can specify any additional services by their port number or name. For example, to allow MySQL (port 3306) or other specific ports:

```
sudo ufw allow 3306
```

##### **Step 4: Enable UFW**

Once you’ve defined your rules, enable UFW to activate the firewall. UFW will immediately apply the rules.

```
sudo ufw enable
```

You’ll be prompted to confirm your choice. Enabling UFW will enforce the rules you’ve configured to secure your server.

![](https://truehost.com/support/wp-content/uploads/2024/10/image-599-1024x102.png)

Step 5: Verify UFW Status and Rules

- After enabling UFW, check its status to ensure your rules are correctly applied:
- This command provides a list of active rules, showing which ports and services are allowed or denied.

```
sudo ufw status verbose
```

![](https://truehost.com/support/wp-content/uploads/2024/10/image-600.png)

##### **Step 6: Configure IP-Based Restrictions (Optional)**

- UFW can allow or deny traffic based on specific IP addresses. This is useful for restricting access to certain services or adding extra security.

**Allow Connections from a Specific IP**:

```
sudo ufw allow from 192.168.1.100
```

![](https://truehost.com/support/wp-content/uploads/2024/10/image-601.png)

**Deny Connections from a Specific IP:**

```
sudo ufw deny from 192.168.1.100
```

![](https://truehost.com/support/wp-content/uploads/2024/10/image-602.png)

**Allow an IP Address on a Specific Port**:

```
sudo ufw allow from 192.168.1.100 to any port 22
```

![](https://truehost.com/support/wp-content/uploads/2024/10/image-603.png)

These rules provide more granular control, which can help limit access to critical services.

**Step 7: Disable UFW (if necessary)**

To turn off UFW, use the following command:

```
sudo ufw disable
```

![](https://truehost.com/support/wp-content/uploads/2024/10/image-604.png)

This command disables the firewall, though you can re-enable it at any time using

```
sudo ufw enable
```

##### **Step 8: Remove or Modify Existing Rules**

To delete or modify an existing rule, first list rules by numbering them:

```
sudo ufw status numbered
```

![](https://truehost.com/support/wp-content/uploads/2024/10/image-605-1024x309.png)

To delete a specific rule, use its rule number:

```
sudo ufw delete <rule-number>
```

In the image below, we delete rule number 6 using ***sudo ufw delete 6***

![](https://truehost.com/support/wp-content/uploads/2024/10/image-606.png)

This is useful when you need to make adjustments to your firewall configuration.

Setting up a firewall is essential for securing a server, and UFW makes it easy to implement a basic firewall configuration on Linux. By following the steps outlined here, you can limit access to essential services, control connections from specific IPs, and maintain a secure server environment with minimal configuration.
