Setting Up a Basic Firewall on Debian Using IPTABLES

Setting up a basic firewall using IPTABLES(Debian)

Evans

Last Update منذ عام واحد

iptables is a powerful command-line tool used to configure Linux kernel firewall rules. It offers packet filtering, network address translation (NAT), and traffic mangling capabilities. This guide walks you through setting up a basic but functional firewall on Debian.

Prerequisites
  • A Debian-based server (Debian 10/11/12)

  • Root or sudo access

  • iptables installed (usually included by default)

  • Optional: iptables-persistent for saving rules across reboots

Viewing Current IPTABLES Configuration

Check currently active rules:

To see rules for all chains (INPUT, OUTPUT, FORWARD), with packet and byte counts.

 Create a Basic IPTABLES Rules File


Let’s configure a simple firewall by creating a rules file:

Paste in the following configuration:

Apply Your Rules

Load your rules using:

Verify the rules:

You should now see only the allowed ports (80, 443, 22, etc.) open.

Make IPTABLES Rules Persistent (Survive Reboot)

Since iptables rules are not persistent by default, you must save and reload them at boot:

Option 1: Manual Script (Legacy Method)
  1. Save the rules:

  1. Create a pre-up script:

Add:

#!/bin/sh/sbin/iptables-restore < /etc/iptables.up.rules

3. Make it executable:

Option 2: Modern Method Using iptables-persistent

1. Install the package:

  1. Save current rules:

This stores IPv4 rules in /etc/iptables/rules.v4 and IPv6 rules in /etc/iptables/rules.v6.

3. Restart the firewall:

Best Practices
Limit SSH access by IP if possible using:
  • Consider UFW or nftables for simpler management on newer Debian systems.

  • Regularly audit your firewall using:

Notes
  • Debian 10+ supports nftables, the successor to iptables. For new systems, consider migrating.

  • If using IPv6, replicate rules using ip6tables.

Was this article helpful?

1 out of 1 liked this article

Still need help? Message Us