温馨提示×

centos redis如何配认证

小樊
44
2026-09-24 03:43:10
栏目: 云计算

在 CentOS 上给 Redis 配置密码(认证)通常有两种常用方式:修改配置文件(推荐,持久化) 和 命令行临时设置(重启失效)。下面以 CentOS 7/8 + Redis 6.x/7.x 为例说明。


一、通过配置文件配置(推荐)

1. 找到 Redis 配置文件

通常位置:

/etc/redis.conf
# 或
/etc/redis/redis.conf

2. 编辑配置文件

vim /etc/redis.conf

找到这一行(默认是被注释的):

# requirepass foobared

修改为:

requirepass your_strong_password

建议密码复杂一些,避免被暴力破解

3. 重启 Redis 服务

# CentOS 7
systemctl restart redis

# 如果是 redis-server 直接启动
redis-cli shutdown
redis-server /etc/redis.conf

4. 验证认证

redis-cli
127.0.0.1:6379> ping
(error) NOAUTH Authentication required.

127.0.0.1:6379> auth your_strong_password
OK

或者一行命令:

redis-cli -a your_strong_password ping

二、命令行临时配置(重启失效)

redis-cli
127.0.0.1:6379> CONFIG SET requirepass "your_password"
OK

⚠️ 该方式 重启 Redis 后失效,仅用于测试。


三、Redis 6+ 的 ACL(更细粒度,可选)

如果你使用的是 Redis 6 或以上,可以使用 ACL:

redis-cli
ACL SETUSER default on >your_password ~* +@all

或创建新用户:

ACL SETUSER appuser on >apppass ~* +get +set

配置文件方式(redis.conf):

user appuser on #apppass ~* +get +set

四、安全建议(非常重要)

  1. 绑定内网 IP
bind 127.0.0.1 192.168.x.x
  1. 关闭或保护危险命令
rename-command FLUSHALL ""
rename-command CONFIG ""
  1. 开启防火墙
firewall-cmd --add-port=6379/tcp --permanent
firewall-cmd --reload
  1. 不要暴露公网 6379

如果你愿意,我可以:

  • 帮你写一份 生产环境 redis.conf 模板
  • 或说明 Docker 中 Redis 如何配密码
  • 或说明 主从 / 哨兵如何配认证

直接告诉我你的使用场景即可。

0 踩