Skip to content
Tutorials Center
My learning

Topics

Lesson 31 of 42 ยท Operations, Security and Performance

Securing Redis

Secure Redis the right way: bind and protected mode, ACL users and permissions, requirepass, TLS, dangerous commands, exposed-server incidents and safe defaults.

Redis was designed to run inside a trusted network, spoken to by your own application servers. It is fast partly because it does very little checking: by default, anyone who can open a TCP connection can read every key, delete everything and, as a quick search for "exposed Redis" incidents shows, often take over the host. Securing Redis is therefore mostly about three questions: who can reach it, who may log in, and what may a logged-in user do.

This lesson walks through the layers in that order: network exposure (bind, protected mode, firewalls), authentication (requirepass and ACL users), authorization (command and key permissions), encryption (TLS) and the commands that deserve special care. You will also run two small stand-ins.

Redis cannot run on this page, so all redis-cli commands and config files are read-only. The two runnable TryIts are plain JavaScript models: an ACL checker that follows the real rule semantics for a small subset of commands, and a configuration auditor. They teach the logic; they do not replace testing your real server with ACL WHOAMI, ACL LOG and redis-cli.

Defense in layers

Network Bind to private addresses.
Login ACL users with passwords.
Permissions Only the commands and keys needed.
Encryption TLS between client and server.

Each layer should hold even if another fails.

1 / 2

Layer 1: keep it off the internet

The first defense is not letting strangers connect at all.

  • bind chooses the network interfaces Redis listens on. The shipped config uses bind 127.0.0.1 -::1 (loopback only). If the application runs on other machines, bind to a private interface address (bind 127.0.0.1 10.0.0.5), never 0.0.0.0 on a machine with a public address.
  • Protected mode (protected-mode yes, the default) is a safety net: if Redis is not explicitly bound to some interfaces and has no password, it refuses connections from anything but loopback and explains how to fix the problem in the error message. Do not "fix" the message by switching protected mode off; set a bind address and authentication instead.
  • Firewall or security group. Allow port 6379 (and the cluster bus port, if used) only from your application servers. In Docker, remember that -p 6379:6379 publishes the port on all host interfaces; use -p 127.0.0.1:6379:6379 or an internal network.
  • Do not count a non-default port as a defense. Scanners find it anyway.

A real and recurring incident pattern: a Redis server reachable from the internet without a password gets found by scanners within hours. The attacker connects and, if the server allows it, uses CONFIG SET dir and CONFIG SET dbfilename plus SAVE to make Redis write a file of their choosing (an SSH key, a cron job, a web shell) into a directory it has permission to write. Newer versions block some of those config changes by default, but "attacker can run FLUSHALL and read your data" is enough reason for the rules above.

Layer 2 and 3: users, passwords and permissions

The old way to protect Redis is one shared password:

Text
# redis.conf
requirepass "use-a-long-random-value-here"

Clients then send AUTH password. That password protects the built-in default user, and everyone who knows it can do anything. It is better than nothing, but it gives you no way to separate your application from an administrator or a monitoring tool.

Since Redis 6 the right way is ACLs (access control lists): named users, each with its own passwords, allowed commands and allowed keys. The commands below are read-only here:

Text
127.0.0.1:6379> ACL SETUSER shop on >a-long-random-password ~cache:* ~session:* +@read +@write -@dangerous
OK
127.0.0.1:6379> ACL SETUSER metrics on >another-password ~* +info +client|list +slowlog|get
OK
127.0.0.1:6379> ACL SETUSER default off
OK
127.0.0.1:6379> AUTH shop a-long-random-password
OK
127.0.0.1:6379> ACL WHOAMI
"shop"
127.0.0.1:6379> GET orders:17
(error) NOPERM No permissions to access a key
127.0.0.1:6379> FLUSHALL
(error) NOPERM User shop has no permissions to run the 'flushall' command
127.0.0.1:6379> ACL LOG

The main rule tokens are:

Rule Meaning
on / off Enable or disable the user.
>password Add a password (stored hashed as SHA-256). nopass allows any password.
~pattern The user may touch keys matching the glob pattern. allkeys is ~*.
+command, -command Allow or forbid one command; +client|list allows a subcommand.
+@category, -@category Allow or forbid a command category such as @read, @write, @admin, @dangerous.
+@all / allcommands Allow every command.
&pattern Pub/Sub channel patterns the user may use.

Rules are applied in order. +@all -@dangerous means "everything except the dangerous ones", while -@dangerous +@all ends up with everything. In Redis 7 you can also use %R~pattern or %W~pattern to grant read-only or write-only access to keys, and selectors in parentheses for combining several independent permission sets.

Good practice: one user for each application and each tool, passwords that are long and random (an ACL password is not typed by a person, so make it 32+ random characters), and default off or at least a strong password on default. Persist users with an aclfile (ACL SAVE and ACL LOAD) rather than only in redis.conf, so that you can change them without a restart. ACL LOG lists denied attempts, which is useful for both debugging and spotting probes.

The first TryIt implements the rule logic for a handful of commands.

JS An ACL checker

Press Run (or Ctrl + Enter) to see the console output here.

No output.

The legacy user (nopass, all keys, +@all) is exactly what a fresh install gives to the default user. Every other user above is an improvement: the shop application can read and write only its own key prefixes and cannot flush, the metrics user can only run INFO and PING.

Layer 4: encryption with TLS

Passwords and data travel in plain text on a normal Redis connection. Anyone on the network path can read them. When Redis is reached across machines you do not fully trust (another network, a cloud provider's shared infrastructure, a different data center), turn on TLS:

Text
# redis.conf
port 0
tls-port 6379
tls-cert-file /etc/redis/tls/redis.crt
tls-key-file /etc/redis/tls/redis.key
tls-ca-cert-file /etc/redis/tls/ca.crt
tls-auth-clients yes
tls-replication yes
tls-cluster yes

port 0 disables the plain-text port, tls-auth-clients yes additionally requires clients to present a certificate (mutual TLS), and tls-replication and tls-cluster encrypt replication and the cluster bus. Test with redis-cli --tls --cacert ca.crt -h host. TLS costs some CPU and a little latency, so measure it, but on any untrusted network it is not optional. Managed services usually give you a TLS endpoint ready to use.

Dangerous commands

Even a restricted user should not run everything. The @dangerous category (visible with ACL CAT dangerous) holds the usual suspects: FLUSHALL, FLUSHDB, KEYS, CONFIG, DEBUG, SHUTDOWN, REPLICAOF, MONITOR, MODULE. Remove them from application users with -@dangerous. Redis 7 also ships with enable-debug-command no, enable-module-command no and enable-protected-configs no as defaults, which block DEBUG, MODULE and the riskiest CONFIG SET changes (like the file path ones) unless you opt in. The older rename-command FLUSHALL "" trick still works but is awkward: it needs a restart, cannot differ per user and complicates replication. Prefer ACLs.

Also remember what Lua scripts and EVAL can do: they run inside the server with the permissions of the calling user, so deny @scripting to users who do not need it. See Lua Scripting and Functions.

Auditing a configuration

The second TryIt turns the advice into a small checker. It takes a map of settings and prints findings, from critical to informational.

JS A configuration auditor

Press Run (or Ctrl + Enter) to see the console output here.

No output.

A safe baseline

  1. Bind to loopback or a private address, and firewall the port.
  2. Keep protected mode on.
  3. Turn default off (or give it a long random password) and create one ACL user per application and tool, each limited to the keys and command categories it needs, with -@dangerous.
  4. Enable TLS when traffic leaves a trusted network.
  5. Keep enable-debug-command, enable-module-command and enable-protected-configs at no.
  6. Run Redis as an unprivileged user, never as root, and keep the data directory writable only by that user.
  7. Protect the Sentinel and Cluster ports the same way, and watch ACL LOG and connection counts (see Monitoring and Troubleshooting).

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. What does protected mode do?

    Protected mode is a safety net for misconfigured servers: without explicit bind addresses or authentication, only loopback clients are accepted.
  2. 2. Which ACL rule set gives a user every command except the dangerous ones?

    ACL rules are applied in order. Adding everything first and then removing the dangerous category leaves the rest; the reverse order ends with everything allowed.
  3. 3. What is the main problem with relying only on requirepass?

    requirepass protects the default user. There is no separation between an application, a monitor and an administrator, which ACL users provide.
  4. 4. A Redis server is reachable from the internet with no password. Which is the realistic worst case?

    Scanners find open Redis servers quickly. Without authentication an attacker can read, wipe and sometimes abuse config commands to run code on the host.
  5. 5. When is TLS most important?

    ACL passwords are stored hashed, but on the wire a normal connection is plain text. TLS protects both credentials and data in transit.

Summary

  • Keep Redis off the internet: bind to private interfaces, keep protected mode on and firewall the port.
  • Replace the single shared password with ACL users: passwords, key patterns and command categories per application, with default turned off.
  • ACL rules apply in order; use -@dangerous for application users and check with ACL WHOAMI and ACL LOG.
  • Turn on TLS (and mutual TLS if needed) when traffic leaves a trusted network.
  • Redis 7 disables DEBUG, MODULE and risky CONFIG SET changes by default; keep it that way.
  • Run as an unprivileged user and secure Sentinel and Cluster ports too.

Next, making Redis fast and finding what slows it down: Performance and Latency.