“DENIED Redis is running in protected mode because protected mode is enabled and no password is set for the default user” 是第一次从别的机器连 Redis 时几乎必遇到的报错,网上流传的解法是关掉保护模式,而这会把没有认证的 Redis 直接暴露在网络上。这篇在本机用局域网地址复现它,看清楚触发条件,以及 bind、密码、保护模式三者的关系。
CONFIG GET protected-mode = yes,bind = 0.0.0.0,requirepass = '' 连 127.0.0.1:8150,发 PING -> 服务器主动发的: (无);PING 回复: +PONG 连 <本机局域网IP>:8150 -> 服务器在我发任何东西之前主动发来: -DENIED Redis is running in protected mode because protected mode is enabled and no password is set for the default user. In this mode connections are only accepted from the loopback interface. If you want to connect from external computers to Redis you may adopt one of the following solutions: 1) Just disable protected mode sen…(共 1064 个字符);随后连接被对方关闭: True 完整报错的结尾部分: n authentication password for the default user. NOTE: You only need to do one of the above things in order for the server to start accepting connections from the outside. INFO clients 里的 connected_clients 现在是 1,只有我这次的管理连接
protected-mode 为 yes 且默认用户没有密码时,来源不是回环地址的连接一建立就收到 DENIED 并被关闭,服务器不等客户端发命令。
设了 requirepass(bind 0.0.0.0):连 <本机局域网IP>:8151 发 PING -> -NOAUTH Authentication required.
同一个局域网地址上 AUTH secret -> +OK,PING -> +PONG
只绑定 127.0.0.1:连 <本机局域网IP>:8152 -> ConnectionRefusedError: Connection refused(连不上,根本没有进到 Redis,所以也没有 DENIED)
bind 127.0.0.1 和局域网地址、没有密码、protected-mode yes:连 <本机局域网IP>:8153 -> -DENIED Redis is running in protected mode because protected mode is enabled and…(共 1064 个字符)
同一个实例从 127.0.0.1 连 -> +PONG设了 requirepass 变成 NOAUTH;只 bind 回环地址则连接被拒绝;bind 成具体地址不改变保护模式。
回环连接 CONFIG GET protected-mode -> yes 局域网连接想发 CONFIG SET protected-mode no:来不及发,服务器先发来了 -DENIED Redis is running in protected mo…(共 1064 个字符),然后关闭连接 在回环连接上 CONFIG SET requirepass secret 之后,局域网连接发 PING -> -NOAUTH Authentication required.(DENIED 消失,变成要求认证)
局域网的客户端自己关不掉保护;在服务器本机的回环连接上设密码是最稳妥的处理。关掉 protected-mode 会让无认证的 Redis 暴露。