Introduction

Redis is an open-source, in-memory data structure store which excels at caching. A non-relational database, Redis is known for its flexibility, performance, scalability, and wide language support.

Redis is designed for trusted network environments. By default, it assumes access is restricted at the network layer. Without proper configuration, an exposed Redis instance can allow data manipulation or deletion without authentication.

Redis does, however, have a few security features that include a basic unencrypted password and command renaming and disabling. This tutorial provides instructions on how to configure these security features, and also covers a few other settings that can boost the security of a standalone Redis installation on CentOS 7.

Compatibility: This tutorial was originally written for CentOS 7. CentOS 7 has reached end of life. The steps remain applicable to CentOS Stream 9 and other RHEL-based distributions, but you should use a supported operating system for production deployments.

Note that this guide does not address situations where the Redis server and the client applications are on different hosts or in different data centers. Installations where Redis traffic has to traverse an insecure or untrusted network will require a different set of configurations, such as setting up an SSL proxy or a VPN between the Redis machines.

Prerequisites

redis illustration for: Prerequisites

To follow along with this tutorial, you will need:

  • Firewalld installed and configured using this guide, up to and including the “Turning on the Firewall” step.

With those prerequisites in place, we are ready to install Redis and perform some initial configuration tasks.

Step 1 — Installing Redis

Verify your operating system version before installing packages:

				
					
cat /etc/os-release

				
			

Before we can install Redis, we must first add Extra Packages for Enterprise Linux (EPEL) repository to the server’s package lists. EPEL is a package repository containing a number of open-source add-on software packages, most of which are maintained by the Fedora Project.

CentOS 7 uses yum as its package manager; CentOS Stream 9 and later use dnf. Replace yum with dnf in the following commands if you are on CentOS Stream 9 or another dnf-based distribution.

We can install EPEL using yum:

				
					
sudo yum install epel-release

				
			

Once the EPEL installation has finished you can install Redis, again using yum:

				
					
sudo yum install redis -y

				
			

This may take a few minutes to complete. After the installation finishes, start the Redis service:

				
					
sudo systemctl start redis.service

				
			

If you’d like Redis to start on boot, you can enable it with the enable command:

				
					
sudo systemctl enable redis.service

				
			

You can check Redis's status by running the following. On some distributions, verify the service name using systemctl list-units | grep redis.

				
					
sudo systemctl status redis.service

				
			
				
					
Output:

● redis.service - Redis persistent key-value database

 Loaded: loaded (/usr/lib/systemd/system/redis.service; disabled; vendor preset: disabled)

 Drop-In: /etc/systemd/system/redis.service.d

 └─limit.conf

 Active: active (running) since Thu 2018-03-01 15:50:38 UTC; 7s ago

 Main PID: 3962 (redis-server)

 CGroup: /system.slice/redis.service

 └─3962 /usr/bin/redis-server 127.0.0.1:6379

				
			

Once you’ve confirmed that Redis is indeed running, test the setup with this command:

				
					
redis-cli ping

				
			

This should print PONG as the response. If this is the case, it means you now have Redis running on your server and we can begin configuring it to enhance its security.

Understanding Redis Persistence (Optional but Recommended)

Redis supports two persistence mechanisms:

  • RDB (snapshotting): Saves point-in-time snapshots of your dataset at configured intervals.
  • AOF (Append Only File): Logs every write operation for improved durability.

By default, most CentOS-based installations enable RDB snapshots. For production workloads where data durability is important, review the save and appendonly directives in /etc/redis.conf to ensure the persistence mode aligns with your recovery objectives.

For caching-only deployments, persistence may be unnecessary and can be disabled to reduce disk I/O.

Step 2 — Binding Redis and Securing it with a Firewall

An effective way to safeguard Redis is to secure the server it’s running on. You can do this by ensuring that Redis is bound only to either localhost or to a private IP address and that the server has a firewall up and running.

