Uncomplicated FireWall

Aus Foxwiki
(Weitergeleitet von Ufw)

Uncomplicated FireWall (ufw) ist eine Schnittstelle zur Konfiguration des Paketfilters iptables, die darauf ausgerichtet ist, die Konfiguration einer Firewall zu vereinfachen.

Beschreibung

Installation

# apt install ufw

Anwendung

Syntax

Optionen

Option Beschreibung
--help Auflistung möglicher Optionen
--dry-run detaillierte Ansicht einer Regel, ohne diese anzuwenden
enable aktiviert die Firewall und deren Ausführung beim Systemstart
disable deaktiviert die Firewall und deren Ausführung beim Systemstart
reload Neustart der Firewall
reset Deaktivierung der Firewall und Zurücksetzen auf Standardeinstellungen
status Ausgabe des Status und aktiver Regeln
show
logging [off, low, medium, high, full] Log-Level (Ausführlichkeit mit der Zugriffe protokolliert werden)

Parameter

Umgebungsvariablen

Exit-Status

Konfiguration

SSH erlauben

Für den Fall, daß die Firewall auf einem entfernten Rechner eingerichtet werden soll, ist zwingend darauf zu achten, daß vor deren Aktiviierung der entsprechnede Port für SSH-Verbindungen zugelassen wird:

# ufw allow ssh

oder

# ufw allow 22

Da mit der Datei /etc/services eine Liste existiert, in der für jeden Dienst eine Port-Nummer hinterlegt ist, kann bei der Erstellung der Regeln gewählt werden, ob der jeweilige Dienst (hier: ssh) oder die Port-Nummer (hier: 22) angegeben wird.

Sollte der Dienst SSH so konfiguriert worden sein, daß dieser einen abweichenden Port verwendet (hier: 2222), kann auch dieser bei Erstellung der Regel angegeben werden:

# ufw allow 2222

UFW aktivieren

Das Starten der Firewall erfolgt mit dem Kommando:

# ufw enable

Sollte die Einrichtung der Firewall über eine bereits aktive SSH-Sitzung erfolgen, wird eine Meldung ausgegeben, die davor warnt, daß die Verbindung gestört werden könnte:

# ufw enable
Command may disrupt existing ssh connections. 
  • Proceed with operation (y|n)? y
Firewall is active and enabled on system startup

Wurde die Regel für SSH-Verbindungen bereits erstellt, kann die Frage mit y(es) beantwortet werden.

Die Firewall sollte nun aktiv sein.

  • Der aktuelle Status kann wie folgt abgefragt werden:
# ufw status verbose
Status: active
Logging: on (low)
Default: deny (incoming), allow (outgoing), disabled (routed)
New profiles: skip

To                         Action      From
--                         ------      ----
22/tcp                     ALLOW IN    Anywhere
22/tcp (v6)                ALLOW IN    Anywhere (v6)

IPv6

Unterstützung von IPv6

Eintrag in der Konfigurationsdatei

# vi /etc/default/ufw

Hier muss sichergestellt werden, dass der Eintrag IPV6=yes existiert

# /etc/default/ufw
#

# Set to yes to apply rules to support IPv6 (no means only IPv6 on loopback
# accepted). 
  • You will need to 'disable' and then 'enable' the firewall for
# the changes to take affect.
IPV6=yes

Wurde dieser Eintrag hinzugefügt, werden die entsprechenden Regeln sowohl für IPv4, als auch für IPv6 erstellt

Standardrichtlinien

  • Standardrichtlinien sollten zuerst erstellt werden & definieren das Verhalten der Firewall, wenn keine der anderen Regeln auf die jeweilige Situation passt.
  • Die Standardeinstellung ist die, dass eingehende Verbindungen abgelehnt & ausgehende Verbindungen zugelassen werden - so wird sichergestellt, dass niemand den entsprechenden Rechner von außen erreichen kann, während Anwendungen mit der Außenwelt kommunizieren können.
  • Dieses Standardverhalten lässt sich mit den folgenden Befehlen wiederherstellen:
# ufw default deny incoming
# ufw default allow outgoing
  • Zum Schutz eines Arbeitsplatzrechners könnten diese Standardeinstellungen bereits ausreichen, während die Regeln von Rechnern, die ihre Dienste Clients zur Verfügung stellen (Web-Server, Mail-Server, etc.) entsprechend angepasst werden müssen.

ICMP

Um ICMP-Anfragen zu blockieren, ist eine Anpassung der Standardeinstellungen erforderlich, da hierfür keine Regeln mit ufw über die Kommandozeile definiert werden können.

  • Diese Dateien müssen so modifiziert werden, daß am Ende jeder Zeile des Abschnitts # ok icmp codes for INPUT das Schlüsselwort ACCEPT gegen DROP ausgetauscht wird.
