How to configure a firewall with UFW on Ubuntu
· Category: Linux
Short answer
UFW is a user-friendly frontend for iptables. Enable it with sudo ufw enable, allow services with sudo ufw allow ssh, and check status with sudo ufw status. For deeper networking rules, see how to configure networking with iptables and ufw. For remote server security, see how to configure SSH key-based login.
Steps
- Check UFW status:
sudo ufw status - Set default deny incoming:
sudo ufw default deny incoming - Allow SSH:
sudo ufw allow ssh - Allow HTTP and HTTPS:
sudo ufw allow 80/tcpandsudo ufw allow 443/tcp - Enable firewall:
sudo ufw enable
Tips
- Always allow SSH before enabling UFW to avoid lockout
- Use
sudo ufw allow from <ip>to restrict by IP address - Configure DNS properly by reviewing how to configure DNS on Linux