โˆš
+
+
lua
+
+
gcp
+
+
flask
+
lisp
+
+
sqlite
ฯ€
koa
+
+
+
->
apex
+
oauth
pip
torch
+
+
+
micronaut
+
sails
+
+
+
&&
+
+
+
+
+
+
+
+
+
+
+
+
+
+
tf
+
koa
css
+
+
+
haskell
tcl
packer
jenkins
+
rest
centos
+
+
julia
k8s
+
postgres
+
argocd
+
cosmos
htmx
fortran
gatsby
+
+
+
||
objc
+
esbuild
htmx
+
+
elasticsearch
fiber
marko
Back to Blog
๐Ÿ”’ Setting Up Firewall Rules in Alpine Linux: Simple Guide
Alpine Linux Firewall Beginner

๐Ÿ”’ Setting Up Firewall Rules in Alpine Linux: Simple Guide

Published May 30, 2025

Easy tutorial to set up firewall protection in Alpine Linux safely. Perfect for beginners with step-by-step instructions and clear examples.

6 min read
0 views
Table of Contents

๐Ÿ”’ Setting Up Firewall Rules in Alpine Linux: Simple Guide

Setting up a firewall is like building a security guard for your computer! ๐Ÿ›ก๏ธ Letโ€™s learn how to protect your Alpine Linux system. Itโ€™s easier than you think! ๐Ÿ˜Š

๐Ÿค” What is a Firewall?

A firewall is like a security guard at your door! ๐Ÿšช

Think of it like:

  • ๐Ÿ  A fence around your house
  • ๐Ÿ‘ฎ A security guard checking visitors
  • ๐Ÿšง A checkpoint controlling traffic

On your computer:

  • ๐Ÿ” Firewall = Security barrier for your system
  • ๐Ÿšช Ports = Doors where programs connect
  • ๐Ÿ›ก๏ธ Rules = Instructions for allowing or blocking
  • ๐Ÿ“ก Traffic = Data coming and going

๐ŸŽฏ What You Need

Before we start, you need:

  • โœ… Alpine Linux computer
  • โœ… Admin access (root or sudo)
  • โœ… Terminal access
  • โœ… Basic typing skills

Letโ€™s become security experts! ๐ŸŽ“

๐Ÿ“‹ Step 1: Understanding iptables

What is iptables?

iptables is Alpineโ€™s security guard! ๐Ÿ‘ฎ

What weโ€™re doing: Learning about Alpineโ€™s firewall system.

# Check if iptables is installed
which iptables

# Check current firewall rules
sudo iptables -L

# Check firewall status
sudo iptables -L -n

What this does: ๐Ÿ“– Shows the current firewall configuration.

Command explained:

  • iptables = Firewall management tool ๐Ÿ”ง
  • -L = List all rules ๐Ÿ“‹
  • -n = Show numbers instead of names ๐Ÿ”ข

Example output:

Chain INPUT (policy ACCEPT)
target     prot opt source               destination         

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination         

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination   

What this means:

  • INPUT = Traffic coming to your computer ๐Ÿ“ฅ
  • OUTPUT = Traffic leaving your computer ๐Ÿ“ค
  • FORWARD = Traffic passing through ๐Ÿ”„
  • ACCEPT = Currently allowing everything โš ๏ธ

Cool! You can see your firewall status! ๐Ÿ‘๏ธ

Install Firewall Tools

Letโ€™s get the right tools! ๐Ÿ”ง

What weโ€™re doing: Installing firewall management tools.

# Install iptables (usually already installed)
sudo apk add iptables

# Install iptables save/restore tools
sudo apk add iptables-utils

# Install easy firewall manager
sudo apk add ufw

# Check installations
echo "Firewall tools installed:"
which iptables
which ufw

What this does: ๐Ÿ“– Installs tools to manage your firewall easily.

Tools explained:

  • iptables = Main firewall system ๐Ÿ”
  • iptables-utils = Helper tools for saving rules ๐Ÿ’พ
  • ufw = User-friendly firewall (easier to use) ๐Ÿ˜Š

