SoFunction
Updated on 2025-03-03

Three ways to set password for Redis

Preface

Redis is a high-performance key-value pair database, widely used in caching, message queueing and other scenarios. In order to ensure the security of Redis services, setting up password authentication is a very important step.

Method 1: Set password by editing the configuration file

1. Find the configuration file

Normally,The file is located in the Redis installation directory or/etc/redis/in the directory. The specific location depends on your operating system and installation method.

2. Open the file using a text editor

You can use any text editor to open and edit files. Commonly used text editors include:

  • Windows: Notepad, Notepad++, Visual Studio Code
  • Linux/MacOS: Vim, Nano, GEdit, Visual Studio Code

Suppose you use the nano editor on Linux, you can execute the following commands:

sudo nano /etc/redis/

3. Modify the configuration file

existIn the file, find the following line:

# requirepass foobared

Remove the previous one#comment andfoobaredReplace with the password you want to set. For example:

requirepass yourpassword

hereyourpasswordIt is a password you set yourself, and it is recommended to use complex and difficult to guess strings.

4. Save and close the file

  • existnanomiddle:according toCtrl + OSave the file and pressEnterConfirm, finally pressCtrl + XExit the editor.
  • existVimmiddle:according toEscKey, then enter:wqAnd pressEnterSave and exit.
  • In other editors: Save and close operations according to the editor's prompts.

5. Restart Redis service

After modifying the configuration file, you need to restart the Redis service to make the configuration take effect. The restart command may vary depending on your operating system. Common commands are:

  • For systems using Systemd (such as the latest Ubuntu, CentOS, etc.)
sudo systemctl restart 
  • For older systems, you may need to use it
sudo service redis-server restart

Method 2: Set password through the command line

1. Connect to the Redis server

useredis-cliConnect to the Redis server:

redis-cli

2. Set password

existredis-cliAt the prompt, useCONFIG SETCommand set password:

127.0.0.1:6379> CONFIG SET requirepass yourpassword

hereyourpasswordIt is the password you set yourself.

3. Persistent configuration

In order for the set password to take effect permanently, the configuration needs to be persisted toin the file. Can be usedCONFIG REWRITEOrder:

127.0.0.1:6379> CONFIG REWRITE

This will write the configuration of the current runtime back toin the file.

4. Verify password settings

quitredis-cliAnd reconnect and verify whether the password is required:

redis-cli
127.0.0.1:6379> AUTH yourpassword

If the password is correct,redis-cliWill returnOK; If the password is wrong, an error message will be received.

Method 3: Directly specify the password in the connection command

1. Use the redis-cli command line tool

Directly specify the password in the connection command, so that after the connection is successful, there is no need to execute it separately.AUTHOrdered:

redis-cli -a yourpassword

Here-aThe following parameters is the password.

Things to note

  • Password complexity: Passwords should be complex enough to increase security.
  • Network transmission security: If you are connecting to the Redis server remotely, ensure that the network transmission is safe and avoid password intercepting during transmission.
  • Other safety measures: In a production environment, in addition to setting passwords, other security measures should be combined, such as firewall rule restrictions, using SSL/TLS to encrypt connections, etc.

Summarize

With the above method, you can set password protection for Redis to improve its security.

This is the end of this article about three ways to set passwords for Redis. For more related content on Redis password settings, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!