0
0
RedisHow-ToBeginner · 3 min read

How to Enable Authentication in Redis: Simple Steps

To enable authentication in Redis, set a password by adding requirepass yourpassword in the redis.conf file. Then restart Redis, and clients must use AUTH yourpassword to connect securely.
📐

Syntax

The main syntax to enable authentication in Redis is to set the requirepass directive in the Redis configuration file. This directive requires clients to authenticate with the specified password before executing commands.

  • requirepass yourpassword: Sets the password clients must provide.
  • Located in redis.conf file, usually found in Redis installation directory.
  • After setting, Redis must be restarted for changes to take effect.
plaintext
requirepass yourpassword
💻

Example

This example shows how to enable authentication by setting a password and then connecting to Redis using the redis-cli tool with authentication.

bash
# Step 1: Edit redis.conf file
# Add or uncomment the following line:
requirepass mysecretpassword

# Step 2: Restart Redis server
sudo systemctl restart redis

# Step 3: Connect using redis-cli with authentication
redis-cli
127.0.0.1:6379> AUTH mysecretpassword
OK
127.0.0.1:6379> PING
PONG
Output
OK PONG
⚠️

Common Pitfalls

Common mistakes when enabling Redis authentication include:

  • Not restarting Redis after changing redis.conf, so the password setting is not applied.
  • Forgetting to authenticate with AUTH command before running other commands, causing errors.
  • Using weak or default passwords, which reduces security.
  • Editing the wrong configuration file or having multiple Redis instances with different configs.
plaintext
## Wrong way: Trying to run commands without AUTH
redis-cli
127.0.0.1:6379> PING
(error) NOAUTH Authentication required.

## Right way: Authenticate first
127.0.0.1:6379> AUTH mysecretpassword
OK
127.0.0.1:6379> PING
PONG
Output
(error) NOAUTH Authentication required. OK PONG
📊

Quick Reference

Summary tips for enabling Redis authentication:

  • Set requirepass in redis.conf.
  • Restart Redis to apply changes.
  • Use AUTH yourpassword before other commands.
  • Choose a strong, unique password.
  • Secure your Redis server network access.

Key Takeaways

Set the password using requirepass in redis.conf to enable authentication.
Restart Redis after changing the configuration to activate the password.
Clients must use AUTH command with the password before running other commands.
Always use a strong password to protect your Redis server.
Failing to authenticate results in NOAUTH errors when issuing commands.