firewall-cmd命令安全加固OpenClaw服务器
你当前的 rich rule 写反了方向。destination address 是 目标地址,表示“发往某个网段的流量”,而你需要的是 限制来源地址 (source) 访问本机。
因此规则应全部使用 source address。
另外还要注意 规则优先级顺序:
-
允许
192.168.0.1 -
再拒绝
192.168.0.0/24 -
再拒绝
192.168.1.0/24 -
再拒绝
192.168.4.0/24
否则 192.168.0.1 可能被 /24 规则误拦截。
一、先删除错误规则
firewall-cmd --permanent --remove-rich-rule='rule family="ipv4" destination address="192.168.4.0/24" reject'
firewall-cmd --permanent --remove-rich-rule='rule family="ipv4" destination address="192.168.1.0/24" reject'
firewall-cmd --permanent --remove-rich-rule='rule family="ipv4" source address="192.168.0.1" accept'
二、按正确逻辑添加规则
1 允许网关访问
firewall-cmd --permanent --add-rich-rule='rule family="ipv4" source address="192.168.0.1" accept'
2 禁止 192.168.0.0/24 访问(排除网关)
firewall-cmd --permanent --add-rich-rule='rule family="ipv4" source address="192.168.0.0/24" reject'
3 禁止其它两个网段
firewall-cmd --permanent --add-rich-rule='rule family="ipv4" source address="192.168.1.0/24" reject'
firewall-cmd --permanent --add-rich-rule='rule family="ipv4" source address="192.168.4.0/24" reject'
三、确保 SSH 开放
你已经有:
services: ssh
如果需要可以再确认:
firewall-cmd --permanent --add-service=ssh
四、重新加载规则
firewall-cmd --reload
五、最终预期结果
firewall-cmd --list-rich-rules
应该类似:
rule family="ipv4" source address="192.168.0.1" accept
rule family="ipv4" source address="192.168.0.0/24" reject
rule family="ipv4" source address="192.168.1.0/24" reject
rule family="ipv4" source address="192.168.4.0/24" reject
六、实现效果
| 来源IP | 是否能访问 |
|---|---|
| 192.168.0.1 | ✅ 允许 |
| 192.168.0.x | ❌ 禁止 |
| 192.168.1.x | ❌ 禁止 |
| 192.168.4.x | ❌ 禁止 |
| 其他公网IP | ✅ 可访问 SSH |
并且:
-
服务器访问外网不受影响
-
默认 SSH 对外开放
七、生产环境更严谨写法(推荐)
只限制 SSH 而不是全部流量:
rule family="ipv4" source address="192.168.1.0/24" service name="ssh" reject
否则会影响:
-
ping
-
http
-
agent
-
监控
你的当前情况需要先明确一个关键点:
1️⃣ 当前 openclaw 端口实际上 外部无法访问
从 netstat 看:
127.0.0.1:18789
127.0.0.1:18791
127.0.0.1:18792
绑定在 127.0.0.1 (loopback),意味着:
-
只有 本机 localhost 可以访问
-
任何外部 IP 都无法连接
所以即使在 firewalld 放行,这些端口 仍然不会对外开放。
如果你的目标是:
允许 除禁止网段外的所有IP访问 openclaw
则必须先让程序监听 0.0.0.0 或 192.168.0.33。
例如:
0.0.0.0:18789
0.0.0.0:18791
0.0.0.0:18792
否则防火墙规则不会生效。
一、firewalld 完整推荐规则
你的需求总结:
| 条件 | 规则 |
|---|---|
| 192.168.0.1 | 允许 |
| 192.168.0.0/24 | 禁止 |
| 192.168.1.0/24 | 禁止 |
| 192.168.4.0/24 | 禁止 |
| 其他IP | 允许 SSH + openclaw |
二、开放 openclaw 端口
firewall-cmd --permanent --add-port=18789/tcp
firewall-cmd --permanent --add-port=18791/tcp
firewall-cmd --permanent --add-port=18792/tcp
UDP 5353:
firewall-cmd --permanent --add-port=5353/udp
三、配置 IP 限制规则
1 允许网关
firewall-cmd --permanent \
--add-rich-rule='rule family="ipv4" source address="192.168.0.1" accept'
2 禁止三个网段
firewall-cmd --permanent \
--add-rich-rule='rule family="ipv4" source address="192.168.0.0/24" reject'
firewall-cmd --permanent \
--add-rich-rule='rule family="ipv4" source address="192.168.1.0/24" reject'
firewall-cmd --permanent \
--add-rich-rule='rule family="ipv4" source address="192.168.4.0/24" reject'
四、重新加载
firewall-cmd --reload
五、最终规则验证
firewall-cmd --list-all
应看到:
services: ssh
ports: 18789/tcp 18791/tcp 18792/tcp 5353/udp
rich rules:
rule family="ipv4" source address="192.168.0.1" accept
rule family="ipv4" source address="192.168.0.0/24" reject
rule family="ipv4" source address="192.168.1.0/24" reject
rule family="ipv4" source address="192.168.4.0/24" reject
六、访问效果
| 来源IP | SSH | Openclaw |
|---|---|---|
| 192.168.0.1 | ✅ | ✅ |
| 192.168.0.x | ❌ | ❌ |
| 192.168.1.x | ❌ | ❌ |
| 192.168.4.x | ❌ | ❌ |
| 公网IP | ✅ | ✅ |
服务器访问外网 不受影响。
七、非常重要(openclaw 必须改监听地址)
如果你想让别人访问 openclaw,必须看到:
0.0.0.0:18789
0.0.0.0:18791
0.0.0.0:18792
而不是:
127.0.0.1:18789
常见修改方式:
config.yaml
gateway.yaml
server.conf
--host 0.0.0.0
你遇到的错误是 正常现象:
firewalld rich rule 不支持 direction="out" 属性,所以会报:
INVALID_RULE: bad attribute 'direction'
在 firewalld 架构中:
-
rich rule 默认作用于 INPUT / FORWARD
-
不直接支持 OUTPUT 方向控制
如果要限制 本机访问其它网段(egress filtering),通常有三种方法:
1️⃣ 使用 direct rule(推荐)
2️⃣ 使用 policy objects(firewalld 1.0+)
3️⃣ 直接使用 iptables/nftables
在 Ubuntu 24.04 + firewalld,最简单稳定的是 direct rule。
一、实现你的需求的正确方法
需求:
禁止本机访问:
192.168.0.0/24 (除192.168.0.1)
192.168.1.0/24
192.168.4.0/24
二、允许访问网关
firewall-cmd --permanent \
--direct --add-rule ipv4 filter OUTPUT 0 \
-d 192.168.0.1 -j ACCEPT
三、禁止访问三个网段
禁止 192.168.0.0/24
firewall-cmd --permanent \
--direct --add-rule ipv4 filter OUTPUT 1 \
-d 192.168.0.0/24 -j REJECT
禁止 192.168.1.0/24
firewall-cmd --permanent \
--direct --add-rule ipv4 filter OUTPUT 1 \
-d 192.168.1.0/24 -j REJECT
禁止 192.168.4.0/24
firewall-cmd --permanent \
--direct --add-rule ipv4 filter OUTPUT 1 \
-d 192.168.4.0/24 -j REJECT
四、重新加载
firewall-cmd --reload
五、验证 direct 规则
firewall-cmd --direct --get-all-rules
应该看到:
ipv4 filter OUTPUT 0 -d 192.168.0.1 -j ACCEPT
ipv4 filter OUTPUT 1 -d 192.168.0.0/24 -j REJECT
ipv4 filter OUTPUT 1 -d 192.168.1.0/24 -j REJECT
ipv4 filter OUTPUT 1 -d 192.168.4.0/24 -j REJECT
六、最终完整防火墙策略
入站限制
192.168.0.1 允许
192.168.0.0/24 禁止
192.168.1.0/24 禁止
192.168.4.0/24 禁止
出站限制
192.168.0.1 允许
192.168.0.0/24 禁止
192.168.1.0/24 禁止
192.168.4.0/24 禁止
外网允许
七、最终访问效果
| 方向 | IP | 结果 |
|---|---|---|
| 外部 → 本机 | 192.168.0.1 | ✅ |
| 外部 → 本机 | 192.168.0.x | ❌ |
| 外部 → 本机 | 192.168.1.x | ❌ |
| 外部 → 本机 | 192.168.4.x | ❌ |
| 外部 → 本机 | 其他IP | ✅ |
| 本机 → 192.168.0.1 | ✅ | |
| 本机 → 192.168.0.x | ❌ | |
| 本机 → 192.168.1.x | ❌ | |
| 本机 → 192.168.4.x | ❌ | |
| 本机 → 外网 | ✅ |
八、运维建议(非常重要)
你的服务器:
192.168.0.33
如果完全禁止访问:
192.168.0.0/24
可能影响:
-
DNS
-
NTP
-
内网API
-
NFS / SMB
-
LDAP / AD
生产环境通常只限制 SSH / 管理端口,而不是整个网段。
如果需要,我可以给你一套 运维服务器 firewalld 最佳实践配置(约25条规则),特点:
-
SSH 防暴力破解
-
内网扫描防护
-
只允许必要端口
-
自动记录攻击日志
-
适合 Ubuntu 20/22/24 运维服务器。