Sunday, November 22, 2026
TR
LinuxOpen Source

How to Install Redis on Ubuntu 22.04

How to Install Redis on Ubuntu 22.04
Contents
  1. Step 1 — Installing and Configuring Redis
  2. Step 2 — Testing Redis
  3. Step 3 — Binding to localhost
  4. Step 4 — Configuring a Redis Password
  5. Step 5 — Renaming Dangerous Commands

Step 1 — Installing and Configuring Redis

We will use the APT package manager to install Redis from the official Ubuntu repositories. As of this writing, the version available in the default repositories is: 6.0.16.

Start by updating your local apt package cache :

sudo apt update

Then install Redis by typing:

sudo apt install redis-server

This will download and install Redis and its dependencies. Following this, there is an important configuration change to make in the Redis configuration file, which was generated automatically during the installation.

Open this file with your preferred text editor:

sudo nano /etc/redis/redis.conf

Inside the file, find the supervised directive. This directive allows you to declare an init system to manage Redis as a service, giving you more control over its operation. By default, the supervised directive is not enabled. Because you are running Ubuntu, which uses the systemd no init system, change the default value to the following: systemd/etc/redis/redis.conf

. . .

# 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

. . .

This is the only change you need to make to the Redis configuration file at this point, so save and close it when you are finished. If you used nano to edit the file, do so by pressing CTRL + X, Y, then ENTER.

Next, restart the Redis service to reflect the changes you made to the configuration file:

sudo systemctl restart redis.service

With that, you have installed and configured Redis, and it is running on your machine. Before you start using it, though, it is wise to check whether Redis is functioning correctly.

Step 2 — Testing Redis

As with any newly installed software, it is a good idea to make sure Redis is working as expected before making any changes to its configuration. In this step, we will go over a few ways to check whether Redis is working correctly.

Start by checking whether the Redis service is running:

sudo systemctl status redis

If it is running without any errors, this command will produce output similar to the following:

Output● redis-server.service - Advanced key-value store
     Loaded: loaded (/lib/systemd/system/redis-server.service; enabled; vendor preset: enabled)
     Active: active (running) since Wed 2022-06-28 20:40:52 UTC; 4s ago
       Docs: http://redis.io/documentation,
             man:redis-server(1)
   Main PID: 2899 (redis-server)
     Status: "Ready to accept connections"
      Tasks: 5 (limit: 2327)
     Memory: 2.5M
        CPU: 65ms
     CGroup: /system.slice/redis-server.service
             └─2899 "/usr/bin/redis-server 127.0.0.1:6379
. . .

This output shows that Redis is running and is already enabled, meaning it is set to start every time the server boots.

Note: This setting is desirable for many common Redis use cases. However, if you prefer to start Redis manually every time your server boots, you can configure that with the following command:

sudo systemctl disable redis

To test that Redis is working properly, connect to the server using redis-cli, the Redis command-line client:

redis-cli

At the prompt that follows, test the connection with the ping command:

ping
Output
PONG

This output confirms that the server connection is still alive. Next, check that you can set keys by running:

set test "It's working!"
Output
OK

Retrieve the value by typing:

get test

Assuming everything is working, you will be able to retrieve the value you stored:

Output"It's working!"

After confirming that you can retrieve the value, exit the Redis prompt to get back to the shell:

exit

As a final test, we will check whether Redis can persist data even after it is stopped or restarted. To do this, first restart the Redis instance:

sudo systemctl restart redis

Then connect to the command-line client again:

redis-cli

And confirm that your test value is still available

get test

The value of your key should still be accessible:

Output"It's working!"

When you are finished, exit back to the shell again:

exit

With that, your Redis installation is fully operational and ready for use. However, some of the default configuration settings are insecure and provide malicious actors with opportunities to attack and gain access to your server and its data. The remaining steps in this tutorial cover methods for mitigating these vulnerabilities, as laid out on the official Redis website . Although these steps are optional and Redis will continue to work if you choose not to follow them, it is strongly recommended that you complete them to harden the security of your system.

Step 3 — Binding to localhost

By default, Redis is only accessible from localhost . However, if you installed and configured Redis by following a different tutorial than this one, you may have updated the configuration file to allow connections from anywhere. This is not as secure as binding to localhost .

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

sudo nano /etc/redis/redis.conf

