We can make Redis servers password protected in two ways, by directly setting it on the redis config file or via the Redis command line interface.
We use the AUTH comment to authenticate the password of an Redis protected server. And once the password given matches with the previously set password then, we will get an OK status code as response from the server, other wise the redis server will response an ERR invalid password error.
Basic AUTH command
AUTH Password
Step 1 :
Open Terminal and connect to the Redis command line interface
redis-cli
##or if a different host or same host with a binded Ip Address
redis-cli -h <IPADDRESS>
Step 2 :
Let’s check if the Redis Server already got a password or not.
AUTH mypassword
#Response
#If Password is not set then server replies
(error) ERR Client sent AUTH, but no password is set
#Else if Incorrect
(error) ERR invalid password
Step 3 :
# Set a Password
CONFIG SET requirepass "mypassword"
# Server Response will be
OK
Step 4 :
# Authenticate the password
AUTH "mypassword"
# If Password Matches then Server Response will be
OK
# Else if Incorrect
(error) ERR invalid password
Step 1 :
#Open the redis config file using nano.
sudo nano /etc/redis/redis.conf
Step 2 :
Find the line starts with requirepass, Uncomment the line and set a new password
# requirepass yourpassword
-to-
requirepass mypassword
Step 3 :
Save the File and restart the Redis-Server.
Use the AUTH Comment to test the password.