在 CentOS 上安全配置 Redis,目标是:防止未授权访问、降低被攻击风险、避免数据泄露或勒索。下面按 生产环境标准 给你一套完整安全配置方案(适用于 CentOS 7/8、Rocky、Alma 同理)。
# redis.conf
port 6379 → 改为非默认端口
port 16379
bind 127.0.0.1 192.168.1.100
❌ 禁止
bind 0.0.0.0
requirepass StrongPassword123!
建议:
rename-command FLUSHALL ""
rename-command FLUSHDB ""
rename-command CONFIG ""
rename-command SHUTDOWN ""
rename-command DEBUG ""
✅ 生产环境 强烈建议
useradd -r -s /sbin/nologin redis
chown -R redis:redis /var/lib/redis
chown -R redis:redis /var/log/redis
chown -R redis:redis /etc/redis
# /usr/lib/systemd/system/redis.service
[Service]
User=redis
Group=redis
systemctl daemon-reload
systemctl restart redis
firewall-cmd --permanent --add-port=16379/tcp
firewall-cmd --reload
✅ 只允许应用服务器访问
检查:
ps -ef | grep redis
✅ 应为 redis 用户
❌ 不应是 root
dir /var/lib/redis
dbfilename dump.rdb
确保目录权限:
chmod 750 /var/lib/redis
tls-port 16379
port 0
tls-cert-file /etc/redis/redis.crt
tls-key-file /etc/redis/redis.key
tls-ca-cert-file /etc/redis/ca.crt
⚠️ 配置复杂,适用于金融/高安全要求环境
loglevel notice
logfile /var/log/redis/redis.log
CONFIG、FLUSHALL❌ 公网暴露 Redis
❌ 无密码
❌ root 运行
❌ 默认端口
❌ 未禁用危险命令
| 项目 | 是否 |
|---|---|
| 非默认端口 | ✅ |
| 绑定内网 IP | ✅ |
| 设置密码 | ✅ |
| 禁用危险命令 | ✅ |
| 非 root 运行 | ✅ |
| 防火墙限制 | ✅ |
如果你愿意,我可以:
直接告诉我你的 Redis 版本 + CentOS 版本 + 使用场景 即可。