Skip to content
Skillv1.0.0

firewall

防火墙配置

by chaterm(0) 0 installs
Free
Sign in to install

Free account. Installing gives you the manifest plus copy-paste snippets.

See reviews

About

Imported from chaterm/terminal-skills (security/firewall/SKILL.md). Install upstream with npx skills add chaterm/terminal-skills --skill firewall. Copyright stays with the author.

防火墙配置

概述

iptables、firewalld、nftables、ufw 防火墙配置技能。

iptables

基础命令

# 查看规则
iptables -L -n -v
iptables -L -n --line-numbers
iptables -t nat -L -n -v

# 清空规则
iptables -F
iptables -X
iptables -t nat -F

# 默认策略
iptables -P INPUT DROP
iptables -P FORWARD DROP
iptables -P OUTPUT ACCEPT

常用规则

# 允许回环
iptables -A INPUT -i lo -j ACCEPT

# 允许已建立连接
iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT

# 允许 SSH
iptables -A INPUT -p tcp --dport 22 -j ACCEPT

# 允许 HTTP/HTTPS
iptables -A INPUT -p tcp --dport 80 -j ACCEPT
iptables -A INPUT -p tcp --dport 443 -j ACCEPT

# 允许特定 IP
iptables -A INPUT -s 192.168.1.100 -j ACCEPT

# 允许网段
iptables -A INPUT -s 192.168.1.0/24 -p tcp --dport 3306 -j ACCEPT

# 拒绝其他
iptables -A INPUT -j DROP

删除规则

# 按行号删除
iptables -D INPUT 3

# 按规则删除
iptables -D INPUT -p tcp --dport 80 -j ACCEPT

NAT 配置

# 开启转发
echo 1 > /proc/sys/net/ipv4/ip_forward

# SNAT
iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE

# DNAT 端口转发
iptables -t nat -A PREROUTING -p tcp --dport 8080 -j DNAT --to 192.168.1.10:80
iptables -A FORWARD -p tcp -d 192.168.1.10 --dport 80 -j ACCEPT

保存规则

# Debian/Ubuntu
iptables-save > /etc/iptables/rules.v4
iptables-restore < /etc/iptables/rules.v4

# CentOS/RHEL
service iptables save

firewalld

基础命令

# 状态
systemctl status firewalld
firewall-cmd --state

# 重载
firewall-cmd --reload

# 查看区域
firewall-cmd --get-zones
firewall-cmd --get-default-zone
firewall-cmd --get-active-zones

服务管理

# 查看服务
firewall-cmd --list-services
firewall-cmd --get-services

# 添加服务
firewall-cmd --add-service=http --permanent
firewall-cmd --add-service=https --permanent

# 删除服务
firewall-cmd --remove-service=http --permanent

端口管理

# 查看端口
firewall-cmd --list-ports

# 添加端口
firewall-cmd --add-port=8080/tcp --permanent
firewall-cmd --add-port=5000-5100/tcp --permanent

# 删除端口
firewall-cmd --remove-port=8080/tcp --permanent

富规则

# 允许特定 IP
firewall-cmd --add-rich-rule='rule family="ipv4" source address="192.168.1.100" accept' --permanent

# 允许网段访问端口
firewall-cmd --add-rich-rule='rule family="ipv4" source address="192.168.1.0/24" port port="3306" protocol="tcp" accept' --permanent

# 拒绝 IP
firewall-cmd --add-rich-rule='rule family="ipv4" source address="10.0.0.100" reject' --permanent

ufw (Ubuntu)

基础命令

# 启用/禁用
ufw enable
ufw disable

# 状态
ufw status
ufw status verbose
ufw status numbered

规则管理

# 允许端口
ufw allow 22
ufw allow 80/tcp
ufw allow 443/tcp

# 允许服务
ufw allow ssh
ufw allow http

# 允许 IP
ufw allow from 192.168.1.100

# 允许网段到端口
ufw allow from 192.168.1.0/24 to any port 3306

# 拒绝
ufw deny 23

# 删除规则
ufw delete allow 80
ufw delete 3

默认策略

ufw default deny incoming
ufw default allow outgoing

常见场景

场景 1:Web 服务器

