How to Set Password in Redis for Secure Access
To set a password in Redis, add
requirepass yourpassword in the Redis configuration file (redis.conf) or use the command CONFIG SET requirepass yourpassword. After setting the password, clients must authenticate using AUTH yourpassword before executing commands.Syntax
The password in Redis is set using the requirepass directive in the configuration file or dynamically with the CONFIG SET command.
requirepass <password>: Sets the password inredis.conf.CONFIG SET requirepass <password>: Sets the password at runtime.AUTH <password>: Used by clients to authenticate after password is set.
plaintext
requirepass yourpassword
Example
This example shows how to set a password in Redis using the configuration file and how a client authenticates with the password.
bash
# Step 1: Add password in redis.conf requirepass mysecretpassword # Step 2: Restart Redis server to apply changes # Step 3: Connect with redis-cli and authenticate redis-cli > AUTH mysecretpassword OK > PING PONG
Output
OK
PONG
Common Pitfalls
Common mistakes when setting a password in Redis include:
- Not restarting Redis after changing
redis.conf, so the password is not applied. - Forgetting to authenticate with
AUTHbefore running commands, causing errors. - Setting an empty or weak password, which does not secure Redis.
- Using
CONFIG SET requirepasswithout persisting it, so password resets after restart.
redis
Wrong way: redis-cli > PING (error) NOAUTH Authentication required. Right way: redis-cli > AUTH mysecretpassword OK > PING PONG
Output
(error) NOAUTH Authentication required.
OK
PONG
Quick Reference
| Command | Description |
|---|---|
| requirepass | Set password in redis.conf file |
| CONFIG SET requirepass | Set password at runtime (not persistent) |
| AUTH | Authenticate client with password |
| PING | Test connection after authentication |
Key Takeaways
Set the password using requirepass in redis.conf or CONFIG SET requirepass at runtime.
Clients must authenticate with AUTH before running commands after password is set.
Restart Redis after changing redis.conf for the password to take effect.
CONFIG SET requirepass is temporary and resets after Redis restarts.
Always use a strong password to secure your Redis server.