Geavanceerd
Zoeken inInstallatiegidsen
Zo zoek je
  • Plak een foutmelding tussen aanhalingstekens, bijv. "permission denied", om de oplossingstabel te vinden.
  • Kies een gebied en een deel van de gids (installeren, eerste instellingen, dagelijks gebruik, beveiliging, oplossingen) om direct naar de juiste stap te gaan.
  • "twee woorden" vindt precies die zin.
  • Gewone woorden vinden namen, teksten en voorbeelden.
  • ↑ ↓ bewegen · Enter openen · Esc sluiten · / zoeken
Probeer:

Gids 32UFW · Network

Install and set up UFW (Uncomplicated Firewall)

UFW is the simple firewall of Ubuntu: one short command per rule ("allow SSH", "allow 443", "allow this IP to port 5432"). It writes the real netfilter rules for you. This guide sets the safe defaults, opens SSH before turning the firewall on, adds rules by service, port and source IP, rate-limits SSH, turns on logging, and explains the one big trap: Docker ports go around UFW.

  • 12 Lessen
  • 7 Commando's

Dit deel is nog niet vertaald. Je ziet de Engelse tekst.

$ als gewone gebruiker# als root (of met sudo)

32.1

Know it before you install

UFW has default policies (incoming: deny, outgoing: allow) and a numbered list of rules. The first matching rule wins. App profiles (like OpenSSH, Nginx Full) are named port sets.

ItemValue
Packageufw (installed on Ubuntu, but inactive)
Serviceufw.service (loads the rules at boot)
Settings/etc/default/ufw (IPv6, forward policy), /etc/ufw/ufw.conf
Rule files/etc/ufw/user.rules, /etc/ufw/user6.rules (written by ufw), /etc/ufw/before.rules (your own low-level rules)
App profiles/etc/ufw/applications.d/
Log/var/log/ufw.log (and journalctl -k)

32.2

Install on Ubuntu and Debian

apt-get update
apt-get install -y ufw
ufw status
Status: inactive

32.3

Install on RHEL, Rocky and Alma

RHEL uses firewalld. If you really want UFW, it is in EPEL — and you must turn firewalld off (one firewall tool only):

dnf -y install epel-release
dnf -y install ufw
systemctl disable --now firewalld
systemctl enable ufw

32.4

First configuration: safe defaults, SSH first

Set the defaults and allow SSH before you enable UFW — otherwise your SSH session is cut:

ufw default deny incoming
ufw default allow outgoing
ufw allow OpenSSH
ufw enable
Command may disrupt existing ssh connections. Proceed with operation (y|n)? y
Firewall is active and enabled on system startup
ufw status verbose
Status: active
Logging: on (low)
Default: deny (incoming), allow (outgoing), disabled (routed)
To                         Action      From
--                         ------      ----
OpenSSH                    ALLOW IN    Anywhere

Check that IPv6 is filtered too (IPV6=yes is the default):

grep IPV6 /etc/default/ufw
IPV6=yes

Commando's in deze les

32.5

Add rules

By app profile, port, and source:

ufw app list
ufw allow 'Nginx Full'
ufw allow 443/tcp
ufw allow from 10.0.0.0/24 to any port 22 proto tcp
ufw allow from 10.0.1.0/24 to any port 5432 proto tcp
ufw deny from 203.0.113.50
ufw limit OpenSSH
ufw allow in on eth1 to any port 9100 proto tcp
RuleMeaning
allow 'Nginx Full'80 and 443/tcp (from the app profile).
allow from NET to any port P proto tcpOnly that network may reach that port — the safe way for databases and admin ports.
limit OpenSSHBlock an IP that opens 6+ connections in 30 seconds (brute force).
in on eth1Only on one interface (for example the private network).

Then make SSH admin-only: allow it from the admin network and delete the open rule:

ufw status numbered
[ 1] OpenSSH                    ALLOW IN    Anywhere
[ 2] 22/tcp                     ALLOW IN    10.0.0.0/24
ufw delete 1
ufw insert 1 deny from 198.51.100.0/24

Onthoud

The order matters — the first match wins. Use ufw insert N to put a deny before a broader allow.

32.6

Logging

ufw logging medium
tail -f /var/log/ufw.log
Nov 16 10:12:01 web1 kernel: [UFW BLOCK] IN=eth0 OUT= SRC=203.0.113.9 DST=192.0.2.10 PROTO=TCP SPT=51544 DPT=3306

low logs blocked packets; medium also logs some allowed ones. [UFW BLOCK] lines show who tried which port (DPT).

Commando's in deze les

32.7

Docker and UFW

Let op

Ports published by Docker (-p 8080:80) are open to the world even if UFW denies them: Docker writes its own rules before UFW's. Solutions:

  • Publish only on localhost: -p 127.0.0.1:8080:80, with Nginx in front.
  • Or add rules to the DOCKER-USER chain in /etc/ufw/after.rules (the ufw-docker method) so UFW rules also apply to containers.
  • Check from another machine with nmap -p 8080 SERVER.

Commando's in deze les

32.8

Daily use: the commands and the real uses

ufw status numbered
ufw status verbose
ufw show added
ufw reload
ufw --dry-run allow 8080/tcp
ufw route allow in on eth1 out on eth0
ufw disable
ufw reset

ufw reset deletes all rules and turns UFW off (it keeps a backup of the old files).

Real uses at work:

  • Every Ubuntu VM and VPS: deny in, allow SSH (admin IPs) and the web ports.
  • Database and cache servers: only the app subnet to 5432/3306/6379.
  • Quick blocks during an incident: ufw insert 1 deny from IP.

32.9

Make it safe

Security checklist:

  • Default deny incoming; allow only what the server offers.
  • SSH only from admin networks (or VPN); limit on SSH if it must be public.
  • IPv6 filtered (IPV6=yes).
  • Docker ports bound to 127.0.0.1, or DOCKER-USER rules; test from outside.
  • Keep the cloud firewall (security groups) too — two layers.
  • Logging on while you build rules; check /var/log/ufw.log for blocked attempts.

32.10

Update and remove

apt-get install --only-upgrade -y ufw
ufw disable
apt-get purge -y ufw

32.11

Fix common problems

ProblemCauseFix
SSH cut after ufw enableSSH was not allowed first.Use the console: ufw allow OpenSSH; next time allow before enable.
A port is open although UFW denies itDocker published it (its rules come first).-p 127.0.0.1:… or DOCKER-USER rules; test with nmap from outside.
A deny rule has no effectAn earlier allow rule matches first.ufw status numbered; ufw insert 1 deny ….
ERROR: Could not find a profile matching 'Nginx Full'The app's package is not installed or the name differs.ufw app list; use the port instead.
Rules work for IPv4 but not IPv6IPV6=no.Set IPV6=yes in /etc/default/ufw, then ufw reload.
UFW and firewalld fightTwo firewall tools are active.Keep one: systemctl disable --now firewalld (or ufw disable).
Forwarded traffic (VPN, containers) blockedDefault routed policy is disabled/deny.ufw route allow …, or DEFAULT_FORWARD_POLICY in /etc/default/ufw (carefully).

32.12

Next steps

1What must you allow before ufw enable on a remote server?

SSH (ufw allow OpenSSH), or you lose your connection.

2Docker published port 8080 and UFW has no rule for it. Is it closed?

No. Docker's rules come before UFW, so it is open. Bind it to 127.0.0.1.

Alle commando's in dit hoofdstuk