# iptables
iptables -A INPUT -i lo -j ACCEPT
iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
iptables -A INPUT -p tcp --dport 22 -j ACCEPT
iptables -A INPUT -p tcp --dport 80 -j ACCEPT
iptables -A INPUT -p tcp --dport 443 -j ACCEPT
iptables -A INPUT -j DROP

# firewalld
firewall-cmd --add-service=ssh --permanent
firewall-cmd --add-service=http --permanent
firewall-cmd --add-service=https --permanent
firewall-cmd --reload

场景 2:数据库服务器

# 只允许应用服务器访问
iptables -A INPUT -s 192.168.1.10 -p tcp --dport 3306 -j ACCEPT
iptables -A INPUT -s 192.168.1.11 -p tcp --dport 3306 -j ACCEPT
iptables -A INPUT -p tcp --dport 3306 -j DROP

场景 3:限速防护

# 限制 SSH 连接频率
iptables -A INPUT -p tcp --dport 22 -m state --state NEW -m recent --set
iptables -A INPUT -p tcp --dport 22 -m state --state NEW -m recent --update --seconds 60 --hitcount 4 -j DROP

故障排查

问题 排查方法
连接被拒 检查规则顺序、默认策略
规则不生效 检查 --permanent、reload
NAT 不工作 检查 ip_forward、FORWARD 链
# 查看计数
iptables -L -n -v

# 日志
iptables -A INPUT -j LOG --log-prefix "IPT_DROP: "
tail -f /var/log/messages | grep IPT_DROP

# 连接跟踪
conntrack -L

Use it

Copy one of these into your project. Installing also returns the manifest and these snippets.

yaml
targets:
  - https://api.opensmartroute.ai/api/v1/registry/chaterm-terminal-skills-firewall/manifest   # or paste the manifest below

Manifest

An Open Capability Manifest: the router reads it to know what this does, what it costs and when to pick it.

chaterm-terminal-skills-firewall.ocm.jsonjson
{
  "ocm": "1",
  "id": "chaterm-terminal-skills-firewall",
  "kind": "skill",
  "name": "firewall",
  "description": "防火墙配置",
  "publisher": "chaterm",
  "version": "1.0.0",
  "capabilities": {
    "domains": [
      "general"
    ],
    "tags": [
      "skill-md",
      "security",
      "firewall",
      "iptables",
      "firewalld",
      "nftables",
      "ufw",
      "skills-sh"
    ],
    "languages": [
      "en"
    ]
  },
  "quality_prior": 0.6,
  "examples": [
    "防火墙配置"
  ],
  "primary": false,
  "metadata": {
    "source": {
      "provider": "skills.sh",
      "repository": "https://github.com/chaterm/terminal-skills",
      "path": "security/firewall/SKILL.md",
      "ref": "HEAD",
      "url": "https://github.com/chaterm/terminal-skills/blob/HEAD/security/firewall/SKILL.md",
      "key": "chaterm/terminal-skills/security/firewall/SKILL.md"
    }
  },
  "instructions": "# 防火墙配置\n\n## 概述\niptables、firewalld、nftables、ufw 防火墙配置技能。\n\n## iptables\n\n### 基础命令\n```bash\n# 查看规则\niptables -L -n -v\niptables -L -n --line-numbers\niptables -t nat -L -n -v\n\n# 清空规则\niptables -F\niptables -X\niptables -t nat -F\n\n# 默认策略\niptables -P INPUT DROP\niptables -P FORWARD DROP\niptables -P OUTPUT ACCEPT\n```\n\n### 常用规则\n```bash\n# 允许回环\niptables -A INPUT -i lo -j ACCEPT\n\n# 允许已建立连接\niptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT\n\n# 允许 SSH\niptables -A INPUT -p tcp --dport 22 -j ACCEPT\n\n# 允许 HTTP/HTTPS\niptables -A INPUT -p tcp --dport 80 -j ACCEPT\niptables -A INPUT -p tcp --dport 443 -j AC",
  "cost": {
    "context_tokens": 1014
  }
}

Fetch it by URL: GET /api/v1/registry/chaterm-terminal-skills-firewall/manifest?version=1.0.0

Reviews

Star ratings from people who tried it. One review per account; edit yours any time.

No reviews yet. Install it, try it, and be the first to rate it.