search
HomeOperation and MaintenanceNginxWhat is nftables? How is it different from iptables?

什么是 nftables ? 它与 iptables 的区别是什么?

#What is nftables? What is the difference between it and iptables?

Almost every Linux administrator has used iptables, which is a firewall for Linux systems. But you may not be familiar with nftables, a new firewall that provides us with some necessary upgrades and may replace iptables.

Why use nftables?

Nftables was developed by the Netfilter organization, which currently maintains iptables. Nftables is designed to solve the performance and scalability problems of iptables.

nftables functions almost identically to iptables except for some upgrades and changed syntax. Another reason why nftables was introduced is because the iptables framework has become a bit complicated. iptables, ip6tables, arptables and ebtables all have different but similar functions.

For example, it is very inefficient to create IPv4 rules in iptables and IPv6 rules in ip6tables and keep the two in sync. Nftables aims to replace all of these and become a centralized solution.

Although nftables has been included in the Linux kernel since 2014, it has become increasingly popular recently as adoption expands. Change is slow in the Linux world, and it often takes a few years or more for outdated utilities to be phased out and replaced by upgraded ones.

Today we will briefly introduce the differences between nftables and iptables, and show examples of configuring firewall rules in the new nftables syntax.

Chains and rules in nftables

In iptables, there are three default chains: input , output and forwarding. These three "chains" (as well as other chains) contain "rules" and iptables works by matching network traffic to a list of rules in the chain. When the traffic being inspected does not match any rules, the chain's default policy (such as ACCEPT or DROP) will be applied to the traffic.

Nftables works similarly, with "chains" and "rules". However, it starts without any underlying chain, which makes the configuration more flexible.

One of the inefficiencies of iptables is that all network data must traverse one or more of the above chains, even if the traffic does not match any rules. Even if you don't configure the link, iptables will still inspect your network data and process it.

Installing nftables in Linux

nftables is available in all major Linux distributions, you can use the distribution version of the package manager to install.

In Ubuntu or Debian-based systems, you can use the following command:

sudo apt install nftables

Set nftables to start automatically when the system restarts, executable Do as follows:

sudo systemctl enable nftables.service

The syntax difference between iptables and nftables

The syntax of nftables compared to iptables It's simpler, but the syntax in iptables can also be used in nftables.

You can use the iptables-translate tool, which accepts iptables commands and translates them into equivalent nftables commands. This is an easy way to understand the difference between the two syntaxes.

Install iptables-translate on Ubuntu and Debian-based distributions using the following command:

sudo apt install iptables-nftables-compat

After installation, you can convert iptables-translate to Passed to the iptables-translate command, it returns the nftables equivalent command.

Let’s look at some specific syntax examples below.

Block incoming connections

The following command will block incoming connections from the IP address 192.168.2.1:

$ iptables-translate -A INPUT -s 192.168.2.1 -j DROPnft add rule ip filter INPUT ip saddr 192.168.2.1 counter drop

Allow incoming SSH connections##Release ssh connection permissions:

$ iptables-translate -A INPUT -p tcp --dport 22 -m conntrack --ctstate NEW,ESTABLISHED -j ACCEPTnft add rule ip filter INPUT tcp dport 22 ct state new,established counter accept
Allow incoming SSH connections from specific IP ranges

If you only want to allow incoming SSH connections from 192.168.1.0/24:

$ iptables-translate -A INPUT -p tcp -s 192.168.1.0/24 --dport 22 -m conntrack --ctstate NEW,ESTABLISHED -j ACCEPTnft add rule ip filter INPUT ip saddr 192.168.1.0/24 tcp dport 22 ct state new,established counter accept

允许MySQL连接到eth0网络接口

$ iptables-translate -A INPUT -i eth0 -p tcp --dport 3306 -m conntrack --ctstate NEW,ESTABLISHED -j ACCEPTnft add rule ip filter INPUT iifname eth0 tcp dport 3306ct state new,established counter accept

允许传入HTTP和HTTPS流量

为了允许特定类型的流量,以下是这两个命令的语法:

$ iptables-translate -A INPUT -p tcp -m multiport --dports 80,443 -m conntrack --ctstate NEW,ESTABLISHED -j ACCEPTnft add rule ip filter INPUT ip protocol tcp tcp dport { 80,443} ct state new,established counter accept

从这些例子中可以看出,nftables 语法与 iptables 非常相似,但命令更直观一些。

nftables 日志

上述nft命令示例中的“counter”选项告诉nftables统计规则被触碰的次数,就像默认情况下使用的iptables一样。

在nftables中,需要指定:

