Example Firewall Rules
Start course
1h 7m

In this course, you'll learn some techniques and strategies to secure network services. You'll also learn how the local firewall works in Linux and how to configure it. We'll talk about information leakage and ways to prevent it. Next, you'll learn how to test for open ports and perform port scans. You'll also learn about xinetd, what it does, and how to secure it. We'll also cover how to secure SSH. This course includes some guided demonstrations so that you can see the concepts being used in real-world environments.

Learning Objectives

  • Learn how to secure SSH
  • Understand the fundamentals of Linux firewall security, including configuring a firewall from the command line and setting up firewall rules
  • Learn how to use TCP wrappers to secure your Linux system

Intended Audience

This course is intended for anyone who wants to understand how to secure their Linux systems.


To get the most out of this course, you should already have a good working knowledge of Linux. If you want to brush up on your Linux skills, consider taking our Learn Linux in 5 Days learning path first.


Let's look at a few example rules. Here's how you would block all packets originating from the IP address of The dash capital A input option will append the rule that follows it to the input chain. Since no table was specified, the filter table is assumed. The source address was specified with a dash S option, followed by the source IP address. The target or where to jump to is specified by dash J DROP. When you display this rule using IP tables dash N followed by capital L, you see that the target is DROP, the source is the two dot 16 IP address, and the destination is, which means anywhere. Here are two more rules. The first rule accepts TCP packets destined to port 22 from the network. This will allow SSH connections from that network. The forward slash on your screen is the line continuation character. If I had enough room on this screen, I would have left that out and put this command on one line. The second IP tables command a pens or rule that blocks all TCP connections that are inbound to port 22. This is one way you can allow SSH access from your company network and then block all other SSH connections. If a packet is matched, it jumps to the target. If a packet does not match, it gets evaluated by the next rule in the chain. The first IP tables example here on your screen inserts a rule into the input chain. The rule limits TCP packets that are destined for port 80. This is one way you can fight against a dos attack on your web server. The second rule is more specific in that it uses the state module to only match new connections. Net filter provides connection tracking, so it knows if a packet is a new packet or part of an established connection. To create a custom chain, use the dash capital N option, followed by the name of the chain you want to create. Like the other commands, use dash T followed by a table name to specify a table. If no table has given the filter table is assumed. If you want to delete your chain, use the dash capital X option followed by the chain name. The IP tables command allows you to manipulate FireWall rules in real time, but it doesn't save the state or make it persist between reboots. Each distribution does this slightly differently, but the concept is the same. The running rules are dumped to a configuration file and then that configuration file is used to load the rules when the IP tables service is started at boot time. For Debian based systems like Ubuntu, you can install the IP tables dash persistent package. That will allow you to run the net filter dash persistent save command, which stores the running IP tables configuration in the slash Etsy slash IP tables directory. For CentOS or RedHat systems, you can use the service IP table save command. This will save the rules to the slash Etsy slash cis config slash IP tables file and will be reloaded on boot. If you're using CentOS or RedHat version seven or later, this might not be installed by default. This brings us to our final topic on IP tables. You can use rappers or front ends to net filter and IP tables. For example, RedHat version seven ships with FireWall D. You can use the FireWall D command to create rules, but IP tables is being used in the background to do the actual work. Likewise, on Ubuntu systems, you can use UFW, which stands for Uncomplicated FireWall. There are other options and even graphical front ends like G UFW, which is the gooey for a UFW and system dash config dash firewall.

About the Author
Learning Paths

Jason is the founder of the Linux Training Academy as well as the author of "Linux for Beginners" and "Command Line Kung Fu." He has over 20 years of professional Linux experience, having worked for industry leaders such as Hewlett-Packard, Xerox, UPS, FireEye, and Amazon.com. Nothing gives him more satisfaction than knowing he has helped thousands of IT professionals level up their careers through his many books and courses.