佳木斯湛栽影视文化发展公司

主頁 > 知識庫 > Redis密碼設(shè)置與訪問限制實現(xiàn)方法

Redis密碼設(shè)置與訪問限制實現(xiàn)方法

熱門標(biāo)簽:百度競價排名 服務(wù)外包 地方門戶網(wǎng)站 鐵路電話系統(tǒng) AI電銷 Linux服務(wù)器 呼叫中心市場需求 網(wǎng)站排名優(yōu)化

現(xiàn)在用redis緩存熱數(shù)據(jù)越來越常見了,甚至一些配置,開關(guān)等等的東西也寫到redis里。原因就是redis簡單高效。redis里的數(shù)據(jù)也越來越重要了,例如一些業(yè)務(wù)的中間數(shù)據(jù)會暫時存放在redis里,所以限制redis的訪問還是很有必要。

本文通過幾個手段說一下生產(chǎn)環(huán)境中redis的訪問權(quán)限控制。

1、綁定網(wǎng)卡bind

redis的配置文件redis.conf中對于網(wǎng)絡(luò)安全部分有這樣一段話

################################## 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吧,否則你相當(dāng)于暴露了你的redis給外部所有攻擊。

2) 如果你再生產(chǎn)環(huán)境中,那么你一般會需要綁在網(wǎng)卡上,以便其他主機也能訪問redis,那么我們會有一些其他的方式保證redis數(shù)據(jù)的安全。

2、設(shè)置密碼requirepass

redis.conf里有這樣的配置,設(shè)置了密碼之后。

#requirepass yourpassword>
requirepass mypass

重啟redis服務(wù)后,你的客戶端都需要通過密碼的方式訪問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服務(wù),且禁用該賬號的登錄權(quán)限。另外可以限制攻擊者往敏感寫入文件,但是Redis數(shù)據(jù)還是能被黑客訪問到,或者被黑客惡意刪除。

禁止Linux用戶登錄的方法,一般是修改用戶的shell類型為/sbin/nologin

這種方式會更加人性化一點,因為不僅可以禁止用戶登錄,還可以在禁用登陸時給提示告訴它這么做的原因。
修改/etc/nologin.txt,沒有的話就手動新建一個,在里面添加給被禁止用戶的提示(這種方式的所有用戶的鎖定信息都在這個文件中,在登陸時給與提示)。

其實就是把/etc/passwd文件里的/bin/bash對應(yīng)改成/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è)務(wù)中Redis服務(wù)需要被其他服務(wù)器來訪問,可以設(shè)置iptables策略僅允許指定的IP來訪問Redis服務(wù)。

在你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)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

您可能感興趣的文章:
  • Docker 啟動Redis 并設(shè)置密碼的操作
  • Docker安裝Tomcat、MySQL和Redis的步驟詳解
  • docker安裝redis設(shè)置密碼并連接的操作

標(biāo)簽:銅川 衡水 湖南 湘潭 蘭州 崇左 仙桃 黃山

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《Redis密碼設(shè)置與訪問限制實現(xiàn)方法》,本文關(guān)鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266
    翁源县| 甘南县| 万源市| 和政县| 阜平县| 呈贡县| 咸宁市| 建昌县| 南丰县| 屏东市| 凤翔县| 长治市| 云浮市| 克拉玛依市| 贡嘎县| 连江县| 长沙县| 苗栗市| 镇原县| 库伦旗| 商水县| 合山市| 许昌县| 洛宁县| 宜都市| 阿拉善左旗| 巴中市| 咸丰县| 宁国市| 武邑县| 泽普县| 金溪县| 玛纳斯县| 应城市| 大同县| 开封市| 武威市| 农安县| 灌南县| 开鲁县| 辽阳县|