Home Backend Development PHP Tutorial Linux firewall iptables tutorial for beginners

Linux firewall iptables tutorial for beginners

Nov 25, 2016 pm 03:11 PM
iptables linux

Iptables is an extremely flexible firewall tool specially built for Linux operating systems. For Linux geeks and system administrators, iptables is very useful. This article will show you how to configure the most versatile Linux firewall.

About iptables

Iptables is a command line based firewall tool that uses rule chains to allow/block network traffic. When a network connection is attempted to be established on your system, iptables looks for matching rules. If not found, iptables will take default action on it.
Almost all Linux distributions come with iptables pre-installed. The command to update/install iptables in Ubuntu/Debian is:

sudo apt-get install iptables
Copy after login

Some existing graphical interface software can also replace iptables, such as Firestarter. But iptables is not difficult to use. Be especially careful when configuring iptables rules, especially when you log in to the server remotely. Because an error at this time may cause you to permanently lose connection with the server, and you must go to the server to solve it.

Types of Iptables rule chains

Iptables rule chains are divided into three types: input, forwarding and output.

Input - This chain is used to filter connections whose destination address is the local machine. For example, if a user attempts to log into your PC/server using SSH, iptables will first match their IP address and port to the iptables input chain rules.

Forwarding - This chain is used to filter connections where both the destination address and the source address are not local. For example, most of the data received by the router needs to be forwarded to other hosts. If your system does not have router-like features enabled, such as NATing, you do not need to use this chain.
There is a safe and reliable way to detect whether your system requires forwarding chains:

iptables -L -v

Linux firewall iptables tutorial for beginners

The picture above is a screenshot of a server that has been running for a few weeks. This server does not place any restrictions on input and output. As can be seen, the input chain and output chain have processed 11GB and 17GB of data respectively, while the forwarding chain has not processed any data. This is because this server does not have a router-like forwarding function enabled.


Output - This chain is used to filter connections whose source address is the local machine. For example, when you try to ping howtogeek.com, iptables will check the output chain for rules related to ping and howtogeek.com, and then decide whether to allow or deny your connection request.

Note: When pinging an external host, it will appear as if only the output chain is working. But remember, the data returned by the external host needs to be filtered through the input chain. When configuring iptables rules, keep in mind that many protocols require bidirectional communication, so you need to configure both the input chain and the output chain. When people configure SSH, they often forget to configure it on both the input and output chains.

Default behavior of chains

Before configuring specific rules, maybe you want to configure the default behavior of these chains. In other words, what do you want iptables to do when it cannot match an existing rule?
You can run the following command to display the current iptables default action for unmatched connections:

iptables -L

Linux firewall iptables tutorial for beginners

As shown above, we can use grep to make the output more concise . In the screenshot above, all chains accept all connections by default.
Normally, you will want your system to receive all network data by default. This setting is also the default configuration of iptables. The configuration command to receive network connections is:

iptables --policy INPUT ACCEPT iptables --policy OUTPUT ACCEPT iptables --policy FORWARD ACCEPT
Copy after login

You can also add some commands to filter specific IP addresses or port numbers while using the default configuration. We introduce these commands later in this article.
If you want to deny all network connections by default and then add allowed IP addresses or port numbers on top of it, you can change ACCEPT in the default configuration to DROP, as shown in the image below. This is extremely useful for servers containing sensitive data. Usually these servers only allow specific IP addresses to access them.

iptables --policy INPUT DROP iptables --policy OUTPUT DROP iptables --policy FORWARD DROP
Copy after login

Configuration of specific connections

Let’s take a look at how to set a specific IP address or port. This article mainly introduces the three most basic and common settings.

Accept – accept all data.

Drop – Drop data. Application scenario: When you don’t want the source address of the data to be aware of the existence of your system (the best way to handle it).

Reject – Does not allow the connection to be established, but returns an error response. Application scenario: When you don't want a certain IP address to access your system, but you want them to know that your firewall blocks their access.

为了直观的区分上述三种情况,我们使用一台PC来ping一台配置了iptables的Linux电脑:

Linux firewall iptables tutorial for beginners

允许或阻止特定的连接

在配置完基本的规则链之后,你就可以配置iptables来允许或者阻止特定的IP地址或者端口。
注意:在这些例子中,我们使用iptables -A将额外的规则添加到现存的链中。Iptables在执行匹配的时候,会从列表的顶端开始搜索。你可以使用iptables -I [chain] [number]将新的规则插入到列表的指定位置。

来自同一IP地址的连接
下面这个例子展示了如何阻止来自IP地址为10.10.10.10的所有连接。

iptables -A INPUT -s 10.10.10.10 -j DROP
Copy after login

