Redis密碼設定與訪問限制(網路安全)
現在用redis快取熱資料越來越常見了,甚至一些配置,開關等等的東西也寫到redis裡。原因就是redis簡單高效。redis裡的資料也越來越重要了,例如一些業務的中間資料會暫時存放在redis裡,所以限制redis的訪問還是很有必要。
本文通過幾個手段說一下生產環境中redis的訪問許可權控制。
1、繫結網絡卡bind
redis的配置檔案redis.conf中對於網路安全部分有這樣一段話
################################## 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上,可以理解成繫結在哪個網絡卡上。那麼我們有幾個網絡卡呢?可以看一下。
$ 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乙太網卡(10.93.84.53),一個是本地迴路lo(127.0.0.1)。
那麼會有這兩種情況:
1) bind 10.93.84.53 #同一網段的所有主機都可以連線redis
2) bind 127.0.0.1 #本地迴路,那麼只有你redis例項所在的主機能訪問redis
你可以根據你的需要進行使用:
1) 如果你的機器直接暴露給網際網路,那麼你還是慎重的將bind設定為127.0.0.1吧,否則你相當於暴露了你的redis給外部所有攻擊。
2) 如果你再生產環境中,那麼你一般會需要綁在網絡卡上,以便其他主機也能訪問redis,那麼我們會有一些其他的方式保證redis資料的安全。
2、設定密碼requirepass
redis.conf裡有這樣的配置,設定了密碼之後。
#requirepass <yourpassword>
重啟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降低賬號許可權
以較低許可權賬號執行Redis服務,且禁用該賬號的登入許可權。另外可以限制攻擊者往敏感寫入檔案,但是Redis資料還是能被黑客訪問到,或者被黑客惡意刪除。
禁止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設定防火牆
在生產環境中設定防火牆還是很有必要的。如果正常業務中Redis服務需要被其他伺服器來訪問,可以設定iptables策略僅允許指定的IP來訪問Redis服務。
在你redis例項所在的主機,執行如下命令。
# 檢視iptables規則配置的規則
$ 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 ]
設定成功後,只有配置的那四臺機器可以訪問redis例項。