Advanced
Searching inHow-to guides
How to search
  • Type the task in your own words (find port, open firewall, disk full) or paste an error message in quotes.
  • Choose "Quick answer" to see only the commands, or an area to stay in one field.
  • "two words" finds the exact phrase.
  • Plain words find names, texts and examples.
  • ↑ ↓ to move · Enter to open · Esc to close · / to search
Try:

How-to 190Data · Redis

How to connect apps to Redis securely

Redis is fast and simple — and an open Redis on the internet is taken over within minutes (CONFIG SET dir + a cron file, crypto miners). Since Redis 6 you can do better than one shared password: ACL users with their own password, allowed keys and allowed commands, and TLS so passwords and data are encrypted on the network. This how-to turns off the default user, creates an app user that can only touch its own keys, enables TLS, and connects with redis-cli and a URL.

  • 5 Lessons
  • 7 Commands

$ run as a normal user# run as root (or with sudo)

190.1

The quick answer

/etc/redis/users.acl
user default off
user app on >Long-Random-App-Password ~app:* &app:* +@connection +@read +@write -@dangerous
user admin on >Long-Random-Admin-Password ~* &* +@all
/etc/redis/redis.conf (part)
bind 10.0.0.21 127.0.0.1
protected-mode yes
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 optional
aclfile /etc/redis/users.acl
systemctl restart redis-server
redis-cli --tls --cacert /etc/redis/tls/ca.crt -h redis.example.com --user app --askpass ping

190.2

Step by step

  1. Network first — bind only to the private address and localhost, and a firewall rule that allows port 6379 only from the app servers. Never expose Redis to the internet.
  2. ACL users (quick answer) — default off disables the old password-less user. The app user may read and write keys that start with app: (~app:*), use Pub/Sub channels app:*, run connection commands (PING, AUTH, HELLO), and no dangerous commands (FLUSHALL, CONFIG, KEYS, DEBUG …). Make passwords long and random; store them in the secret store. The file is readable only by Redis (chown redis:redis, chmod 640).
  3. Hashed passwords — instead of >password you can write # followed by the SHA-256 of the password, so the file does not hold the plain text.
  4. TLS (quick answer) — certificate and key for the server name, plus the CA. port 0 turns off plain-text connections. tls-auth-clients optional = clients do not need their own certificate; yes = mutual TLS (each client needs a certificate).
  5. Connect — --askpass asks for the password (it is not saved in the shell history). Test what the app user may do, and list the users as admin:
redis-cli --tls --cacert /etc/redis/tls/ca.crt -h redis.example.com --user app --askpass SET app:test 1
redis-cli --tls --cacert /etc/redis/tls/ca.crt -h redis.example.com --user app --askpass SET other:test 1
redis-cli --tls --cacert /etc/redis/tls/ca.crt -h redis.example.com --user admin --askpass ACL LIST
  1. App connection string — rediss:// (two s) means TLS:
app config
REDIS_URL=rediss://app:Long-Random-App-Password@redis.example.com:6379/0

Load it from a secret (environment variable or secret file), never from the Git repo.

  1. Watch denied commands — failed logins and blocked commands are in the ACL log:
redis-cli --tls --cacert /etc/redis/tls/ca.crt -h redis.example.com --user admin --askpass ACL LOG 10
  1. Change users without restart — ACL SETUSER … then ACL SAVE writes the file. Rotate the app password with two passwords on the user (>new added, later <old removed).

190.3

Check that it worked

ping with the app user returns PONG; SET app:test 1 returns OK but SET other:test 1 and FLUSHALL fail with NOPERM; a connection without TLS (redis-cli -h redis.example.com ping) fails; a connection without a user gets NOAUTH Authentication required (tested with Redis 7).

190.4

Common problems

ProblemCauseFix
NOPERM this user has no permissions to access one of the keysKey pattern does not match.Use the app: prefix in the app, or widen ~ for that user.
Error: Connection reset by peerClient speaks plain text to a TLS port.--tls / rediss://; check the CA file.
certificate verify failedWrong CA, or the server name is not in the certificate.Use the right CA; connect with the name in the certificate.
Redis does not start after the changeACL file syntax error, or both requirepass and aclfile.Check the log; do not mix requirepass with aclfile.
Old app breaksIt only sends a password, not a user.Upgrade the client; it must support AUTH user password.
Replica or Sentinel loses the connectionThey also need a user and TLS settings.masteruser/masterauth, tls-replication yes, users for Sentinel.

190.5

Go further

1Why turn off the default Redis user?

The default user is used by any client that does not log in; turning it off forces every client to use its own named user.

2What does ~app:* in a Redis ACL mean?

The user may only use keys whose names start with app: — other keys are refused with NOPERM.

All commands in this chapter