Iptables cheat sheet

iptables is a powerful tool for network administrators and security professionals to manage network traffic and protect their systems from unwanted access. It is used to create, modify, and delete rules for the Linux kernel firewall, and can be used to secure servers, block malicious traffic, and allow or deny access to specific ports or services. This cheat sheet provides a comprehensive guide to the most common iptables commands and options, making it an essential reference for anyone working with Linux networking and security.

I. iptables basics

To understand iptables, it’s important to start with the basics. Here are some key concepts you need to know:

  1. What is iptables? iptables is a command-line tool used to configure and manage the Linux kernel firewall. It allows you to create rules that determine how incoming and outgoing traffic is handled, and can be used to block or allow traffic based on IP address, port number, protocol, and other criteria.

  2. iptables command syntax The syntax for iptables commands can be complex, but it follows a basic structure. Each command begins with the keyword “iptables,” followed by options and arguments that specify the rule or action you want to perform. For example:

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

This command adds a rule to the INPUT chain that accepts incoming TCP traffic on port 22 (SSH).

  1. Basic iptables rules and chains iptables works by defining rules and placing them in chains, which determine the order in which the rules are evaluated. There are three built-in chains: INPUT, OUTPUT, and FORWARD. Each chain corresponds to a different type of network traffic:
  • INPUT: incoming traffic to the local system
  • OUTPUT: outgoing traffic from the local system
  • FORWARD: traffic that is being routed through the system

You can create your own custom chains as well, which can be used to group related rules together and make your firewall configuration more organized and manageable.

  1. Saving and restoring iptables rules Once you have defined your iptables rules, you can save them to a file and restore them later. This is useful if you need to transfer your firewall configuration to another system or if you want to restore your rules after a reboot. The most common way to save and restore iptables rules is by using the “iptables-save” and “iptables-restore” commands. For example:

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

This command saves the current iptables rules to a file named “rules.v4” in the /etc/iptables directory. To restore the rules, you can use the following command:

iptables-restore < /etc/iptables/rules.v4

This command reads the saved rules from the file and applies them to the current iptables configuration.

II. iptables commands and options

In this section, we’ll cover the various commands and options available in iptables. These commands and options allow you to customize the rules and chains you create and give you more control over how your firewall operates.

The iptables command provides a wide range of options that can be used to customize your firewall rules. Some of the most common options include:

  • Chain and rule options: These options allow you to specify the chain and rule to which you want to apply a particular command.
  • IP address and port options: These options let you specify the source and destination IP addresses and ports that a rule should apply to.
  • Target options: These options specify the action that should be taken when a rule is matched. For example, a rule may be used to block traffic, allow traffic, or redirect traffic to a different port or IP address.

By using these options, you can create complex firewall rules that are tailored to your specific needs. In this section, we’ll also cover some common iptables examples that illustrate how these options can be used in practice.

Overall, understanding the various iptables commands and options is crucial for creating a powerful and effective firewall. By mastering these tools, you can secure your network and protect your systems from a wide range of threats.

III. Common iptables use cases

Iptables can be used to secure a server by controlling incoming and outgoing network traffic. Here are some common use cases:

  1. Securing a server with iptables: Iptables can be used to restrict incoming and outgoing traffic to only those that are necessary for the server’s operation, protecting it from potential attacks.

  2. Blocking incoming traffic from specific IP addresses or networks: Iptables can be used to block traffic from certain IP addresses or network ranges, such as those associated with known malicious activity.

  3. Allowing incoming traffic to specific ports or services: Iptables can be used to allow traffic to specific ports or services, such as SSH or HTTP, while blocking traffic to other ports or services.

  4. Setting up a basic firewall with iptables: Iptables can be used to set up a basic firewall to control network traffic, blocking all incoming traffic except for specific ports or services that are necessary for the server’s operation.

  5. Limiting connections and preventing DDoS attacks with iptables: Iptables can be used to limit the number of connections from a single IP address, preventing DDoS attacks and other types of abuse.

IV. Advanced iptables topics

  1. Using iptables with NAT and port forwarding: Network Address Translation (NAT) is a technique used to modify network address information in IP packet headers while in transit across a traffic routing device. iptables can be used to configure NAT and port forwarding for network traffic.

  2. Configuring iptables for IPv6: ipv6 is the latest version of the internet protocol (IP) which is intended to eventually replace ipv4. iptables can also be configured to filter traffic on ipv6 addresses and ports.

  3. iptables logging and monitoring: iptables can be used to log and monitor network traffic for debugging and security purposes. It can log information such as source and destination IP addresses, ports, and protocols.

  4. Troubleshooting common iptables issues: Sometimes iptables rules may cause connectivity issues or block legitimate traffic. Troubleshooting common issues such as incorrect rule syntax or chain order can be crucial to maintaining a functional network.

More ressources

Here are some additional resources that readers can use to learn more about iptables:

I hope these resources are helpful for readers looking to learn more about iptables!


iptables is a complex but essential tool for managing network traffic and securing your systems. This cheat sheet provides a comprehensive guide to the most common iptables commands and options, making it an invaluable reference for anyone working with Linux networking and security. With this cheat sheet, you’ll be able to quickly and confidently configure and manage iptables rules to protect your systems from unwanted access and malicious traffic.

Tools I use for this site

  • I buy all my domain names on  Namecheap, as thetrendycoder.com
  • The hosting of this website is made on Bluehost.
  • The website is created with WordPress.org (and not WordPress.com).
  • I use the page builder Elementor because it makes it easy to create modern pages with drag and drop.
  • I have multiple websites, and on most of them, I use themes from wpKoi. I love their design, they are very original and work well with Elementor.
  • All the designs and images are created using canvas.
  • I use Grammarly and languagetool to correct all my spelling and grammar mistakes.
  • SEO is a big thing on a website, I use a WordPress plugin called YoastSEO to help me with the basic analysis. I also use a tool called Keysearch for choosing the right keywords.
  • To handle affiliate links, I use two platforms: impact and ShareASale.

You want to write on TheTrendyCoder ?

If you are interested in publishing guest articles on this website, sharing your experience or coding tutorials, apply through this form.

NO PERFECT English needed!
NO DEGREE needed!
NO AGE limits!

No matter at what stage we are in our tech journey, we all have learned things and experienced things. Sharing them can help others and even help us. So, if you are a student, a professional, or a self-taught coder, feel at home and share some of your knowledge with the community.

More cheatsheets

The Ultimate iptables Cheat Sheet

/*! elementor - v3.12.1 - 02-04-2023 */ .elementor-heading-title{padding:0;margin:0;line-height:1}.elementor-widget-heading .elementor-heading-title[class*=elementor-size-]>a{color:inherit;font-size:inherit;line-height:inherit}.elementor-widget-heading .elementor-heading-title.elementor-size-small{font-size:15px}.elementor-widget-heading .elementor-heading-title.elementor-size-medium{font-size:19px}.elementor-widget-heading …

Agile cheat sheet for beginners

/*! elementor - v3.12.1 - 02-04-2023 */ .elementor-heading-title{padding:0;margin:0;line-height:1}.elementor-widget-heading .elementor-heading-title[class*=elementor-size-]>a{color:inherit;font-size:inherit;line-height:inherit}.elementor-widget-heading .elementor-heading-title.elementor-size-small{font-size:15px}.elementor-widget-heading .elementor-heading-title.elementor-size-medium{font-size:19px}.elementor-widget-heading …

More resources

coding games

Women in tech