Best Steps To Install Redis on Ubuntu 24/22

Posted on

Best Steps To Install Redis on Ubuntu 24/22

In this tutorial, we want to teach you to Install Redis on Ubuntu 24.04 and 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 on the Orcacore website to Install and Configure Redis on Ubuntu 24.04 and Ubuntu 22.04. Also, you will learn to secure your Redis server by renaming the Redis dangerous commands.

Steps To Install and Configure Redis on Ubuntu 24/22

To set up Redis on Ubuntu 24.04 and Ubuntu 22.04, you must log in to your server as a non-root user with sudo privileges. For example, you can follow our guide on Initial Server Setup with Ubuntu 22.04.

1. Install Redis on Ubuntu

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

sudo apt update

Add Redis PPA Repository on Ubuntu

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

sudo add-apt-repository ppa:redislabs/redis

Press Enter to continue. When you are done, you can use the command below to install Redis:

sudo apt-get install redis -y

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

redis-server -v
Install Redis on Ubuntu

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 commands below:

# sudo systemctl start redis-server
# sudo systemctl enable redis-server

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

sudo systemctl status redis

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

redis-cli ping

In your output you should see:

**Output**
PONG

2. Configure Redis Networking on Ubuntu

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

It should look like this:

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:

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.

3. Configure Redis Password on Ubuntu

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 **StrongPassword**

When you are done, save and close the file.

To apply this change, restart Redis on Ubuntu 24.04 or 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.

4. Rename Redis Dangerous commands

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

Conclusion

At this point, you have learned to Install Redis on Ubuntu 24.04 and Ubuntu 22.04. Also, you have learned to rename the dangerous commands for more security and protection. Hope you enjoy it. Please subscribe to us on Facebook, Instagram, and YouTube.

You may like these articles:

Install and Configure Redis on Rocky Linux 8

Install and Secure Redis on AlmaLinux 8

Set up Redis on Rocky Linux 9

Setting Up Redis on Debian 12

Alternative Solutions for Installing and Securing Redis on Ubuntu

While the previous steps detailed a reliable method for installing and securing Redis on Ubuntu, here are two alternative approaches to consider:

1. Using Docker for Installation and Containerization

Docker provides a containerization solution, simplifying the installation and management of applications like Redis. This approach offers benefits like isolation, portability, and ease of deployment.

Explanation:

Using Docker, you create a containerized environment for Redis, which isolates it from the host operating system. This eliminates dependency conflicts and makes upgrading or migrating Redis instances easier. Docker Hub provides pre-built Redis images, streamlining the installation process.

Steps:

  1. Install Docker: If Docker is not already installed, install it using apt:

    sudo apt update
    sudo apt install docker.io -y
    sudo systemctl start docker
    sudo systemctl enable docker
  2. Pull the Redis Image: Download the official Redis image from Docker Hub:

    sudo docker pull redis
  3. Run the Redis Container: Create and start a Redis container with desired configurations. For example, to expose the default Redis port (6379) and mount a local volume for data persistence:

    sudo docker run -d -p 6379:6379 --name my-redis -v redis_data:/data redis redis-server --requirepass "StrongPassword"
    • -d: Runs the container in detached mode (background).
    • -p 6379:6379: Maps port 6379 on the host to port 6379 in the container.
    • --name my-redis: Assigns the name "my-redis" to the container.
    • -v redis_data:/data: Creates a named volume "redis_data" and mounts it to the /data directory in the container (where Redis stores its data), ensuring data persistence.
      redis redis-server --requirepass "StrongPassword": starts the redis server and sets the password to the redis server.
  4. Verify the Container: Check that the container is running:

    sudo docker ps
  5. Connect to Redis: Access the Redis server inside the container using redis-cli. To authenticate, use the password you set in the docker run command:

    sudo docker exec -it my-redis redis-cli
    AUTH StrongPassword
    PING

2. Using UFW Firewall for Enhanced Security

Instead of solely relying on binding Redis to localhost or a private IP and password authentication, using a firewall like UFW (Uncomplicated Firewall) provides an additional layer of security.

Explanation:

UFW acts as a gatekeeper, controlling network traffic to and from your server. By configuring UFW rules, you can explicitly allow connections to the Redis port (default 6379) only from trusted IP addresses or networks. This prevents unauthorized access even if the Redis instance is exposed to the public internet (though it’s still highly recommended to bind to localhost or a private IP whenever possible).

Steps:

  1. Install UFW: If UFW is not already installed, install it using apt:

    sudo apt update
    sudo apt install ufw -y
  2. Enable UFW: Enable the firewall:

    sudo ufw enable

    You may get a warning that enabling the firewall might disrupt existing SSH connections. If you are accessing the server remotely via SSH, you need to allow SSH connections before enabling UFW. The default SSH port is 22.

    sudo ufw allow 22
    sudo ufw enable
  3. Allow Redis Traffic from Specific IPs (Example): Allow connections to Redis only from a specific IP address (e.g., 192.168.1.100):

    sudo ufw allow from 192.168.1.100 to any port 6379
  4. Deny Redis Traffic from All Other IPs (Default): By default, UFW denies all incoming connections. Ensure this is the case to prevent access from unauthorized IPs. If you have changed the default policy, explicitly deny all other incoming traffic to port 6379:

    sudo ufw deny 6379
  5. Check UFW Status: Verify that the rules are correctly configured:

    sudo ufw status

    The output should show the allowed traffic from the specified IP address and denied traffic from all other sources to port 6379.

These alternative approaches provide different ways to install and secure Redis on Ubuntu 24/22. Using Docker simplifies deployment and management, while UFW provides a robust firewall solution for enhanced security. Depending on your specific requirements and environment, these methods can be combined or used independently to achieve optimal results when you Install Redis on Ubuntu 24/22. You now have alternative ways to Install Redis on Ubuntu 24/22. These steps are reliable ways to Install Redis on Ubuntu 24/22.