/etc/ufw/before.rules
...
# ok icmp codes for INPUT
-A ufw-before-input -p icmp --icmp-type destination-unreachable -j ACCEPT
-A ufw-before-input -p icmp --icmp-type time-exceeded -j ACCEPT
-A ufw-before-input -p icmp --icmp-type parameter-problem -j ACCEPT
-A ufw-before-input -p icmp --icmp-type echo-request -j ACCEPT
...
/etc/ufw/before6.rules
...
# ok icmp codes for INPUT (rfc4890, 4.4.1 and 4.4.2)
-A ufw6-before-input -p icmpv6 --icmpv6-type destination-unreachable -j ACCEPT
-A ufw6-before-input -p icmpv6 --icmpv6-type packet-too-big -j ACCEPT
# codes 0 and 1
-A ufw6-before-input -p icmpv6 --icmpv6-type time-exceeded -j ACCEPT
# codes 0-2 (echo-reply needs to be before INVALID, see above)
-A ufw6-before-input -p icmpv6 --icmpv6-type parameter-problem -j ACCEPT
-A ufw6-before-input -p icmpv6 --icmpv6-type echo-request -j ACCEPT
-A ufw6-before-input -p icmpv6 --icmpv6-type router-solicitation -m hl --hl-eq 255 -j ACCEPT
-A ufw6-before-input -p icmpv6 --icmpv6-type router-advertisement -m hl --hl-eq 255 -j ACCEPT
-A ufw6-before-input -p icmpv6 --icmpv6-type neighbor-solicitation -m hl --hl-eq 255 -j ACCEPT
-A ufw6-before-input -p icmpv6 --icmpv6-type neighbor-advertisement -m hl --hl-eq 255 -j ACCEPT
# IND solicitation
-A ufw6-before-input -p icmpv6 --icmpv6-type 141 -m hl --hl-eq 255 -j ACCEPT
# IND advertisement
-A ufw6-before-input -p icmpv6 --icmpv6-type 142 -m hl --hl-eq 255 -j ACCEPT
# MLD query
-A ufw6-before-input -p icmpv6 --icmpv6-type 130 -s fe80::/10 -j ACCEPT
# MLD report
-A ufw6-before-input -p icmpv6 --icmpv6-type 131 -s fe80::/10 -j ACCEPT
# MLD done
-A ufw6-before-input -p icmpv6 --icmpv6-type 132 -s fe80::/10 -j ACCEPT
# MLD report v2
-A ufw6-before-input -p icmpv6 --icmpv6-type 143 -s fe80::/10 -j ACCEPT
# SEND certificate path solicitation
-A ufw6-before-input -p icmpv6 --icmpv6-type 148 -m hl --hl-eq 255 -j ACCEPT
# SEND certificate path advertisement
-A ufw6-before-input -p icmpv6 --icmpv6-type 149 -m hl --hl-eq 255 -j ACCEPT
# MR advertisement
-A ufw6-before-input -p icmpv6 --icmpv6-type 151 -s fe80::/10 -m hl --hl-eq 1 -j ACCEPT
# MR solicitation
-A ufw6-before-input -p icmpv6 --icmpv6-type 152 -s fe80::/10 -m hl --hl-eq 1 -j ACCEPT
# MR termination
-A ufw6-before-input -p icmpv6 --icmpv6-type 153 -s fe80::/10 -m hl --hl-eq 1 -j ACCEPT
...

Dateien

Datei Funktion
/usr/sbin/ufw das Programm selbst
/etc/default/ufw Haupteinstellungen
/etc/ufw/ufw.conf Einstellungen
/etc/ufw/before.rules Regeln vor den benutzerdefinierten Regeln
/etc/ufw/user.rules mit ufw erstellte, benutzerdefinierte Regeln
/etc/ufw/after.rules Regeln nach den benutzerdefinierten Regeln
/etc/ufw/applications.d Anwendungsprofile
/etc/services (Systemdatei) Auflistung von Ports & der zugehörigen Dienste

Regeln

Verbindungen erlauben

Jetzt können bzw. sollten alle anderen Regeln erstellt werden.

  • Welche Ports von außen erreichbar sein sollen, hängt davon ab, welche Dienste auf dem entsprechenden Server ausgeführt werden.
  • Ist dies beispielsweise ein Web-Server, so müssen Regeln definiert werden, die den Zugriff auf Port 80 (HTTP unverschlüsselt) bzw. Port 443 (HTTPS verschlüsselt) gestatten:
# ufw allow http
Rule added
Rule added (v6)
# ufw allow https
Rule added
Rule added (v6)