Perfect! You have all the security tools! ๐Ÿ› ๏ธ

๐Ÿ› ๏ธ Step 2: Basic Firewall Setup

Enable UFW (Simple Method)

Letโ€™s start with the easy firewall! ๐Ÿš€

What weโ€™re doing: Setting up basic protection using UFW.

# Check UFW status
sudo ufw status

# Enable UFW firewall
sudo ufw enable

# Check status again
sudo ufw status verbose

# Allow SSH (important - don't lock yourself out!)
sudo ufw allow ssh

# Check the rules
sudo ufw status numbered

What this does: ๐Ÿ“– Enables basic firewall protection with UFW.

Commands explained:

  • ufw enable = Turn on the firewall ๐Ÿ”›
  • ufw allow ssh = Allow SSH connections ๐Ÿ”‘
  • ufw status = Check firewall status ๐Ÿ“Š

Example output:

Status: inactive

Firewall is active and enabled on system startup

Status: active
Logging: on (low)
Default: deny (incoming), allow (outgoing), disabled (routed)

To                         Action      From
--                         ------      ----
22/tcp                     ALLOW IN    Anywhere

What this means:

  • Firewall is now protecting you! โœ…
  • SSH is allowed (you wonโ€™t be locked out) ๐Ÿ”‘
  • All other incoming traffic is blocked ๐Ÿšซ

Amazing! You have basic protection running! ๐ŸŒŸ

Allow Common Services

Letโ€™s allow useful services! ๐ŸŒ

What weโ€™re doing: Opening ports for common services you might need.

# Allow web server (HTTP)
sudo ufw allow 80

# Allow secure web server (HTTPS) 
sudo ufw allow 443

# Allow FTP
sudo ufw allow 21

# Allow email (SMTP)
sudo ufw allow 25

# Check all rules
sudo ufw status numbered

Commands explained:

  • Port 80 = HTTP (websites) ๐ŸŒ
  • Port 443 = HTTPS (secure websites) ๐Ÿ”’
  • Port 21 = FTP (file transfer) ๐Ÿ“
  • Port 25 = SMTP (email) ๐Ÿ“ง

Example output:

Status: active

To                         Action      From
--                         ------      ----
[ 1] 22/tcp                 ALLOW IN    Anywhere
[ 2] 80/tcp                 ALLOW IN    Anywhere
[ 3] 443/tcp                ALLOW IN    Anywhere
[ 4] 21/tcp                 ALLOW IN    Anywhere
[ 5] 25/tcp                 ALLOW IN    Anywhere

Excellent! You have common services protected! ๐ŸŽฏ

๐Ÿ“Š Quick Firewall Commands

What to DoCommandExample
๐Ÿ”› Enable firewallufw enablesudo ufw enable
๐Ÿ“Š Check statusufw statussudo ufw status
โœ… Allow portufw allow portsudo ufw allow 80
๐Ÿšซ Block portufw deny portsudo ufw deny 23
๐Ÿ—‘๏ธ Delete ruleufw delete numbersudo ufw delete 3

๐Ÿ”’ Step 3: Advanced Rules

Allow Specific IPs

Letโ€™s allow trusted computers! ๐Ÿ‘ฅ

What weโ€™re doing: Creating rules for specific IP addresses.

# Allow SSH from specific IP only
sudo ufw allow from 192.168.1.100 to any port 22

# Allow web access from local network
sudo ufw allow from 192.168.1.0/24 to any port 80

# Allow specific IP for any service
sudo ufw allow from 203.0.113.45

# Check the new rules
sudo ufw status numbered

Commands explained:

  • from 192.168.1.100 = Only from this IP address ๐Ÿ“
  • 192.168.1.0/24 = Entire local network (192.168.1.1-254) ๐Ÿ 
  • to any port 22 = Specifically for SSH service ๐Ÿ”‘

Example output:

