Securing Redis via IPTables

Here's a simple script to secure Redis via IPTables (tested on CentOS 6.3):

#!/usr/bin/env bash

# redis_secure.sh

# this script will add an ip address to iptables
# allowing the ip address to connect to redis

# should be run with localhost first

IPADDRESS="$1"
if ! test "$IPADDRESS"; then
    echo "Please enter the IP Address you want to be able to connection to Redis."
    exit 1
fi

sudo iptables -A INPUT -s $IPADDRESS -p tcp -m tcp --dport 6379 -j ACCEPT
sudo bash -c 'iptables-save > /etc/sysconfig/iptables'

Then run as follows:

$ ./redis_secure.sh localhost
$ ./redis_secure.sh 555.555.555.555 # < your ip goes here

Published on in Linux