Knowledgebase
HOW TO: Set Up a Firewall with UFW on Ubuntu 18.04
Posted by Asilah A. on 02 February 2021 11:08 AM


The Uncomplicated Firewall (UFW) is a frontend for iptables and is particularly well-suited for host-based firewalls. UFW provides a framework for managing net filter, as well as a command-line interface for manipulating the firewall. UFW aims to provide an easy to use interface for people unfamiliar with firewall concepts, while at the same time simplifies complicated iptables commands to help an administrator who knows what he or she is doing. UFW is an upstream for other distributions and graphical frontends.

Step 1 — Setting up default policies
To set the defaults used by UFW, use these commands:

# sudo ufw default deny incoming
# sudo ufw default allow outgoing

Note: These commands set the defaults to deny incoming and allow outgoing connections. These firewall defaults alone might suffice for a personal computer, but servers typically need to respond to incoming requests from outside users.

Step 2 — Allowing SSH connections
To configure your server to allow incoming SSH connections, you can use this command:

# sudo ufw allow ssh

Equivalent rule by specifying the port instead of the service name:
# sudo ufw allow <ssh port>

Step 3 — Enabling UFW
To enable UFW, use this command:

# sudo ufw enable

Step 4 — Allowing Other Connections
At this point, you should allow all of the other connections that your server needs to respond to. The connections that you should allow depends on your specific needs.
Luckily, you already know how to write rules that allow connections based on a service name or port; we already did this for SSH on port 22. You can also do this for:

1) HTTP on port 80, which is what unencrypted web servers use, using # sudo ufw allow http or # sudo ufw allow 80
2) HTTPS on port 443, which is what encrypted web servers use, using # sudo ufw allow https or # sudo ufw allow 443

There are several others ways to allow other connections, aside from specifying a port or known service.

---Specific Port Ranges---
For example, to allow X11 connections, which use ports 6000-6007, use these commands:

# sudo ufw allow 6000:6007/tcp
# sudo ufw allow 6000:6007/udp

---Specific IP Addresses---
When working with UFW, you can also specify IP addresses. For example, if you want to allow connections from a specific IP address, such as a work or home IP address of 111.111.111.111, you need to specify from, then the IP address:

# sudo ufw allow from 111.111.1111.111

Step 5 — Denying Connections
To write deny rules, you can use the commands described above, replacing allow with deny.

# sudo ufw deny http
OR
# sudo ufw deny from 111.111.111.111

Step 6 — Deleting Rules
If you’re using the rule number to delete firewall rules, the first thing you’ll want to do is get a list of your firewall rules.

# sudo ufw status numbered

If you decide that you want to delete rule 1, the one that allows port 80 (HTTP) connections, you can specify it in a UFW delete command like this:

# sudo ufw delete 1

Step 7 — Checking UFW Status and Rules
At any time, you can check the status of UFW with this command:

# sudo ufw status verbose

Step 8 — Disabling or Resetting UFW (optional)
# sudo ufw disable

 

If you face any difficulties on the setup, please feel free to contact our support team by submitting a ticket on https://247livesupport.biz or emailing out support team at [email protected]

(0 vote(s))
Helpful
Not helpful

Comments (0)
Copyright © 1998 - 2021 Shinjiru International Inc. All Rights Reserved.