背景
? ? 我們知道,redis默認是不配置密碼的,這就造成只要有redis的IP+Port就可以無需驗證,登陸redis。如果恰巧你的redis是開放在公網上的,很容易就被******,獲取你的系統權限,經常被黑去當成了礦機。
? ? redis的安全,配置防火墻當然是一種方法,但是,給redis配置一個密碼,也是一個不錯的選擇。
環境
? redis:
? ? ? 192.168.1.227:6379(master)
? ? ? 192.168.1.227:6380(slave)
? ? ? 192.168.1.227:6381(slave)
? redis Sentinel:
? ? ? 192.168.1.227:26379
? ? ? 192.168.1.227:26380
? ? ? 192.168.1.227:26381
操作配置(redis的部署這里不描述,redis的安裝部署可參考文章:http://blog.51cto.com/icenycmh/1792017)
注:該文章中的redis和哨兵配置均為密碼驗證所需的部分配置。
? ? ?redis的密碼是直接配置在配置文件中的,如下:
----192.168.1.227:6379(redis?Master) #?vi?/path/to/conf/6379.conf requirepass?123456???????????-----配置redis?Master密碼為123456
----192.168.1.227:6380、192.168.1.227:6381(redis?Slave) #?vi?/path/to/conf/6379.conf requirepass?123456???????????-----配置redis?Slave密碼為123456 masterauth?123456????????????-----由于slave需要和master交互,在slave上需配置master的密碼驗證
? ? 開啟redis:
#/path/to/redis/bin/redis-server?/path/to/conf/6379.conf #/path/to/redis/bin/redis-server?/path/to/conf/6380.conf #/path/to/redis/bin/redis-server?/path/to/conf/6381.conf
測試密碼驗證
----不提供密碼,連接redis查看信息,提示需要驗證 #?/path/to/redis/bin/redis-cli?-h?192.168.1.227?-p?6379?info?Replication??????????? NOAUTH?Authentication?required.----提供密碼,連接redis查看信息,正常顯示,slave連接正常 #?/path/to/redis/bin/redis-cli?-h?192.168.1.227?-p?6379?-a?123456?info?Replication? #?Replication role:master connected_slaves:2 slave0:ip=192.168.1.227,port=6380,state=online,offset=35215766,lag=1 slave1:ip=192.168.1.227,port=6381,state=online,offset=35215780,lag=1 master_repl_offset:35216203 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:34167628 repl_backlog_histlen:1048576
redis Sentinel
? ? 如果系統中使用了redis 哨兵集群,由于在切換master的時候,原本的master可能變成slave,故也需要在原本redis master上配置masterauth:
#?vi?/path/to/conf/6379.conf masterauth?123456
? ? 在哨兵的配置中,也需要填入獲取到的master密碼:
#?vi?/path/to/conf/sentinel.conf sentinel?auth-pass?master?123456?????????----master為你的自定義哨兵集群master字符串