How to Install Redis on Ubuntu
Contents
Step 1: Install Redis
sudo apt update
sudo apt install redis-server
Configure Redis
sudo nano /etc/redis/redis.conf
find the line specifying the supervised directive. By default, this line is set to no. However, to manage Redis as a service, set the supervised directive to systemd (Ubuntu’s init system).
sudo systemctl restart redis.service
Step 3: Verify Redis Installation
sudo systemctl status redis
The output should display the service is active (and running), as in the image below.
Check Redis Connection
redis-cli
ping
output should respond with PONG
Test Setting up Key-Value Pairs
set key1 "You have successfully set up a key-value pair!"
Once you hit Enter, the prompt responds with OK.
get key1
and
quit
Step 4: Secure Redis
Set Up Redis Authentication
sudo nano /etc/redis/redis.conf
locate the requirepass directive under the SECURITY section and uncomment it (by removing #).
Once you have uncommented the line, replace foobared with the password of your choice.
sudo systemctl restart redis.service
Use the command:
auth [your_password]
Bind Redis to Localhost
sudo nano /etc/redis/redis.conf
Scroll down and find the NETWORK section in the file. Then, uncomment the bind 127.0.0.1 ::1 line (by removing #), as in the image below.
sudo systemctl restart redis
Rename or Kill Dangerous Commands
Another way to protect your data is to disable specific commands or rename them, so they are unguessable. This is a useful security feature that can restrict normal users from using commands that could harm the system.
To disable or rename such commands, open the Redis configuration file:
sudo nano /etc/redis/redis.conf
Locate the SECURITY section and scroll down to the #Command renaming line. There you can find examples on how to rename or kill commands.
For example, to rename the CONFIG command, add the line:
rename-command CONFIG [new_command_name]
In this example, the config command is renamed to sys_admin_config_836 (something a normal user would not be able to guess).
To remove certain commands, you can disable (kill) them. To do this, rename the dangerous command into an empty string.
Therefore, if you want to disable the config command, add the line:
rename-command CONFIG ""