Best Steps to Install and Configure Redis on Ubuntu 22.04

In this tutorial, we want to teach you to Install and Configure Redis on Ubuntu 22.04. Redis is a fast in-memory database and cache, open source under a BSD license, written in C, and optimized for speed. Redis’ name comes from “REmote DIctionary Server”.

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. It also provides many other data structures and features for approximate counting, geolocation, and stream processing.

Now you can proceed to the following steps to Install and Configure Redis on Ubuntu 22.04. Also, you will learn to secure your Redis server.

Steps To Install and Configure Redis on Ubuntu 22.04

To Install and Configure Redis on Ubuntu 22.04, you must log in to your server as a non-root user with sudo privileges. To do this, you can follow our guide on Initial Server Setup with Ubuntu 22.04.

Install Redis on Ubuntu 22.04

First, you need to update your local package index with the command below:

sudo apt update

Add PPA Repository

Then, you need to add the “redislabs” PPA repository to your server:

sudo add-apt-repository ppa:redislabs/redis

When you are done, you can use the command below to install Redis:

sudo apt-get install redis

When your installation is completed, verify it by checking its version:

redis-server -v
Output
Redis server v=7.0.5 sha=00000000:0 malloc=jemalloc-5.2.1 bits=64 build=68bf11aad5b039df

Start and Enable Redis Server

At this point, you need to start and enable your Redis server to start on boot. To do this, run the command below:

sudo systemctl enable --now redis-server

Then, confirm that your Redis server is active and running on Ubuntu 22.04:

sudo systemctl status redis
Output
● redis-server.service - Advanced key-value store
     Loaded: loaded (/lib/systemd/system/redis-server.service; enabled; vendor >
     Active: active (running) since Wed 2022-11-02 08:24:59 UTC; 13min ago
       Docs: http://redis.io/documentation,
             man:redis-server(1)
   Main PID: 3609 (redis-server)
     Status: "Ready to accept connections"
      Tasks: 5 (limit: 4575)
...

Here you can test Redis’s functionality with the following command:

redis-cli ping

In your output you should see:

Output
PONG

Configure Redis on Ubuntu 22.04

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, here we use vi:

sudo vi /etc/redis/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 to bind it to a private IP address.

. . .
bind your_private_ip

After you make this change, save and close the file.

Then, restart Redis to apply this change with the following command:

sudo systemctl restart redis

Now run the command below to check that this change has gone into effect:

sudo netstat -lnp | grep redis

In your output you will see something similar to this:

Output
tcp        0      0 127.0.0.1:6379          0.0.0.0:*               LISTEN      4020/redis-server 1
tcp6       0      0 ::1:6379                :::*                    LISTEN      4020/redis-server 1

It means that the Redis server program is bound to localhost (127.0.0.1). If you see another IP address that you haven’t set in the Redis configuration file, check the file again and restart the Redis again.

Let’s see how to configure Redis to only be accessible with a strong password.

Configure a Redis password on Ubuntu 22.04

You can configure a Redis password directly from the Redis configuration file. It will enable one of its two built-in security features, the auth command, which requires clients to authenticate to access the database.

Open the file again with your favorite text editor:

sudo vi /etc/redis/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 strong password

When you are done, save and close the file.

To apply this change, restart Redis on Ubuntu 22.04 with the following command:

sudo systemctl restart redis

Test Redis Server password

To test that the password that you have set works correctly, open the Redis client 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

At this point, you can rename Redis commands to protect Redis from malicious actors.

Rename Redis Dangerous commands

At this point, you have learned to Install and Configure Redis on Ubuntu 22.04. 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/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 a 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 Ubuntu 22.04 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/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.

Now you have completed this guide on Install and Configure Redis on Ubuntu 22.04.

Conclusion

At this point, you have learned to Install and Configure Redis on Ubuntu 22.04. Also, you have learned to rename the dangerous commands for more security and protection. Hope you enjoy it.

You may like these articles:

Install and Configure Redis on Rocky Linux 8

Install and Secure Redis on AlmaLinux 8

Newsletter Updates

Enter your email address below and subscribe to our newsletter

Stay informed and not overwhelmed, subscribe now!