Analog der Regeldefinition für SSH, kann dies auch durch Angabe der zu öffnenden Port-Nummern geschehen:

# ufw allow 80
Rule added
Rule added (v6)
# ufw allow 443
Rule added
Rule added (v6)

Port-Bereiche

Neben der Angabe von Port-Nummern bzw.

  • Dienstnamen, besteht auch die Möglichkeit der Angabe eines Port-Bereiches.
  • Dies ist für Anwendungen sinnvoll, die mehrere Ports verwenden.
  • So können Verbindungen zum X11-Server über die Port-Nummern 6000 bis 6007 erfolgen:
# ufw allow 6000:6007/tcp
# ufw allow 6000:6007/udp

Hierbei ist jedoch darauf zu achten, daß bei der Verwendung von Port-Bereichen die Angabe getätigt wird, für welches Protokoll die Regeln gelten sollen - TCP oder UDP.

  • Werden nur Port-Nummern angegeben, erfolgt die Freigabe für jeweils beide Protokolle.

Spezifische IP-Adressen

Die Konfiguration ist nicht auf die Angabe von Port-Nummern beschränkt, sondern kann auch über die Angabe von spezifischen IP-Adressen erfolgen.

  • Sollen beispielsweise Verbindungen ausgehend von der IP-Adresse 203.0.113.4 zugelassen werden, so wird dies durch folgende Regel gewährleistet:
# ufw allow from 203.0.113.4
Rule added

Um den Zugriff noch restriktiver zu gestalten, kann zusätzlich zur IP-Adresse ein Port angegeben werden.

  • Dies wird mit to any port gefolgt von der gewünschten Port-Nummer bewerkstelligt:
# ufw allow from 203.0.113.4 to any port 22
Rule added

Subnetze

Soll der Zugriff allen IP-Adressen eines bestimmten Subnetzes gestattet werden, kann dies durch die Angabe einer Netzmaske nach der CIDR-Notation konfiguriert werden.

  • Beispielsweise gestattet der folgende Befehl den Zugriff von Geräten des IP-Adressbereiches von 203.0.113.1 bis 203.0.113.254:
# ufw allow from 203.0.113.0/24

Auch hier läßt sich der Zugriff auf einen spezifischen Port beschränken:

# ufw allow from 203.0.113.0/24 to any port 22

Spezifische Netzwerkschnittstellen

Verfügt ein Gerät über mehrere Netzwerkschnittstellen, so kann mit der Angabe von allow in on der Zugriff auf eine dieser Schnittstellen beschränkt werden.

  • Der anzugebende Name der entsprechenden Schnittstelle läßt sich mit dem Befehl ip a ermitteln:
$ ip a
    ...
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
   ...
3: wlan0: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN group default qlen 1000
   ...

Für einen Web-Server mit der öffentlichen Netzwerkschnittstelle eth0 kann der Verkehr über Port 80 (HTTP) durch folgende Regel zugelassen werden:

# ufw allow in on eth0 to any port 80

Verbindungen verbieten

Ebenso, wie das explizite Zulassen von Verbindungen, können auch Regeln erstellt werden, die bestimmte Zugriffe verbieten.

  • An die Stelle von Regeln, die das Schlüsselwort allow verwenden, treten nun solche, die das Schlüsselwort deny enthalten.
  • Mit solchen Regeln bietet sich beispielsweise die Möglichkeit, einem IP-Adressbereich den Zugriff zu gewähren, während einzelne Geräte aus diesem davon ausgenommen werden:
# ufw deny from 192.168.200.10 to any port 22
# ufw deny from 192.168.200.20 to any port 22
# ufw allow from 192.168.200.0/24 to any port 22 proto tcp

Hierbei ist jedoch unbedingt die Reihenfolge zu beachten, in der die Regeln erstellt werden, da die Regel Anwendung findet, die auf die jeweilige Situation zuerst passt.

Regeln löschen

Bereits erstellte Regeln lassen sich mit dem Schlüsselwort delete auch wieder entfernen.

  • Dies kann über Angabe der Regelnummer bzw.
  • der vollständigen Regel erreicht werden.

Löschen mit Regelnummer

Zur Ermittlung der Regelnummer wird ufw status mit dem Schalter numbered aufgerufen:

# ufw status numbered
Status: active

     To                         Action      From
     --                         ------      ----
[1] 22/tcp                     ALLOW IN    Anywhere
[2] 80/tcp                     ALLOW IN    Anywhere
[3] 443/tcp                    ALLOW IN    Anywhere
[4] 22/tcp (v6)                ALLOW IN    Anywhere (v6)
[5] 80/tcp (v6)                ALLOW IN    Anywhere (v6)
[6] 443/tcp (v6)               ALLOW IN    Anywhere (v6)

