Install Redis in Ubuntu 16.04 along with exposing the port & adding password


This post shows you how to install Redis in the Ubuntu machine and setting up for a production i.e exposing the port outside from the machine and adding a password-based authentication.

1. Install Redis

Run the following series commands to install Redis

sudo apt-get update
sudo apt-get install redis-server

2. Expose the port (changing bind port)

With the default configurations, Redis is not exposed outside of the machine i.e bind port value is 127.0.0.1 we need to change this to 0.0.0.0 so that we can access the Redis from outside of the machine.
Open the redis configuration file, located at /etc/redis/redis.conf

and find the bind config and change it to 0.0.0.0 from 127.0.0.1

old value:

bind 127.0.0.1

new value:

bind 0.0.0.0

3. Add security with an auth password

With the default configurations, Redis does not add any password to add password go to the SECURITY section in conf file, which looks like following

################################## SECURITY ###################################

# Require clients to issue AUTH <PASSWORD> before processing any other
# commands.  This might be useful in environments in which you do not trust
# others with access to the host running redis-server.
#
# This should stay commented out for backward compatibility and because most
# people do not need auth (e.g. they run their own servers).
#
# Warning: since Redis is pretty fast an outside user can try up to
# 150k passwords per second against a good box. This means that you should
# use a very strong password otherwise it will be very easy to break.
#
# requirepass foobared

find the conf requirepass and add your new password

old value:

# requirepass foobared

new value:

requirepass <new password>

4. Restart the server to make the changes get effected

Run the following command to restart the server

sudo service redis-server restart

the all required changes are done, now you can test the Redis connection using redis-cli