Linux Redis啟用遠端連線
阿新 • • 發佈:2019-01-02
Linux上Redis要想被遠端程式通過6379介面telnet通。需要滿足一下幾個條件
1. redis-server ../conf/redis.conf 運行了。 並且telnet localhost 6379是通的。
2. conf/redis.conf 裡面的
bind 127.0.0.1 ::1 這一行註釋掉。
這裡的bind指的是隻有指定的網段才能遠端訪問這個redis。 註釋掉後,就沒有這個限制了。
或者bind 自己所在的網段
3. conf/redis.conf裡面
protected-mode 要設定成no (預設是設定成yes的, 防止了遠端訪問,在redis3.2.3版本後)
4. 看伺服器用的是firewall-cmd 還是iptables
如果是iptables,就vim /etc/sysconfig/iptables
增加一行
-A INPUT -p tcp -m state --state NeW -m tcp --dport 6379 -j ACCEPT
結果如下
# Generated by iptables-save v1.4.21 on Sat May 13 16:49:26 2017 *filter :INPUT DROP [0:0] :FORWARD ACCEPT [0:0] :OUTPUT ACCEPT [180:33265] :syn-flood - [0:0] -A INPUT -i lo -j ACCEPT -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT -A INPUT -p tcp -m state --state NEW -m tcp --dport 22 -j ACCEPT -A INPUT -p tcp -m state --state NEW -m tcp --dport 21 -j ACCEPT -A INPUT -p tcp -m state --state NEW -m tcp --dport 20000:30000 -j ACCEPT -A INPUT -p tcp -m state --state NEW -m tcp --dport 443 -j ACCEPT -A INPUT -p tcp -m state --state NeW -m tcp --dport 3306 -j ACCEPT -A INPUT -p tcp -m state --state NeW -m tcp --dport 6379 -j ACCEPT -A INPUT -p icmp -m limit --limit 100/sec --limit-burst 100 -j ACCEPT -A INPUT -p icmp -m limit --limit 1/sec --limit-burst 10 -j ACCEPT -A INPUT -p tcp -m tcp --tcp-flags FIN,SYN,RST,ACK SYN -j syn-flood -A INPUT -j REJECT --reject-with icmp-host-prohibited -A syn-flood -p tcp -m limit --limit 3/sec --limit-burst 6 -j RETURN -A syn-flood -j REJECT --reject-with icmp-port-unreachable COMMIT # Completed on Sat May 13 16:49:26 2017
然後重啟iptables服務,在centos7裡操作如下,並且檢視iptables最終的設定資訊
[[email protected]_196_224_centos redis-3.2.9]# systemctl restart iptables [[email protected]_196_224_centos redis-3.2.9]# iptables -L Chain INPUT (policy DROP) target prot opt source destination ACCEPT all -- anywhere anywhere ACCEPT all -- anywhere anywhere state RELATED,ESTABLISHED ACCEPT tcp -- anywhere anywhere state NEW tcp dpt:ssh ACCEPT tcp -- anywhere anywhere state NEW tcp dpt:webcache ACCEPT tcp -- anywhere anywhere state NEW tcp dpt:http ACCEPT tcp -- anywhere anywhere state NEW tcp dpt:ftp ACCEPT tcp -- anywhere anywhere state NEW tcp dpts:dnp:ndmps ACCEPT tcp -- anywhere anywhere state NEW tcp dpt:https ACCEPT tcp -- anywhere anywhere state NEW tcp dpt:mysql ACCEPT tcp -- anywhere anywhere state NEW tcp dpt:6379 ACCEPT icmp -- anywhere anywhere limit: avg 100/sec burst 100 ACCEPT icmp -- anywhere anywhere limit: avg 1/sec burst 10 syn-flood tcp -- anywhere anywhere tcp flags:FIN,SYN,RST,ACK/SYN REJECT all -- anywhere anywhere reject-with icmp-host-prohibited