温馨提示×

centos redis安全怎么配置

小樊
46
2026-09-02 19:58:15
栏目: 云计算

在 CentOS 上配置 Redis 安全,可以从网络、认证、权限、系统层面几个维度来做。下面按生产环境推荐做法说明。


一、基础安全配置(redis.conf)

Redis 配置文件一般在:

/etc/redis.conf
# 或
/etc/redis/redis.conf

1️⃣ 绑定内网 IP(禁止公网)

bind 127.0.0.1 192.168.1.100

❌ 不要:

bind 0.0.0.0

2️⃣ 关闭保护模式(仅在内网+有密码时可关)

protected-mode yes

如果 Redis 只在本地或内网使用,保持 yes

3️⃣ 设置访问密码(必须)

requirepass StrongPassword123!

连接时需:

redis-cli -a StrongPassword123!

4️⃣ 修改默认端口(可选)

port 6379 → port 16379

5️⃣ 禁止危险命令

rename-command FLUSHALL ""
rename-command FLUSHDB ""
rename-command CONFIG "CONFIG_随机字符串"
rename-command KEYS ""

二、网络与防火墙(CentOS)

1️⃣ 防火墙限制

firewall-cmd --permanent --add-rich-rule='rule family="ipv4" source address="192.168.1.0/24" port protocol="tcp" port="6379" accept'
firewall-cmd --reload

2️⃣ 云服务器安全组

  • 仅允许 应用服务器 IP
  • 禁止 0.0.0.0/0

三、以非 root 用户运行

useradd -r -s /sbin/nologin redis
chown -R redis:redis /var/lib/redis
chown -R redis:redis /var/log/redis

systemd 示例:

User=redis
Group=redis

四、TLS 加密(Redis 6+)

适合 跨机房 / 公网

tls-port 6380
tls-cert-file /etc/redis/tls/redis.crt
tls-key-file /etc/redis/tls/redis.key
tls-ca-cert-file /etc/redis/tls/ca.crt

五、禁用或限制 Redis 命令执行

如不需要 Lua:

lua-time-limit 100

六、日志与监控

logfile /var/log/redis/redis.log
slowlog-log-slower-than 10000

监控建议:

  • redis-cli info
  • Prometheus + Redis Exporter
  • 告警:内存、连接数、慢查询

七、最小安全配置模板(推荐)

bind 127.0.0.1 10.0.0.5
protected-mode yes
port 6379
requirepass StrongPass!
rename-command FLUSHALL ""
rename-command CONFIG "CONFIG_abc123"

八、常见错误 ❌

  • Redis 暴露在公网
  • 无密码
  • 用 root 跑 Redis
  • protected-mode no + 无密码

如果你愿意,我可以:

  • ✅ 给你一份 CentOS 7/8 完整 redis.conf
  • ✅ 帮你做 等保 / 安全加固清单
  • ✅ 教你 Redis 主从 + 密码 + TLS

你是 CentOS 几?Redis 几版本?

0