To                         Action      From
--                         ------      ----
[ 1] 22/tcp                 ALLOW IN    192.168.1.100
[ 2] 80/tcp                 ALLOW IN    192.168.1.0/24
[ 3] Anywhere               ALLOW IN    203.0.113.45

Perfect! You control exactly who can access your system! ๐ŸŽฏ

Block Dangerous Ports

Letโ€™s block risky services! ๐Ÿ›‘

What weโ€™re doing: Blocking ports that are commonly attacked.

# Block telnet (insecure)
sudo ufw deny 23

# Block old FTP data port
sudo ufw deny 20

# Block NetBIOS (Windows sharing)
sudo ufw deny 139
sudo ufw deny 445

# Block SNMP (network management)
sudo ufw deny 161

# Check blocked rules
sudo ufw status | grep DENY

Why block these:

  • Port 23 (telnet) = Sends passwords in clear text! ๐Ÿ˜ฑ
  • Ports 139/445 = Windows file sharing vulnerabilities ๐Ÿšซ
  • Port 161 (SNMP) = Often has weak passwords ๐Ÿ”“

Example output:

23/tcp                     DENY IN     Anywhere
20/tcp                     DENY IN     Anywhere
139/tcp                    DENY IN     Anywhere
445/tcp                    DENY IN     Anywhere
161/udp                    DENY IN     Anywhere

Great! You blocked dangerous services! ๐Ÿ›ก๏ธ

๐ŸŽฎ Letโ€™s Practice!

Time for a complete firewall setup! ๐Ÿš€

What weโ€™re doing: Setting up a secure firewall configuration from scratch.

# Step 1: Reset firewall (start fresh)
echo "Step 1: Resetting firewall... ๐Ÿ”„"
sudo ufw --force reset

# Step 2: Set default policies
echo "Step 2: Setting secure defaults... ๐Ÿ›ก๏ธ"
sudo ufw default deny incoming
sudo ufw default allow outgoing

# Step 3: Allow essential services
echo "Step 3: Allowing essential services... ๐Ÿ”‘"
sudo ufw allow ssh
sudo ufw allow 80
sudo ufw allow 443

# Step 4: Block dangerous ports
echo "Step 4: Blocking dangerous ports... ๐Ÿšซ"
sudo ufw deny 23
sudo ufw deny 135
sudo ufw deny 139
sudo ufw deny 445

# Step 5: Enable firewall
echo "Step 5: Enabling firewall... ๐Ÿ”›"
sudo ufw enable

# Step 6: Show final configuration
echo "Step 6: Final security configuration... ๐Ÿ“‹"
echo ""
sudo ufw status numbered

echo ""
echo "๐ŸŽ‰ Secure firewall setup completed!"
echo "โœ… Incoming traffic blocked by default"
echo "โœ… SSH access allowed"
echo "โœ… Web services allowed"
echo "โœ… Dangerous ports blocked"
echo "โœ… Firewall enabled and active"

What this does:

  • Creates secure firewall from scratch ๐Ÿ—๏ธ
  • Blocks all unwanted traffic ๐Ÿšซ
  • Allows only necessary services โœ…
  • Protects against common attacks ๐Ÿ›ก๏ธ

Example output:

Step 1: Resetting firewall... ๐Ÿ”„
Firewall stopped and disabled

Step 5: Enabling firewall... ๐Ÿ”›
Firewall is active and enabled on system startup

Step 6: Final security configuration... ๐Ÿ“‹

Status: active

To                         Action      From
--                         ------      ----
[ 1] 22/tcp                 ALLOW IN    Anywhere
[ 2] 80/tcp                 ALLOW IN    Anywhere
[ 3] 443/tcp                ALLOW IN    Anywhere
[ 4] 23/tcp                 DENY IN     Anywhere
[ 5] 135/tcp                DENY IN     Anywhere

๐ŸŽ‰ Secure firewall setup completed!

Incredible! You built a complete security system! ๐ŸŒŸ

๐Ÿ”ง Step 4: Managing Firewall Rules

View and Modify Rules

Letโ€™s learn to manage your rules! ๐Ÿ“

