現(xiàn)在用redis緩存熱數(shù)據(jù)越來越常見了,甚至一些配置,開關(guān)等等的東西也寫到redis里。原因就是redis簡單高效。redis里的數(shù)據(jù)也越來越重要了,例如一些業(yè)務的中間數(shù)據(jù)會暫時存放在redis里,所以限制redis的訪問還是很有必要。
本文通過幾個手段說一下生產(chǎn)環(huán)境中redis的訪問權(quán)限控制。
1、綁定網(wǎng)卡bind
redis的配置文件redis.conf中對于網(wǎng)絡安全部分有這樣一段話
################################## NETWORK #####################################
# By default, if no "bind" configuration directive is specified, Redis listens
# for connections from all the network interfaces available on the server.
# It is possible to listen to just one or multiple selected interfaces using
# the "bind" configuration directive, followed by one or more IP addresses.
#
# ~~~ WARNING ~~~ If the computer running Redis is directly exposed to the
# internet, binding to all the interfaces is dangerous and will expose the
# instance to everybody on the internet. So by default we uncomment the
# following bind directive, that will force Redis to listen only into
# the IPv4 lookback interface address (this means Redis will be able to
# accept connections only from clients running into the same computer it
# is running).
這段話的意思道出了bind的深意:bind的意思是你的redis實例綁定在哪個interface上,可以理解成綁定在哪個網(wǎng)卡上。那么我們有幾個網(wǎng)卡呢?可以看一下。
$ ifconfig
eth0 Link encap:Ethernet HWaddr 6C:92:BF:22:D7:FC
inet addr:10.93.84.53 Bcast:10.93.84.127 Mask:255.255.255.128
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
這里就兩個,一個是eth0以太網(wǎng)卡(10.93.84.53),一個是本地回路lo(127.0.0.1)。
那么會有這兩種情況:
1) bind 10.93.84.53 #同一網(wǎng)段的所有主機都可以連接redis
2) bind 127.0.0.1 #本地回路,那么只有你redis實例所在的主機能訪問redis
你可以根據(jù)你的需要進行使用:
1) 如果你的機器直接暴露給互聯(lián)網(wǎng),那么你還是慎重的將bind設(shè)置為127.0.0.1吧,否則你相當于暴露了你的redis給外部所有攻擊。
2) 如果你再生產(chǎn)環(huán)境中,那么你一般會需要綁在網(wǎng)卡上,以便其他主機也能訪問redis,那么我們會有一些其他的方式保證redis數(shù)據(jù)的安全。
2、設(shè)置密碼requirepass
redis.conf里有這樣的配置,設(shè)置了密碼之后。
#requirepass yourpassword>
requirepass mypass
重啟redis服務后,你的客戶端都需要通過密碼的方式訪問redis
# 密碼正確
$ redis-cli -h 10.93.84.53 -p 6379 -a mypass ping
PONG
# 密碼錯誤
$ redis-cli -h 10.93.84.53 -p 6379 -a hehe ping
(error) NOAUTH Authentication required.
3、nologin降低賬號權(quán)限
以較低權(quán)限賬號運行Redis服務,且禁用該賬號的登錄權(quán)限。另外可以限制攻擊者往敏感寫入文件,但是Redis數(shù)據(jù)還是能被黑客訪問到,或者被黑客惡意刪除。
禁止Linux用戶登錄的方法,一般是修改用戶的shell類型為/sbin/nologin
這種方式會更加人性化一點,因為不僅可以禁止用戶登錄,還可以在禁用登陸時給提示告訴它這么做的原因。
修改/etc/nologin.txt,沒有的話就手動新建一個,在里面添加給被禁止用戶的提示(這種方式的所有用戶的鎖定信息都在這個文件中,在登陸時給與提示)。
其實就是把/etc/passwd文件里的/bin/bash對應改成/sbin/nologin
[root@host-192-168-1-117 ~]# useradd wangshibo
[root@host-192-168-1-117 ~]# echo "123456"|passwd --stdin wangshibo
Changing password for user wangshibo.
passwd: all authentication tokens updated successfully.
[root@host-192-168-1-117 ~]# cat /etc/passwd|grep wangshibo
wangshibo:x:500:500::/home/wangshibo:/bin/bash
[root@host-192-168-1-117 ~]# sed -i 's#/home/wangshibo:/bin/bash#/home/wangshibo:/sbin/nologin#g' /etc/passwd
[root@host-192-168-1-117 ~]# cat /etc/passwd|grep wangshibo
wangshibo:x:500:500::/home/wangshibo:/sbin/nologin
[root@host-192-168-1-117 ~]# touch /etc/nologin.txt
[root@host-192-168-1-117 ~]# cat /etc/nologin.txt
In order to protect the system security, this type of user is locked!
4、iptables設(shè)置防火墻
在生產(chǎn)環(huán)境中設(shè)置防火墻還是很有必要的。如果正常業(yè)務中Redis服務需要被其他服務器來訪問,可以設(shè)置iptables策略僅允許指定的IP來訪問Redis服務。
在你redis實例所在的主機,執(zhí)行如下命令。
# 查看iptables規(guī)則配置的規(guī)則
$ iptables -nL --line-number
# 禁止所有的主機訪問本機6379端口
$ iptables -I INPUT -p TCP --dport 6379 -j DROP
# 打開如下的機器-s指定,這些機器可以訪問本機的6379端口
$ iptables -I INPUT -s 10.93.21.21 -p tcp --dport 6379 -j ACCEPT
$ iptables -I INPUT -s 10.93.18.34 -p tcp --dport 6379 -j ACCEPT
$ iptables -I INPUT -s 10.93.18.35 -p tcp --dport 6379 -j ACCEPT
$ iptables -I INPUT -s 10.93.84.53 -p tcp --dport 6379 -j ACCEPT
# 保存iptables配置
$ service iptables save
iptables: Saving firewall rules to /etc/sysconfig/iptables:[ OK ]
# 重啟防火墻
$ service iptables restart
iptables: Setting chains to policy ACCEPT: filter [ OK ]
iptables: Flushing firewall rules: [ OK ]
iptables: Unloading modules: [ OK ]
iptables: Applying firewall rules: [ OK ]
設(shè)置成功后,只有配置的那四臺機器可以訪問redis實例。
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
您可能感興趣的文章:- Docker 啟動Redis 并設(shè)置密碼的操作
- Docker安裝Tomcat、MySQL和Redis的步驟詳解
- docker安裝redis設(shè)置密碼并連接的操作