Databases

How to install and secure Redis on a Linux VPS

Install Redis on Ubuntu, Debian, AlmaLinux or Rocky Linux, keep it on localhost, require a password or ACL user, and run it under systemd.

BeginnerLinux server5 min read

Redis is an in-memory data store that apps use for caching, sessions and queues. It is fast and simple, but a default install trusts anyone who can reach its port. This guide installs Redis from your distribution's packages, keeps it on localhost, adds authentication and checks that systemd supervises it.

Before you start

  • A Linux VPS running Ubuntu 22.04 or 24.04, Debian 12, or AlmaLinux or Rocky Linux 8 or 9.
  • A user with sudo rights. If you only have root, see create a sudo user.
  • An SSH session to the server. The login details are in your VPS provider's control panel.

Note: Recent Redis releases changed licence terms, and some distributions now ship Valkey, a compatible fork, instead. The steps below use the redis package your distribution provides. If it is not available, check your distribution's documentation for the current name.

Install Redis

Ubuntu and Debian

Bash
sudo apt update
sudo apt install redis-server

The main config file is /etc/redis/redis.conf and the service is called redis-server.

AlmaLinux and Rocky Linux

Bash
sudo dnf install redis
sudo systemctl enable --now redis

The service is called redis. The config file location differs between releases, so ask the package where it put it:

Bash
rpm -qc redis

Look for redis.conf in the output. It is usually /etc/redis/redis.conf on version 9 and /etc/redis.conf on version 8. Use that path wherever this guide says /etc/redis/redis.conf.

Keep Redis on localhost

If only apps on the same server use Redis, it should listen on the loopback address only. Open the config file:

Bash
sudo nano /etc/redis/redis.conf

Find the bind line and make sure it reads:

INI
bind 127.0.0.1 -::1

The - before ::1 tells Redis to carry on if IPv6 is not available. Also check that protected mode is on:

INI
protected-mode yes

Protected mode refuses connections from other hosts when no password is set. It is a safety net, not a replacement for a password.

Warning: Never bind Redis to 0.0.0.0 with no password and no firewall. Exposed Redis servers are scanned for constantly and are a common way servers get taken over.

Require a password or an ACL user

You have two options. Use one of them.

Option 1: a single password

Find the commented # requirepass line and set your own long, random password. Generate one with:

Bash
openssl rand -base64 32

Then add it to the config (the value below is an example only, so use your own):

INI
requirepass StrongPassword123!ReplaceMe

Clients then send AUTH <password> before running commands.

Option 2: ACL users

ACLs let you give each app its own user with limited rights. Add a line like this to redis.conf, using your own password:

INI
user default off
user appuser on >StrongPassword123!ReplaceMe ~app:* &* +@all -@dangerous

This turns off the passwordless default user and creates appuser, who can only touch keys starting with app: and cannot run commands in the @dangerous group, such as FLUSHALL or CONFIG.

Warning: If you turn off the default user, make sure every client that connects to Redis is updated to log in as the new user first, or it will stop working.

Keep the file private, because it now holds a secret:

Bash
sudo chmod 640 /etc/redis/redis.conf

Let systemd supervise Redis

In the config file, set:

INI
supervised systemd

This lets Redis tell systemd when it is ready, so restarts and status reports are accurate. Packaged versions often set this already. Restart and enable the service:

Bash
# Ubuntu and Debian
sudo systemctl restart redis-server
sudo systemctl enable redis-server

# AlmaLinux and Rocky Linux
sudo systemctl restart redis

Check it started cleanly:

Bash
systemctl status redis-server   # or: systemctl status redis

You should see active (running).

Check it works

Confirm Redis only listens on localhost:

Bash
sudo ss -ltnp | grep 6379

The output should show 127.0.0.1:6379 and [::1]:6379, not 0.0.0.0:6379.

Now connect with redis-cli and try a command without logging in:

Bash
redis-cli ping

With authentication on, you get NOAUTH Authentication required. Log in and try again. Typing the password at the prompt keeps it out of your shell history:

Bash
redis-cli
127.0.0.1:6379> AUTH appuser your-password-here
OK
127.0.0.1:6379> SET app:test hello
OK
127.0.0.1:6379> GET app:test
"hello"

If you used requirepass, run AUTH your-password-here with no username.

Troubleshooting

  • Service fails to start after editing: a typo in redis.conf stops Redis. Read the log with journalctl -u redis-server -n 50 (or -u redis) to find the line it rejected.
  • App gets NOAUTH or WRONGPASS errors: the app is not sending the new password or username. Update its Redis settings, then restart the app.
  • NOPERM errors: the ACL user lacks rights to that key or command. Widen the key pattern or command list for that user only.
  • Could not connect to Redis: check systemctl status and confirm the app connects to 127.0.0.1, not the public IP.
  • Redis needed from another server: do not open port 6379 to the internet. Allow only that server's IP with your firewall, for example with UFW rules for one IP, and keep authentication on.

Next steps

Was this guide helpful?

Still stuck?

Ask MdmHost AI for a quick answer, or open a ticket and our team will take a look. We are here 24/7.