However, if you chose to set up a Redis cluster using this tutorial, then you will have updated the configuration file to allow connections from anywhere, which is not as secure as binding to localhost or a private IP.

To remedy this, open the Redis configuration file for editing:

				
					
sudo vi /etc/redis.conf

				
			

Locate the line beginning with bind and make sure it’s uncommented:

				
					
/etc/redis.conf

bind 127.0.0.1

				
			

If you need to bind Redis to another IP address (as in cases where you will be accessing Redis from a separate host) we strongly encourage you to bind it to a private IP address. Binding to a public IP address increases the exposure of your Redis interface to outside parties.

Common Misconfiguration: Setting bind 0.0.0.0 without proper firewall restrictions exposes Redis to the public internet. Redis instances exposed without authentication are frequently scanned and exploited. Only allow remote access if authentication and network-level controls are properly configured.

				
					
/etc/redis.conf

bind your_private_ip

				
			

If you’ve followed the prerequisites and installed firewalld on your server and you do not plan to connect to Redis from another host, then you do not need to add any extra firewall rules for Redis. After all, any incoming traffic will be dropped by default unless explicitly allowed by the firewall rules. Since a default standalone installation of Redis server is listening only on the loopback interface (127.0.0.1 or localhost), there should be no concern for incoming traffic on its default port.

If, however, you do plan to access Redis from another host, you will need to make some changes to your firewalld configuration using the firewall-cmd command. Again, 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.

To begin, add a dedicated Redis zone to your firewalld policy:

				
					
sudo firewall-cmd --permanent --new-zone=redis

				
			

Then, specify which port you’d like to have open. Redis uses port 6379 by default:

				
					
sudo firewall-cmd --permanent --zone=redis --add-port=6379/tcp

				
			

Next, specify any private IP addresses which should be allowed to pass through the firewall and access Redis:

				
					
sudo firewall-cmd --permanent --zone=redis --add-source=client_server_private_IP

				
			

After running those commands, reload the firewall to implement the new rules:

				
					
sudo firewall-cmd --reload

				
			

Under this configuration, when the firewall sees a packet from your client's IP address, it will apply the rules in the dedicated Redis zone to that connection. All other connections will be processed by the default public zone. 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.

If you chose to set up a firewall using Iptables, you will need to grant your secondary hosts access to the port Redis is using with the following commands:

				
					
sudo iptables -A INPUT -i lo -j ACCEPT

sudo iptables -A INPUT -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT

sudo iptables -A INPUT -p tcp -s client_servers_private_IP/32 --dport 6379 -m conntrack --ctstate NEW,ESTABLISHED -j ACCEPT

sudo iptables -P INPUT DROP

				
			

Make sure to save your Iptables firewall rules using the mechanism provided by your distribution. You can learn more about Iptables by taking a look at our Iptables essentials guide.

Keep in mind that using either firewall tool will work. What’s important is that the firewall is up and running so that unknown individuals cannot access your server. In the next step, we will configure Redis to only be accessible with a strong password.

Step 3 — Configuring a Redis Password

If you installed Redis using the How To Configure a Redis Cluster on CentOS 7 tutorial, you should have configured a password for it. At your discretion, you can make a more secure password now by following this section. If you haven’t set up a password yet, instructions in this section show how to set the database server password.

The requirepass directive enforces client authentication. Without it, any process that can reach the Redis port can execute commands, including destructive operations such as FLUSHALL.

Redis does not encrypt traffic by default; for remote deployments, use TLS, an SSL proxy, or a VPN to protect data in transit. Like the bind setting, the password is configured directly in Redis's configuration file, /etc/redis.conf. Reopen that file:

				
					
sudo vi /etc/redis.conf

				
			

Scroll to the SECURITY section and look for a commented directive that reads:

				
					
/etc/redis.conf

# requirepass foobared

				
			

