📖 Scenario: You are setting up a Redis server for a small business. To keep the data safe, you want to require a password before anyone can access the database.
🎯 Goal: Learn how to configure Redis to require a password using requirepass and how to authenticate clients with that password.
📋 What You'll Learn
Create a Redis configuration file with a password set using requirepass
Start Redis server with the configuration file
Connect to Redis client and authenticate using the password
Verify that commands fail without authentication and succeed after authentication
💡 Why This Matters
🌍 Real World
Password protecting Redis servers is important to prevent unauthorized access to sensitive data in real-world applications.
💼 Career
Many jobs require securing databases and caches like Redis; knowing how to configure authentication is a key skill for system administrators and backend developers.
Progress0 / 4 steps
1
Create Redis configuration file with requirepass
Create a file named redis.conf and add the line requirepass mysecretpassword to set the password for Redis.
Redis
Hint
The line should exactly be requirepass mysecretpassword to set the password.
2
Start Redis server with the configuration file
Start the Redis server using the command redis-server redis.conf to apply the password protection.
Redis
Hint
Use the exact command redis-server redis.conf to start the server with your config.
3
Connect Redis client and authenticate
Connect to Redis client using redis-cli and authenticate with the password by running AUTH mysecretpassword.
Redis
Hint
First run redis-cli to open the client, then run AUTH mysecretpassword to authenticate.
4
Verify authentication enforcement
Try running a command like PING before authentication to see it fail, then run AUTH mysecretpassword and then PING again to see it succeed.
Redis
Hint
Run PING before authentication to see failure, then authenticate and run PING again to see success.
Practice
(1/5)
1. What does the requirepass directive do in Redis?
easy
A. It sets a password that clients must use to authenticate before accessing Redis.
B. It disables all client connections to Redis.
C. It automatically encrypts all data stored in Redis.
D. It sets the maximum number of clients allowed to connect.
Solution
Step 1: Understand the purpose of requirepass
The requirepass directive is used to protect Redis by requiring clients to authenticate with a password.
Step 2: Compare options with this purpose
Only It sets a password that clients must use to authenticate before accessing Redis. describes setting a password for client authentication, matching the purpose of requirepass.
Final Answer:
It sets a password that clients must use to authenticate before accessing Redis. -> Option A
Quick Check:
requirepass = password protection [OK]
Hint: requirepass means password required to connect [OK]
Common Mistakes:
Thinking requirepass disables connections
Confusing requirepass with encryption
Assuming requirepass limits client count
2. Which of the following is the correct way to authenticate a Redis client after requirepass is set?
easy
A. AUTH mypassword
B. LOGIN mypassword
C. CONNECT mypassword
D. PASSWORD mypassword
Solution
Step 1: Identify the Redis command for authentication
Redis uses the AUTH command followed by the password to authenticate clients.
Step 2: Check the options for the AUTH command
Only AUTH mypassword uses the correct AUTH command syntax.
Final Answer:
AUTH mypassword -> Option A
Quick Check:
AUTH command = authenticate password [OK]
Hint: Use AUTH command to send password [OK]
Common Mistakes:
Using LOGIN instead of AUTH
Using CONNECT or PASSWORD commands which don't exist
Forgetting to include the password after AUTH
3. Given the Redis configuration has requirepass foobar set, what will be the output of the following commands sequence?
SET key1 value1
GET key1
AUTH foobar
GET key1
medium
A. OK
(error) NOAUTH Authentication required.
OK
"value1"
B. OK
"value1"
OK
"value1"
C. OK
(error) NOAUTH Authentication required.
(error) NOAUTH Authentication required.
"value1"
D. (error) NOAUTH Authentication required.
(error) NOAUTH Authentication required.
OK
(nil)
Solution
Step 1: Understand requirepass effect on commands
When requirepass is set, all commands except AUTH fail with NOAUTH error until the client authenticates.
Step 2: Analyze command sequence
SET key1 value1 fails with NOAUTH (unauthenticated). GET key1 fails with NOAUTH. AUTH foobar succeeds (OK). GET key1 returns (nil) because the key was never successfully set.
Final Answer:
(error) NOAUTH Authentication required.
(error) NOAUTH Authentication required.
OK
(nil) -> Option D
Quick Check:
Pre-AUTH: NOAUTH errors; post-AUTH: (nil) since key unset [OK]
Hint: Non-AUTH commands fail with NOAUTH before successful AUTH [OK]
Common Mistakes:
Assuming commands work before AUTH
Thinking AUTH returns error
Confusing order of outputs
4. You set requirepass mysecret in your Redis config. A client tries to run AUTH wrongpass followed by GET key. What is the problem and how to fix it?
medium
A. The client must restart Redis; fix by restarting server.
B. The client used wrong password; fix by using correct password with AUTH.
C. requirepass disables GET command; fix by removing requirepass.
D. The client must run AUTH after GET; fix by swapping commands.
Solution
Step 1: Identify the error cause
Using a wrong password with AUTH means client is not authenticated, so GET command will fail with NOAUTH error.
Step 2: Correct the authentication process
Client must provide the correct password with AUTH to authenticate and access commands like GET.
Final Answer:
The client used wrong password; fix by using correct password with AUTH. -> Option B
Quick Check:
Wrong password = no access; correct password fixes it [OK]
Hint: AUTH password must match requirepass exactly [OK]