Locate this line and make sure it is uncommented (remove the #if there is one):/etc/redis/redis.conf

. . .
bind 127.0.0.1 ::1
. . .

Save and close the file when you are finished ( CTRL + X, Y, then press ENTER).

Then, restart the service to make sure systemd reads your changes:

sudo systemctl restart redis

To check that this change has gone into effect, run the following netstat command:

sudo netstat -lnp | grep redis
Output
tcp    0  0 127.0.0.1:6379  0.0.0.0:*        LISTEN      14222/redis-server  
tcp6   0  0 ::1:6379   :::*                  LISTEN      14222/redis-server  

Note : The netstat command may not be available on your system by default. If so, you can install it (along with a number of other useful networking tools) with the following command:

sudo apt install net-tools

This output shows that the program is bound to localhost ( ) redis-server, reflecting the change you just made in the configuration file. If you see another IP address in that column ( , for example), then you should double-check that you uncommented the correct line and restart the Redis service. 127.0.0.10.0.0.0

Now that your Redis installation is listening only on localhost , it will be more difficult for malicious parties to make requests to or gain access to your server. However, Redis is not currently set up to require users to authenticate themselves before making changes to its configuration or the data it holds. To fix this, Redis allows you to require users to authenticate with a password before making changes via the Redis client ( redis-cli).

Step 4 — Configuring a Redis Password

Configuring a Redis password enables one of its two built-in security features – the authcommand, which requires clients to authenticate in order to access the database. The password is configured directly in Redis's configuration file, /etc/redis/redis.conf, so open that file again with your preferred editor:

sudo nano /etc/redis/redis.conf

Scroll to the SECURITY section and look for a commented directive that reads: /etc/redis/redis.conf

. . .
# requirepass foobared
. . .

Uncomment it by removing the #and change foobared to a secure password.

Note: requirepass Above the directive in the redis.conf file there is a commented warning: /etc/redis/redis.conf

. . .
# Warning: since Redis is pretty fast an outside user can try up to
# 150k passwords per second against a good box. This means that you should
# use a very strong password otherwise it will be very easy to break.
#
. . .

Therefore, it is important to specify a very strong and very long value as your password. Rather than coming up with a password yourself, you can use the openssl command to generate a random one, as in the following example. By piping the output of the first openssl command into the second command, as shown here, all line breaks produced by the first command will be removed:

openssl rand 60 | openssl base64 -A

This command will return output like this:

OutputRBOJ9cCNoGCKhlEBwQLHri1g+atWgn4Xn4HwNUbtzoVxAYxkiYBi7aufl4MILv1nxBqR4L6NNzI0X6cE

After copying and pasting the output of that command as the new value for requirepass, it should look like this:

/etc/redis/redis.confrequirepass RBOJ9cCNoGCKhlEBwQLHri1g+atWgn4Xn4HwNUbtzoVxAYxkiYBi7aufl4MILv1nxBqR4L6NNzI0X6cE

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

sudo systemctl restart redis.service

To test that the password works, open the Redis client:

redis-cli

The following shows a sequence of commands used to test whether the Redis password works. The first command tries to set a key to a value before authenticating:

set key1 10

That will not work because you have not authenticated, so Redis returns an error:

Output(error) NOAUTH Authentication required.

The next command authenticates with the password specified in the Redis configuration file:

auth your_redis_password

Redis acknowledges:

Output
OK

After that, running the previous command again will succeed:

set key1 10
Output
OK

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

get key1
Output"10"

After confirming that you can run commands in the Redis client after authenticating, you can exit redis-cli:

quit

Next, we will look at renaming Redis commands that, if entered by mistake or by a malicious actor, could have serious consequences for your data.

Step 5 — Renaming Dangerous Commands

The other security feature built into Redis involves renaming or completely disabling certain commands that are considered dangerous.

When run by unauthorized users, such commands can be used to reconfigure, destroy or otherwise wipe your data. Like the authentication password, renaming or disabling commands is configured in the same SECURITY section of the /etc/redis/redis.conf file.

Some of the commands that are considered dangerous include: FLUSHDB, FLUSHALL, KEYS, PEXPIRE, DEL, CONFIG, SHUTDOWN, BGREWRITEAOF, BGSAVE, SAVE, SPOP, SREM, RENAMEand DEBUG. This is not an exhaustive list, but renaming or disabling all of the commands in this list is a good starting point for improving the security of your Redis server.

Whether you should disable or rename a command depends on your specific needs or the needs of your site. If you know you will never use a command that could be abused, you can disable it. Otherwise, it might be in your interest to rename it.

To rename or disable Redis commands, open the configuration file once more:

sudo nano  /etc/redis/redis.conf

Warning: The following steps showing how to disable and rename commands are examples. You should only choose to disable or rename the commands that make sense for you. You can review the full list of commands yourself at Redis.io/commands and determine how they might be abused .

To disable a command, rename it to an empty string (indicated by a pair of quotation marks with no characters between them), as shown below: /etc/redis/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 as shown in the examples below. Renamed commands should be difficult for others to guess, but easy for you to remember: /etc/redis/redis.conf

. . .
# rename-command CONFIG ""
rename-command SHUTDOWN SHUTDOWN_MENOT
rename-command CONFIG ASC12_CONFIG
. . .

Save your changes and close the file.

After renaming a command, apply the change by restarting Redis:

sudo systemctl restart redis.service

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

redis-cli

Then, authenticate:

auth your_redis_password
Output
OK

Let's assume you renamed the CONFIGcommand to ASC12_CONFIG, as in the previous example. First, try using the original CONFIG command. It should fail, because you renamed it:

config get requirepass
Output(error) ERR unknown command `config`, with args beginning with:

However, calling the renamed command will succeed. It is not case-sensitive:

asc12_config get requirepass
Output1) "requirepass"
2) "your_redis_password"

Finally, you can exit redis-cli:

exit

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

OutputNOAUTH Authentication required.

WarningSECURITY : Regarding the practice of renaming commands, at the end of the section in /etc/redis/redis.conf there is a notice containing this warning: /etc/redis/redis.conf

. . .
# Please note that changing the name of commands that are logged into the
# AOF file or transmitted to replicas may cause problems.
. . .

Kerim Koç

Systems and infrastructure engineer. I write about Linux, open source, high availability and security.