Install and configure Redis 7 with clustering and security hardening

Intermediate 45 min Mar 31, 2026 29 views
Ubuntu 24.04 Ubuntu 22.04 Debian 12 AlmaLinux 9 Rocky Linux 9 Fedora 41

Set up Redis 7 with master-replica clustering, SSL/TLS encryption, ACL authentication, and production-grade security hardening. Includes performance tuning, backup strategies, and monitoring configuration for high-availability deployments.

Prerequisites

  • Root or sudo access
  • At least 4GB RAM
  • Network connectivity between cluster nodes
  • Basic understanding of Redis concepts

What this solves

Redis is a high-performance in-memory data store used for caching, session management, and real-time applications. This tutorial shows you how to install Redis 7 with production-grade clustering, security hardening including SSL/TLS encryption and ACL authentication, plus performance optimization and backup configuration.

Step-by-step installation

Update system packages

Start by updating your package manager to ensure you get the latest versions.

sudo apt update && sudo apt upgrade -y
sudo dnf update -y

Install Redis 7 from official repository

Add the official Redis repository to get the latest Redis 7 version with all security updates.

curl -fsSL https://packages.redis.io/gpg | sudo gpg --dearmor -o /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-server redis-tools
sudo dnf install -y https://packages.redis.io/rpm/el9/x86_64/redis-7.2.4-1.el9.x86_64.rpm

Create Redis user and directories

Create a dedicated redis user and set up proper directory structure with correct permissions.

sudo useradd --system --home /var/lib/redis --shell /bin/false redis
sudo mkdir -p /etc/redis /var/lib/redis /var/log/redis
sudo chown redis:redis /var/lib/redis /var/log/redis
sudo chmod 755 /var/lib/redis
sudo chmod 755 /var/log/redis

Generate SSL certificates for Redis

Create SSL certificates for encrypted client-server and inter-cluster communication.