来自一组IP地址的连接
下面这个例子展示了如何阻止来自子网10.10.10.0/24内的任意IP地址的连接。你可以使用子网掩码或者标准的/符号来标示一个子网:

iptables -A INPUT -s 10.10.10.0/24 -j DROP
Copy after login

iptables -A INPUT -s 10.10.10.0/255.255.255.0 -j DROP
Copy after login

特定端口的连接
这个例子展示了如何阻止来自10.10.10.10的SSH连接。

iptables -A INPUT -p tcp --dport ssh -s 10.10.10.10 -j DROP
Copy after login

你可以将“ssh”替换成其它任何协议或者端口号。上述命令中的-p tcp告诉iptables连接使用的是何种协议。

下面这个例子展示了如何阻止来自任意IP地址的SSH连接。

iptables -A INPUT -p tcp --dport ssh -j DROP
Copy after login

连接状态

我们之前提到过,许多协议均需要双向通信。例如,如果你打算允许SSH连接,你必须同时配置输入和输出链。但是,如果你只想允许来自外部的SSH请求,那该怎么做?
下面这个例子展示了如何允许源IP地址为10.10.10.10同时阻止目的地址为10.10.10.10的SSH连接:

iptables -A INPUT -p tcp --dport ssh -s 10.10.10.10 -m state --state NEW,ESTABLISHED -j ACCEPT
iptables -A OUTPUT -p tcp --sport 22 -d 10.10.10.10 -m state --state ESTABLISHED -j ACCEPT
Copy after login

保存更改

上述方法对iptables规则作出的改变是临时的。如果你想永久保存这些更改,你需要运行额外的命令(不同Linux发行版下的保存命令也不相同):

Ubuntu:

sudo /sbin/iptables-save
Copy after login

Red Hat / CentOS:

/sbin/service iptables save
Copy after login

或者

/etc/init.d/iptables save
Copy after login

其它命令

列出iptables的当前配置:

iptables -L
Copy after login

使用-v选项将显示数据包和字节信息;使用-n选项将以数字形式列出信息,即不将IP地址解析为域名。
换句话讲,主机名,协议和网络都以数字的形式列出。

清除当前所有的配置规则:

iptables -F
Copy after login

原文 The Beginner’s Guide to iptables, the Linux Firewall


Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1669
14
PHP Tutorial
1273
29
C# Tutorial
1256
24
Linux Architecture: Unveiling the 5 Basic Components Linux Architecture: Unveiling the 5 Basic Components Apr 20, 2025 am 12:04 AM

The five basic components of the Linux system are: 1. Kernel, 2. System library, 3. System utilities, 4. Graphical user interface, 5. Applications. The kernel manages hardware resources, the system library provides precompiled functions, system utilities are used for system management, the GUI provides visual interaction, and applications use these components to implement functions.

vscode Previous Next Shortcut Key vscode Previous Next Shortcut Key Apr 15, 2025 pm 10:51 PM

VS Code One-step/Next step shortcut key usage: One-step (backward): Windows/Linux: Ctrl ←; macOS: Cmd ←Next step (forward): Windows/Linux: Ctrl →; macOS: Cmd →

How to check the warehouse address of git How to check the warehouse address of git Apr 17, 2025 pm 01:54 PM

To view the Git repository address, perform the following steps: 1. Open the command line and navigate to the repository directory; 2. Run the "git remote -v" command; 3. View the repository name in the output and its corresponding address.

How to run java code in notepad How to run java code in notepad Apr 16, 2025 pm 07:39 PM

Although Notepad cannot run Java code directly, it can be achieved by using other tools: using the command line compiler (javac) to generate a bytecode file (filename.class). Use the Java interpreter (java) to interpret bytecode, execute the code, and output the result.

How to run sublime after writing the code How to run sublime after writing the code Apr 16, 2025 am 08:51 AM

There are six ways to run code in Sublime: through hotkeys, menus, build systems, command lines, set default build systems, and custom build commands, and run individual files/projects by right-clicking on projects/files. The build system availability depends on the installation of Sublime Text.

What is the main purpose of Linux? What is the main purpose of Linux? Apr 16, 2025 am 12:19 AM

The main uses of Linux include: 1. Server operating system, 2. Embedded system, 3. Desktop operating system, 4. Development and testing environment. Linux excels in these areas, providing stability, security and efficient development tools.

laravel installation code laravel installation code Apr 18, 2025 pm 12:30 PM

To install Laravel, follow these steps in sequence: Install Composer (for macOS/Linux and Windows) Install Laravel Installer Create a new project Start Service Access Application (URL: http://127.0.0.1:8000) Set up the database connection (if required)

git software installation git software installation Apr 17, 2025 am 11:57 AM

Installing Git software includes the following steps: Download the installation package and run the installation package to verify the installation configuration Git installation Git Bash (Windows only)

See all articles