Dit deel is nog niet vertaald. Je ziet de Engelse tekst.
$ als gewone gebruiker# als root (of met sudo)
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.
| Item | Value |
|---|---|
| Package | ufw (installed on Ubuntu, but inactive) |
| Service | ufw.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) |
Install on Ubuntu and Debian
apt-get update
apt-get install -y ufw
ufw status
Status: inactiveInstall on RHEL, Rocky and Alma
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 AnywhereCheck that IPv6 is filtered too (IPV6=yes is the default):
grep IPV6 /etc/default/ufw
IPV6=yesAdd 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| Rule | Meaning |
|---|---|
allow 'Nginx Full' | 80 and 443/tcp (from the app profile). |
allow from NET to any port P proto tcp | Only that network may reach that port — the safe way for databases and admin ports. |
limit OpenSSH | Block an IP that opens 6+ connections in 30 seconds (brute force). |
in on eth1 | Only 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/24Onthoud
The order matters — the first match wins. Use ufw insert N to put a deny before a broader allow.
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=3306low logs blocked packets; medium also logs some allowed ones. [UFW BLOCK] lines show who tried which port (DPT).
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-USERchain 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.
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 resetufw 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.
Make it safe
Security checklist:
- Default deny incoming; allow only what the server offers.
- SSH only from admin networks (or VPN);
limiton 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.logfor blocked attempts.
Update and remove
apt-get install --only-upgrade -y ufw
ufw disable
apt-get purge -y ufwFix common problems
| Problem | Cause | Fix |
|---|---|---|
SSH cut after ufw enable | SSH was not allowed first. | Use the console: ufw allow OpenSSH; next time allow before enable. |
| A port is open although UFW denies it | Docker 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 effect | An 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 IPv6 | IPV6=no. | Set IPV6=yes in /etc/default/ufw, then ufw reload. |
| UFW and firewalld fight | Two firewall tools are active. | Keep one: systemctl disable --now firewalld (or ufw disable). |
| Forwarded traffic (VPN, containers) blocked | Default routed policy is disabled/deny. | ufw route allow …, or DEFAULT_FORWARD_POLICY in /etc/default/ufw (carefully). |
Next steps
- Quick how-tos: Open a port in the firewall, Allow a port only from one IP, Test if a remote port is open, Set up a firewall with UFW.
- The RHEL firewall: Install and set up firewalld. Test from outside: Command page: nmap.
- Block password guessing: Command page: fail2ban-client. Harden SSH too: OpenSSH server.
- Host security at work: Host and cluster security.
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.