Uncomment it by removing the #, and change foobared to a very strong password of your choosing. Rather than make up a password yourself, you may use a tool like apg or pwgen to generate one. If you don't want to install an application just to generate a password, though, you may use the command below.

Note that entering this command as written will generate the same password every time. To create a password different from the one that this would generate, change the word in quotes to any other word or phrase.

				
					
echo "your-secret-phrase" | sha256sum

				
			

Though the generated password will not be pronounceable, it is a very strong and very long one, which is exactly the type of password required for Redis. After copying and pasting the output of that command as the new value for requirepass, it should read:

				
					
/etc/redis.conf

requirepass password_copied_from_output

				
			

If you prefer a shorter password, use the output of the command below instead. Again, change the word in quotes so it will not generate the same password as this one:

				
					
echo "your-secret-phrase" | sha1sum

				
			

After setting the password, save and close the file then restart Redis:

				
					
sudo systemctl restart redis.service

				
			

To test that the password works, access the Redis command line:

				
					
redis-cli

				
			

The following is 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 as we have not yet been authenticated, so Redis returns an error.

				
					
Output:

(error) NOAUTH Authentication required.

				
			

The following command authenticates with the password specified in the Redis configuration file.

				
					
auth your_redis_password

				
			

Redis will acknowledge that we have been authenticated:

				
					
Output:

OK

				
			

After that, running the previous command again should be successful:

				
					
set key1 10

				
			
				
					
Output:

OK

				
			

The get key1 command queries Redis for the value of the new key.

				
					
get key1

				
			
				
					
Output:

"10"

				
			

This last command exits redis-cli. You may also use exit:

				
					
quit

				
			

It should now be very difficult for unauthorized users to access your Redis installation. Please note, though, that without SSL or a VPN the unencrypted password will still be visible to outside parties if you’re connecting to Redis remotely.

Redis does not encrypt traffic by default. If Redis must be accessed across networks, enable TLS support (Redis 6 and later) or use a TLS proxy such as stunnel. Avoid transmitting Redis credentials over unencrypted connections.

Next, we'll look at renaming Redis commands to further protect Redis from malicious actors.

Step 4 — Renaming Dangerous Commands

The other security feature built into Redis allows you to rename or completely disable certain commands that are considered dangerous. When run by unauthorized users, such commands can be used to reconfigure, destroy, or otherwise wipe your data. Some of the commands that are known to be dangerous include:

  • FLUSHDB
  • FLUSHALL
  • KEYS
  • PEXPIRE
  • DEL
  • CONFIG
  • SHUTDOWN
  • BGREWRITEAOF
  • BGSAVE
  • SAVE
  • SPOP
  • SREM
  • RENAME
  • DEBUG

This is not a comprehensive list, but renaming or disabling all of the commands in that list is a good starting point.

Whether you disable or rename a command is site-specific. If you know you will never use a command that can be abused, then you may disable it. Otherwise, you should rename it instead.

Like the authentication password, renaming or disabling commands is configured in the SECURITY section of the /etc/redis.conf file. To enable or disable Redis commands, open the configuration file for editing one more time:

				
					
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 check the commands for yourself and determine how they might be misused at redis.io/commands.

To disable or kill a command, simply rename it to an empty string, as shown below:

				
					
/etc/redis.conf

# 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 like in the examples below. Renamed commands should be difficult for others to guess, but easy for you to remember:

				
					
/etc/redis.conf

rename-command SHUTDOWN SHUTDOWN_MENOT

rename-command CONFIG ASC12_CONFIG

				
			

Save your changes and close the file, and then apply the change by restarting Redis:

				
					
sudo systemctl restart redis.service

				
			

To test the new command, enter the Redis command line:

				
					
redis-cli

				
			

Authenticate yourself using the password you defined earlier:

				
					
auth your_redis_password

				
			
				
					
Output:

OK

				
			

Assuming that you renamed the CONFIG command to ASC12_CONFIG, attempting to use the config command should fail.

				
					
