iptables
БесплатноБез исполняемых скриптовНе проверенiptables and nftables are the Linux kernel packet-filtering tools used to build host firewalls, open or block ports and set up NAT. Use when a user asks to…
от TerminalSkills★ 221 звезда на GitHubОб этом скилле
iptables / nftables
Overview
Both tools filter packets with rules grouped into chains and tables. nftables is the current kernel framework, and the default backend behind iptables on Debian 10+ and RHEL 8+. Plain iptables on those systems is usually iptables-nft (check with iptables --version, which prints (nf_tables) or (legacy)), a compatibility front end that writes nftables rules. New setups should write nftables directly; iptables syntax is still common in existing guides, Docker and scripts. Releases checked: iptables 1.8.13, nftables 1.1.7 (October 2026). nft -c and iptables-translate were run here; applying rules needs root and was not done.
Rules are lost on reboot unless you persist them. A wrong default-drop rule on a remote machine cuts your own SSH session.
Instructions
Step 1: Inspect before changing
sudo nft list ruleset # everything, including rules iptables-nft created
sudo iptables -S # rules as commands
sudo iptables -L INPUT -n -v --line-numbers
Step 2: nftables, a complete host firewall
Write /etc/nftables.conf. The flush ruleset line also removes tables created by Docker or fail2ban. On such hosts drop that line and, to reload cleanly, start the file with table inet filter and delete table inet filter so only your own table is replaced.
#!/usr/sbin/nft -f
flush ruleset
table inet filter {
set admin_ips {
type ipv4_addr
flags interval
elements = { 203.0.113.0/24 }
}
chain input {
type filter hook input priority filter; policy drop;
iif lo accept
ct state established,related accept
ct state invalid drop
ip protocol icmp icmp type echo-request limit rate 5/second accept
meta l4proto ipv6-icmp accept
ip saddr @admin_ips tcp dport 22 accept
tcp dport { 80, 443 } accept
limit rate 5/minute log prefix "nft-drop: "
}
chain forward {
type filter hook forward priority filter; policy drop;
}
chain output {
type filter hook output priority filter; policy accept;
}
}
inet covers IPv4 and IPv6 in one table. ipv6-icmp must stay open or IPv6 breaks (neighbor discovery). Check the syntax, load it, and enable it at boot:
sudo nft -c -f /etc/nftables.conf # check only, nothing is applied
sudo nft -f /etc/nftables.conf
sudo systemctl enable --now nftables # Debian/Ubuntu: loads /etc/nftables.conf at boot
# RHEL family: the service reads /etc/sysconfig/nftables.conf, so put your rules (or an include line) there
Step 3: iptables, the equivalent
Order matters: accept rules first, drop policy last, and add the SSH rule before changing the policy.
sudo iptables -A INPUT -i lo -j ACCEPT
sudo iptables -A INPUT -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT
sudo iptables -A INPUT -m conntrack --ctstate INVALID -j DROP
sudo iptables -A INPUT -p tcp --dport 22 -s 203.0.113.0/24 -j ACCEPT
sudo iptables -A INPUT -p tcp -m multiport --dports 80,443 -j ACCEPT
sudo iptables -A INPUT -p icmp --icmp-type echo-request -m limit --limit 5/second -j ACCEPT
sudo iptables -P INPUT DROP
sudo iptables -P FORWARD DROP
iptables handles IPv4 only. Repeat the rules with ip6tables (and allow -p ipv6-icmp), otherwise IPv6 stays wide open. Insert with -I INPUT 1 ... to put a rule at the top, delete with -D INPUT 3 (the number from --line-numbers).
Persist the rules:
sudo apt install iptables-persistent # Debian/Ubuntu, asks to save current rules
sudo netfilter-persistent save # rewrites /etc/iptables/rules.v4 and rules.v6
sudo dnf install iptables-services && sudo service iptables save # RHEL family
Validate a saved file before loading it: sudo iptables-restore --test < /etc/iptables/rules.v4.
Step 4: ufw, the shortcut
sudo ufw default deny incoming
sudo ufw default allow outgoing
sudo ufw limit 22/tcp # allows SSH, rate-limits repeated attempts
sudo ufw allow 443/tcp
sudo ufw allow from 10.0.0.0/8 to any port 5432 proto tcp
sudo ufw enable
sudo ufw status verbose
Step 5: Translate old rules
iptables-translate -A INPUT -p tcp -m multiport --dports 80,443 -j ACCEPT
# nft 'add rule ip filter INPUT ip protocol tcp tcp dport { 80, 443 } counter accept'
sudo iptables-save | iptables-restore-translate -f /dev/stdin # whole ruleset
Matches with no nft equivalent, such as -m recent, are printed as comments and need a manual rewrite (use a set with a timeout or limit).
Examples
Example 1: "Lock down a new web server, SSH only from the office"
Use the Step 2 file with the office range in admin_ips. Before applying it over SSH, schedule an automatic rollback so a mistake cannot lock you out:
sudo nft list ruleset > /root/nft-before.conf
echo 'nft flush ruleset; nft -f /root/nft-before.conf' | sudo at now
Установить iptables в Claude Code и Claude Desktop
Зарегайся, чтобы установить скилл
Создай бесплатный аккаунт, чтобы открыть команду установки и сохранить скилл в библиотеку.
- Открой команду установки в одну строку
- Сохраняй скиллы в синхронизируемую библиотеку
- Уведомления, когда скиллы обновляются
Разрешённые инструменты
Инструменты, которые скиллу разрешено вызывать.
Без ограничений — скилл может использовать любой инструмент.
Вложенные файлы
FAQ
Что делает скилл iptables?
iptables and nftables are the Linux kernel packet-filtering tools used to build host firewalls, open or block ports and set up NAT. Use when a user asks to set up a firewall on a Linux server, block or allow a port or IP, restrict SSH, configure masquerade or port forwarding, persist rules across reboots, migrate from iptables to nftables, or fix Docker and ufw conflicts.
Как установить скилл iptables?
Скопируй папку скилла в ~/.claude/skills (вкладка Claude Code выше делает это одной командой), либо поставь как плагин.
Скилл iptables запускает скрипты?
Нет, скилл состоит только из инструкций (SKILL.md), без исполняемых скриптов.
Похожие скиллы
mcp-builder
Guide for creating high-quality MCP (Model Context Protocol) servers that enable LLMs to interact with external services through well-designed tools. Use when…
от Anthropicclaude-api
Reference for the Claude API / Anthropic SDK — model ids, pricing, params, streaming, tool use, MCP, agents, caching, token counting, model migration. TRIGGER…
от Anthropicalgorithmic-art
Creating algorithmic art using p5.js with seeded randomness and interactive parameter exploration. Use this when users request creating art using code…
от Anthropicmcp-integration
This skill should be used when the user asks to "add MCP server", "integrate MCP", "configure MCP in plugin", "use .mcp.json", "set up Model Context…
от Anthropic