Published on October 3, 2024 by tms

How to Set Up a Basic Linux Firewall Using iptables or firewalld

Categories: Knowledge hub System Admin Tags:

In today’s digital age, securing your Linux server or workstation is paramount. One of the best ways to safeguard your system is by implementing a firewall. Firewalls act as a barrier between your system and potential threats from the internet or local network. In this blog post, we’ll guide you through setting up a basic Linux firewall using two popular tools: iptables and firewalld.

Understanding Firewalls

A firewall is a security system that monitors and controls incoming and outgoing network traffic based on predetermined security rules. Firewalls can be hardware-based, software-based, or a combination of both. On Linux systems, iptables and firewalld are two commonly used tools for managing firewall rules.

  • iptables: A command-line utility that allows you to configure the IP packet filter rules of the Linux kernel. It provides a robust and flexible way to filter network traffic but requires a solid understanding of its syntax and structure.
  • firewalld: A more user-friendly alternative to iptables, firewalld provides a dynamically managed firewall with support for zones, allowing you to manage traffic based on the source or destination of the traffic. It offers a simpler syntax and can be easier for beginners to use.

Setting Up a Basic Firewall with iptables

Let’s start by setting up a basic firewall using iptables.

Step 1: Installing iptables

Most Linux distributions come with iptables pre-installed. To check if it’s installed, run:

iptables --version

If it’s not installed, you can install it using your package manager. For example, on Debian-based systems (like Ubuntu), you can use:

sudo apt update
sudo apt install iptables

On Red Hat-based systems (like CentOS), use:

bashCopy codesudo yum install iptables

Step 2: Understanding iptables Rules

The basic structure of an iptables rule is as follows:

iptables -A <chain> -p <protocol> -s <source> -d <destination> --dport <port> -j <target>
  • chain: INPUT, OUTPUT, FORWARD
  • protocol: tcp, udp, icmp
  • source: IP address of the source
  • destination: IP address of the destination
  • port: Port number
  • target: ACCEPT, DROP, REJECT

Step 3: Setting Default Policies

Before adding specific rules, it’s a good idea to set default policies for the chains. The following commands set the default policy to DROP for incoming connections and ACCEPT for outgoing connections:

sudo iptables -P INPUT DROP
sudo iptables -P OUTPUT ACCEPT
sudo iptables -P FORWARD DROP

Step 4: Allowing Established Connections

To allow already established connections, run:

sudo iptables -A INPUT -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT

Step 5: Allowing SSH Access

If you want to allow SSH access to your server (which is essential for remote management), use:

sudo iptables -A INPUT -p tcp --dport 22 -j ACCEPT

Step 6: Allowing Specific Services

You can also allow other specific services such as HTTP and HTTPS. For example, to allow web traffic, run:

sudo iptables -A INPUT -p tcp --dport 80 -j ACCEPT
sudo iptables -A INPUT -p tcp --dport 443 -j ACCEPT

Step 7: Saving Your iptables Rules

Once you’ve configured your rules, you’ll want to save them so they persist across reboots. On Debian-based systems, you can use:

sudo iptables-save > /etc/iptables/rules.v4

For Red Hat-based systems, use:

sudo service iptables save

Setting Up a Basic Firewall with firewalld

Now, let’s see how to set up a basic firewall using firewalld.

Step 1: Installing firewalld

On most Linux distributions, firewalld is available in the default repositories. Install it using the following commands:

For Debian-based systems:

sudo apt update
sudo apt install firewalld

For Red Hat-based systems:

sudo yum install firewalld

Step 2: Starting firewalld

Once installed, start the firewalld service:

sudo systemctl start firewalld

You can also enable it to start at boot:

sudo systemctl enable firewalld

Step 3: Understanding firewalld Zones

firewalld uses zones to define the trust level of network connections. The default zone is usually set to public. You can check the active zones with:

sudo firewall-cmd --get-active-zones

Step 4: Setting Default Zone

You can set the default zone to drop (which will reject all incoming connections) with:

sudo firewall-cmd --set-default-zone=drop

Step 5: Allowing Services

To allow specific services such as SSH, HTTP, and HTTPS, use the following commands:

sudo firewall-cmd --zone=public --add-service=ssh --permanent
sudo firewall-cmd --zone=public --add-service=http --permanent
sudo firewall-cmd --zone=public --add-service=https --permanent

Remember to reload firewalld for the changes to take effect:

sudo firewall-cmd --reload

Step 6: Checking Your Firewall Rules

You can check the current rules with the following command:

sudo firewall-cmd --list-all

Conclusion

Setting up a basic Linux firewall is an essential step in securing your server or workstation. Both iptables and firewalld offer powerful options for configuring your firewall, but they cater to different levels of expertise. While iptables provides fine-grained control, firewalld simplifies management through its zone-based system.

By following the steps outlined in this blog post, you can establish a basic firewall configuration tailored to your needs, protecting your system from unauthorized access while allowing legitimate traffic. As you gain more experience, you can explore advanced configurations and additional security measures to further enhance your Linux security posture. Always remember to regularly review and update your firewall rules to adapt to new security threats and changes in your network environment.


Leave a Reply

Your email address will not be published. Required fields are marked *