Home/Engineer/Networking / Servers/UFW Firewall on Ubuntu: Open…
Networking / ServersBeginnerubuntudebianufwfirewalliptables

UFW Firewall on Ubuntu: Opening Ports, Allowing Specific IPs, and Keeping SSH Access Safe

A practical guide to Ubuntu UFW: opening and closing ports, allowing specific IPs, logging, and checking firewall status.

Contents
  1. Basic Usage
  2. Allowing and Blocking Ports
  3. Managing Rules
  4. Logging
  5. Recommended Initial Setup (SSH Server)

Basic Usage

Bash
sudo apt install ufw
sudo ufw status verbose

sudo ufw default deny incoming
sudo ufw default allow outgoing

Allowing and Blocking Ports

Bash
sudo ufw allow 22/tcp
sudo ufw allow ssh
sudo ufw allow 80/tcp
sudo ufw allow 443/tcp
sudo ufw allow 'Nginx Full'

sudo ufw deny 8080/tcp
sudo ufw allow 3000:4000/tcp

# Allow only a specific IP
sudo ufw allow from 192.168.1.100 to any port 5432
sudo ufw allow from 10.0.0.0/24

Managing Rules

Bash
sudo ufw status numbered
sudo ufw delete 3
sudo ufw delete allow 80/tcp
sudo ufw reset

Logging

Bash
sudo ufw logging on
sudo ufw logging high
sudo tail -f /var/log/ufw.log
sudo grep "BLOCK" /var/log/ufw.log | tail -20

Bash
sudo ufw default deny incoming
sudo ufw default allow outgoing
sudo ufw allow 22/tcp
sudo ufw allow 80/tcp
sudo ufw allow 443/tcp
sudo ufw logging on
sudo ufw enable
sudo ufw status verbose
#ufw#firewall#iptables#Security#ubuntu
Editorial note

AI tools may assist drafting. Check the evidence in each guide for whether commands were run and which environment was used. Confirm the current docs for your version before applying instructions, and email us about errors. email us

Topic-related official docs (auto-linked · not per-claim sources)Linux man pages ↗OWASP documentation ↗

Questions & answers

Ask a question about this guide. We'll review and reply.