Home Operation and Maintenance Nginx What is nftables? How is it different from iptables?

What is nftables? How is it different from iptables?

Jun 09, 2023 pm 09:34 PM
iptables nftables

什么是 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
Copy after login

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

sudo systemctl enable nftables.service
Copy after login

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
Copy after login

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
Copy after login

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
Copy after login
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
Copy after login

允许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
Copy after login

允许传入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
Copy after login

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

nftables 日志

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

在nftables中,需要指定:

nft add rule ip filter INPUT ip saddr 192.168.2.1 counter accept
Copy after login

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

nft export xml
Copy after login

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 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 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)

How to enable or disable firewall on Alpine Linux? How to enable or disable firewall on Alpine Linux? Feb 21, 2024 pm 12:45 PM

On AlpineLinux, you can use the iptables tool to configure and manage firewall rules. Here are the basic steps to enable or disable the firewall on AlpineLinux: Check the firewall status: sudoiptables -L If the output shows rules (for example, there are some INPUT, OUTPUT, or FORWARD rules), the firewall is enabled. If the output is empty, the firewall is currently disabled. Enable firewall: sudoiptables-PINPUTACCEPTsudoiptables-POUTPUTACCEPTsudoiptables-PFORWARDAC

What is nftables? How is it different from iptables? What is nftables? How is it different from iptables? Jun 09, 2023 pm 09:34 PM

What is nftables? How is it different from iptables? Almost every Linux administrator has used iptables, which is a firewall for Linux systems. But you may not be familiar with nftables, which is a new firewall that provides us with some necessary upgrades and may replace iptables. Why use nftables? nftables was developed by Netfilter, the organization that currently maintains iptables. nftables was created to solve some performance and scaling issues with iptables. In addition to the new syntax and some upgrades, nftables has the same functionality as iptab

iptables installation and configuration guide under Debian iptables installation and configuration guide under Debian Feb 15, 2024 am 08:30 AM

In Linux systems, iptables is a tool used to configure and manage network packet filtering rules. It allows users to filter packets entering and leaving the network according to preset rules, thereby realizing network access control, packet forwarding, etc. Function, in Debian system, iptables is installed by default, but if it is not installed, you need to install it manually. This article will introduce how to install iptables under Debian and configure related rules. Install iptables1. Open the terminal and log in as root user. 2. Run the following command to install iptables: ```shellsudoapt-getupdatesudoapt-ge

Don't know how to use Linux firewall software IPtables! What kind of operation and maintenance person are you? Don't know how to use Linux firewall software IPtables! What kind of operation and maintenance person are you? Aug 01, 2023 pm 05:36 PM

Connection tracking is the basis of many web applications. For example, Kubernetes Service, ServiceMesh sidecar, software four-layer load balancer LVS/IPVS, Docker network, OVS, iptables host firewall, etc., all rely on the connection tracking function.

What is the difference between iptables and Firewalld firewall in Linux system? What is the difference between iptables and Firewalld firewall in Linux system? Feb 19, 2024 pm 05:18 PM

Both iptables and Firewalld in Linux systems are tools for configuring firewall rules. They have some differences in functions and usage methods: iptables: iptables is the most classic and traditional firewall tool in Linux systems. Early versions of Linux use iptables by default. As a firewall configuration tool. Iptables is based on the netfilter framework of the kernel space and filters and processes network data packets by directly operating the iptables rule table in the kernel. iptables uses the concepts of rule chains and tables to organize and manage firewall rules, such as the common filter

Detailed tutorial on Linux firewall configuration (iptables and firewalld). Detailed tutorial on Linux firewall configuration (iptables and firewalld). Feb 19, 2024 pm 12:36 PM

The following is a brief Linux firewall configuration tutorial, covering two commonly used firewall tools: iptables and firewalld. iptables is one of the most commonly used firewall tools on Linux, and firewalld is the default firewall management tool in CentOS7 and its derivatives. iptables firewall configuration: View current firewall rules: iptables -L -n Clear current firewall rules: iptables -F Allow inbound connections on specific ports: iptables-AINPUT-p--dport-jACCEPT For example, allow port 80 of the TCP protocol

In-depth analysis of how to use iptables under CentOS In-depth analysis of how to use iptables under CentOS Jan 11, 2024 pm 05:27 PM

1: Introduction Firewalls, to put it bluntly, are used to implement access control functions under Linux. They are divided into two types: hardware or software firewalls. No matter which network you are in, the place where the firewall works must be at the edge of the network. Our task is to define how the firewall works. This is the firewall's strategy and rules, so that it can detect IP and data entering and exiting the network. Currently, the more common firewalls on the market include layer 3 and layer 4 firewalls, which are called network layer firewalls, and layer 7 firewalls, which are actually gateways at the proxy layer. For the seven-layer model of TCP/IP, we know that the third layer is the network layer, and the three-layer firewall will detect the source address and destination address at this layer. But for a seven-layer firewall, no

How to replace Iptables with Ipvs in a Kubernetes cluster How to replace Iptables with Ipvs in a Kubernetes cluster Mar 02, 2024 am 11:58 AM

Everyone knows that in Kubernetes, kube-proxy is a network proxy. Its main responsibility is to provide load balancing and service discovery functions for services in the cluster. kube-proxy has different operating modes, among which iptables mode and ipvs mode are two common modes. In iptables mode, kube-proxy implements load balancing and service discovery through iptables rules, while ipvs mode uses the IPVS (IPVirtualServer) technology in the Linux kernel to achieve more efficient load balancing. Choosing the appropriate mode depends on your cluster's needs and performance requirements. iptables mode is suitable for small sets

See all articles