nft add rule ip filter INPUT ip saddr 192.168.2.1 counter accept

nftables内置了用于导出配置的选项。它目前支持XML和JSON。

nft export xml

The above is the detailed content of What is nftables? How is it different from iptables?. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:51CTO.COM. If there is any infringement, please contact admin@php.cn delete
NGINX Unit's Purpose: Running Web ApplicationsNGINX Unit's Purpose: Running Web ApplicationsApr 30, 2025 am 12:06 AM

The purpose of NGINXUnit is to simplify the deployment and management of web applications. Its advantages include: 1) Supports multiple programming languages, such as Python, PHP, Go, Java and Node.js; 2) Provides dynamic configuration and automatic reloading functions; 3) manages application lifecycle through a unified API; 4) Adopt an asynchronous I/O model to support high concurrency and load balancing.

NGINX: An Introduction to the High-Performance Web ServerNGINX: An Introduction to the High-Performance Web ServerApr 29, 2025 am 12:02 AM

NGINX started in 2002 and was developed by IgorSysoev to solve the C10k problem. 1.NGINX is a high-performance web server, an event-driven asynchronous architecture, suitable for high concurrency. 2. Provide advanced functions such as reverse proxy, load balancing and caching to improve system performance and reliability. 3. Optimization techniques include adjusting the number of worker processes, enabling Gzip compression, using HTTP/2 and security configuration.

NGINX vs. Apache: A Look at Their ArchitecturesNGINX vs. Apache: A Look at Their ArchitecturesApr 28, 2025 am 12:13 AM

The main architecture difference between NGINX and Apache is that NGINX adopts event-driven, asynchronous non-blocking model, while Apache uses process or thread model. 1) NGINX efficiently handles high-concurrent connections through event loops and I/O multiplexing mechanisms, suitable for static content and reverse proxy. 2) Apache adopts a multi-process or multi-threaded model, which is highly stable but has high resource consumption, and is suitable for scenarios where rich module expansion is required.

NGINX vs. Apache: Examining the Pros and ConsNGINX vs. Apache: Examining the Pros and ConsApr 27, 2025 am 12:05 AM

NGINX is suitable for handling high concurrent and static content, while Apache is suitable for complex configurations and dynamic content. 1. NGINX efficiently handles concurrent connections, suitable for high-traffic scenarios, but requires additional configuration when processing dynamic content. 2. Apache provides rich modules and flexible configurations, which are suitable for complex needs, but have poor high concurrency performance.

NGINX and Apache: Understanding the Key DifferencesNGINX and Apache: Understanding the Key DifferencesApr 26, 2025 am 12:01 AM

NGINX and Apache each have their own advantages and disadvantages, and the choice should be based on specific needs. 1.NGINX is suitable for high concurrency scenarios because of its asynchronous non-blocking architecture. 2. Apache is suitable for low-concurrency scenarios that require complex configurations, because of its modular design.

NGINX Unit: Key Features and CapabilitiesNGINX Unit: Key Features and CapabilitiesApr 25, 2025 am 12:17 AM

NGINXUnit is an open source application server that supports multiple programming languages ​​and provides functions such as dynamic configuration, zero downtime updates and built-in load balancing. 1. Dynamic configuration: You can modify the configuration without restarting. 2. Multilingual support: compatible with Python, Go, Java, PHP, etc. 3. Zero downtime update: Supports application updates that do not interrupt services. 4. Built-in load balancing: Requests can be distributed to multiple application instances.

NGINX Unit vs. Other Application ServersNGINX Unit vs. Other Application ServersApr 24, 2025 am 12:14 AM

NGINXUnit is better than ApacheTomcat, Gunicorn and Node.js built-in HTTP servers, suitable for multilingual projects and dynamic configuration requirements. 1) Supports multiple programming languages, 2) Provides dynamic configuration reloading, 3) Built-in load balancing function, suitable for projects that require high scalability and reliability.

NGINX Unit: The Architecture and How It WorksNGINX Unit: The Architecture and How It WorksApr 23, 2025 am 12:18 AM

NGINXUnit improves application performance and manageability with its modular architecture and dynamic reconfiguration capabilities. 1) Modular design includes master processes, routers and application processes, supporting efficient management and expansion. 2) Dynamic reconfiguration allows seamless update of configuration at runtime, suitable for CI/CD environments. 3) Multilingual support is implemented through dynamic loading of language runtime, improving development flexibility. 4) High performance is achieved through event-driven models and asynchronous I/O, and remains efficient even under high concurrency. 5) Security is improved by isolating application processes and reducing the mutual influence between applications.

See all articles

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 Tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function