Tutorial Konfigurasi Firewall dengan UFW di Ubuntu Lengkap
Tutorial Konfigurasi Firewall dengan UFW di Ubuntu Lengkap
UFW (Uncomplicated Firewall) adalah frontend yang user-friendly untuk iptables di Ubuntu. UFW menyederhanakan manajemen firewall dengan sintaks yang mudah dipahami. Artikel ini membahas setup firewall lengkap dengan UFW untuk melindungi server Ubuntu Anda.
Pengenalan UFW
UFW adalah tool untuk mengelola netfilter firewall yang terintegrasi dengan baik di Ubuntu. Meski disebut “Uncomplicated”, UFW powerful dan mendukung konfigurasi kompleks melalui iptables backend.
Keunggulan UFW:
- Sintaks sederhana dan intuitif
- IPv4 dan IPv6 support secara otomatis
- Application profiles (app profiles)
- Logging dan rate limiting built-in
- Integrasi dengan Ubuntu ecosystem
Install UFW
UFW biasanya sudah terinstall by default di Ubuntu. Jika belum:
sudo apt update
sudo apt install ufw
Konfigurasi Dasar UFW
1. Cek Status UFW
# Cek status UFW
sudo ufw status
# Output jika inactive:
# Status: inactive
# Output jika active:
# Status: active
# To Action From
# -- ------ ----
# 22/tcp ALLOW Anywhere
# 80/tcp ALLOW Anywhere
# Cek status verbose (detail)
sudo ufw status verbose
# Cek status numbered (untuk delete rules)
sudo ufw status numbered
2. Default Policy
# Set default: deny all incoming, allow all outgoing
sudo ufw default deny incoming
sudo ufw default allow outgoing
# Atau untuk server yang lebih ketat:
sudo ufw default deny incoming
sudo ufw default deny outgoing
# Kemudian allow outgoing spesifik:
sudo ufw allow out 53 # DNS
sudo ufw allow out 80 # HTTP
sudo ufw allow out 443 # HTTPS
sudo ufw allow out 123 # NTP
3. Enable/Disable UFW
# Enable UFW (hati-hati! Pastikan SSH sudah di-allow jika remote)
sudo ufw enable
# Disable UFW
sudo ufw disable
# Reset UFW ke default (hapus semua rules)
sudo ufw reset
Warning: Jika Anda mengakses server via SSH, pastikan port SSH sudah di-allow sebelum enable UFW. Jika tidak, Anda akan terkunci dari server!
Mengelola Rules UFW
Allow Rules
# Allow by port number
sudo ufw allow 22
sudo ufw allow 80
sudo ufw allow 443
# Allow by service name
sudo ufw allow ssh
sudo ufw allow http
sudo ufw allow https
# Allow by port/protocol
sudo ufw allow 22/tcp
sudo ufw allow 53/udp
# Allow port range
sudo ufw allow 1000:2000/tcp
sudo ufw allow 1000:2000/udp
# Allow dari IP spesifik
sudo ufw allow from 192.168.1.100
# Allow dari IP ke port spesifik
sudo ufw allow from 192.168.1.100 to any port 22
# Allow dari subnet
sudo ufw allow from 192.168.1.0/24
sudo ufw allow from 192.168.1.0/24 to any port 3306
# Allow ke interface spesifik
sudo ufw allow in on eth0 to any port 80
Deny Rules
# Deny by port
sudo ufw deny 3306
sudo ufw deny 8080
# Deny by service
sudo ufw deny mysql
sudo ufw deny mongodb
# Deny dari IP spesifik
sudo ufw deny from 192.168.1.200
# Deny dari IP ke port spesifik
sudo ufw deny from 192.168.1.200 to any port 22
# Deny subnet
sudo ufw deny from 10.0.0.0/8
Delete Rules
# Method 1: By rule specification
sudo ufw delete allow 80
sudo ufw delete allow from 192.168.1.100
# Method 2: By rule number (dari ufw status numbered)
sudo ufw status numbered
sudo ufw delete 3
# Method 3: Delete all rules
sudo ufw reset
Application Profiles
UFW menyediakan application profiles yang memudahkan mengizinkan services umum.
List Available Profiles
# List semua available profiles
sudo ufw app list
# Output contoh:
# Available applications:
# Apache
# Apache Full
# Apache Secure
# CUPS
# Nginx Full
# Nginx HTTP
# Nginx HTTPS
# OpenSSH
# Postfix
Info Profile
# Lihat info profile spesifik
sudo ufw app info "Nginx Full"
# Output:
# Profile: Nginx Full
# Title: Web Server (Nginx, HTTP + HTTPS)
# Description: Small, but very powerful and efficient web server
# Ports:
# 80/tcp
# 443/tcp
Allow Application
# Allow application profile
sudo ufw allow "OpenSSH"
sudo ufw allow "Nginx Full"
sudo ufw allow "Apache Full"
# Allow multiple
sudo ufw allow "OpenSSH"
sudo ufw allow "Nginx Full"
Custom Application Profile
# Buat custom profile di /etc/ufw/applications.d/
sudo nano /etc/ufw/applications.d/myapp
[myapp]
title=My Custom Application
description=This is my custom application
ports=8080/tcp|9000/tcp|3000/tcp
# Reload UFW
sudo ufw app update myapp
sudo ufw allow myapp
Advanced UFW Configuration
Limit Connection Rate (DDoS Protection)
# Limit SSH connection (6 attempts per 30 seconds)
sudo ufw limit ssh
sudo ufw limit 22/tcp
# Limit custom port
sudo ufw limit 8080/tcp
# Limit dengan informasi:
sudo ufw limit proto tcp from any to any port 22
Logging
# Enable logging
sudo ufw logging on
# Set log level
sudo ufw logging low
sudo ufw logging medium
sudo ufw logging high
sudo ufw logging full
# Disable logging
sudo ufw logging off
# View logs
sudo tail -f /var/log/ufw.log
Custom Rules (Direct iptables)
Untuk rules kompleks yang tidak didukung UFW langsung:
# Edit before.rules
sudo nano /etc/ufw/before.rules
# Tambahkan custom iptables rules di bagian *filter
# Contoh: Allow loopback
-A ufw-before-input -i lo -j ACCEPT
-A ufw-before-output -o lo -j ACCEPT
# Reload UFW
sudo ufw reload
UFW dan IPv6
UFW otomatis meng-handle IPv6 jika enabled di konfigurasi.
# Cek IPv6 settings
sudo nano /etc/default/ufw
# Pastikan:
IPV6=yes
# Reload jika mengubah
sudo ufw reload
Common Scenarios
1. Web Server Setup (Nginx/Apache)
# Allow SSH
sudo ufw allow OpenSSH
# Allow web traffic
sudo ufw allow 'Nginx Full'
# atau:
sudo ufw allow 'Apache Full'
# atau manual:
sudo ufw allow 80/tcp
sudo ufw allow 443/tcp
# Enable UFW
sudo ufw enable
# Verifikasi
sudo ufw status
2. Database Server (MySQL/PostgreSQL)
# Allow SSH
sudo ufw allow ssh
# Allow database port dari subnet internal only
sudo ufw allow from 192.168.1.0/24 to any port 3306
# atau untuk PostgreSQL:
sudo ufw allow from 192.168.1.0/24 to any port 5432
# Deny dari internet (seharusnya sudah denied by default)
sudo ufw deny 3306
# Enable UFW
sudo ufw enable
3. Mail Server
# Allow SSH
sudo ufw allow ssh
# Allow mail services
sudo ufw allow 25/tcp # SMTP
sudo ufw allow 587/tcp # SMTP Submission
sudo ufw allow 465/tcp # SMTPS
sudo ufw allow 110/tcp # POP3
sudo ufw allow 995/tcp # POP3S
sudo ufw allow 143/tcp # IMAP
sudo ufw allow 993/tcp # IMAPS
# Enable UFW
sudo ufw enable
4. VPN Server (WireGuard/OpenVPN)
# Allow SSH
sudo ufw allow ssh
# Allow VPN
sudo ufw allow 51820/udp # WireGuard
sudo ufw allow 1194/udp # OpenVPN
# Enable forwarding (edit /etc/default/ufw)
sudo nano /etc/default/ufw
DEFAULT_FORWARD_POLICY="ACCEPT"
# Edit before.rules untuk NAT
sudo nano /etc/ufw/before.rules
# Tambahkan di bagian top:
*nat
:POSTROUTING ACCEPT [0:0]
-A POSTROUTING -s 10.8.0.0/8 -o eth0 -j MASQUERADE
COMMIT
# Reload
sudo ufw reload
sudo ufw enable
5. Development Server
# Allow SSH
sudo ufw allow ssh
# Allow common development ports
sudo ufw allow 3000/tcp # Node.js dev server
sudo ufw allow 8080/tcp # Alternative HTTP
sudo ufw allow 8000/tcp # Python dev server
sudo ufw allow 5000/tcp # Flask dev server
sudo ufw allow 5173/tcp # Vite dev server
sudo ufw allow 4200/tcp # Angular dev server
# Enable UFW
sudo ufw enable
Troubleshooting UFW
1. Cek Rules yang Aktif
# List semua rules dengan nomor
sudo ufw status numbered
# List verbose
sudo ufw status verbose
# Cek iptables rules yang di-generate
sudo iptables -L -v -n
2. UFW Blocking Legitimate Traffic
# Cek logs untuk blocked connections
sudo grep UFW /var/log/syslog
sudo tail -f /var/log/ufw.log
# Temporarily disable untuk troubleshooting
sudo ufw disable
# Test koneksi, kemudian enable kembali
sudo ufw enable
3. Reset UFW ke Default
# Reset semua rules
sudo ufw reset
# Konfirmasi dan setup ulang
sudo ufw default deny incoming
sudo ufw default allow outgoing
sudo ufw allow ssh
sudo ufw enable
4. UFW Tidak Start
# Cek status service
sudo systemctl status ufw
# Restart UFW
sudo systemctl restart ufw
# Enable UFW service
sudo systemctl enable ufw
UFW Best Practices
1. Prinsip Least Privilege
# Jangan allow port yang tidak dibutuhkan
# Contoh: Database tidak perlu expose ke internet
sudo ufw deny 3306
sudo ufw deny 5432
sudo ufw deny 27017
# Allow hanya dari trusted IPs
sudo ufw allow from 192.168.1.0/24 to any port 3306
2. Dokumentasikan Rules
# Buat script setup UFW
sudo nano /usr/local/bin/ufw-setup.sh
#!/bin/bash
# UFW Setup Script untuk Web Server
ufw reset
ufw default deny incoming
ufw default allow outgoing
# SSH
ufw allow ssh
# Web
ufw allow 80/tcp
ufw allow 443/tcp
# Database (internal only)
ufw allow from 192.168.1.0/24 to any port 3306
# Enable
ufw --force enable
echo "UFW configured successfully!"
3. Regular Review
# Monthly review
sudo ufw status verbose
# Check logs untuk suspicious activity
sudo grep "BLOCK" /var/log/ufw.log | tail -20
4. Backup dan Restore Rules
# Export rules
sudo ufw status numbered > /backup/ufw-rules-backup.txt
# Export user rules
sudo cp /etc/ufw/user.rules /backup/ufw-user-rules.backup
# Restore (manual copy back)
sudo cp /backup/ufw-user-rules.backup /etc/ufw/user.rules
sudo ufw reload
Kesimpulan
UFW menyediakan cara yang sangat mudah untuk mengelola firewall di Ubuntu tanpa harus mempelajari iptables yang kompleks. Dengan mengikuti panduan ini, Anda bisa:
- Setup firewall basic dalam hitungan menit
- Melindungi services dengan allow/deny rules
- Implementasi DDoS protection dengan rate limiting
- Mengelola complex scenarios dengan application profiles
- Troubleshoot dengan mudah menggunakan logging
UFW adalah tool yang sempurna untuk sysadmin level pemula sampai menengah. Untuk kebutuhan firewall enterprise yang lebih kompleks, pertimbangkan untuk belajar iptables atau nftables langsung.
Jangan lupa: selalu allow SSH sebelum enable UFW jika Anda mengakses server remotely, atau Anda akan terkunci dari sistem!
Artikel Terkait
Link Postingan : https://www.tirinfo.com/tutorial-konfigurasi-firewall-ufw-ubuntu/