Share your love
How To Install and Secure Redis on AlmaLinux 8

In this article, we want to teach you How To Install and Secure Redis on AlmaLinux 8.
Redis stands for Remote Dictionary Server. It is an open-source and fast in-memory database and cache under a BSD license, written in C and optimized for speed.
It is often called a data structure server because its core data types are similar to those found in programming languages. Like strings, lists, dictionaries (or hashes), sets, and sorted sets.
Also, it provides many other data structures and features for approximate counting, geolocation, and stream processing.
How To Install and Secure Redis on AlmaLinux 8
To complete this article, you need to log in to your server as a non-root user with sudo privileges and a basic setup for the firewall. To do this you can check the Initial server setup article for AlmaLinux 8.
When you are done with this, let’s start to install and secure Redis on AlmaLinux 8.
Install Redis on AlmaLinux 8
You can install Redis and its dependencies with the DNF package manager on AlmaLinux 8.
To install Redis run the following command:
sudo dnf install redis
Then, you need to make some configuration changes in the Redis configuration file.
Open the file with your favorite text editor, here we use vi:
sudo vi /etc/redis.conf
Inside the file, search for the supervised directive. This directive allows you to declare an init system to manage Redis as a service, providing you with more control over its operation.
By default it is set to no, you have to change it 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
. . .
When you are finished, save and close the file.
Here you need to start the Redis service on AlmaLinux 8 with the following command:
sudo systemctl start redis.service
If you want to Redis start at boot, enable it with the following command:
sudo systemctl enable redis
To check that Redis is active and running you can use the following command:
sudo systemctl status redis
In your output you should see:
Output redis.service - Redis persistent key-value database Loaded: loaded (/usr/lib/systemd/system/redis.service; enabled; vendor prese> Drop-In: /etc/systemd/system/redis.service.d └─limit.conf Active: active (running) since Mon 2021-10-18 08:39:41 EDT; 2min 32s ago Main PID: 52874 (redis-server) Tasks: 4 (limit: 11409) Memory: 6.5M CGroup: /system.slice/redis.service └─52874 /usr/bin/redis-server 127.0.0.1:6379
Now you can test Redis’s functionality with the following command:
redis-cli ping
In your output you should see:
Output
PONG
It means that you have Redis running on your AlmaLinux 8 and you start to configure it to raise its security.
Configure and Secure Redis on AlmaLinux 8
An effective way to protect Redis is to secure the server it’s running on. To do this you can be sure that Redis is limited only to localhost or to a private IP address and also that the server has a firewall up and running.
Open the Redis configuration file with your favorite text editor again:
sudo vi /etc/redis.conf
Inside the file, search for the “bind” line and uncomment it by removing the # sign at the beginning of the line:
. . . bind 127.0.0.1
Note: If you need to bind Redis to another IP address, it’s strongly recommended that to bind it to a private IP address.
. . .
bind your_private_ip
After you made this change, save and close the file.
We assumed that you installed and enabled firewalld in the requirements part.
You should only allow access to your Redis server from your hosts by using their private IP addresses in order to limit the number of hosts your service is exposed to.
First, you need to add a dedicated Redis zone to your firewalld policy with the following command:
sudo firewall-cmd --permanent --new-zone=redis
Redis uses port 6379 by default. you need to open it through the firewall with the following command:
sudo firewall-cmd --permanent --zone=redis --add-port=6379/tcp
Then, specify any private IP addresses which should be allowed to pass through the firewall and access Redis with the command below:
sudo firewall-cmd --permanent --zone=redis --add-source=client_server_private_IP
To apply these changes, reload the firewall with the following command:
sudo firewall-cmd --reload
Note: The services in the default zone apply to every connection, not just those that don’t match explicitly, so you don’t need to add other services (e.g. SSH) to the Redis zone because those rules will be applied to that connection automatically.
Let’s see how to configure Redis to only be accessible with a strong password.
Configure a Redis Password on AlmaLinux 8
You can configure a Redis Password directly in the Redis configuration file.
Open the file again with the following command:
sudo vi /etc/redis.conf
Find the Security section and search for the “requirepass foobared” directive. Uncomment it by removing the # and replacing the foobared phrase with a very strong password of your choosing.
requirepass foobared
When you are finished, save and close the file.
Then restart Redis to apply these changes with the following command:
sudo systemctl restart redis
To test that the password that you have set works correctly, open the Redis client on AlmaLinux 8 with the following command:
redis-cli
The first command tries to set a key to a value before authentication:
127.0.0.1:6379> set key1 10
At this point, Redis returns an error, because you have not yet authenticated:
127.0.0.1:6379> NOAUTH Authentication required.
Use the following command to authenticate with the password you have set in the Redis configuration file:
127.0.0.1:6379> auth your_redis_password
After entering your Redis password, in your output, you will see OK.
Then run the previous command, it should be working now:
127.0.0.1:6379> set key1 10
In your output, you should see OK.
Now use the get key1 command to query Redis for the value of the new key:
127.0.0.1:6379> get key1
Output
"10"
Exit from the Redis client with the following command:
127.0.0.1:6379> quit
Note: At this point, it should be very difficult for unauthorized users to access your Redis installation. Remember that if you are using the Redis client and then restart Redis, you’ll need to re-authenticate. Also, please note that without SSL or a VPN, the unencrypted password will still be visible to outside parties if you’re connecting to Redis remotely.
Additionally, you can rename Redis commands to protect Redis from malicious actors.
Rename Redis Dangerous commands
For more security, Redis allows you to rename or completely disable certain commands that are considered dangerous. like: FLUSHDB, FLISHALL, KEYS, CONFIG, DEBUG, SHUTDOWN, SAVE, STOP, RENAME, etc.
If you know that you will never use a command that can be abused, you can disable it. Otherwise, you should rename it instead.
To enable or disable Redis commands, open the Redis configuration file and go to the Security section:
sudo vi /etc/redis.conf
Note: These are examples. You should choose to disable or rename the commands that make sense for you. You can learn more about Redis’s commands and determine how they might be misused at redis.io/commands.
here you can disable or kill command by renaming it to an empty string like this:
# 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 ""
You can rename a command by giving it another name like this:
# 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 "" rename-command SHUTDOWN SHUTDOWN_ORCA rename-command CONFIG ORCA_CONFIG
When you are finished, save and close the file.
To apply the changes, restart Redis on AlmaLinux 8 with the following command:
sudo systemctl restart redis.service
Now you can open the Redis client to test your new commands:
redis-cli
Then, authenticate yourself with the password that you have set:
127.0.0.1:6379> auth your_redis_password
We assumed that you rename the config command to orca_config. If you use config you will get an error:
127.0.0.1:6379> config get requirepass
Output
(error) ERR unknown command `config`
Now use the renamed command instead:
127.0.0.1:6379> orca_config get requirepass
In your output you will see:
Output
1) "requirepass"
2) "your_redis_password"
Now you can exit from the Redis client with the following command:
127.0.0.1:6379> exit
Warning: at the end of the Security section in the /ect/redis.conf file, there is a warning statement which is:
. . . # Please note that changing the name of commands that are logged into the # AOF file or transmitted to slaves may cause problems. . . .
This means if the renamed command is not in the AOF file, or if it is but the AOF file has not been transmitted to replicas, then there should be no problem. The best time to rename the command is when you’re not using AOF persistence or right after installation.
Set Data directory Ownership and File Permissions
In this step, you need to set ownership and make some permission changes to improve the security profile of your Redis installation on AlmaLinux 8. With this, you will be sure that only the Redis user has permission to read its data.
Run the following command to see the Redis data directory ownership and its permissions:
ls -l /var/lib | grep redis
Output drwxr-x--- 2 redis redis Oct 18 10:20 redis
As you can see, the Redis data directory is owned by the Redis user, with secondary access granted to the Redis group.
If your Redis directory has insecure permissions, you can use the following command to change the file permissions settings:
sudo chmod 770 /var/lib/redis
Then, you need to change the Redis configuration file permissions. By default, it is owned by the root and secondary ownership by the root group.
ls -l /etc/redis.conf
Output -rw-r----- 1 redis root 62345 Oct 18 10:19 /etc/redis.conf
It means that the Redis configuration file is readable only by the Redis user and the root group. you should set the file to readable by the Redis user and the Redis group. To do this run the following command:
sudo chown redis:redis /etc/redis.conf
Now you need to change the permissions so that only the owner of the file can read and write to it:
sudo chmod 600 /etc/redis.conf
Verify the new changes with the following command:
ls -l /var/lib | grep redis
Output drwxrwx--- 2 redis redis Oct 18 10:20 redis
ls -l /etc/redis.conf
Output -rw------- 1 redis redis 62345 Oct 18 10:19 /etc/redis.conf
Then, restart Redis to apply these changes:
sudo systemctl restart redis
Finally, your Redis installation on AlmaLinux 8 has been secured.
Conclusion
At this point, you learn How To Install and Secure Redis on AlmaLinux 8. Remember that when someone logged in to your server, it’s very easy to find a way around the Redis security features you’ve put in place. because of that, the most security feature is the firewall, to prevent unknown users from logging into your server in the first place.
Hope you enjoy it.
May this article about How To Install and Secure Redis on Debian 11 be useful for you.