#1.查看当前所有用户
127.0.0.1:6379> acl list
user default on #a665a45920422f9d417e4867efdc4fb8a04a1f3fff1fa07e998e86f7f7a27ae3 ~* +@all
#2.配置ACL
127.0.0.1:6379> config rewrite
OK #执行config rewrite重写配置后会自动在conf文件最下面新增一行记录配置default的密码和权限
[root@redis01 ~]# cd /usr/local/redis/conf
[root@redis01 ~]# egrep 'user default on' /usr/local/redis/conf/redis.conf
user default on #a665a45920422f9d417e4867efdc4fb8a04a1f3fff1fa07e998e86f7f7a27ae3 ~* +@all
#设置密码
[root@redis01 ~]# vim /usr/local/redis/conf/redis.conf
requirepass 123 #可将此内容注释即没有密码,或修改其他密码
#验证
[root@redis01 ~]# systemctl restart redis
[root@redis01 ~]# redis-cli
127.0.0.1:6379> set a b
OK
#1.创建用户
127.0.0.1:6379> acl setuser test #创建用户(不设置密码权限默认没有密码没有任何权限)
OK
127.0.0.1:6379> acl list #查看用户
user default on #a665a45920422f9d417e4867efdc4fb8a04a1f3fff1fa07e998e86f7f7a27ae3 ~* +@all
user test on -@all
#2.启用用户
(用户默认情况下,是未激活状态)
127.0.0.1:6379> acl setuser test on #激活用户
OK
127.0.0.1:6379> acl list
user default on #a665a45920422f9d417e4867efdc4fb8a04a1f3fff1fa07e998e86f7f7a27ae3 ~* +@all
user test on -@all
#3.给用户增加密码
127.0.0.1:6379> acl setuser test on >123
OK
127.0.0.1:6379> acl list
user default on #a665a45920422f9d417e4867efdc4fb8a04a1f3fff1fa07e998e86f7f7a27ae3 ~* +@all
#+@all代表拥有所有权限
user test on #a665a45920422f9d417e4867efdc4fb8a04a1f3fff1fa07e998e86f7f7a27ae3 -@all #-@all代表所有权限都没有
#4.查看当前用户
127.0.0.1:6379> acl whoami
"default" #可以看做Redis的超级用户,拥有所有权限
#5.登录普通用户,是没有acl权限的
[root@redis01 ~]# redis-cli
127.0.0.1:6379> auth test 123
OK
127.0.0.1:6379> acl list
(error) NOPERM this user has no permissions to run the 'acl' command or its subcommand
#6.认证
127.0.0.1:6379> auth default 123
OK
127.0.0.1:6379> acl whoami
"default"
#7.给用户设置权限
127.0.0.1:6379> acl setuser test on >123 ~name* +@all #给test用户以name开头的key加上所有权限
127.0.0.1:6379> acl setuser test on >123 ~name* +set #给test用户以name开头的key加上set权限
OK
127.0.0.1:6379> acl list
1) "user default on nopass ~* +@all"
2) "user test on #a665a45920422f9d417e4867efdc4fb8a04a1f3fff1fa07e998e86f7f7a27ae3 ~name* +@all"
127.0.0.1:6379> acl setuser dashuju #创建大数据用户
OK
127.0.0.1:6379> acl setuser dashuju on >123 ~dashuju* +set +get #给大数据用户的dashuju开头的key授set get权限
OK
127.0.0.1:6379> acl list
1) "user dashuju on #a665a45920422f9d417e4867efdc4fb8a04a1f3fff1fa07e998e86f7f7a27ae3 ~dashuju* -@all +set +get"
2) "user default on nopass ~* +@all"
3) "user test on #a665a45920422f9d417e4867efdc4fb8a04a1f3fff1fa07e998e86f7f7a27ae3 ~name* +@all"
#8.展示所有的用户
127.0.0.1:6379> acl list
user dashuji on #a665a45920422f9d417e4867efdc4fb8a04a1f3fff1fa07e998e86f7f7a27ae3 ~dashuju* -@all +get +set
user dashuju off -@all
user default on #a665a45920422f9d417e4867efdc4fb8a04a1f3fff1fa07e998e86f7f7a27ae3 ~* +@all
user test on #a665a45920422f9d417e4867efdc4fb8a04a1f3fff1fa07e998e86f7f7a27ae3 -@all
#9.展示所有用户名
127.0.0.1:6379> acl users
dashuji
dashuju
default
test
#10.展示当前用户
127.0.0.1:6379> acl whoami
"default"
#11.展示当前可授权限
127.0.0.1:6379> acl cat
1) "keyspace"
2) "read"
3) "write"
4) "set"
#12.查看一个用户所有的key
127.0.0.1:6379> acl getuser test
flags
on
passwords
a665a45920422f9d417e4867efdc4fb8a04a1f3fff1fa07e998e86f7f7a27ae3
commands
-@all
keys
#13.删除一个用户
127.0.0.1:6379> acl list
user dashuju off -@all
user default on #a665a45920422f9d417e4867efdc4fb8a04a1f3fff1fa07e998e86f7f7a27ae3 ~* +@all
127.0.0.1:6379> acl deluser dashuji s#删除大数据用户
user dashuju off -@all
user default on #a665a45920422f9d417e4867efdc4fb8a04a1f3fff1fa07e998e86f7f7a27ae3 ~* +@all
user test on #a665a45920422f9d417e4867efdc4fb8a04a1f3fff1fa07e998e86f7f7a27ae3 -@all
4.保存ACL
我们可以使用 acl save 命令将当前服务器中的 ACL 权限持久化到 aclfile 中,如果没持久化就关闭 redis 服务, 那些 ACL 权限就会丢失,因此我们每次授权之后一定要记得 ACL SAVE 将 ACL 权限持久化到 aclfile中。
#1.修改users.acl文件内容
[root@redis01 conf]# vim users.acl #添加一行tests用户信息
user default on nopass ~* +@all
user test on #a665a45920422f9d417e4867efdc4fb8a04a1f3fff1fa07e998e86f7f7a27ae3 ~name* +@all
user tests on #a665a45920422f9d417e4867efdc4fb8a04a1f3fff1fa07e998e86f7f7a27ae3 ~name* +@all
#2.使用命令重新加载用户信息会生成一个文件中指定的新用户tests
127.0.0.1:6379> acl load
OK
127.0.0.1:6379> acl list
1) "user default on nopass ~* +@all"
2) "user test on #a665a45920422f9d417e4867efdc4fb8a04a1f3fff1fa07e998e86f7f7a27ae3 ~name* +@all"
3) "user tests on #a665a45920422f9d417e4867efdc4fb8a04a1f3fff1fa07e998e86f7f7a27ae3 ~name* +@all"