Das Löschen einer Regel erfolgt anschließend durch das Kommando ufw delete, gefolgt von der jeweiligen Nummer der Regel.

  • Um unbeabsichtigtes Löschen zu vermeiden, muß der Vorgang mit (y)es bestätigt werden:
# ufw delete 2
Deleting:
 allow 80/tcp
Proceed with operation (y|n)? y
Rule deleted

Eine erneute Statusabfrage bestätigt das Entfernen der Regel:

# ufw status numbered
Status: active

     To                         Action      From
     --                         ------      ----
[1] 22/tcp                     ALLOW IN    Anywhere
[2] 443/tcp                    ALLOW IN    Anywhere
[3] 22/tcp (v6)                ALLOW IN    Anywhere (v6)
[4] 80/tcp (v6)                ALLOW IN    Anywhere (v6)
[5] 443/tcp (v6)               ALLOW IN    Anywhere (v6)

Löschen mit Regel

Die Alternative zum Löschen über die Regelnummer ist, daß dem Kommando ufw deny die vollständige Regel hintangestellt wird:

# ufw status
Status: active

To                         Action      From
--                         ------      ----
22/tcp                     ALLOW       Anywhere
80                         ALLOW       Anywhere
80/tcp                     ALLOW       Anywhere
22/tcp (v6)                ALLOW       Anywhere (v6)
80/tcp (v6)                ALLOW       Anywhere (v6)
#  ufw delete allow http
Rule deleted
Rule deleted (v6)

Hinweis: Es werden jeweils beide Regeln (IPv4 & IPv6) gelöscht.

  • Hierbei erfolgt keine Bestätigungsabfrage.
# ufw status
Status: active

To                         Action      From
--                         ------      ----
22/tcp                     ALLOW       Anywhere
22/tcp (v6)                ALLOW       Anywhere (v6)

Regeldefinition

Option Beschreibung Beispiel
Aktion
allow Verbindung erlauben ufw allow ssh
deny Verbindung ablehnen ufw deny http
limit erlauben - jedoch maximal 6 Verbindungen innerhalb von 30 Sekunden ufw limit ssh
reject lehnt die Verbindung ab - informiert jedoch den Absender
Richtung
in eingehende Verbindung ufw allow in to any port 22
out ausgehende Verbindung ufw deny out to any port 22
Protokoll
proto [ tcp / udp ] Regel auf Protokoll beschränken (TCP oder UDP) ufw allow proto tcp to any port 22 (kürzer: ufw allow 22/tcp)
Port
port [N] Regel gilt für einen Port N ufw allow 6660
port [N1],[N2]..[N15] Regel gilt für eine Liste von Ports (maximal 15) ufw allow 6660,6670,6680
port [N1]:[N2] Regel gilt für einen Port-Bereich von N1 bis N2 ufw allow 6660:6670
Adresse
a.b.c.d Regel auf eine IP-Adresse beschränken ufw allow from 192.168.100.123
a.b.c.d/N Regel für Subnetz ufw allow from 192.168.100.1/24
0.0.0.0 / any Regel gilt für beliebige Adressen

Status & Reset

Statusüberprüfung

# ufw status
Status: inactive
# ufw status
Status: active

To                         Action      From
--                         ------      ----
22/tcp                     ALLOW       Anywhere
22/tcp (v6)                ALLOW       Anywhere (v6)
# ufw status numbered
Status: active

     To                         Action      From
     --                         ------      ----
[ 1] 22/tcp                     ALLOW IN    Anywhere
[ 2] 22/tcp (v6)                ALLOW IN    Anywhere (v6)
# ufw status verbose
Status: active
Logging: on (low)
Default: deny (incoming), allow (outgoing), disabled (routed)
New profiles: skip

To                         Action      From
--                         ------      ----
22/tcp                     ALLOW IN    Anywhere
22/tcp (v6)                ALLOW IN    Anywhere (v6)

Start & Stop von UFW

Mit der Option disable läßt sich die Firewall deaktivieren:

# ufw disable
Firewall stopped and disabled on system startup

Die Aktivierung erfolgt mit dem Schlüsselwort enable:

# ufw enable
Command may disrupt existing ssh connections. 
  • Proceed with operation (y|n)? y
Firewall is active and enabled on system startup

Ein Neustart der Firewall erfolgt mit der Option reload:

# ufw reload
Firewall reloaded

Zurücksetzen der Firewall

Sollen die Standardeinstellungen der Firewall wiederhergestellt werden, kann dies mit der Option reset erreicht werden und erfordert eine Bestätigung mit y:

# ufw reset
Resetting all rules to installed defaults. 
  • This may disrupt existing ssh
