The first thing we need to do is install Redis by opening a command line terminal and typing the following command.

If you are only using your machine to connect to Redis (hosted elsewhere), you’ll only need to install the Redis client. Use this command:

$ sudo apt install redis-tools

Once it’s been installed, you’ll be able to use the redis-cli command to open a Redis terminal to a remote server. For example, this would be the command used to connect to a Redis server with a hostname redis-ubuntu. Notice we also use the ping command to verify connectivity.

$ redis-cli -h redis-ubuntu
redis-ubuntu:6379> ping
PONG
redis-ubuntu:6379>

If the Redis server isn’t using the default port, you can specify a port in your redis-cli command with the -p option, like so:

$ redis-cli -h redis-ubuntu -p 1234

In case you’re getting a “connection refused” error message, we’ll give you some troubleshooting tips further into this article.

Could not connect to Redis at redis-ubuntu:6379: Connection refused

Install Redis Server on Ubuntu

If you’re planning to host a Redis server, you’ll need the server package. This will also automatically install the Redis client package. Use this command in the terminal:

$ sudo apt install redis-server

You can verify that Redis is installed on a system and check the installed version with the following command:

$ redis-server -v
Redis server v=5.0.7 sha=00000000:0 malloc=jemalloc-5.2.1 bits=64 build=636cde3b5c7a3923

Furthermore, you can use the ss command to confirm that Redis is listening for incoming connection on its default port of 6379:


$ ss -nlt
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 5 127.0.0.1:631 0.0.0.0:*
LISTEN 0 511 127.0.0.1:6379 0.0.0.0:*
LISTEN 0 4096 127.0.0.53%lo:53 0.0.0.0:*
LISTEN 0 5 [::1]:631 [::]:*
LISTEN 0 511 [::1]:6379 [::]:*

By default, the Redis server will start automatically when your system is rebooted. You can change this behavior by using systemd’s systemctl command. You can also use it to check the current status of Redis.

$ sudo systemctl disable redis-server	#disable Redis from starting up automatically
$ sudo systemctl enable redis-server	#enable Redis to start up automatically
$ systemctl status redis-server			#check the current status of Redis server

By default, the Redis server will only listen on the local loopback interface 127.0.0.1, meaning that it doesn’t accept remote connections. You can configure Redis to listen on a different network interface, or all network interfaces, by opening the Redis conf file with Nano or your favorite text editor:

$ sudo nano /etc/redis/redis.conf

To let Redis listen on all network interfaces, just comment on the following line by inserting a preceding #:

bind 127.0.0.1 ::1

There’s one other line we’ll need to change if we want Redis to accept remote connections. Find the protected-mode part of the config file and change it to this:

FROM:
protected-mode yes
TO:
protected-mode no

Save your changes to this file and close it. Be sure to restart Redis for the changes to take effect:

$ sudo systemctl restart redis-server


You should now see that Redis is listening on 0.0.0.0, which represents all network interfaces.


$ ss -nlt
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 5 127.0.0.1:631 0.0.0.0:*
LISTEN 0 511 0.0.0.0:6379 0.0.0.0:*
LISTEN 0 4096 127.0.0.53%lo:53 0.0.0.0:*
LISTEN 0 5 [::1]:631 [::]:*
LISTEN 0 511 [::]:6379 [::]:*

The last thing you may need to do to accept incoming connections is to allow port 6379 through the UFW firewall.

$ sudo ufw allow from any to any port 6379 proto tcp
Rules updated
Rules updated (v6)

The Redis server should now accept incoming connections.

Was this answer helpful? 0 Users Found This Useful (0 Votes)