For Ubuntu/Debian systems, the most common and user-friendly firewall tool is UFW (Uncomplicated Firewall).
Step 1: Install UFW (if not already installed)
sudo apt update sudo apt install ufw -y
Step 2: Enable UFW
Enable the firewall. This will start blocking all incoming connections except for those explicitly allowed.
sudo ufw enable
Step 3: Allow Necessary Connections
You can allow specific ports or services. Here are some common examples:
-
Allow SSH (Port 22):
sudo ufw allow ssh
-
Allow HTTP (Port 80):
sudo ufw allow http
-
Allow HTTPS (Port 443):
sudo ufw allow https
-
Allow a Specific IP Address (e.g., 192.168.1.100):
sudo ufw allow from 192.168.1.100
-
Allow a Port from a Specific IP (e.g., SSH from 192.168.1.100):
sudo ufw allow from 192.168.1.100 to any port 22
Step 4: Check UFW Status
To see which rules are active:
sudo ufw status verbose
Step 5: Deny Unwanted Connections
By default, UFW denies all incoming connections. If you want to explicitly deny a port or service:
-
Deny a Specific Port (e.g., 3306 for MySQL):
sudo ufw deny 3306/tcp
Step 6: Reload UFW (if needed)
UFW applies changes automatically, but if you need to reload manually:
sudo ufw reload
Step 7: Disable UFW (if you need to turn it off)
If you need to temporarily disable the firewall:
sudo ufw disable
Step 8: Reset UFW (to remove all rules)
If you want to reset UFW to its default state:
sudo ufw reset