What weโ€™re doing: Viewing, editing, and organizing firewall rules.

# Show all rules with numbers
sudo ufw status numbered

# Delete a specific rule (by number)
sudo ufw delete 4

# Insert rule at specific position
sudo ufw insert 1 allow from 192.168.1.50

# Replace a rule
sudo ufw delete 2
sudo ufw allow 8080

# Check the changes
sudo ufw status numbered

Commands explained:

  • delete 4 = Remove rule number 4 ๐Ÿ—‘๏ธ
  • insert 1 = Add rule at position 1 (top priority) โฌ†๏ธ
  • Always check changes after modifying! โœ…

Perfect! You can manage your security rules! ๐ŸŽฏ

Save and Backup Rules

Letโ€™s protect your firewall setup! ๐Ÿ’พ

What weโ€™re doing: Saving firewall configuration so you donโ€™t lose it.

# Create backup directory
mkdir -p ~/firewall-backups

# Save current UFW rules
sudo cp /etc/ufw/user.rules ~/firewall-backups/ufw-backup-$(date +%Y%m%d).rules
sudo cp /etc/ufw/user6.rules ~/firewall-backups/ufw6-backup-$(date +%Y%m%d).rules

# Save iptables rules
sudo iptables-save > ~/firewall-backups/iptables-backup-$(date +%Y%m%d).rules

# Check backups
ls -la ~/firewall-backups/

echo "Firewall configuration backed up! ๐Ÿ’พ"

What this does: ๐Ÿ“– Creates backup copies of your firewall rules.

Backup benefits:

  • ๐Ÿ“ Restore rules if something goes wrong
  • ๐Ÿ”„ Apply same rules to other computers
  • ๐Ÿ’พ Keep history of rule changes
  • ๐Ÿ›ก๏ธ Quick recovery from mistakes

Excellent! Your firewall is safely backed up! ๐Ÿ“‹

๐Ÿ”„ Step 5: Monitor Firewall Activity

Check Firewall Logs

Letโ€™s see whatโ€™s happening! ๐Ÿ“Š

What weโ€™re doing: Monitoring firewall activity and blocked attempts.

# Enable UFW logging
sudo ufw logging on

# Check recent firewall logs
sudo tail -20 /var/log/ufw.log

# Check for blocked connections
sudo grep "BLOCK" /var/log/ufw.log | tail -10

# Monitor real-time activity
echo "Monitoring firewall (press Ctrl+C to stop):"
sudo tail -f /var/log/ufw.log

What this shows: ๐Ÿ“– Real activity on your firewall.

Log information:

  • ๐Ÿšซ Blocked connection attempts
  • โœ… Allowed connections
  • ๐Ÿ“ Source IP addresses
  • ๐ŸŽฏ Target ports and services

Great! You can monitor your security! ๐Ÿ‘๏ธ

Check Connection Status

Letโ€™s see active connections! ๐Ÿ”

What weโ€™re doing: Viewing current network connections.

# Show listening ports
sudo netstat -tulpn

# Show active connections
sudo netstat -tun

# Show connections to specific port
sudo netstat -tun | grep :22

# Count connections per IP
sudo netstat -tun | awk '{print $5}' | cut -d: -f1 | sort | uniq -c | sort -nr

Commands explained:

  • netstat -tulpn = Show all listening services ๐Ÿ‘‚
  • grep :22 = Filter for SSH connections ๐Ÿ”‘
  • uniq -c = Count connections per IP ๐Ÿ“Š

Amazing! You can see all network activity! ๐Ÿ“ก

๐Ÿšจ Fix Common Problems

Problem 1: Locked out of SSH โŒ

What happened: Firewall blocked your SSH access. How to fix it: Use console access to fix rules.

# If you have console access:
sudo ufw allow ssh
sudo ufw reload

# Or temporarily disable firewall:
sudo ufw disable

Problem 2: Service not working โŒ

What happened: Firewall is blocking a service you need. How to fix it: Check what port the service uses.

# Find what port your service uses
sudo netstat -tulpn | grep service-name

