100===Dev Ops/Iptables

Iptables Introduced

블로글러 2024. 6. 13. 09:35

In this explanation, we'll explore what firewalls and iptables are, and how to set up iptables for a production environment with an analogy of securing a house against unwanted visitors.

The Big Picture

Imagine your computer or server is like a house. A firewall is the security system that controls who can enter or leave the house, just like how you control who comes through your front door. Iptables is a tool in Linux that helps you manage these security rules, like a smart security guard that follows your specific instructions about who to let in and who to keep out.

Core Concepts

  1. Firewall: A system that controls network traffic to protect a network from unauthorized access.
  2. Iptables: A command-line utility in Linux that allows a system administrator to configure the IP packet filter rules of the Linux kernel firewall.
  3. Chains: Paths that network traffic follows through rules. The main chains are INPUT (for incoming traffic), OUTPUT (for outgoing traffic), and FORWARD (for traffic routed through the server).
  4. Rules: Specific conditions that define what traffic is allowed or denied.
  5. Tables: Collections of chains. The most commonly used table is the filter table, which is used for packet filtering.

Detailed Walkthrough

1. Installing iptables

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

sudo iptables --version

If it’s not installed, you can install it using your package manager:

  • For Debian/Ubuntu:
    sudo apt-get install iptables
  • For RedHat/CentOS:
    sudo yum install iptables

2. Basic iptables Configuration

Flushing Existing Rules

Before setting up new rules, it’s a good practice to clear any existing rules:

sudo iptables -F
sudo iptables -X
sudo iptables -t nat -F
sudo iptables -t nat -X
sudo iptables -t mangle -F
sudo iptables -t mangle -X
sudo iptables -t raw -F
sudo iptables -t raw -X
Setting Default Policies

Set the default policies for the main chains to DROP, so all traffic is blocked unless explicitly allowed:

sudo iptables -P INPUT DROP
sudo iptables -P FORWARD DROP
sudo iptables -P OUTPUT DROP
Allowing Essential Traffic

Allow traffic on the loopback interface:

sudo iptables -A INPUT -i lo -j ACCEPT
sudo iptables -A OUTPUT -o lo -j ACCEPT

Allow established and related connections:

sudo iptables -A INPUT -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT
sudo iptables -A OUTPUT -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT
Allowing SSH Access

To allow SSH traffic (replace 22 with your SSH port if it's different):

sudo iptables -A INPUT -p tcp --dport 22 -j ACCEPT
sudo iptables -A OUTPUT -p tcp --sport 22 -j ACCEPT
Allowing Web Traffic (HTTP and HTTPS)

To allow web traffic on ports 80 and 443:

sudo iptables -A INPUT -p tcp --dport 80 -j ACCEPT
sudo iptables -A INPUT -p tcp --dport 443 -j ACCEPT
sudo iptables -A OUTPUT -p tcp --sport 80 -j ACCEPT
sudo iptables -A OUTPUT -p tcp --sport 443 -j ACCEPT
Saving iptables Rules

To ensure the rules persist after a reboot, save them:

  • On Debian/Ubuntu:
    sudo sh -c "iptables-save > /etc/iptables/rules.v4"
  • On RedHat/CentOS:
    sudo service iptables save

Understanding Through an Example

Let’s consider a simple example. Imagine you are setting up a web server that should only allow web traffic (HTTP and HTTPS) and SSH for remote management.

  1. Flush existing rules to ensure a clean slate.
  2. Set default policies to block all incoming and outgoing traffic.
  3. Allow loopback traffic to ensure local system processes can communicate.
  4. Allow established and related connections to maintain ongoing connections.
  5. Allow SSH traffic to manage your server remotely.
  6. Allow HTTP and HTTPS traffic to serve web pages.

By following these steps, you ensure that only the necessary traffic reaches your server, enhancing its security.

Conclusion and Summary

We’ve learned that a firewall is like a security system for your server, controlling traffic based on rules. Iptables is the tool we use in Linux to set these rules. By flushing existing rules, setting default policies, and then allowing essential traffic (like SSH and web traffic), we can secure our server effectively.

Test Your Understanding

  1. What is the purpose of setting default policies to DROP in iptables?
  2. How would you allow DNS traffic (UDP port 53) in iptables?
  3. Why is it important to save iptables rules, and how can you do it on Ubuntu?

Reference

For further reading on iptables, you can refer to the official iptables documentation.

728x90