connections. 
  • Proceed with operation (y|n)? y
Backing up 'user.rules' to '/etc/ufw/user.rules.20210222_111416'
Backing up 'before.rules' to '/etc/ufw/before.rules.20210222_111416'
Backing up 'after.rules' to '/etc/ufw/after.rules.20210222_111416'
Backing up 'user6.rules' to '/etc/ufw/user6.rules.20210222_111416'
Backing up 'before6.rules' to '/etc/ufw/before6.rules.20210222_111416'
Backing up 'after6.rules' to '/etc/ufw/after6.rules.20210222_111416'

Dies bewirkt eine Deaktivierung der Firewall und das Löschen sämtlicher zuvor definierter Regeln.

  • Es erfolgt jedoch eine Sicherung der benutzerdefinierten Regeln in entsprechende Backup-Dateien.
  • Zu beachten ist hierbei, daß die Standardrichtlinien, sofern diese geändert wurden, nicht zurückgesetzt werden.

Grafische Benutzeroberfläche

Sollte sich die Konfiguration der unkomplizierten Firewall über die Kommandozeile dennoch als zu kompliziert erweisen, existiert mit gufw eine grafisches Frontend, welches ebenfalls über die Paketverwaltung installierbar ist:

# apt install gufw
  • Nach dem Start der Anwendung muß die Firewall, wenn nicht bereits via Terminal geschehen, zunächst aktiviert werden.
  • Dies geschieht mit dem Schalter neben Status:.
  • Ist die Firewall nun aktiv, verhält sie sich gemäß den Standardrichtlinien so, daß ausgehende Verbindungen erlaubt und eingehende blockiert werden.
  • Für einen gewöhnlichen Arbeitsplatzrechner könnte die Konfiguration hiermit bereits abgeschlossen sein.
Nach dem Start
  • Über das Drop-Down-Menü Profile: lassen sich unterschiedliche Einstellungen für verschiedene Umgebungen aktivieren (Büro, Zuhause, Öffentlich).
  • Eigene Profile können über Bearbeiten --> Einstellungen hinzugefügt (+) oder entfernt (-) werden.
  • Hier kann auch die Einstellung des Log-Levels (Niedrig ..
  • Vollständig) verändert, bzw.
  • die Protokollierung abgeschaltet werden.
Einstellungen
  • Mit einem Klick auf die Registerkarte Regeln wird eine Liste der benutzerdefinierten Regeln angezeigt, die anfänglich noch keine Einträge enthält.
  • Hier lassen sich eigene Regeln hinzufügen (+), entfernen (-) und bearbeiten ().
Definition von Regeln
Definition von Regeln

Sicherheit

Siehe auch

Links

Projekt-Homepage

Weblinks

  1. https://www.digitalocean.com/community/tutorials/how-to-set-up-a-firewall-with-ufw-on-ubuntu-18-04


Testfragen

Wie verhält sich die Firewall, wenn sie direkt nach der Installation gestartet wird?

Sämtliche eingehenden Zugriffe werden blockiert, während ausgehende Verbindungen zugelassen werden.

Was ist vor dem Starten der Firewall zu beachten, wenn sie aus der Ferne administriert werden soll?

Es ist unbedingt darauf zu achten, daß vor der Aktivierung eine Regel für den SSH-Zugriff definiert wird, um sich nicht selbst auszusperren.

Gesetzt den Fall, daß Port 22 für SSH geöffnet werden muß - wie läßt sich einem Brute-Force-Angriff auf diesem Port entgegenwirken?

Mit dem Kommando ufw limit ssh/tcp läßt sich die Anzahl der erlaubten Zugriffe auf 6 pro 30 Sekunden limitieren.

Definieren Sie eine Regel, die den Zugriff auf den Port 12345 nur von der IP-Adresse 60.70.80.90 zulässt!

# ufw allow from 60.70.80.90 to any port 12345

Sie betreiben einen Web-Server, der ständig aus dem Netzwerk 60.70.80.0 angegriffen wird.

  • Mit welcher Regel können Sie dies unterbinden?
# ufw deny from 60.70.80.0/24

Wie müsste die Antwort der vorherigen Frage modifiziert werden, wenn Sie möchten, daß Ihre beste Freundin mit der IP-Adresse 60.70.80.90 auf Ihre Homepage zugreifen kann? Was ist hierbei zu beachten?

# ufw allow from 60.70.80.90 to any port 80,443
# ufw deny from 60.70.80.0/24

Hierbei ist die Reihenfolge der beiden Regeln entscheidend, da die Firewall die erste zutreffende Regel anwendet.

TMP

UFW Essentials: Common Firewall Rules and Commands

Introduction

