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 andfoobared
Replace with the password you want to set. For example:
requirepass yourpassword
hereyourpassword
It is a password you set yourself, and it is recommended to use complex and difficult to guess strings.
4. Save and close the file
-
exist
nano
middle:according toCtrl + O
Save the file and pressEnter
Confirm, finally pressCtrl + X
Exit the editor. -
exist
Vim
middle:according toEsc
Key, then enter:wq
And pressEnter
Save 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-cli
Connect to the Redis server:
redis-cli
2. Set password
existredis-cli
At the prompt, useCONFIG SET
Command set password:
127.0.0.1:6379> CONFIG SET requirepass yourpassword
hereyourpassword
It 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 used
CONFIG REWRITE
Order:
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-cli
And reconnect and verify whether the password is required:
redis-cli 127.0.0.1:6379> AUTH yourpassword
If the password is correct,redis-cli
Will 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.AUTH
Ordered:
redis-cli -a yourpassword
Here-a
The 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!