Questo contenuto non è disponibile nella lingua selezionata.
6.2. Creating and managing nftables tables, chains, and rules
nftables rule set, and how to manage it.
6.2.1. Displaying the nftables rule set Copia collegamentoCollegamento copiato negli appunti!
nftables contains tables, chains, and rules. This section explains how to display this rule set.
Note
nftables does not pre-create tables. As a consequence, displaying the rule set on a host without any tables, the nft list ruleset command shows no output.
6.2.2. Creating an nftables table Copia collegamentoCollegamento copiato negli appunti!
nftables is a name space that contains a collection of chains, rules, sets, and other objects. This section explains how to create a table.
ip: Matches only IPv4 packets. This is the default if you do not specify an address family.ip6: Matches only IPv6 packets.inet: Matches both IPv4 and IPv6 packets.arp: Matches IPv4 address resolution protocol (ARP) packets.bridge: Matches packets that traverse a bridge device.netdev: Matches packets from ingress.
Procedure 6.4. Creating an nftables table
- Use the
nft add tablecommand to create a new table. For example, to create a table named example_table that processesIPv4andIPv6packets:nft add table inet example_table
# nft add table inet example_tableCopy to Clipboard Copied! Toggle word wrap Toggle overflow - Optionally, list all tables in the rule set:
nft list tables
# nft list tables table inet example_tableCopy to Clipboard Copied! Toggle word wrap Toggle overflow
Additional resources
- For further details about address families, see the
Address familiessection in thenft(8)man page. - For details on other actions you can run on tables, see the
Tablessection in thenft(8)man page.
6.2.3. Creating an nftables chain Copia collegamentoCollegamento copiato negli appunti!
- Base chain: You can use base chains as an entry point for packets from the networking stack.
- Regular chain: You can use regular chains as a
jumptarget and to better organize rules.
Prerequisites
- The table to which you want to add the new chain exists.
Procedure 6.5. Creating an nftables chain
- Use the
nft add chaincommand to create a new chain. For example, to create a chain named example_chain in example_table:nft add chain inet example_table example_chain '{ type filter hook input priority 0 ; policy accept ; }'# nft add chain inet example_table example_chain '{ type filter hook input priority 0 ; policy accept ; }'Copy to Clipboard Copied! Toggle word wrap Toggle overflow Important
To avoid that the shell interprets the semicolons as the end of the command, you must escape the semicolons with a backslash. Moreover, some shells interpret the curly braces as well, so quote the curly braces and anything inside them with ticks (').This chain filters incoming packets. Thepriorityparameter specifies the order in whichnftablesprocesses chains with the same hook value. A lower priority value has precedence over higher ones. Thepolicyparameter sets the default action for rules in this chain. Note that if you are logged in to the server remotely and you set the default policy todrop, you are disconnected immediately if no other rule allows the remote access. - Optionally, display all chains:
Copy to Clipboard Copied! Toggle word wrap Toggle overflow
Additional resources
- For further details about address families, see the
Address familiessection in thenft(8)man page. - For details on other actions you can run on chains, see the
Chainssection in thenft(8)man page.
6.2.4. Appending a rule to the end of an nftables chain Copia collegamentoCollegamento copiato negli appunti!
Prerequisites
- The chain to which you want to add the rule exists.
Procedure 6.6. Appending a rule to the end of an nftables chain
- To add a new rule, use the
nft add rulecommand. For example, to add a rule to the example_chain in the example_table that allows TCP traffic on port 22:nft add rule inet example_table example_chain tcp dport 22 accept
# nft add rule inet example_table example_chain tcp dport 22 acceptCopy to Clipboard Copied! Toggle word wrap Toggle overflow You can alternatively specify the name of the service instead of the port number. In the example, you could usesshinstead of the port number22. Note that a service name is resolved to a port number based on its entry in the/etc/servicesfile. - Optionally, display all chains and their rules in example_table:
Copy to Clipboard Copied! Toggle word wrap Toggle overflow
Additional resources
- For further details about address families, see the
Address familiessection in thenft(8)man page. - For details on other actions you can run on chains, see the
Rulessection in thenft(8)man page.
6.2.5. Inserting a rule at the beginning of an nftables chain Copia collegamentoCollegamento copiato negli appunti!
nftables chain.
Prerequisites
- The chain to which you want to add the rule exists.
Procedure 6.7. Inserting a rule at the beginning of an nftables chain
- To insert a new rule, use the
nft insert rulecommand. For example, to insert a rule to the example_chain in the example_table that allows TCP traffic on port22:nft insert rule inet example_table example_chain tcp dport 22 accept
# nft insert rule inet example_table example_chain tcp dport 22 acceptCopy to Clipboard Copied! Toggle word wrap Toggle overflow You can alternatively specify the name of the service instead of the port number. In the example, you could usesshinstead of the port number22. Note that a service name is resolved to a port number based on its entry in the/etc/servicesfile. - Optionally, display all chains and their rules in example_table:
Copy to Clipboard Copied! Toggle word wrap Toggle overflow
Additional resources
- For further details about address families, see the
Address familiessection in thenft(8)man page. - For details on other actions you can run on chains, see the
Rulessection in thenft(8)man page.
6.2.6. Inserting a rule at a specific position of an nftables chain Copia collegamentoCollegamento copiato negli appunti!
nftables chain. This way you can place new rules at the right position.
Prerequisites
- The chain to which you want to add the rule exists.
Procedure 6.8. Inserting a rule at a specific position of an nftables chain
- Use the
nft -a list rulesetcommand to display all chains and their rules in the example_table including their handle:Copy to Clipboard Copied! Toggle word wrap Toggle overflow Using the-adisplays the handles. You require this information to position the new rules in the next steps. - Insert the new rules to the example_chain chain in the example_table:
- To insert a rule that allows TCP traffic on port 636 before handle 3, enter:
nft insert rule inet example_table example_chain position 3 tcp dport 636 accept
# nft insert rule inet example_table example_chain position 3 tcp dport 636 acceptCopy to Clipboard Copied! Toggle word wrap Toggle overflow - To add a rule that allows TCP traffic on port 80 after handle 3, enter:
nft add rule inet example_table example_chain position 3 tcp dport 80 accept
# nft add rule inet example_table example_chain position 3 tcp dport 80 acceptCopy to Clipboard Copied! Toggle word wrap Toggle overflow
- Optionally, display all chains and their rules in example_table:
Copy to Clipboard Copied! Toggle word wrap Toggle overflow
Additional resources
- For further details about address families, see the
Address familiessection in thenft(8)man page. - For details on other actions you can run on chains, see the
Rulessection in thenft(8)man page.