温馨提示×

Centos Redis配置密码怎么设

小樊
58
2026-07-20 02:29:53
栏目: 云计算

CentOS 上为 Redis 设置密码,常用有以下几种方式,推荐 修改配置文件 的方式(永久生效)。


一、通过配置文件设置密码(✅ 推荐)

1. 编辑 Redis 配置文件

Redis 配置文件一般在这里:

vim /etc/redis.conf

如果你是用源码安装,可能是:

vim /usr/local/redis/redis.conf

2. 找到 requirepass 配置项

默认是注释掉的:

# requirepass foobared

取消注释并修改为你的密码,例如:

requirepass your_password

⚠️ 密码不要有特殊字符过多,避免启动失败
⚠️ 生产环境建议使用 强密码


3. 重启 Redis

systemd 方式

systemctl restart redis

老版本或源码安装

redis-server /etc/redis.conf

4. 验证密码是否生效

方式一:redis-cli

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 方式,推荐新版本)

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

四、CentOS 防火墙 & 安全建议(非常重要)

1. 不要暴露 6379 到公网

firewall-cmd --permanent --remove-port=6379/tcp
firewall-cmd --reload

2. 绑定本机访问

bind 127.0.0.1

3. 禁止危险命令(可选)

rename-command FLUSHALL ""
rename-command CONFIG ""

五、常见问题

❓ Redis 启动后提示 NOAUTH

✅ 没认证密码

auth your_password

❓ 修改配置不生效

✅ 检查:

  • 是否重启 Redis
  • 是否修改的是 正确配置文件
  • 是否有多个 Redis 实例

六、总结(推荐方案)

CentOS + Redis

requirepass 强密码
bind 127.0.0.1
protected-mode yes

如果你愿意,可以告诉我:

  • Redis 版本(redis-server -v
  • 单机 / 集群 / 哨兵
  • 是否对外网开放

我可以给你一份 生产级 Redis 安全配置模板

0