UFW (uncomplicated firewall) is a firewall configuration tool that runs on top of iptables, included by default within Ubuntu distributions. It provides a streamlined interface for configuring common firewall use cases via the command line.

This cheat sheet-style guide provides a quick reference to common UFW use cases and commands, including examples of how to allow and block services by port, network interface, and source IP address.

How To Use This Guide

  • This guide is in cheat sheet format with self-contained command-line snippets.
  • Jump to any section that is relevant to the task you are trying to complete.
  • When you see highlighted text in this guide’s commands, keep in mind that this text should refer to IP addresses from your own network.

Remember that you can check your current UFW ruleset with sudo ufw status or sudo ufw status verbose.

Verify UFW Status

To check if ufw is enabled, run:

sudo ufw status

Output
Status: inactive

The output will indicate if your firewall is active or not.

Enable UFW

If you got a Status: inactive message when running ufw status, it means the firewall is not yet enabled on the system. You’ll need to run a command to enable it.

By default, when enabled UFW will block external access to all ports on a server. In practice, that means if you are connected to a server via SSH and enable ufw before allowing access via the SSH port, you’ll be disconnected. Make sure you follow the section on how to enable SSH access of this guide before enabling the firewall if that’s your case.

To enable UFW on your system, run:

sudo ufw enable

You’ll see output like this:

Output
Firewall is active and enabled on system startup

To see what is currently blocked or allowed, you may use the verbose parameter when running ufw status, as follows:

sudo ufw status

Output
Status: active
Logging: on (low)
Default: deny (incoming), allow (outgoing), deny (routed)
New profiles: skip

Disable UFW

If for some reason you need to disable UFW, you can do so with the following command:

sudo ufw disable

Be aware that this command will fully disable the firewall service on your system.

Block an IP Address

To block all network connections that originate from a specific IP address, run the following command, replacing the highlighted IP address with the IP address that you want to block:

sudo ufw deny from 203.0.113.100

Output
Rule added

In this example, from 203.0.113.100 specifies a source IP address of “203.0.113.100”.

If you run sudo ufw status now, you’ll see the specified IP address listed as denied:

Output
Status: active

To Action From
-- ------ ----
Anywhere DENY 203.0.113.100 

All connections, coming in or going out, are blocked for the specified IP address.

Block a Subnet

If you need to block a full subnet, you may use the subnet address as from parameter on the ufw deny command. This would block all IP addresses in the example subnet 203.0.113.0/24:

sudo ufw deny from 203.0.113.0/24

Output
Rule added

Block Incoming Connections to a Network Interface

To block incoming connections from a specific IP address to a specific network interface, run the following command, replacing the highlighted IP address with the IP address you want to block:

sudo ufw deny in on eth0 from 203.0.113.100

Output
Rule added

The in parameter tells ufw to apply the rule only for incoming connections, and the on eth0 parameter specifies that the rule applies only for the eth0 interface. This might be useful if you have a system with several network interfaces (including virtual ones) and you need to block external access to some of these interfaces, but not all.

Allow an IP Address

To allow all network connections that originate from a specific IP address, run the following command, replacing the highlighted IP address with the IP address that you want to allow access:

sudo ufw allow from 203.0.113.101

Output
Rule added

If you run sudo ufw status now, you’ll see output similar to this, showing the word ALLOW next to the IP address you just added.

Output
Status: active

To Action From
-- ------ ----
... 
Anywhere ALLOW 203.0.113.101 

You can also allow connections from a whole subnet by providing the corresponding subnet mask for a host, such as 203.0.113.0/24.

Allow Incoming Connections to a Network Interface

To allow incoming connections from a specific IP address to a specific network interface, run the following command, replacing the highlighted IP address with the IP address you want to allow:

sudo ufw allow in on eth0 from 203.0.113.102

Output
Rule added

The in parameter tells ufw to apply the rule only for incoming connections, and the on eth0 parameter specifies that the rule applies only for the eth0 interface.

If you run sudo ufw status now, you’ll see output similar to this:

Output
Status: active

To Action From
-- ------ ----
... 
Anywhere on eth0 ALLOW 203.0.113.102 

Delete UFW Rule

To delete a rule that you previously set up within UFW, use ufw delete followed by the rule (allow or deny) and the target specification. The following example would delete a rule previously set to allow all connections from an IP address of 203.0.113.101:

sudo ufw delete allow from 203.0.113.101

Output
Rule deleted

Another way to specify which rule you want to delete is by providing the rule ID. This information can be obtained with the following command:

sudo ufw status numbered

Output
Status: active

To Action From
-- ------ ----
[ 1] Anywhere DENY IN 203.0.113.100 
[ 2] Anywhere on eth0 ALLOW IN 203.0.113.102 

