+
*
vite
>=
+
css
+
vb
travis
+
+
gatsby
haiku
vb
atom
+
neo4j
+
netlify
+
+
asm
cobol
+
+
+
jax
vim
+
+
jest
+
mysql
+
+
+
+
mocha
cdn
+
lit
django
+
supabase
webstorm
wsl
sinatra
+
+
+
+
xml
+
+
axum
+
+
git
+
+
*
+
+
+
+
+
+
+
kotlin
+
+
cdn
<-
helm
+
+
xml
+
webstorm
+
rollup
+
+
+
|>
+
mysql
next
+
apex
Back to Blog
๐Ÿ›ก๏ธ Configuring Network Firewall (iptables): Simple Guide
Alpine Linux Security Beginner

๐Ÿ›ก๏ธ Configuring Network Firewall (iptables): Simple Guide

Published Jun 1, 2025

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

15 min read
0 views
Table of Contents

๐Ÿ›ก๏ธ Configuring Network Firewall (iptables): Simple Guide

Want to protect your system with a firewall? Iโ€™ll show you how to configure iptables! ๐Ÿ’ป This tutorial makes firewall setup super easy. Even if security seems complex, you can do this! ๐Ÿ˜Š

๐Ÿค” What is iptables Firewall?

iptables is like a security guard for your computer. It decides which network traffic can enter or leave your system!

iptables provides:

  • ๐Ÿšซ Blocking unwanted network traffic
  • ๐Ÿ”’ Protecting against attacks
  • ๐ŸŽฏ Controlling access to services
  • ๐Ÿ“Š Logging security events

๐ŸŽฏ What You Need

Before we start, you need:

  • โœ… Alpine Linux system running
  • โœ… Root or sudo permissions
  • โœ… Basic understanding of networking
  • โœ… About 35 minutes to complete

๐Ÿ“‹ Step 1: Install iptables

Set Up Firewall Components

Letโ€™s install iptables and related tools. Think of this as getting your security equipment ready! ๐Ÿ”ง

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

# Update package database
apk update

# Install iptables firewall
apk add iptables

# Install iptables save/restore tools
apk add iptables-openrc

# Install connection tracking
apk add iptables-legacy

# Check installation
which iptables
iptables --version

What this does: ๐Ÿ“– Gives you a complete firewall system.

Example output:

โœ… iptables firewall installed
โœ… Management tools available
โœ… Version: iptables v1.8.x

What this means: Your system can now filter network traffic! โœ…

๐Ÿ’ก Firewall Basics

Tip: iptables works with chains: INPUT, OUTPUT, and FORWARD! ๐Ÿ’ก

Note: Always test firewall rules carefully to avoid locking yourself out! โš ๏ธ

๐Ÿ› ๏ธ Step 2: Configure Basic Rules

Create Initial Firewall Rules

Now letโ€™s set up basic security rules. Think of this as creating your security policy! ๐Ÿ“‹

What weโ€™re doing: Creating fundamental firewall rules for system protection.

# Check current rules (should be empty)
iptables -L

# Set default policies (DROP means block)
iptables -P INPUT DROP
iptables -P FORWARD DROP
iptables -P OUTPUT ACCEPT

# Allow loopback traffic (localhost communication)
iptables -A INPUT -i lo -j ACCEPT
iptables -A OUTPUT -o lo -j ACCEPT

# Allow established connections
iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT

# Allow SSH access (IMPORTANT: don't lock yourself out!)
iptables -A INPUT -p tcp --dport 22 -j ACCEPT

# Check new rules
iptables -L -n

Code explanation:

  • -P: Sets default policy for chain
  • -A: Adds rule to chain
  • -i lo: Loopback interface
  • --state ESTABLISHED,RELATED: Existing connections
  • --dport 22: SSH port

Expected Output:

โœ… Default policies set to secure mode
โœ… Loopback traffic allowed
โœ… SSH access maintained

What this means: Your firewall is now active and protecting your system! ๐ŸŽ‰

๐ŸŽฎ Letโ€™s Try It!

Time to add more firewall rules and test the setup! This is where security gets real! ๐ŸŽฏ

What weโ€™re doing: Adding common service rules and testing firewall functionality.

# Allow web traffic (HTTP and HTTPS)
iptables -A INPUT -p tcp --dport 80 -j ACCEPT
iptables -A INPUT -p tcp --dport 443 -j ACCEPT

# Allow DNS queries
iptables -A INPUT -p udp --dport 53 -j ACCEPT
iptables -A INPUT -p tcp --dport 53 -j ACCEPT

# Allow ping (ICMP)
iptables -A INPUT -p icmp --icmp-type echo-request -j ACCEPT

# Block specific IP address (example)
iptables -A INPUT -s 192.168.1.100 -j DROP

# View all rules with line numbers
iptables -L INPUT -n --line-numbers

# Test SSH connection still works
echo "SSH should still work on port 22"

You should see:

โœ… Web traffic rules added
โœ… DNS queries allowed
โœ… Ping responses enabled
โœ… Specific IP blocked

Amazing! Your firewall is now configured with common rules! ๐ŸŒŸ

๐Ÿ“Š iptables Commands Table

CommandPurposeExample
๐Ÿ” iptables -LList all rulesiptables -L -n
โž• iptables -AAdd rule to chainiptables -A INPUT -p tcp --dport 80 -j ACCEPT
โŒ iptables -DDelete ruleiptables -D INPUT 1
๐Ÿ”„ iptables -FFlush all rulesiptables -F INPUT

