Manager, Developer Education
Redis is an in-memory key-value store known for its flexibility, performance, and wide language support. This quickstart tutorial demonstrates how to install, configure, and secure Redis on an Ubuntu 20.04 server.
To complete this guide, you will need access to an Ubuntu 20.04 server that has a non-root user with sudo
privileges and a firewall configured with ufw
. You can set this up by following our Initial Server Setup guide for Ubuntu 20.04.
Begin by updating your local apt
package cache:
- sudo apt update
Then install Redis by typing:
- sudo apt install redis-server
Next, open up the Redis configuration file with your preferred text editor:
- sudo nano /etc/redis/redis.conf
Inside the file, find the supervised
directive which allows you to declare an init system to manage Redis as a service. Since you are running Ubuntu, which uses the systemd init system, change its value from no
to systemd
:
. . .
# If you run Redis from upstart or systemd, Redis can interact with your
# supervision tree. Options:
# supervised no - no supervision interaction
# supervised upstart - signal upstart by putting Redis into SIGSTOP mode
# supervised systemd - signal systemd by writing READY=1 to $NOTIFY_SOCKET
# supervised auto - detect upstart or systemd method based on
# UPSTART_JOB or NOTIFY_SOCKET environment variables
# Note: these supervision methods only signal "process is ready."
# They do not enable continuous liveness pings back to your supervisor.
supervised systemd
. . .
Save and close the file when you are finished. If you used nano
to edit the file, do so by pressing CTRL + X
, Y
, then ENTER
.
Then, restart the Redis service to reflect the changes you made to the configuration file:
- sudo systemctl restart redis.service
To test that Redis is functioning correctly, connect to the server using redis-cli
, Redis’s command-line client:
- redis-cli
In the prompt that follows, test connectivity with the ping
command:
- ping
OutputPONG
This output confirms that the server connection is active. Next, check that you’re able to set keys by running:
- set test "It's working!"
OutputOK
Retrieve the value by typing:
- get test
Assuming everything is working, you will be able to retrieve the value you stored:
Output"It's working!"
After confirming that you can fetch the value, exit the Redis prompt to get back to the shell:
- exit
You can configure a Redis password directly in Redis’s configuration file, /etc/redis/redis.conf
. Open that file again with your preferred editor:
- sudo nano /etc/redis/redis.conf
Scroll to the SECURITY
section and look for a commented directive that reads:
. . .
# requirepass foobared
. . .
Uncomment it by removing the #
, and change foobared
to a secure password:
. . .
requirepass your_redis_password
. . .
After setting the password, save and close the file, then restart Redis:
- sudo systemctl restart redis.service
To test that the password works, open up the Redis client:
- redis-cli
The following shows a sequence of commands used to test whether the Redis password works. The first command tries to set a key to a value before authentication:
- set key1 10
That won’t work because you didn’t authenticate, so Redis returns an error:
Output(error) NOAUTH Authentication required.
The next command authenticates with the password specified in the Redis configuration file:
- auth your_redis_password
Redis acknowledges:
OutputOK
After that, running the previous command again will succeed:
- set key1 10
OutputOK
get key1
queries Redis for the value of the new key.
- get key1
Output"10"
After confirming that you’re able to run commands in the Redis client after authenticating, you can exit redis-cli
:
- quit
The other security feature built into Redis involves renaming or completely disabling certain commands that are considered dangerous. Some of the commands that are considered dangerous include: FLUSHDB
, FLUSHALL
, KEYS
, PEXPIRE
, DEL
, CONFIG
, SHUTDOWN
, BGREWRITEAOF
, BGSAVE
, SAVE
, SPOP
, SREM
, RENAME
, and DEBUG
. By disabling or renaming these and other commands, you make it more difficult for unauthorized users to reconfigure, destroy, or otherwise wipe your data.
To rename or disable Redis commands, open the configuration file once more:
- sudo nano /etc/redis/redis.conf
Warning: The following steps showing how to disable and rename commands are examples. You should only choose to disable or rename the commands that make sense for you. You can review the full list of commands for yourself and determine how they might be misused at redis.io/commands.
To disable a command, simply rename it to an empty string (signified by a pair of quotation marks with no characters between them), as shown below:
. . .
# It is also possible to completely kill a command by renaming it into
# an empty string:
#
rename-command FLUSHDB ""
rename-command FLUSHALL ""
rename-command DEBUG ""
. . .
To rename a command, give it another name as shown in the examples below. Renamed commands should be difficult for others to guess, but easy for you to remember:
. . .
# rename-command CONFIG ""
rename-command SHUTDOWN SHUTDOWN_MENOT
rename-command CONFIG ASC12_CONFIG
. . .
Save your changes and close the file.
After renaming a command, apply the change by restarting Redis:
- sudo systemctl restart redis.service
To test the new command, enter the Redis command line:
- redis-cli
Then authenticate:
- auth your_redis_password
OutputOK
Assuming that you renamed the CONFIG
command to ASC12_CONFIG
as in the preceding example, try using the original CONFIG
command. It should fail, because you’ve renamed it:
- config get requirepass
Output(error) ERR unknown command `config`, with args beginning with:
Calling the renamed command, however, will be successful. It is not case-sensitive:
- asc12_config get requirepass
Output1) "requirepass"
2) "your_redis_password"
In this quickstart tutorial, you installed and configured Redis, validated that your Redis installation is functioning correctly, and used its built-in security features to make it less vulnerable to attacks from malicious actors.
Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!
pam_unix(sudo:auth): Couldn’t open /etc/securetty: No such file or directory