Configuring the Firewall from the Command Line

The course is part of this learning path

Linux Security and Hardening
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.


Now you have all the background and concepts you need to implement a Linux firewall. The command that makes all this happen is IPtables. The IPtables command is for IPV4 and IP6tables is for IPV6. We're going to talk about the IP tables command but if you're working with IPV6 addresses use the IP6tables command. To view or list the chains and rules in a table use IPtables space dash capital L. You can specify a table with a dash T option followed by the table name. If you don't specify a table, the filter table is assumed. If you want to force numeric output use dash N. This will prevent DNS lookups from occurring and all the output will contain IP addresses and no host names. Also port names, won't be translated to service names and the port numbers will be shown instead of those nice service names. Using the dash dash line dash numbers option causes the line numbers to be printed before each rule in a chain. You can use these rule numbers to specify exactly where additional rules should be inserted. You can also delete rules by rule number. Here's some sample output from an IPtables dash capital L command. Because no table was specified the filter table is being displayed. You'll see that the filter table has three chains. The input forward and output chains. There are no rules in these chains at the moment. This is what an IP tables listing could look like on a system that doesn't have the firewall configured. Here's some more sample output from an IPtables listing. Here you can see the same three chains but this time the input chain has some rules. The output shows the target of each rule along with the protocol, any additional options, the source and destination. The built-in chains have a default policy. If a packet reaches the end of a built-in chain then the default policy is applied to that packet. The policy is really a target, like accept or drop for example. To set the default policy use the IP table space dash capital P command followed by the name of the chain, and then the target. If you want to drop all packets that do not match any rules in the input chain of the filter table you would run IPtables space dash capital P space input, space drop. If we go back to our sample output you can see that the default policy is listed for each chain in parentheses. If a packet doesn't match any of the rules in the input chain, it will be dropped. The forward and output chains have a default target of accept. Since there aren't any rules in those chains any packets that traverse the forward or output chain so the filter table will be accepted. If you want to append a rule to the end of the chain use the dash capital A option followed by the name of the chain, and then the rule specification. Again, the filter table is assumed unless the dash T option followed by a table name is supplied. You can use the dash T option with all the other options listed here as well. If you want to insert a rule at the beginning of the chain use dash capital I followed by the rule specification. If you want to answer to rule at the beginning of the chain use dash capital I followed by the name of the chain and then the rule specifications. You can optionally supply a rule number where the rule will be inserted. To delete a chain use dash capital D followed by the name of the chain and the rule specification to delete. If you wanna delete by rule number, use dash capital D followed by the name of the chain, and then the rule number. To flush, which is firewall speak for delete, all the rules in a given table use IPtables space dash capital F. Again, you can specify the table to use with the dash T option. If you want to flush the rules for a specified chain then provide the name of that chain following dash capital F.

About the Author
Jason Cannon
Founder, Linux Training Academy
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 Nothing gives him more satisfaction than knowing he has helped thousands of IT professionals level up their careers through his many books and courses.