Mapbaya/Custom-Firewall
GitHub: Mapbaya/Custom-Firewall
基于 Shell CLI 的 Linux 防火墙管理工具,通过预置配置文件简化 nftables/iptables 规则的切换与管理。
Stars: 0 | Forks: 0
# Custom-Firewall
个人项目:使用命名配置(strict、dev、home)管理 Linux 防火墙规则,提供安全的 SSH 默认设置和实用的 shell CLI。
## 功能特性
- **优先使用 nftables** — 现代化的 netfilter;如果 nft 不可用,自动回退到 iptables
- **配置** — 使用单一命令在 strict、dev 和 home 预设之间切换
- **安全默认值** — 除非您禁用 `PROTECT_SSH`,否则 SSH(端口 22)将始终保持允许状态
- **简单配置** — 纯 `key=value` 的 `.conf` 文件,易于编辑
- **日志记录** — 通过 nftables/iptables 实现内核日志前缀,可在 syslog/journal 中查看
- **彩色 CLI** — `start`、`stop`、`status`、`apply`、`list-profiles`、`show-rules`、`enable-logging`
## 环境要求
- Linux(Debian/Ubuntu、Arch、RHEL/Fedora 等)
- Root / sudo 访问权限
- `nftables`(推荐)或 `iptables`
## 快速开始
### FR — 快速开始
```
git clone https://github.com/Mapbaya/Custom-Firewall.git
cd Custom-Firewall
sudo ./install.sh
sudo custom-firewall list-profiles
sudo custom-firewall apply home
sudo custom-firewall status
```
### EN — 快速开始
```
git clone https://github.com/Mapbaya/Custom-Firewall.git
cd Custom-Firewall
sudo ./install.sh
sudo custom-firewall apply dev
sudo custom-firewall show-rules
```
无需安装,可直接在仓库中运行:
```
sudo ./scripts/firewall.sh apply strict
```
## 配置
| Profile | TCP 端口 | UDP 端口 | 用例 |
|---------|-----------|-----------|----------|
| `strict` | 22 (SSH) | — | 最小化暴露,适用于服务器 |
| `dev` | 22, 3000, 8080, 5173 | — | 本地开发 |
| `home` | 22, 80, 443, 8080 | 53, 123 | 家庭 NAS / Web 服务 |
| `default` | 22 | — | 仅基础配置 |
当 `PROTECT_SSH=true`(默认值)时,始终合并 SSH 规则。
## CLI 示例
```
# 应用 profile
sudo custom-firewall apply dev
# 检查状态
sudo custom-firewall status
# 列出带有描述的 profiles
sudo custom-firewall list-profiles
# 查看活跃的 rules
sudo custom-firewall show-rules
# 启用 logging 并重新应用当前 profile
sudo custom-firewall enable-logging
# 停止 firewall (permissive mode)
sudo custom-firewall stop
```
## 项目结构
```
Custom-Firewall/
├── install.sh # Install to /etc/custom-firewall
├── uninstall.sh
├── config/
│ ├── default.conf # Global defaults
│ └── profiles/ # strict, dev, home
├── rules/
│ └── base.nft # nftables template reference
├── scripts/
│ ├── firewall.sh # Main CLI
│ ├── apply-profile.sh # Generate and apply rules
│ └── utils.sh # Shared helpers
└── docs/
└── USAGE.md # Detailed French usage guide
```
## 配置说明
编辑 `/etc/custom-firewall/config/`(安装后)或仓库中的 `config/` 目录下的文件。
配置文件示例片段(`config/profiles/dev.conf`):
```
PROFILE_NAME=dev
DESCRIPTION=Dev — SSH + dev ports
ALLOW_TCP=22,3000,8080,5173
ALLOW_UDP=
```
`default.conf` 中的全局选项:
| 键 | 默认值 | 描述 |
|-----|---------|-------------|
| `INPUT_POLICY` | drop | 默认入站策略 |
| `OUTPUT_POLICY` | accept | 默认出站策略 |
| `SSH_PORT` | 22 | 受保护的 SSH 端口 |
| `PROTECT_SSH` | true | 始终允许 SSH 端口 |
| `LOGGING` | false | 记录被丢弃的数据包 |
## 日志
```
sudo custom-firewall enable-logging
sudo journalctl -k | grep CUSTOM-FW
# 或
sudo dmesg | grep CUSTOM-FW
```
## 卸载
```
sudo ./uninstall.sh
```
这是关于 Linux 网络和系统管理的个人学习项目。
另请参阅:[OpenVpn-Server](https://github.com/Mapbaya/OpenVpn-Server) — VPN 搭建配套项目。
标签:Cutter, iptables, nftables, 运维工具, 防火墙