# Allow the port
sudo ufw allow [port-number]

Problem 3: Too many rules โŒ

What happened: Firewall rules are confusing. How to fix it: Reset and start over.

# Reset all rules
sudo ufw --force reset

# Start with basic setup again
sudo ufw default deny incoming
sudo ufw default allow outgoing
sudo ufw allow ssh
sudo ufw enable

Donโ€™t worry! Firewall problems are fixable! ๐Ÿ’ช

๐Ÿ’ก Simple Tips

  1. Always allow SSH first ๐Ÿ”‘ - Donโ€™t lock yourself out
  2. Start simple ๐ŸŒฑ - Add rules one at a time
  3. Test after changes ๐Ÿงช - Make sure services still work
  4. Backup configurations ๐Ÿ’พ - Save working setups

โœ… Check Everything Works

Letโ€™s test your firewall skills! ๐ŸŽฏ

# Create firewall test
echo "Testing firewall security skills... ๐Ÿงช"

# Test 1: Check firewall is active
echo "Test 1: Firewall status"
sudo ufw status | grep -q "Status: active" && echo "โœ… Firewall is active"

# Test 2: Check SSH is allowed
echo "Test 2: SSH access"
sudo ufw status | grep -q "22/tcp.*ALLOW" && echo "โœ… SSH is allowed"

# Test 3: Check web ports
echo "Test 3: Web services"
sudo ufw status | grep -q "80/tcp.*ALLOW" && echo "โœ… HTTP is allowed"
sudo ufw status | grep -q "443/tcp.*ALLOW" && echo "โœ… HTTPS is allowed"

# Test 4: Check dangerous ports blocked
echo "Test 4: Security blocking"
sudo ufw status | grep -q "23/tcp.*DENY" && echo "โœ… Telnet is blocked"

# Test 5: Check backup exists
echo "Test 5: Backup verification"
ls ~/firewall-backups/ > /dev/null 2>&1 && echo "โœ… Backups are saved"

echo ""
echo "๐ŸŽ‰ All firewall tests passed!"
echo "Your system is secure! ๐Ÿ›ก๏ธ"

Good output shows all security measures working:

Testing firewall security skills... ๐Ÿงช
Test 1: Firewall status
โœ… Firewall is active
Test 2: SSH access
โœ… SSH is allowed
Test 3: Web services
โœ… HTTP is allowed
โœ… HTTPS is allowed
Test 4: Security blocking
โœ… Telnet is blocked
Test 5: Backup verification
โœ… Backups are saved

๐ŸŽ‰ All firewall tests passed!
Your system is secure! ๐Ÿ›ก๏ธ

Perfect! You mastered firewall security! ๐ŸŒŸ

๐Ÿ† What You Learned

Great job! Now you can:

  • โœ… Set up UFW firewall protection
  • โœ… Allow necessary services safely
  • โœ… Block dangerous ports and services
  • โœ… Create rules for specific IP addresses
  • โœ… Monitor firewall activity and logs
  • โœ… Backup and restore configurations
  • โœ… Troubleshoot common firewall problems
  • โœ… Test firewall security effectiveness

๐ŸŽฏ Whatโ€™s Next?

Now you can try:

  • ๐Ÿ“š Learning advanced iptables rules
  • ๐Ÿ› ๏ธ Setting up intrusion detection
  • ๐Ÿค Configuring network monitoring
  • ๐ŸŒŸ Exploring enterprise security tools

Remember: A good firewall is your first line of defense! ๐Ÿ›ก๏ธ

Keep your Alpine Linux system protected and secure! Youโ€™re a security expert! ๐Ÿ’ซ

Benefits of proper firewall setup:

  • ๐Ÿ”’ Protection from network attacks
  • ๐Ÿ›ก๏ธ Control over system access
  • ๐Ÿ“Š Monitoring of network activity
  • ๐Ÿšซ Blocking of malicious traffic

Youโ€™re becoming a cybersecurity expert! Keep protecting! ๐ŸŒŸ