How to Install and Secure Redis on Ubuntu 20.04

how to install and secure redis on ubuntu 20.04

Redis (short for Remote Dictionary Server), is an open-source in-memory data structure store. It’s used as a flexible, highly available key-value database that maintains a high level of performance.  It helps to reduce time delays and increase the performance of your application by accessing in microseconds.

One of the best features of Redis is the huge range of data types or data structures that it supports, such as Strings, Hashes, Lists, Sets, and more. Redis is written in the C programming language. In this tutorial, We will show you how to install and secure Redis Server on Ubuntu 20.04.

Requirements:

  • For the purposes of this tutorial, we will use an Ubuntu20.04 VPS.
  • Access to the root user account (or a user with sudo privileges)

1: Log in to the Server & Update the Server OS Packages

First, log in to your Ubuntu 20.04 server via SSH as the root user:

ssh root@IP_ADDRESS -p PORT_NUMBER

Don’t forget to replace IP_Address and Port_Number with your server’s actual IP address and the SSH port number. Also, you should replace ‘root’ with the username of the admin account if needed.

Once you are in, run the following commands to update the package index and upgrade all installed packages to the latest available version

sudo apt-get update 
sudo apt-get upgrade

Once the upgrades are completed, we can move on to the next step.

2. Install Redis on Ubuntu 20.04

When an update is completed, you will download and install it from the official Ubuntu repositories. Redis version 5.0.x is included by default on Ubuntu 20.04 repositories.

Run the following command to install Redis on your server:

sudo apt-get install redis-server

With this command, you will also download and install all the required dependencies.

Once the installation is completed, the Redis service will start automatically. To check and verify the status of your Redis service, run the following command:

sudo systemctl status redis-server

You should receive 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 Sat 2021-09-25 11:01:30 UTC; 12min ago
Docs: http://redis.io/documentation,
man:redis-server(1)
Main PID: 199086 (redis-server)
Tasks: 4 (limit: 2279)
Memory: 2.0M
CGroup: /system.slice/redis-server.service
└─199086 /usr/bin/redis-server 127.0.0.1:6379

The default port of Redis is 6379 and IP 127.0.0.1 is localhost.

3. Check Redis Version

You can check the Redis version using the following command:

$ redis-cli -v

You should get the following output:

redis-cli 5.0.7

4. How to configure Redis server

Redis can start without a configuration file using a built-in default configuration. Now you need to make one important configuration change in Redis. Open the Redis config, redis.conf with your favorite editor. We will use nano. You are free to use any other text editor if you prefer:

sudo nano /etc/redis/redis.conf

You need to find a supervised directive. By default, this line is set to no. However, to manage Redis as a service you will change it to systemd.

# 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

To make Redis aware of this, you need to restart it with the following command:

sudo systemctl restart redis-server

5. Check Redis Connection

You also need to verify the connection with the Redis server using the redis-cli tool. To connect with this command-line client, enter the following command:

redis-cli

To test the connectivity, run:

ping

The output should respond with:

PONG

6: Managing the Redis Service

To stop your service, run the following command:

sudo systemctl stop redis-server

To start your service, run the following command:

sudo systemctl start redis-server

To enable your service, run the following command:

sudo systemctl enable redis-server

7. How to Secure Redis

Redis includes an authentication feature as an additional security layer. The feature is not enabled by default. To enable the password authentication, open the Redis configuration file:

sudo nano /etc/redis/redis.conf

Scrolling through the file, there will be a section called SECURITY , where this entry will be shown:

# requirepass foobared

and replace foobared with the password of your choice.

requirepass your_strong_password

Note: Do not forget to uncomment and change your_strong_password with your strong password.

And restart the Redis service for the changes to take effect.

sudo systemctl restart redis.service

Now the password authentication for Redis has been enabled.

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

redis-cli

Once you’re connected to the server, try the ping with the following command.

ping "hello world"

Now you will get the result as below.

installing and securing redis on ubuntu 20.04

To authenticate, just use the auth command, as shown below:

auth your_strong_password

Below is the result after you’re authenticated to the Redis Server.

installation and securing redus on an ubuntu 20.04

That’s it! The installation of Redis Server on Ubuntu 20.04 has been completed, and the basic security for Redis Server has been applied.

Of course, you don’t need to install and secure Redis on Ubuntu 20.04 yourself if you use one of our fully managed VPS Hosting services, in which case you can simply ask our expert Linux admins to install and secure it for you. They are available 24×7 and will take care of your request immediately.

how to install redis on an ubuntu 20.04

P.S. If you liked this post on how to install and secure Redis on Ubuntu 20.04 please share it with your friends on the social networks by using the share shortcuts below, or simply leave a comment in the comments section. Thanks.

Leave a Comment

To prove you are human please solve the following *