How do I configure firewalls in Linux (iptables, firewalld)

Configuring firewalls in Linux can enhance your system's security by controlling the traffic that is allowed to enter or exit your network. Two popular tools for managing firewalls in Linux are iptables and firewalld. Here's a brief overview of how to configure both.

Iptables Configuration

Iptables is a user-space utility program that allows a system administrator to configure the IP packet filter rules of the Linux kernel. Below is an example of how to set up basic iptables rules.

# Flush existing rules iptables -F # Allow loopback traffic iptables -A INPUT -i lo -j ACCEPT # Allow established sessions iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT # Allow SSH iptables -A INPUT -p tcp --dport 22 -j ACCEPT # Drop all other inbound traffic iptables -A INPUT -j DROP # Save the rules service iptables save

Firewalld Configuration

Firewalld is a dynamic firewall manager that supports both IPv4 and IPv6. It provides a more user-friendly interface and is often used as a replacement for iptables. Here’s how to set up basic rules in firewalld.

# Start and enable firewalld systemctl start firewalld systemctl enable firewalld # Allow SSH service firewall-cmd --permanent --add-service=ssh # Allow HTTP and HTTPS services firewall-cmd --permanent --add-service=http firewall-cmd --permanent --add-service=https # Reload firewalld to apply changes firewall-cmd --reload

These configurations can help in securing your Linux system. Be sure to customize the rules according to your specific needs.


Keywords: Linux firewall iptables firewalld network security configure firewalls