From the output, you can see that there are two active rules. The first rule, with highlighted values, denies all connections coming from the IP address 203.0.113.100. The second rule allows connections on the eth0 interface coming in from the IP address 203.0.113.102.

Because by default UFW already blocks all external access unless explicitly allowed, the first rule is redundant, so you can remove it. To delete a rule by its ID, run:

sudo ufw delete 1

You will be prompted to confirm the operation and to make sure the ID you’re providing refers to the correct rule you want to delete.

Output
Deleting:
deny from 203.0.113.100
Proceed with operation (y|n)? y
Rule deleted

If you list your rules again with sudo ufw status, you’ll see that the rule was removed.

List Available Application Profiles

Upon installation, applications that rely on network communications will typically set up a UFW profile that you can use to allow connection from external addresses. This is often the same as running ufw allow from, with the advantage of providing a shortcut that abstracts the specific port numbers a service uses and provides a user-friendly nomenclature to referenced services.

To list which profiles are currently available, run the following:

sudo ufw app list

If you installed a service such as a web server or other network-dependent software and a profile was not made available within UFW, first make sure the service is enabled. For remote servers, you’ll typically have OpenSSH readily available:

Output
Available applications:
OpenSSH

Enable Application Profile

To enable a UFW application profile, run ufw allow followed by the name of the application profile you want to enable, which you can obtain with a sudo ufw app list command. In the following example, we’re enabling the OpenSSH profile, which will allow all incoming SSH connections on the default SSH port.

sudo ufw allow “OpenSSH”

Output
Rule added
Rule added (v6)

Remember to quote profile names that consist of multiple words, such as Nginx HTTPS.

Disable Application Profile

To disable an application profile that you had previously set up within UFW, you’ll need to remove its corresponding rule. For example, consider the following output from sudo ufw status:

sudo ufw status

Output
Status: active

To Action From
-- ------ ----
OpenSSH ALLOW Anywhere 
Nginx Full ALLOW Anywhere 
OpenSSH (v6) ALLOW Anywhere (v6) 
Nginx Full (v6) ALLOW Anywhere (v6) 

This output indicates that the Nginx Full application profile is currently enabled, allowing any and all connections to the web server both via HTTP as well as via HTTPS. If you’d want to only allow HTTPS requests from and to your web server, you’d have to first enable the most restrictive rule, which in this case would be Nginx HTTPS, and then disable the currently active Nginx Full rule:

sudo ufw allow "Nginx HTTPS"
sudo ufw delete allow "Nginx Full"

Remember you can list all available application profiles with sudo ufw app list.

Allow SSH

When working with remote servers, you’ll want to make sure that the SSH port is open to connections so that you are able to log in to your server remotely.

The following command will enable the OpenSSH UFW application profile and allow all connections to the default SSH port on the server:

sudo ufw allow OpenSSH

Output
Rule added
Rule added (v6)

Although less user-friendly, an alternative syntax is to specify the exact port number of the SSH service, which is typically set to 22 by default:

sudo ufw allow 22

Output
Rule added
Rule added (v6)

Allow Incoming SSH from Specific IP Address or Subnet

To allow incoming connections from a specific IP address or subnet, you’ll include a from directive to define the source of the connection. This will require that you also specify the destination address with a to parameter. To lock this rule to SSH only, you’ll limit the proto (protocol) to tcp and then use the port parameter and set it to 22, SSH’s default port.

The following command will allow only SSH connections coming from the IP address 203.0.113.103:

sudo ufw allow from 203.0.113.103 proto tcp to any port 22

Output
Rule added

You can also use a subnet address as from parameter to allow incoming SSH connections from an entire network:

sudo ufw allow from 203.0.113.0/24 proto tcp to any port 22

Output
Rule added

Allow Incoming Rsync from Specific IP Address or Subnet

The Rsync program, which runs on port 873, can be used to transfer files from one computer to another.

To allow incoming rsync connections from a specific IP address or subnet, use the from parameter to specify the source IP address and the port parameter to set the destination port 873. The following command will allow only Rsync connections coming from the IP address 203.0.113.103:

sudo ufw allow from 203.0.113.103 to any port 873

Output
Rule added

To allow the entire 203.0.113.0/24 subnet to be able to rsync to your server, run:

sudo ufw allow from 203.0.113.0/24 to any port 873

Output
Rule added

Allow Nginx HTTP / HTTPS

Upon installation, the Nginx web server sets up a few different UFW profiles within the server. Once you have Nginx installed and enabled as a service, run the following command to identify which profiles are available:

sudo ufw app list | grep Nginx

Output
Nginx Full
Nginx HTTP
Nginx HTTPS

