Redis is a high-performing, resilient, and robust in-memory key-value store that is used for diverse purposes, including database management, message streaming, and storing cache. It is used to retrieve data quickly and instantly by processing it in memory. By default, all the Redis servers operate on the 6379 port, but sometimes, we might need to run it on some other ports.
In this tutorial, we’ll learn how to run the Redis server on a non-default port using different ways.
2. Redis Server Setup
Before running the Redis server on a non-default port, we need to install the Redis server on our Linux system. Let’s install the server using the default package manager available in the Linux system:
$ sudo apt-get install redis-server
The above command installs all the necessary packages on our machine. Let’s start the server using the systemctl command:
$ sudo systemctl start redis
This way, the Redis server will start on the 6379 port. One key point to note is that running the Redis server on 6379 is exploitable via remote code execution. So, it is highly recommended to run it on some other ports.
3. Using the Redis Configuration File
So far, we have set up the Redis server, which is currently operational on the 6379 port. Now, we need to modify the main redis.conf configuration file to reroute the server on a port other than this 6379. In redis.conf, the port attribute specifies what port the server will use. In addition, we need to replace the port in /etc/redis/redis.conf from 6379 to 6380. To demonstrate, we can use the sed command to replace the port from 6379 to 6380:
$ sed -i 's/^port 6379$/port 6380/' redis.conf
Let’s proceed to restart the Redis server to verify whether the changes are in place or not:
$ sudo systemctl restart redis
Using the above command, we restarted the Redis server using the systemctl command. Furthermore, this ensures our changes took place successfully. This whole process is easy to adapt to run the server on non-default ports.
4. Using Command-Line Options
The redis-server command provides some options to override the main configuration file. Except for the configuration in the command, all the configurations will be picked from the main configuration file. Furthermore, the –port option of the redis-server command helps to run the Redis server on a non-default port. Let’s take a look at the command:
$ redis-server --port 6380
Now, the Redis server is running on the 6380 port. This is a temporary way to start the Redis server since its configuration changes won’t be reflected in the main configuration file. To permanently update the Redis port, we should always update the port in the Redis configuration file.
5. Using Docker
We can run the Redis server inside a Docker container. It is a very straightforward process instead of modifying the configuration files on the host machine. To illustrate, let’s check out the command to start the Redis server in the Docker container:
$ docker run -itd --name redis redis
In the above command, we run the Redis server inside the Docker container on a default port. Furthermore, let’s look at the command to run the Redis server on the 6380 port:
$ docker run -itd -p 6380:6379 --name redis redis
In the above command, we run the Redis server internally on 6379 inside the Docker container, along with the 6380 port exposed on the host machine. Now, we can connect to the Redis server on 6380 on the host machine.
In this article, we explored various ways to run a Redis server on a non-default port. First, we installed the Redis server on the default port using the Ubuntu package on a Linux machine. After that, we edited the Redis main configuration file to run it on another port. At last, we looked at running the Redis server using the Docker command.