在 CentOS 上为 Redis 设置密码,常用有以下几种方式,推荐 修改配置文件 的方式(永久生效)。
Redis 配置文件一般在这里:
vim /etc/redis.conf
如果你是用源码安装,可能是:
vim /usr/local/redis/redis.conf
requirepass 配置项默认是注释掉的:
# requirepass foobared
取消注释并修改为你的密码,例如:
requirepass your_password
⚠️ 密码不要有特殊字符过多,避免启动失败
⚠️ 生产环境建议使用 强密码
systemctl restart redis
redis-server /etc/redis.conf
redis-cli
127.0.0.1:6379> ping
(error) NOAUTH Authentication required.
127.0.0.1:6379> auth your_password
OK
127.0.0.1:6379> ping
PONG
redis-cli -a your_password
redis-cli
127.0.0.1:6379> CONFIG SET requirepass "your_password"
OK
重启 Redis 后密码会消失。
Redis 6 以后支持 多用户 ACL,比 requirepass 更灵活。
redis-cli
127.0.0.1:6379> ACL SETUSER default on >your_password ~* +@all
ACL LIST
redis-cli -a your_password
firewall-cmd --permanent --remove-port=6379/tcp
firewall-cmd --reload
bind 127.0.0.1
rename-command FLUSHALL ""
rename-command CONFIG ""
NOAUTH✅ 没认证密码
auth your_password
✅ 检查:
✅ CentOS + Redis
requirepass 强密码
bind 127.0.0.1
protected-mode yes
如果你愿意,可以告诉我:
redis-server -v)我可以给你一份 生产级 Redis 安全配置模板。