How to Install and Configure Redis on Ubuntu 22.04

Redis

Redis is an open source and in-memory data structure store used as a cache and message broker. It is used to manage geospatial data in real-time at large scale and high speed. It is written in C and optimized for speed. You can use it as a database backend for your application to reduce time delays and increase performance. It supports various data structures like strings, hashes, lists, sets and more.

In this post, we’ll show you how to install and secure Redis server on Ubuntu 22.04.

Requirements

  • A server running Ubuntu 22.04.
  • A root password is set up on your server.

Install Redis Server

The Redis package is available in the default Ubuntu repository. You can install it with the following command:

apt-get install redis-server -y

After you install Redis, start the Redis service and enable it to start when you reboot your system:

systemctl start redis
systemctl enable redis

You can check the status of the Redis service with the following command:

systemctl status redis

You should see 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 Mon 2022-11-28 16:23:49 UTC; 5s ago
       Docs: http://redis.io/documentation,
             man:redis-server(1)
   Main PID: 17998 (redis-server)
     Status: "Ready to accept connections"
      Tasks: 5 (limit: 464122)
     Memory: 3.7M
     CGroup: /system.slice/redis-server.service
             ??17998 "/usr/bin/redis-server 127.0.0.1:6379" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" >

Nov 28 16:23:48 ubuntu22041 systemd[1]: Starting Advanced key-value store...
Nov 28 16:23:49 ubuntu22041 systemd[1]: Started Advanced key-value store.

To check the Redis version, run the following command:

redis-cli -v

You should see the Redis version in the following output:

redis-cli 6.0.16

Configure Redis Server

The default Redis configuration file is located in the /etc/redis directory. You need to edit it to declare an init system that manages Redis as a service.

nano /etc/redis/redis.conf

Change the following line:

supervised systemd

Save and close the file and restart the Redis service to apply the changes.

systemctl restart redis

Using Redis CLI to check Redis server

After you have installed the Redis server, you need to verify it using the Redis CLI. Run the following command to connect to the Redis CLI:

redis-cli

Once you are connected, you will get the following shell:

127.0.0.1:6379>

Next, run the following command to test the connection:

127.0.0.1:6379> ping

You will get the following output:

PONG

Next, verify that you can set keys by running the following command:

127.0.0.1:6379> set test "Redis is working fine!"

Next, get the key value with the following command:

127.0.0.1:6379> get test

You will get the following output:

"Redis is working fine!"

Exit the Redis CLI with the following command:

127.0.0.1:6379> exit

Enable authentication in Redis

By default, you can connect to Redis without a password. For security reasons, it is recommended to enable password authentication to access the Redis CLI.

First, create the secure password using the following command:

openssl rand 60 | openssl base64 -A

You will get the following output:

6im/3HRpZLc8w2PxFWiYcSmjj7kcQHbsFY+y5f7uyTpJLfrpxWGvNmS57j5B1O842VzgLKfbYSL9gLrF

Next, edit the Redis configuration file with the following command:

nano /etc/redis/redis.conf

Find the following line:

# requirepass foobared

And replace it with your generated password:

requirepass 1kgFpNpEUempFTxTgAWQcEgGBI+oddM1ztaa7Rhd6UL2+eh/4UOPWVJVKhdp3/xNj5U5OmggeY7Uo+NF

Save and close the file and restart the Redis service to apply the configuration changes:

systemctl restart redis

Check the Redis authentication

After you have enabled Redis password authentication, you need to verify that it works.

To do this, connect to the Redis CLI with the following command:

redis-cli

Next, set the key value with the following command:

127.0.0.1:6379> set name admin

You will get the following error message:

(error) NOAUTH Authentication required.

Next, authenticate Redis with a password by running the following command:

127.0.0.1:6379> auth 1kgFpNpEUempFTxTgAWQcEgGBI+oddM1ztaa7Rhd6UL2+eh/4UOPWVJVKhdp3/xNj5U5OmggeY7Uo+NF

After successful authentication, set the key value with the following command:

127.0.0.1:6379> set name admin

Next, retrieve the key value with the following command:

127.0.0.1:6379> get name

You will get the following output:

"admin"

Exit the Redis CLI with the following command:

127.0.0.1:6379> exit

Rename Dangerous Commands

Redis provides some commands to reconfigure, destroy or delete your data. Some of these commands are very dangerous, including FLUSHDB, FLUSHALL, KEYS, PEXPIRE, DEL, CONFIG and SHUTDOWN. It is recommended to disable or rename a command depending on what you need.

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

nano /etc/redis/redis.conf

Lift the comments and change the following lines:

rename-command SHUTDOWN SHUTDOWN_NOT
rename-command CONFIG CONFIG_NOT

Save and close the file and restart the Redis service to apply the configuration changes:

systemctl restart redis

To test the new commands, connect to the Redis CLI with the following command:

redis-cli

Next, authenticate Redis with the following command:

127.0.0.1:6379> auth 1kgFpNpEUempFTxTgAWQcEgGBI+oddM1ztaa7Rhd6UL2+eh/4UOPWVJVKhdp3/xNj5U5OmggeY7Uo+NF

Next, run the original CONFIG command to retrieve the Redis password:

127.0.0.1:6379> config get requirepass

You get the following error message:

(error) ERR unknown command `config`, with args beginning with: 
Now, run the renamed command to retrieve the password:
127.0.0.1:6379> config_not get requirepass

You get the Redis password in the following output:

1) "requirepass"
2) "1kgFpNpEUempFTxTgAWQcEgGBI+oddM1ztaa7Rhd6UL2+eh/4UOPWVJVKhdp3/xNj5U5OmggeY7Uo+NF"

Finally, you exit the Redis CLI with the following command:

127.0.0.1:6379> exit

Conclusion

Congratulations! You have successfully installed and backed up the Redis server on Ubuntu 22.04. Now you can use Redis as a database in your application to improve the speed and performance of the application. If you still have questions, feel free to contact me.

Published
Categorized as Linux, Ubuntu