redis to configure the authentication password method

  • 2020-05-10 23:10:13
  • OfStack

1. Configuration via a configuration file
The redis configuration file installed in yum mode is usually found in /etc/ redis.conf by opening the configuration file


#requirepass foobared 

Remove the comment before the line and change the password to the desired password, saving the file


requirepass myRedis 

Restart redis


sudo service redis restart 
# or  
sudo service redis stop 
sudo redis-server /etc/redis.conf 

At this time, try to log in redis and find that you can log on, but the specific command is not allowed to execute the prompt operation


redis-cli -h 127.0.0.1 -p 6379 
redis 127.0.0.1:6379> 
redis 127.0.0.1:6379> keys * 
(error) ERR operation not permitted 
redis 127.0.0.1:6379> select 1 
(error) ERR operation not permitted 
redis 127.0.0.1:6379[1]>  

Try logging in with a password and execute the specific command to see that it works


redis-cli -h 127.0.0.1 -p 6379 -a myRedis 
redis 127.0.0.1:6379> keys * 
1) "myset" 
2) "mysortset" 
redis 127.0.0.1:6379> select 1 
OK 
redis 127.0.0.1:6379[1]> config get requirepass 
1) "requirepass" 
2) "myRedis" 

2. Configure from the command line


redis 127.0.0.1:6379[1]> config set requirepass my_redis 
OK 
redis 127.0.0.1:6379[1]> config get requirepass 
1) "requirepass" 
2) "my_redis" 

No need to restart redis
Login to redis with the old password configured in the configuration file in step 1. You will find that the original password is no longer available and the operation is rejected


redis-cli -h 127.0.0.1 -p 6379 -a myRedis 
redis 127.0.0.1:6379> config get requirepass 
(error) ERR operation not permitted 

You can log on to redis with the modified password to perform the corresponding operation


redis-cli -h 127.0.0.1 -p 6379 -a my_redis 
redis 127.0.0.1:6379> config get requirepass 
1) "requirepass" 
2) "my_redis 

Try to restart redis for 1 time and log in redis with the newly configured password to perform the operation. It is found that the new password is invalid and redis has re-used the password in the configuration file


sudo service redis restart 
Stopping redis-server:                   [ OK ] 
Starting redis-server:                   [ OK ] 
redis-cli -h 127.0.0.1 -p 6379 -a my_redis 
redis 127.0.0.1:6379> config get requirepass 
(error) ERR operation not permitted 
redis-cli -h 127.0.0.1 -p 6379 -a myRedis 
redis 127.0.0.1:6379> config get requirepass 
1) "requirepass" 
2) "myRedis" 

In addition to setting the password through the -a parameter at login time, you can login without specifying a password and authenticate before performing the operation.


redis-cli -h 127.0.0.1 -p 6379 
redis 127.0.0.1:6379> config get requirepass 
(error) ERR operation not permitted 
redis 127.0.0.1:6379> auth myRedis 
OK 
redis 127.0.0.1:6379> config get requirepass 
1) "requirepass" 
2) "myRedis" 

3.master is configured with password. How is slave configured
If master has a password configured, slave should also configure the corresponding password parameters, otherwise it cannot be copied properly.
Find the following line in the slave configuration file, remove the comment, and change the password


requirepass myRedis 
0

Related articles: