Redis Security & ACLs
Redis was designed to run inside a trusted network, and a default installation trusts every client that can reach its port. Exposed Redis servers are a favourite target of automated attacks: within minutes of being reachable from the internet, a server without a password can have its data wiped or be used to plant malware. This lesson covers the layers of protection: network, authentication, ACLs (access control lists), TLS and dangerous commands.
Layer 1: the network
| Setting | Recommendation |
|---|---|
bind 127.0.0.1 -::1 |
listen only on local or private interfaces, never 0.0.0.0 on a public host |
protected-mode yes |
the default: refuses outside connections when no password is set |
| Firewall / security groups | allow port 6379 only from your application servers |
port 0 + tls-port 6379 |
accept TLS connections only |
Layer 2: users and passwords
Since Redis 6, access is managed with ACL users. Every connection starts as the user default, which on a fresh server has no password and may do everything. ACL WHOAMI shows who you are:
ACL WHOAMI
ACL LISTredis> ACL WHOAMI
"default"
redis> ACL LIST
1) "user default on nopass sanitize-payload ~* &* +@all"
The rule string reads: user default, enabled (on), no password needed (nopass), all keys (~*), all pub/sub channels (&*), all commands (+@all).
Creating a user
ACL SETUSER name rules... creates or modifies a user. The most important rules:
| Rule | Meaning |
|---|---|
on / off |
enable or disable the user |
>password |
add a password (<password removes it) |
~pattern |
allow keys matching a glob pattern (%R~ read-only, %W~ write-only) |
&pattern |
allow pub/sub channels |
+command / -command |
allow or deny one command |
+@category / -@category |
allow or deny a group: @read, @write, @string, @dangerous, ... |
resetkeys, reset |
clear the key patterns, or every rule |
Here we create a user for a caching service: it may only read and write string keys under cache::
ACL SETUSER cache-svc on >Str0ng-Pa55 ~cache:* +get +set +del +expire
ACL GETUSER cache-svc
ACL DRYRUN cache-svc GET cache:home
ACL DRYRUN cache-svc GET user:1
ACL DRYRUN cache-svc FLUSHALLredis> ACL SETUSER cache-svc on >Str0ng-Pa55 ~cache:* +get +set +del +expire
OK
redis> ACL GETUSER cache-svc
1) "flags"
2) 1) "on"
2) "sanitize-payload"
3) "passwords"
4) 1) "9167e179aaac4ccec629f3f35233ec312738102ce9286bf549bcf725c209f04a"
5) "commands"
6) "-@all +get +set +del +expire"
7) "keys"
8) "~cache:*"
9) "channels"
10) ""
11) "selectors"
12) (empty array)
redis> ACL DRYRUN cache-svc GET cache:home
OK
redis> ACL DRYRUN cache-svc GET user:1
"User cache-svc has no permissions to access the 'user:1' key"
redis> ACL DRYRUN cache-svc FLUSHALL
"User cache-svc has no permissions to run the 'flushall' command"
ACL DRYRUN checks whether a user would be allowed to run a command, without running it: perfect for testing rules. Passwords are stored only as SHA-256 hashes, which is the long hex string in the output.
Logging in
AUTH username password switches the current connection to another user. From then on every command is checked against that user's rules:
ACL SETUSER reporter on >r3port ~stats:* +@read
SET stats:visits 1520
AUTH reporter wrong-password
AUTH reporter r3port
GET stats:visits
SET stats:visits 0
GET user:1redis> ACL SETUSER reporter on >r3port ~stats:* +@read
OK
redis> SET stats:visits 1520
OK
redis> AUTH reporter wrong-password
(error) WRONGPASS invalid username-password pair or user is disabled.
redis> AUTH reporter r3port
OK
redis> GET stats:visits
"1520"
redis> SET stats:visits 0
(error) NOPERM User reporter has no permissions to run the 'set' command
redis> GET user:1
(error) NOPERM No permissions to access a key
The reporter can read stats: keys but cannot write and cannot touch other keys. In application code you pass the user and password to the client (redis://reporter:r3port@host:6379).
Protecting the default user
On a real server the first step is to give default a password or to disable it, after creating an admin user:
# redis.conf (or an external users.acl file with aclfile /etc/redis/users.acl)
user default off
user admin on >a-long-random-password ~* &* +@all
user app on >another-long-password ~app:* &app:* +@all -@dangerousrequirepass secret is the pre-6 way: it simply sets a password on the default user.
Dangerous commands
Some commands should never be available to applications: FLUSHALL/FLUSHDB (delete everything), CONFIG (change the server, even its working directory), KEYS (blocks the server), DEBUG, SHUTDOWN, MODULE. The @dangerous category groups them, so -@dangerous in a user's rules removes them all. Note that -@dangerous also removes KEYS but not SCAN, which is exactly what you want:
ACL SETUSER app on >app-pass ~app:* +@all -@dangerous
ACL DRYRUN app SET app:x 1
ACL DRYRUN app SCAN 0
ACL DRYRUN app KEYS *
ACL DRYRUN app FLUSHALLredis> ACL SETUSER app on >app-pass ~app:* +@all -@dangerous
OK
redis> ACL DRYRUN app SET app:x 1
OK
redis> ACL DRYRUN app SCAN 0
OK
redis> ACL DRYRUN app KEYS *
"User app has no permissions to run the 'keys' command"
redis> ACL DRYRUN app FLUSHALL
"User app has no permissions to run the 'flushall' command"
Layer 3: encryption with TLS
Redis traffic is plain text by default, including passwords in AUTH. Between data centres or in shared networks, enable TLS:
tls-port 6379
port 0
tls-cert-file /etc/redis/tls/redis.crt
tls-key-file /etc/redis/tls/redis.key
tls-ca-cert-file /etc/redis/tls/ca.crtClients then connect with rediss:// URLs or redis-cli --tls.
Checklist
- Bind to private interfaces and firewall the port.
- Disable or password-protect
default; create one ACL user per application with the minimum permissions. - Remove
@dangerouscommands from application users. - Use TLS when traffic crosses untrusted networks.
- Never run Redis as root; keep it updated.
- Monitor
ACL LOG, which records denied commands and failed logins.
Test yourself
Which ACL rule restricts a user to keys starting with orders:?
- +orders:*
- ~orders:*
- &orders:*
- >orders:*
What does ACL DRYRUN alice DEL x do?
- Deletes x as alice
- Checks whether alice would be allowed to run DEL x, without running it
- Removes alice
- Logs alice in
Why is exposing an unauthenticated Redis port to the internet dangerous?
- It makes Redis slower
- Anyone can read, delete or reconfigure the data
- It disables persistence
- It is not dangerous with protected-mode no