在 CentOS 上给 Redis 配置密码(认证)通常有两种常用方式:修改配置文件(推荐,持久化) 和 命令行临时设置(重启失效)。下面以 CentOS 7/8 + Redis 6.x/7.x 为例说明。
通常位置:
/etc/redis.conf
# 或
/etc/redis/redis.conf
vim /etc/redis.conf
找到这一行(默认是被注释的):
# requirepass foobared
修改为:
requirepass your_strong_password
建议密码复杂一些,避免被暴力破解
# CentOS 7
systemctl restart redis
# 如果是 redis-server 直接启动
redis-cli shutdown
redis-server /etc/redis.conf
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-cli
ACL SETUSER default on >your_password ~* +@all
或创建新用户:
ACL SETUSER appuser on >apppass ~* +get +set
配置文件方式(redis.conf):
user appuser on #apppass ~* +get +set
bind 127.0.0.1 192.168.x.x
rename-command FLUSHALL ""
rename-command CONFIG ""
firewall-cmd --add-port=6379/tcp --permanent
firewall-cmd --reload
如果你愿意,我可以:
直接告诉我你的使用场景即可。