To enable both HTTP and HTTPS traffic, choose Nginx Full. Otherwise, choose either Nginx HTTP to allow only HTTP or Nginx HTTPS to allow only HTTPS.

The following command will allow both HTTP and HTTPS traffic on the server (ports 80 and 443):

sudo ufw allow "Nginx Full"

Output
Rule added
Rule added (v6)

Allow Apache HTTP / HTTPS

Upon installation, the Apache web server sets up a few different UFW profiles within the server. Once you have Apache installed and enabled as a service, run the following command to identify which profiles are available:

sudo ufw app list | grep Apache

Output
Apache
Apache Full
Apache Secure

To enable both HTTP and HTTPS traffic, choose Apache Full. Otherwise, choose either Apache for HTTP or Apache Secure for HTTPS.

The following command will allow both HTTP and HTTPS traffic on the server (ports 80 and 443):

sudo ufw allow "Nginx Full"

Output
Rule added
Rule added (v6)

Allow All Incoming HTTP (port 80)

Web servers, such as Apache and Nginx, typically listen for HTTP requests on port 80. If your default policy for incoming traffic is set to drop or deny, you’ll need to create a UFW rule to allow external access on port 80. You can use either the port number or the service name (http) as a parameter to this command.

To allow all incoming HTTP (port 80) connections, run:

sudo ufw allow http

Output
Rule added
Rule added (v6)

An alternative syntax is to specify the port number of the HTTP service:

sudo ufw allow 80

Output
Rule added
Rule added (v6)

Allow All Incoming HTTPS (port 443)

HTTPS typically runs on port 443. If your default policy for incoming traffic is set to drop or deny, you’ll need to create a UFW rule to allow external access on port 443. You can use either the port number or the service name (https) as a parameter to this command.

To allow all incoming HTTPS (port 443) connections, run:

sudo ufw allow https

Output
Rule added
Rule added (v6)

An alternative syntax is to specify the port number of the HTTPS service:

sudo ufw allow 443

Output
Rule added
Rule added (v6)

Allow All Incoming HTTP and HTTPS

If you want to allow both HTTP and HTTPS traffic, you can create a single rule that allows both ports. This usage requires that you also define the protocol with the proto parameter, which in this case should be set to tcp.

To allow all incoming HTTP and HTTPS (ports 80 and 443) connections, run:

sudo ufw allow proto tcp from any to any port 80,443

Output
Rule added
Rule added (v6)

Allow MySQL Connection from Specific IP Address or Subnet

MySQL listens for client connections on port 3306. If your MySQL database server is being used by a client on a remote server, you’ll need to create a UFW rule to allow that access.

To allow incoming MySQL connections from a specific IP address or subnet, use the from parameter to specify the source IP address and the port parameter to set the destination port 3306.

The following command will allow the IP address 203.0.113.103 to connect to the server’s MySQL port:

sudo ufw allow from 203.0.113.103 to any port 3306

Output
Rule added

To allow the entire 203.0.113.0/24 subnet to be able to connect to your MySQL server, run:

sudo ufw allow from 203.0.113.0/24 to any port 3306

Output
Rule added

Allow PostgreSQL Connection from Specific IP Address or Subnet

PostgreSQL listens for client connections on port 5432. If your PostgreSQL database server is being used by a client on a remote server, you need to be sure to allow that traffic.

To allow incoming PostgreSQL connections from a specific IP address or subnet, specify the source with the from parameter, and set the port to 5432:

sudo ufw allow from 203.0.113.103 to any port 5432

Output
Rule added

To allow the entire 203.0.113.0/24 subnet to be able to connect to your PostgreSQL server, run:

sudo ufw allow from 203.0.113.0/24 to any port 5432

Output
Rule added

Block Outgoing SMTP Mail

Mail servers, such as Sendmail and Postfix, typically use port 25 for SMTP traffic. If your server shouldn’t be sending outgoing mail, you may want to block that kind of traffic. To block outgoing SMTP connections, run:

sudo ufw deny out 25

Output
Rule added
Rule added (v6)

This configures your firewall to drop all outgoing traffic on port 25. If you need to reject outgoing connections on a different port number, you can repeat this command and replace 25 with the port number you want to block.

Conclusion

UFW is a powerful tool that can greatly improve the security of your servers when properly configured. This reference guide covers some common UFW rules that are often used to configure a firewall on Ubuntu.

Most of the commands in this guide can be adapted to fit different use cases and scenarios, by changing parameters such as the source IP address and/or destination port. For more detailed information about each command parameter and available modifiers, you can use the man utility to check UFW’s manual:

man ufw

The official UFW page on Ubuntu’s documentation is another resource you can use as reference for more advanced use cases and examples.