sudo mkdir -p /etc/redis/tls
cd /etc/redis/tls
sudo openssl genrsa -out ca-key.pem 4096
sudo openssl req -x509 -new -nodes -key ca-key.pem -sha256 -days 3650 -out ca-cert.pem -subj "/C=US/ST=State/L=City/O=Organization/CN=Redis-CA"
sudo openssl genrsa -out redis-key.pem 2048
sudo openssl req -new -key redis-key.pem -out redis.csr -subj "/C=US/ST=State/L=City/O=Organization/CN=redis-server"
sudo openssl x509 -req -in redis.csr -CA ca-cert.pem -CAkey ca-key.pem -CAcreateserial -out redis-cert.pem -days 365 -sha256
sudo chown -R redis:redis /etc/redis/tls
sudo chmod 600 /etc/redis/tls/*.pem
sudo rm redis.csr
Never use chmod 777. It gives every user on the system full access to your files. Instead, fix ownership with chown and use minimal permissions like 600 for private keys.

Configure Redis master node

Create the main Redis configuration with security hardening, SSL, and clustering enabled.

# Network and security
bind 127.0.0.1 203.0.113.10
port 0
tls-port 6380
tls-cert-file /etc/redis/tls/redis-cert.pem
tls-key-file /etc/redis/tls/redis-key.pem
tls-ca-cert-file /etc/redis/tls/ca-cert.pem
tls-protocols "TLSv1.2 TLSv1.3"
tls-ciphersuites TLS_AES_256_GCM_SHA384:TLS_CHACHA20_POLY1305_SHA256
tls-ciphers ECDHE-RSA-AES256-GCM-SHA384:ECDHE-RSA-AES128-GCM-SHA256
protected-mode yes

Authentication and ACL

requirepass Str0ng_R3d1s_P@ssw0rd_2024! aclfile /etc/redis/users.acl

General configuration

timeout 300 tcp-keepalive 300 daemonize yes pidfile /var/run/redis/redis-master.pid loglevel notice logfile /var/log/redis/redis-master.log

Memory and persistence

maxmemory 2gb maxmemory-policy allkeys-lru save 900 1 save 300 10 save 60 10000 stop-writes-on-bgsave-error yes rdbcompression yes rdbchecksum yes dbfilename dump-master.rdb dir /var/lib/redis

Append Only File

appendonly yes appendfilename "appendonly-master.aof" appendfsync everysec no-appendfsync-on-rewrite no auto-aof-rewrite-percentage 100 auto-aof-rewrite-min-size 64mb

Clustering

cluster-enabled yes cluster-config-file nodes-master.conf cluster-node-timeout 15000 cluster-announce-hostname redis-master.example.com cluster-announce-port 6380 cluster-announce-tls-port 6380

Security hardening

disable-thp yes rename-command FLUSHDB "" rename-command FLUSHALL "" rename-command DEBUG "" rename-command CONFIG "CONFIG_8f3a9d2e1b7c6405"

Configure Redis ACL users

Create ACL users with specific permissions for different application roles.

# Default user (disabled)
user default off

Admin user with full access

user admin on >Adm1n_R3d1s_P@ss_2024! allcommands allkeys

Application user with limited permissions

user app_user on >App_R3d1s_P@ss_2024! ~app:* +@read +@write +@string +@list +@set +@hash +@sortedset -@dangerous

Read-only user for monitoring

user monitor on >Mon1t0r_R3d1s_P@ss_2024! +@read +ping +info +client

Configure Redis replica node

Set up a replica node for high availability and load distribution.

# Network and security
bind 127.0.0.1 203.0.113.11
port 0
tls-port 6381
tls-cert-file /etc/redis/tls/redis-cert.pem
tls-key-file /etc/redis/tls/redis-key.pem
tls-ca-cert-file /etc/redis/tls/ca-cert.pem
tls-protocols "TLSv1.2 TLSv1.3"
tls-ciphersuites TLS_AES_256_GCM_SHA384:TLS_CHACHA20_POLY1305_SHA256
tls-ciphers ECDHE-RSA-AES256-GCM-SHA384:ECDHE-RSA-AES128-GCM-SHA256
protected-mode yes

Replication

replicaof 203.0.113.10 6380 masterauth Str0ng_R3d1s_P@ssw0rd_2024! requirepass Str0ng_R3d1s_P@ssw0rd_2024! aclfile /etc/redis/users.acl

General configuration

timeout 300 tcp-keepalive 300 daemonize yes pidfile /var/run/redis/redis-replica.pid loglevel notice logfile /var/log/redis/redis-replica.log

Memory and persistence

maxmemory 2gb maxmemory-policy allkeys-lru replica-read-only yes replica-serve-stale-data yes dbfilename dump-replica.rdb dir /var/lib/redis

Append Only File

appendonly yes appendfilename "appendonly-replica.aof" appendfsync everysec

Clustering

cluster-enabled yes cluster-config-file nodes-replica.conf cluster-node-timeout 15000 cluster-announce-hostname redis-replica.example.com cluster-announce-port 6381 cluster-announce-tls-port 6381

Security hardening

disable-thp yes rename-command FLUSHDB "" rename-command FLUSHALL "" rename-command DEBUG "" rename-command CONFIG "CONFIG_8f3a9d2e1b7c6405"

Set file permissions and ownership

Apply correct ownership and minimal permissions to all Redis configuration files.

sudo chown redis:redis /etc/redis/redis-master.conf /etc/redis/redis-replica.conf /etc/redis/users.acl
sudo chmod 640 /etc/redis/redis-master.conf /etc/redis/redis-replica.conf
sudo chmod 600 /etc/redis/users.acl
sudo mkdir -p /var/run/redis
sudo chown redis:redis /var/run/redis
sudo chmod 755 /var/run/redis

Create systemd service files

Set up systemd services for both Redis master and replica nodes.

[Unit]
Description=Redis Master Server
After=network.target

[Service]
Type=forking
User=redis
Group=redis
ExecStart=/usr/bin/redis-server /etc/redis/redis-master.conf
ExecStop=/usr/bin/redis-cli --tls --cert /etc/redis/tls/redis-cert.pem --key /etc/redis/tls/redis-key.pem --cacert /etc/redis/tls/ca-cert.pem -p 6380 shutdown
TimeoutStopSec=0
Restart=always
RestartSec=5
RuntimeDirectory=redis
RuntimeDirectoryMode=0755

Security settings

NoNewPrivileges=yes PrivateTmp=yes PrivateDevices=yes ProtectHome=yes ProtectSystem=strict ReadWritePaths=/var/lib/redis /var/log/redis /var/run/redis [Install] WantedBy=multi-user.target

Create Redis replica service

Create the systemd service file for the replica node.

[Unit]
Description=Redis Replica Server
After=network.target

[Service]
Type=forking
User=redis
Group=redis
ExecStart=/usr/bin/redis-server /etc/redis/redis-replica.conf
ExecStop=/usr/bin/redis-cli --tls --cert /etc/redis/tls/redis-cert.pem --key /etc/redis/tls/redis-key.pem --cacert /etc/redis/tls/ca-cert.pem -p 6381 shutdown
TimeoutStopSec=0
Restart=always
RestartSec=5
RuntimeDirectory=redis
RuntimeDirectoryMode=0755

Security settings

NoNewPrivileges=yes PrivateTmp=yes PrivateDevices=yes ProtectHome=yes ProtectSystem=strict ReadWritePaths=/var/lib/redis /var/log/redis /var/run/redis [Install] WantedBy=multi-user.target

Configure system limits for Redis

Optimize system limits for Redis performance and memory management.

redis soft nofile 65535
redis hard nofile 65535
redis soft memlock unlimited
redis hard memlock unlimited

Configure kernel parameters

Set optimal kernel parameters for Redis performance and security.

# Memory overcommit for Redis
vm.overcommit_memory = 1

Disable transparent huge pages

vm.nr_hugepages = 0

TCP settings for Redis

net.core.somaxconn = 65535 net.ipv4.tcp_max_syn_backlog = 65535

Network security

net.ipv4.conf.all.rp_filter = 1 net.ipv4.conf.default.rp_filter = 1 net.ipv4.icmp_echo_ignore_broadcasts = 1 net.ipv4.icmp_ignore_bogus_error_responses = 1

Apply kernel parameters

Load the new kernel parameters and disable transparent huge pages.

sudo sysctl -p /etc/sysctl.d/redis.conf
echo never | sudo tee /sys/kernel/mm/transparent_hugepage/enabled
echo 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' | sudo tee -a /etc/rc.local
sudo chmod +x /etc/rc.local

Configure firewall rules

Set up firewall rules to allow Redis cluster communication securely.

sudo ufw allow from 203.0.113.0/24 to any port 6380 proto tcp comment 'Redis master TLS'
sudo ufw allow from 203.0.113.0/24 to any port 6381 proto tcp comment 'Redis replica TLS'
sudo ufw allow from 203.0.113.0/24 to any port 16380 proto tcp comment 'Redis cluster bus'
sudo ufw reload
sudo firewall-cmd --permanent --add-rich-rule='rule family="ipv4" source address="203.0.113.0/24" port protocol="tcp" port="6380" accept'
sudo firewall-cmd --permanent --add-rich-rule='rule family="ipv4" source address="203.0.113.0/24" port protocol="tcp" port="6381" accept'
sudo firewall-cmd --permanent --add-rich-rule='rule family="ipv4" source address="203.0.113.0/24" port protocol="tcp" port="16380" accept'
sudo firewall-cmd --reload

Start and enable Redis services

Start both Redis master and replica services and enable them for automatic startup.

sudo systemctl daemon-reload
sudo systemctl enable --now redis-master redis-replica
sudo systemctl status redis-master redis-replica

Configure Redis backup script

Create an automated backup script for Redis data with compression and rotation.

#!/bin/bash

Redis backup configuration

BACKUP_DIR="/var/backups/redis" RETENTION_DAYS=7 DATE=$(date +%Y%m%d_%H%M%S)

Create backup directory

mkdir -p $BACKUP_DIR

Redis connection details

REDIS_CLI="/usr/bin/redis-cli --tls --cert /etc/redis/tls/redis-cert.pem --key /etc/redis/tls/redis-key.pem --cacert /etc/redis/tls/ca-cert.pem"

Backup master node

echo "Starting Redis backup at $(date)" $REDIS_CLI -p 6380 -a "Str0ng_R3d1s_P@ssw0rd_2024!" --no-auth-warning BGSAVE

Wait for background save to complete

while [ $($REDIS_CLI -p 6380 -a "Str0ng_R3d1s_P@ssw0rd_2024!" --no-auth-warning LASTSAVE) -eq $($REDIS_CLI -p 6380 -a "Str0ng_R3d1s_P@ssw0rd_2024!" --no-auth-warning LASTSAVE) ]; do sleep 1 done

Copy and compress backup files

cp /var/lib/redis/dump-master.rdb $BACKUP_DIR/redis-master-$DATE.rdb cp /var/lib/redis/appendonly-master.aof $BACKUP_DIR/redis-master-$DATE.aof gzip $BACKUP_DIR/redis-master-$DATE.rdb gzip $BACKUP_DIR/redis-master-$DATE.aof

Remove old backups

find $BACKUP_DIR -name "redis-master-*.gz" -mtime +$RETENTION_DAYS -delete echo "Redis backup completed at $(date)"

Set backup script permissions and schedule

Make the backup script executable and schedule it to run daily via cron.

sudo chmod 750 /etc/redis/backup.sh
sudo chown redis:redis /etc/redis/backup.sh
sudo mkdir -p /var/backups/redis
sudo chown redis:redis /var/backups/redis
sudo chmod 755 /var/backups/redis
echo '0 2   * redis /etc/redis/backup.sh >> /var/log/redis/backup.log 2>&1' | sudo tee -a /etc/crontab

Configure Redis monitoring

Set up Redis monitoring script to check cluster health and performance metrics.

#!/bin/bash

Redis monitoring script

REDIS_CLI="/usr/bin/redis-cli --tls --cert /etc/redis/tls/redis-cert.pem --key /etc/redis/tls/redis-key.pem --cacert /etc/redis/tls/ca-cert.pem" LOG_FILE="/var/log/redis/monitor.log" DATE=$(date '+%Y-%m-%d %H:%M:%S')

Function to log messages

log_message() { echo "[$DATE] $1" >> $LOG_FILE }

Check Redis master status

if $REDIS_CLI -p 6380 -a "Str0ng_R3d1s_P@ssw0rd_2024!" --no-auth-warning ping > /dev/null 2>&1; then log_message "Redis master is running" else log_message "ERROR: Redis master is not responding" fi

Check Redis replica status

if $REDIS_CLI -p 6381 -a "Str0ng_R3d1s_P@ssw0rd_2024!" --no-auth-warning ping > /dev/null 2>&1; then log_message "Redis replica is running" else log_message "ERROR: Redis replica is not responding" fi

Check memory usage

MEM_USED=$($REDIS_CLI -p 6380 -a "Str0ng_R3d1s_P@ssw0rd_2024!" --no-auth-warning info memory | grep used_memory_human | cut -d: -f2 | tr -d '\r') log_message "Memory usage: $MEM_USED"

Check connected clients

CLIENTS=$($REDIS_CLI -p 6380 -a "Str0ng_R3d1s_P@ssw0rd_2024!" --no-auth-warning info clients | grep connected_clients | cut -d: -f2 | tr -d '\r') log_message "Connected clients: $CLIENTS"

Enable monitoring script

Make the monitoring script executable and schedule it to run every 5 minutes.

sudo chmod 750 /etc/redis/monitor.sh
sudo chown redis:redis /etc/redis/monitor.sh
echo '/5    * redis /etc/redis/monitor.sh' | sudo tee -a /etc/crontab

Verify your setup

Test the Redis installation, clustering, SSL connection, and authentication.

# Check Redis services status
sudo systemctl status redis-master redis-replica

Test SSL connection to master

redis-cli --tls --cert /etc/redis/tls/redis-cert.pem --key /etc/redis/tls/redis-key.pem --cacert /etc/redis/tls/ca-cert.pem -p 6380 -a "Str0ng_R3d1s_P@ssw0rd_2024!" --no-auth-warning ping

Test SSL connection to replica

redis-cli --tls --cert /etc/redis/tls/redis-cert.pem --key /etc/redis/tls/redis-key.pem --cacert /etc/redis/tls/ca-cert.pem -p 6381 -a "Str0ng_R3d1s_P@ssw0rd_2024!" --no-auth-warning ping

Check cluster status

redis-cli --tls --cert /etc/redis/tls/redis-cert.pem --key /etc/redis/tls/redis-key.pem --cacert /etc/redis/tls/ca-cert.pem -p 6380 -a "Str0ng_R3d1s_P@ssw0rd_2024!" --no-auth-warning cluster nodes

Test ACL authentication

redis-cli --tls --cert /etc/redis/tls/redis-cert.pem --key /etc/redis/tls/redis-key.pem --cacert /etc/redis/tls/ca-cert.pem -p 6380 --user app_user --pass "App_R3d1s_P@ss_2024!" set test:key "test value"

Verify replication

redis-cli --tls --cert /etc/redis/tls/redis-cert.pem --key /etc/redis/tls/redis-key.pem --cacert /etc/redis/tls/ca-cert.pem -p 6381 -a "Str0ng_R3d1s_P@ssw0rd_2024!" --no-auth-warning get test:key

Check Redis version and info

redis-cli --tls --cert /etc/redis/tls/redis-cert.pem --key /etc/redis/tls/redis-key.pem --cacert /etc/redis/tls/ca-cert.pem -p 6380 -a "Str0ng_R3d1s_P@ssw0rd_2024!" --no-auth-warning info server

Common issues

SymptomCauseFix
Redis won't startConfiguration syntax errorsudo journalctl -u redis-master -f to check logs
SSL connection failsCertificate permissions or path issuesCheck sudo ls -la /etc/redis/tls/ and verify ownership
ACL authentication rejectedUser permissions or password mismatchredis-cli ACL LIST to verify user configuration
Replication not workingNetwork connectivity or authenticationCheck replica logs: sudo tail -f /var/log/redis/redis-replica.log
High memory usageNo maxmemory policy setVerify maxmemory settings in configuration
Cluster nodes can't communicateFirewall blocking cluster bus portEnsure port 16380 is open between cluster nodes
Permission denied errorsIncorrect file ownershipsudo chown -R redis:redis /var/lib/redis /var/log/redis

Next steps

Automated install script

Run this to automate the entire setup

#redis #redis-cluster #redis-security #ssl-tls #acl-authentication #database-clustering #redis-backup #redis-monitoring

Need help?

Don't want to manage this yourself?

We handle infrastructure for businesses that depend on uptime. From initial setup to ongoing operations.

Talk to an engineer