下面给你一套安全加固 CentOS 上 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
编辑 redis.conf:
bind 127.0.0.1
# 或指定内网 IP
# bind 192.168.1.10
❌ 禁止:
bind 0.0.0.0
requirepass StrongPassword123!
连接时:
redis-cli -a StrongPassword123!
✅ 建议配合防火墙使用,不只是依赖密码
port 6379 # 改为非默认端口
rename-command FLUSHALL ""
rename-command FLUSHDB ""
rename-command CONFIG ""
rename-command SHUTDOWN ""
rename-command DEBUG ""
或重命名为随机字符串:
rename-command FLUSHALL "a3f9c2e1"
sudo firewall-cmd --permanent --add-port=6379/tcp
sudo firewall-cmd --reload
✅ 更推荐:只允许应用服务器访问
protected-mode yes
chmod 600 /etc/redis/redis.conf
chown redis:redis /etc/redis/redis.conf
bind 127.0.0.1
port 0
unixsocket /run/redis/redis.sock
unixsocketperm 700
应用通过 socket 连接,彻底避免网络暴露。
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
防止被恶意打爆:
maxmemory 512mb
maxmemory-policy allkeys-lru
maxclients 1000
loglevel notice
logfile /var/log/redis/redis.log
定期检查:
grep -i "unauthorized\|auth\|config" /var/log/redis/redis.log
systemctl disable redis
setsebool -P redis_connect_any off
| 项目 | 状态 |
|---|---|
| 非 root 运行 | ✅ |
| 绑定 127.0.0.1 | ✅ |
| 设置 requirepass | ✅ |
| 禁用 FLUSHALL | ✅ |
| 修改默认端口 | ✅ |
| 防火墙限制 | ✅ |
| protected-mode | ✅ |
0.0.0.0如果你愿意,我可以:
直接把你的 Redis 版本和部署方式发我即可。