๐ŸŽฎ Practice Time!

Letโ€™s practice different firewall scenarios:

Example 1: Allow Specific Service ๐ŸŸข

What weโ€™re doing: Opening firewall for a new service like FTP.

# Allow FTP service (ports 20-21)
iptables -A INPUT -p tcp --dport 20:21 -j ACCEPT

# Allow FTP passive mode (high ports)
iptables -A INPUT -p tcp --dport 1024:65535 -j ACCEPT

# Allow from specific network only
iptables -A INPUT -s 192.168.1.0/24 -p tcp --dport 21 -j ACCEPT

# Check the new rules
iptables -L INPUT | grep -E "ftp|21"

# Test connection
nc -zv localhost 21 2>/dev/null && echo "โœ… FTP port open" || echo "โŒ FTP port closed"

What this does: Opens your system for FTP file transfers! ๐ŸŒŸ

Example 2: Create Rate Limiting ๐ŸŸก

What weโ€™re doing: Protecting against connection flooding attacks.

# Limit SSH connections (max 3 per minute)
iptables -A INPUT -p tcp --dport 22 -m limit --limit 3/min -j ACCEPT
iptables -A INPUT -p tcp --dport 22 -j DROP

# Limit ping requests
iptables -A INPUT -p icmp --icmp-type echo-request -m limit --limit 1/sec -j ACCEPT

# Block port scanning attempts
iptables -A INPUT -m recent --name portscan --rcheck --seconds 86400 -j DROP
iptables -A INPUT -m recent --name portscan --set -j LOG --log-prefix "Portscan:"

# Check rate limiting rules
iptables -L INPUT -n | grep limit

# Test rate limiting
for i in 1 2 3 4 5; do ping -c 1 localhost; done

What this does: Protects against automated attacks and abuse! ๐Ÿ“š

๐Ÿšจ Fix Common Problems

Problem 1: Locked out of SSH โŒ

What happened: Firewall rules blocked your SSH access. How to fix it: Reset from console or reboot!

# If you have console access:
iptables -F INPUT  # Clear all INPUT rules
iptables -A INPUT -p tcp --dport 22 -j ACCEPT  # Re-allow SSH

# Or reset all rules to default
iptables -P INPUT ACCEPT
iptables -P OUTPUT ACCEPT
iptables -P FORWARD ACCEPT
iptables -F

# If locked out, reboot server
# Rules will be lost unless saved
echo "Reboot will clear temporary rules"

Problem 2: Service not accessible โŒ

What happened: Firewall is blocking legitimate traffic. How to fix it: Check and adjust rules!

# Check what's blocked
iptables -L INPUT -n -v

# Find the blocking rule
iptables -L INPUT --line-numbers

# Temporarily allow all traffic to test
iptables -I INPUT 1 -j ACCEPT

# Test service accessibility
nc -zv localhost 80

# Remove test rule when done
iptables -D INPUT 1

# Add proper rule for service
iptables -A INPUT -p tcp --dport 80 -j ACCEPT

Donโ€™t worry! Firewall configuration takes practice but youโ€™ll get it! ๐Ÿ’ช

๐Ÿ’ก Advanced Firewall Tips

  1. Save rules permanently ๐Ÿ“… - Use iptables-save to persist rules
  2. Test in safe environment ๐ŸŒฑ - Always test firewall changes carefully
  3. Monitor logs regularly ๐Ÿค - Check /var/log/messages for firewall events
  4. Document your rules ๐Ÿ’ช - Keep notes about what each rule does

โœ… Verify Firewall Works

Letโ€™s make sure everything is working correctly:

# Save current rules
echo "=== Saving Firewall Rules ==="
iptables-save > /etc/iptables/rules-save

# Check rule counts
echo "=== Rule Statistics ==="
echo "INPUT rules: $(iptables -L INPUT | grep -c "^ACCEPT\|^DROP\|^REJECT")"
echo "OUTPUT rules: $(iptables -L OUTPUT | grep -c "^ACCEPT\|^DROP\|^REJECT")"

# Test common services
echo "=== Service Tests ==="
nc -zv localhost 22 && echo "โœ… SSH accessible"
nc -zv localhost 80 && echo "โœ… HTTP accessible" || echo "โ„น๏ธ HTTP not running"

# Check firewall status
echo "=== Firewall Status ==="
iptables -L -n | head -20

# Enable automatic loading
echo "=== Enable at Boot ==="
rc-update add iptables
rc-service iptables save

Good firewall signs:

โœ… Rules saved successfully
โœ… SSH still accessible
โœ… Unwanted ports blocked
โœ… Service starts at boot

๐Ÿ† What You Learned

Great job! Now you can:

  • โœ… Install iptables firewall in Alpine Linux
  • โœ… Configure basic security rules
  • โœ… Allow specific services through firewall
  • โœ… Implement rate limiting protection
  • โœ… Save and restore firewall rules
  • โœ… Troubleshoot access issues

๐ŸŽฏ Whatโ€™s Next?

Now you can try:

  • ๐Ÿ“š Setting up advanced firewall logging
  • ๐Ÿ› ๏ธ Creating custom firewall scripts
  • ๐Ÿค Implementing intrusion detection
  • ๐ŸŒŸ Building enterprise security policies!

Remember: Every security expert started with basic firewall rules. Youโ€™re building real protection skills! ๐ŸŽ‰

Keep practicing and youโ€™ll become a firewall expert! ๐Ÿ’ซ