温馨提示×

如何安全加固CentOS Redis

小樊
32
2026-08-03 14:31:38
栏目: 云计算

下面给你一套安全加固 CentOS 上 Redis 的实用方案,按必须 → 推荐 → 进阶分级,适合生产环境。


一、基础必备加固(必须做)

1. 不要以 root 运行 Redis

ps -ef | grep redis

如果不是 redis 用户,建议修改:

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

启动方式:

sudo -u redis redis-server /etc/redis/redis.conf

2. 绑定本地监听(禁止公网访问)

编辑 redis.conf

bind 127.0.0.1
# 或指定内网 IP
# bind 192.168.1.10

❌ 禁止:

bind 0.0.0.0

3. 设置强密码

requirepass StrongPassword123!

连接时:

redis-cli -a StrongPassword123!

✅ 建议配合防火墙使用,不只是依赖密码


4. 修改默认端口

port 6379  # 改为非默认端口

二、推荐加固(生产环境必做)

5. 禁用危险命令

rename-command FLUSHALL ""
rename-command FLUSHDB ""
rename-command CONFIG ""
rename-command SHUTDOWN ""
rename-command DEBUG ""

或重命名为随机字符串:

rename-command FLUSHALL "a3f9c2e1"

6. 禁止 Redis 外网访问(防火墙)

sudo firewall-cmd --permanent --add-port=6379/tcp
sudo firewall-cmd --reload

✅ 更推荐:只允许应用服务器访问


7. 启用保护模式

protected-mode yes

8. 限制 Redis 文件权限

chmod 600 /etc/redis/redis.conf
chown redis:redis /etc/redis/redis.conf

三、进阶安全(高安全要求)

9. 使用 Unix Socket(替代 TCP)

bind 127.0.0.1
port 0
unixsocket /run/redis/redis.sock
unixsocketperm 700

应用通过 socket 连接,彻底避免网络暴露。


10. 启用 TLS(Redis ≥ 6.0)

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

11. 限制内存 & 连接数

防止被恶意打爆:

maxmemory 512mb
maxmemory-policy allkeys-lru
maxclients 1000

12. 日志与审计

loglevel notice
logfile /var/log/redis/redis.log

定期检查:

grep -i "unauthorized\|auth\|config" /var/log/redis/redis.log

四、CentOS 特有问题

13. 关闭 Redis 自启动(如不需要)

systemctl disable redis

14. 使用 SELinux(可选)

setsebool -P redis_connect_any off

五、快速安全自检清单 ✅

项目 状态
非 root 运行
绑定 127.0.0.1
设置 requirepass
禁用 FLUSHALL
修改默认端口
防火墙限制
protected-mode

六、常见错误 ❌

  • 只设密码但监听 0.0.0.0
  • 把 Redis 暴露在公网
  • 使用弱密码
  • 未限制 CONFIG / FLUSHALL

如果你愿意,我可以:

  • 帮你检查现有 redis.conf
  • 给你一份生产级 redis.conf 模板
  • 针对 Docker / Kubernetes 场景加固

直接把你的 Redis 版本和部署方式发我即可。

0