Install and Secure Redis
Install Redis from the official repository on Debian or Ubuntu: ACL users, local-only listening, RDB and AOF persistence, memory limit, backup and restore.
On this page
Your values
Enter them once and the commands in this guide update.
This guide explains how to install Redis on your VPS or dedicated server from the official repository, protect it with ACL users, choose the right persistence, limit its memory and back up its data.
Redis is designed to be used by trusted clients. It therefore stays reachable from the local machine only here, and each application connects with an account that has limited privileges.
Order a server
Redis keeps its data in memory: plan enough RAM for your data and for background saves. HostMyServers offers several plans:
Prerequisites
- A server running Debian 12 or 13, or Ubuntu 22.04 or 24.04
- SSH access with a user that has
sudo - An up-to-date system (
sudo apt update && sudo apt upgrade)
Ports to open
None. Redis listens on port 6379 of 127.0.0.1 and ::1. To reach it from your computer, use the SSH tunnel described further down, which goes through your already open SSH port.
Install Redis from the official repository
At the time of writing, the official repository offers Redis 8.10 for Debian 12 and 13 as well as Ubuntu 22.04 and 24.04.
sudo apt install -y lsb-release curl gpg
curl -fsSL https://packages.redis.io/gpg | sudo gpg --dearmor -o /usr/share/keyrings/redis-archive-keyring.gpg
sudo chmod 644 /usr/share/keyrings/redis-archive-keyring.gpg
echo "deb [signed-by=/usr/share/keyrings/redis-archive-keyring.gpg] https://packages.redis.io/deb $(lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/redis.list
sudo apt update
sudo apt install -y redis
sudo systemctl enable --now redis-serverCheck the installation
redis-server --version
sudo systemctl status redis-server --no-pager
redis-cli ping
sudo ss -tlnp | grep 6379redis-cli ping must answer PONG. ss must show a listener on 127.0.0.1:6379 and [::1]:6379 only.
| Item | Location |
|---|---|
| Package configuration | /etc/redis/redis.conf |
| Your settings (created later) | /etc/redis/local.conf |
| Users (created later) | /etc/redis/users.acl |
| Data | /var/lib/redis/ |
| Log | /var/log/redis/redis-server.log |
Create the ACL users
By default, any local connection can run every command. You will disable this default user and create three accounts:
| User | Privileges |
|---|---|
admin |
Full administration |
app |
Read and write on app:* keys, without dangerous commands |
backup |
Triggering backups only, with no access to keys |
-
Generate the passwords and the users file. Only their SHA-256 hashes are stored in it:
TerminalADMIN_PASS="$(openssl rand -hex 32)" APP_PASS="$(openssl rand -hex 32)" BACKUP_PASS="$(openssl rand -hex 32)" sha() { printf '%s' "$1" | sha256sum | cut -d' ' -f1; } sudo install -o redis -g redis -m 640 /dev/null /etc/redis/users.acl sudo tee /etc/redis/users.acl > /dev/null <<EOF user default off user admin on #$(sha "$ADMIN_PASS") ~* &* +@all user app on #$(sha "$APP_PASS") ~app:* resetchannels &app:* -@all +@read +@write +@connection +@transaction -@dangerous user backup on #$(sha "$BACKUP_PASS") resetchannels -@all +ping +bgsave +lastsave EOF -
Keep the backup password for the script, then display the other two so you can store them in your password manager:
Terminalprintf '%s' "$BACKUP_PASS" | sudo sh -c 'umask 077; cat > /root/.redis-backup-pass' printf 'admin: %s\napp: %s\n' "$ADMIN_PASS" "$APP_PASS" unset ADMIN_PASS APP_PASS BACKUP_PASS -
Create your settings file. It stays separate from
redis.conf, which makes updates simpler:Terminalsudo install -o redis -g redis -m 640 /dev/null /etc/redis/local.conf sudo nano /etc/redis/local.confConfigurationaclfile /etc/redis/users.acl maxmemory 512mb maxmemory-policy noevictionAdjust
maxmemoryto your server, as explained in the memory section. -
Load this file at the end of the configuration, then restart:
Terminalecho 'include /etc/redis/local.conf' | sudo tee -a /etc/redis/redis.conf sudo systemctl restart redis-server -
Check the result:
Terminalredis-cli ping redis-cli --user admin --askpass ping redis-cli --user app --askpassThe first command must answer
NOAUTH Authentication required.and the secondPONGafter you enter theadminpassword. In the session opened by the third, with theapppassword:TextSET app:test ok GET app:test DEL app:test SET autre:test non FLUSHALLThe first three commands must succeed. The last two must be rejected with a
NOPERMerror.
In your application, specify the app user and its password, and prefix keys with app:. If it needs other commands, adjust its line in users.acl, then reload the file from an admin session with ACL LOAD.
Access Redis from your computer
Open an SSH tunnel and leave this command running:
ssh -N -L 6380:127.0.0.1:6379 YOUR_USER@YOUR_SERVER_IPIn another terminal, connect through local port 6380:
redis-cli -p 6380 --user admin --askpassIf a remote application really cannot go through a tunnel or a private network, configure TLS while keeping the ACLs, and restrict the port to that client's address in the firewall.
Choose persistence
| Mode | How it works | Risk of loss | Recommended use |
|---|---|---|---|
| RDB | Regular snapshots in dump.rdb |
Since the last snapshot | Data that can be partly recreated |
| AOF | Write log, synced every second by default | About one second | Important data |
| RDB + AOF | Both at once | About one second | The best choice for data you want to keep |
| None | Everything is lost on restart | Total | Pure cache |
RDB is enabled by default, with the thresholds 3600 1 300 100 60 10000: a snapshot after 1 hour if at least 1 key changed, after 5 minutes for 100 keys, after 1 minute for 10,000 keys.
Enable AOF without losing data
-
Open an
adminsession:Terminalredis-cli --user admin --askpass -
Enable AOF and follow its creation:
TextCONFIG SET appendonly yes INFO persistenceRepeat
INFO persistenceuntil you getaof_rewrite_in_progress:0,aof_rewrite_scheduled:0andaof_last_bgrewrite_status:ok. -
Add the following line to
/etc/redis/local.confto keep this setting:Configurationappendonly yes -
Restart Redis, then check with
DBSIZEthat the number of keys has not changed:Terminalsudo systemctl restart redis-server
Use Redis as a pure cache
If all the data can be rebuilt, add these lines to /etc/redis/local.conf and restart:
save ""
appendonly no
maxmemory-policy allkeys-lruControl memory
Redis has no memory limit by default on a 64-bit system. Always set maxmemory, leaving room for the system, your other services and Redis buffers. During a save on an instance with heavy writes, Redis can use up to twice its usual memory.
| Policy | Behavior at the limit | Use |
|---|---|---|
noeviction |
Rejects new writes | Data you must not lose |
allkeys-lru |
Removes the least recently used keys | General cache |
allkeys-lfu |
Removes the least frequently used keys | Cache with very popular keys |
volatile-lru |
Removes among keys that have an expiration | Mixed cache and data |
volatile-ttl |
Removes the keys whose expiration is nearest | Well-controlled expirations |
Enable the kernel setting recommended by Redis to make background saves more reliable:
echo 'vm.overcommit_memory = 1' | sudo tee /etc/sysctl.d/90-redis.conf
sudo sysctl --systemTo monitor usage from an admin session:
INFO memory
INFO stats
MEMORY DOCTORPay particular attention to used_memory_human, maxmemory_human, mem_fragmentation_ratio and evicted_keys.
Automate backups
The RDB file can be copied while Redis is running: it is only replaced once the new snapshot is complete. The script requests a fresh snapshot, waits for it to finish, then copies it.
-
Create the script:
Terminalsudo nano /usr/local/sbin/redis-backup.shTerminal#!/bin/bash set -euo pipefail umask 077 DEST="/var/backups/redis" RETENTION_DAYS=7 export REDISCLI_AUTH="$(cat /root/.redis-backup-pass)" cli() { redis-cli --user backup "$@"; } mkdir -p "$DEST" before="$(cli LASTSAVE)" cli BGSAVE SCHEDULE > /dev/null now="$before" for _ in $(seq 1 1800); do sleep 1 now="$(cli LASTSAVE)" [ "$now" -gt "$before" ] && break done if [ "$now" -le "$before" ]; then echo "BGSAVE did not finish in time" >&2 exit 1 fi cp /var/lib/redis/dump.rdb "$DEST/dump-$(date +%F_%H%M%S).rdb" find "$DEST" -name 'dump-*.rdb' -mtime +"$RETENTION_DAYS" -delete -
Create the service:
Terminalsudo chmod 700 /usr/local/sbin/redis-backup.sh sudo nano /etc/systemd/system/redis-backup.serviceConfiguration[Unit] Description=Redis backup After=redis-server.service [Service] Type=oneshot ExecStart=/usr/local/sbin/redis-backup.sh -
Create the timer:
Terminalsudo nano /etc/systemd/system/redis-backup.timerConfiguration[Unit] Description=Daily Redis backup [Timer] OnCalendar=*-*-* 02:45:00 RandomizedDelaySec=10min Persistent=true [Install] WantedBy=timers.target -
Enable it and run a first backup:
Terminalsudo systemctl daemon-reload sudo systemctl enable --now redis-backup.timer sudo systemctl start redis-backup.service sudo ls -lh /var/backups/redis
Test the restore
This test loads the latest backup into a temporary instance reachable only through a local socket. Your production instance is not modified.
LATEST="$(sudo sh -c 'ls -1t /var/backups/redis/dump-*.rdb | head -n 1')"
sudo install -d -o redis -g redis -m 750 /var/lib/redis-restore-test
sudo install -o redis -g redis -m 640 "$LATEST" /var/lib/redis-restore-test/dump.rdb
sudo -u redis redis-server --port 0 --unixsocket /var/lib/redis-restore-test/redis.sock --unixsocketperm 700 --dir /var/lib/redis-restore-test --dbfilename dump.rdb --appendonly no --save "" --daemonize yes --logfile /var/lib/redis-restore-test/redis.log
sleep 2
sudo -u redis redis-cli -s /var/lib/redis-restore-test/redis.sock DBSIZECompare this number with the one from your production instance and read a few important keys with GET. Then stop the test instance and delete its files:
sudo -u redis redis-cli -s /var/lib/redis-restore-test/redis.sock SHUTDOWN NOSAVE
sudo rm -rf /var/lib/redis-restore-testIf your data uses the JSON, Search, TimeSeries or Bloom modules, add the matching --loadmodule options to the start command, with the paths given in /etc/redis/redis.conf.
Restore after an incident
When AOF is enabled, Redis loads its data from AOF and ignores dump.rdb. The procedure therefore disables AOF for the duration of the restore.
-
Stop the applications and Redis, then set the current files aside:
Terminalsudo systemctl stop redis-server sudo mkdir -p /var/backups/redis/before-restore sudo sh -c 'mv /var/lib/redis/dump.rdb /var/lib/redis/appendonlydir /var/backups/redis/before-restore/ 2>/dev/null || true' -
Copy the backup you chose, adjusting its name:
Terminalsudo install -o redis -g redis -m 660 /var/backups/redis/dump-2026-11-18_024500.rdb /var/lib/redis/dump.rdb -
If you use AOF, replace
appendonly yeswithappendonly noin/etc/redis/local.conf, then start Redis:Terminalsudo systemctl start redis-server -
Check your data with
DBSIZEin anadminsession. If you use AOF, re-enable it live withCONFIG SET appendonly yes, wait for its creation to finish as explained in the persistence section, then putappendonly yesback inlocal.conf.
Update Redis
Back up your data first, then update the package:
sudo systemctl start redis-backup.service
sudo apt update
sudo apt install --only-upgrade redis redis-server redis-tools
redis-server --version
sudo systemctl status redis-server --no-pagerIf apt reports that /etc/redis/redis.conf has changed, compare the versions before deciding. If you adopt the new version, add the include /etc/redis/local.conf line again at the end of the file. Also read the release notes before changing major version.
Troubleshooting
NOAUTH Authentication required.
The client did not authenticate, which is expected since default is disabled. Specify a user with --user and --askpass, or configure these values in your application.
WRONGPASS invalid username-password pair or user is disabled.
The user name or password is wrong. If you lost a password, generate a new one, replace its hash in /etc/redis/users.acl, then restart Redis or run ACL LOAD.
NOPERM
The user does not have access to this command or this key. From an admin session, ACL LOG shows what was denied. Only widen what is necessary.
OOM command not allowed when used memory > 'maxmemory'
The memory limit has been reached with a policy that does not free keys. Increase maxmemory if the server allows it, delete unneeded data, or choose allkeys-lru if Redis serves as a cache.
MISCONF Redis is configured to save RDB snapshots
The last snapshot failed, and Redis rejects writes to alert you. Check the disk space, the permissions and the log:
df -h /var/lib/redis
sudo ls -ld /var/lib/redis
sudo tail -n 50 /var/log/redis/redis-server.logOur guide Check disk space can help you free up space.
WARNING Memory overcommit must be enabled!
Apply the vm.overcommit_memory = 1 setting described in the memory section.
Redis no longer starts
An error in users.acl or local.conf prevents startup. Check the log:
sudo journalctl -u redis-server -n 50 --no-pager
sudo tail -n 50 /var/log/redis/redis-server.logAlso check that users.acl exists and that the redis user can read it. Do not define users in redis.conf when aclfile is used: Redis refuses to mix the two methods.
Could not connect to Redis at 127.0.0.1:6379: Connection refused
Redis is stopped or listening elsewhere. Check sudo systemctl status redis-server and sudo ss -tlnp | grep redis.