config get requirepass

				
			
				
					
Output:

(error) ERR unknown command 'config'

				
			

Calling the renamed command should be successful (it's case-insensitive):

				
					
asc12_config get requirepass

				
			
				
					
Output:

1) "requirepass"

2) "your_redis_password"

				
			

Finally, you can exit from redis-cli:

				
					
exit

				
			

Note that if you're already using the Redis command line and then restart Redis, you'll need to re-authenticate. Otherwise, you'll get this error if you type a command:

				
					
Output:

NOAUTH Authentication required.

				
			

[warning]

Regarding renaming commands, there's a cautionary statement at the end of the SECURITY section in the /etc/redis.conf file, which reads:

>

				
					
/etc/redis.conf

. . .



# Please note that changing the name of commands that are logged into the

# AOF file or transmitted to slaves may cause problems.



. . .

				
			

That means if the renamed command is not in the AOF file, or if it is but the AOF file has not been transmitted to slaves, then there should be no problem. Keep that in mind as you're renaming commands. The best time to rename a command is when you're not using AOF persistence or right after installation (that is, before your Redis-using application has been deployed).

When you're using AOF and dealing with a master-slave installation, consider this answer from the project's GitHub issue page. The following is a reply to the author's question:

> The commands are logged to the AOF and replicated to the slave the same way they are sent, so if you try to replay the AOF on an instance that doesn't have the same renaming, you may face inconsistencies as the command cannot be executed (same for slaves).

The best way to handle renaming in cases like that is to make sure that renamed commands are applied to all instances of master-slave installations.

Step 5 — Setting Data Directory Ownership and File Permissions

In this step, we'll consider a couple of ownership and permissions changes you can make to improve the security profile of your Redis installation. This involves making sure that only the user that needs to access Redis has permission to read its data. That user is, by default, the redis user.

You can verify this by grep-ing for the Redis data directory in a long listing of its parent directory. The command and its output are given below.

				
					
ls -l /var/lib | grep redis

				
			
				
					
Output:

drwxr-xr-x 2 redis redis 4096 Aug 6 09:32 redis

				
			

You can see that the Redis data directory is owned by the redis user, with secondary access granted to the redis group. This ownership setting is secure, but the folder’s permissions (which are set to 755) are not. To ensure that only the Redis user has access to the folder and its contents, change the permissions setting to 770:

				
					
sudo chmod 770 /var/lib/redis

				
			

The other permission you should change is that of the Redis configuration file. By default, it has a file permission of 644 and is owned by root, with secondary ownership by the root group:

				
					
ls -l /etc/redis.conf

				
			
				
					
Output:

-rw-r--r-- 1 root root 30176 Jan 14 2014 /etc/redis.conf

				
			

That permission (644) is world-readable. This presents a security issue as the configuration file contains the unencrypted password you configured in Step 3, meaning we need to change the configuration file’s ownership and permissions. Ideally, it should be owned by the redis user, with secondary ownership by the redis group. To do that, run the following command:

				
					
sudo chown redis:redis /etc/redis.conf

				
			

Then change the permissions so that only the owner of the file can read and/or write to it:

				
					
sudo chmod 600 /etc/redis.conf

				
			

You may verify the new ownership and permissions using:

				
					
ls -l /etc/redis.conf

				
			
				
					
Output:

total 40

-rw------- 1 redis redis 29716 Sep 22 18:32 /etc/redis.conf

				
			

Finally, restart Redis:

				
					
sudo systemctl restart redis.service

				
			

Congratulations, your Redis installation should now be more secure!

Conclusion

Keep in mind that once someone is logged in to your server, it's very easy to circumvent the Redis-specific security features we've put in place. This is why the most important security feature covered in this tutorial is the firewall, as that prevents unknown users from logging into your server in the first place.

If you're attempting to secure Redis communication across an untrusted network you'll have to employ an SSL proxy, as recommended by Redis developers in the official Redis security guide.