To set a password for Redis, you will need to edit the Redis configuration file (usually called “redis.conf”). The configuration file is typically located in the Redis installation directory.
The first step is to locate the configuration file. To locate it, you can use the following command:
$ locate redis.conf
This command will return the path of the redis.conf file.
Once you’ve located the configuration file, you will need to open it in a text editor and uncomment the following line:
# requirepass foobared
Then, replace “foobared” with the desired password.
For example:
requirepass mysecretpassword
Save the changes to the configuration file and close the text editor.
The next step is to restart the Redis server for the changes to take effect.
$ systemctl restart redis
or
$ service redis restart
Once the Redis server has been restarted, you can use the “AUTH” command in redis-cli to authenticate with the Redis server by providing the password you set in the configuration file.
$ redis-cli
127.0.0.1:6379> auth mysecretpassword
It’s important to note that after setting a password, all the clients trying to connect to Redis will have to use the auth command to authenticate before performing any operation, otherwise, they will get an error message.
It’s also important to note that you should protect the redis.conf file, so that unauthorized users can’t read or modify it.