How to Install Redis and Set a Password on Linux

Redis is an in-memory data structure store that can be used as a cache, database, and message broker. In this tutorial, we’ll show you how to install Redis on Linux and set a password to protect your Redis server.

Step 1: Update the package manager

The first step is to update the package manager on your Linux server:

sudo apt-get update

Step 2: Install Redis

Next, install Redis using the following command:

sudo apt-get install redis-server

Step 3: Set a Password

By default, Redis does not require authentication, which means that anyone who can connect to the Redis server can execute commands. To set a password, open the Redis configuration file at /etc/redis/redis.conf and look for the requirepass directive. Uncomment the line and set a password, like this:

requirepass mypassword

Replace “mypassword” with a strong password of your choice.

Step 4: Change the Redis Port

By default, Redis listens on port 6379. If you want to change the port, you can do so by editing the port directive in the Redis configuration file:

port 1234

Replace “1234” with the desired port number.

Step 5: Restart Redis

After making changes to the Redis configuration file, restart the Redis service to apply the changes:

sudo systemctl restart redis

Step 6: Test Redis

To test Redis, you can use the redis-cli command-line tool. Connect to Redis by running:

redis-cli

If Redis is running and the password is set correctly, you’ll be prompted to enter the password. Once you’re authenticated, you can run Redis commands, like this:

set mykey "Hello World"
get mykey

This should return “Hello World”.

Conclusion:

In this tutorial, we showed you how to install Redis on Linux, set a password, and change the Redis port. By following these steps, you can secure your Redis server and protect your data from unauthorized access.