Organisation & Structure
Use Tables and Chains Consistently
Keep separate tables for
inet (IPv4+IPv6 combined), ip, and ip6 only when necessary.Basic Table Structure
Groups & Sets
Sets replace the legacyipset from iptables, providing easier management of IP groups and service ports without rule duplication.
IP Address Sets
Use sets to manage groups of hosts or networks:Port and Service Sets
Simplify multi-service allow/deny rules:Sets use maps and intervals internally, making them significantly faster than multiple individual rules at scale.
Reusability & Abstraction
Name Chains by Purpose
Use descriptive names like
input, forward, output, log_dropSplit Rules Logically
Separate base policy, user-defined services, and logging rules
Example: Logging Chain
Security-Oriented Defaults
Security Best Practices
Rate Limiting Example
Performance & Maintainability
Prefer Sets
Sets are faster than many individual rules (nftables uses maps/intervals internally)
Version Control
Keep ruleset in
/etc/sysconfig/nftables.conf under version control (Git)Add Comments
Use comments generously in rules for future administrators
Single File Configuration
Maintain configuration in one file for easier management
Testing & Validation
Testing Workflow
Stage complex changes on non-production systems first before deploying to production.
Complete Example: IT Host Access Control
This example demonstrates multi-line sets and proper rule organization for IT administrative access:Key Features of This Configuration
Key Features of This Configuration
- IP Sets: Centralized management of IT hosts
- Port Sets: Easy service definition
- Stateful Inspection: Allows established connections
- Logging: All dropped packets are logged with prefix
- Comments: Inline documentation for each IP
- Default Deny: Explicit drop at the end
Quick Reference
| Feature | iptables | nftables |
|---|---|---|
| IP Sets | Separate ipset tool | Native sets |
| IPv4+IPv6 | Duplicate rules | inet family |
| Syntax | Complex | Clean, readable |
| Performance | Good | Better with sets |
| Maintainability | Moderate | Excellent |