redis配置檔案中的保護模式protected-mode

niewj發表於2022-06-30

1. 保護模式

在redis.conf配置檔案中有這樣一個配置:

protected-mode yes

上面有一段註釋很好的說明了它的作用

Protected mode is a layer of security protection, in order to avoid that Redis instances left open on the internet are accessed and exploited.

When protected mode is on and the default user has no password, the server only accepts local connections from the IPv4 address (127.0.0.1), IPv6 address (::1) or Unix domain sockets.

By default protected mode is enabled. You should disable it only if you are sure you want clients from other hosts to connect to Redis even if no authentication is configured.

翻譯下:

  1. 保護模式是一個避免你在網際網路(外網)訪問redis的機制。
  2. 當啟用保護模式,而且沒有密碼時,伺服器只接受來自IPv4地址(127.0.0.1)、IPv6地址(::1)或Unix套接字本地連線。(沒密碼+保護模式啟動=本地訪問)
  3. 預設是開啟的。(如果你要從外部訪問它,甚至沒有認證(不設定密碼,不繫結可訪問ip)的去訪問, 那就弄成 no 自己負責!)

2. 相關指令之 bind 指令

bind 127.0.0.1 -::1
這個指明,只能在本地(執行的節點)訪問redis
如果改為其他的,比如 bind * -::* 或者 bind *0.0.0.0 這都全網可訪問了~~~

如果bind是外部可訪問的設定,那麼保護模式管個球用?!

3. 相關指令之 requirepass 指令

requirepass 123456
這個指明,訪問redis時,比如用 redis-cli 訪問, 要加個 -a 123456 來認證一下。

如果 requirepass 設定了密碼訪問,那麼保護模式也不起作用了,它會認為你有密碼保護了!

4. 保護模式是否奏效的條件

4.1 保護模式起作用

protected-mode yes #開啟保護模式
# bind 127.0.0.1 -::1 
# requirepass password //不設定密碼 

上面是起作用的:
bind 127.0.0.1 -::1 #說明是隻能本地訪問
# bind 127.0.0.1 -::1 #用井號註釋掉,相當於全網可訪問;但是保護模式開啟,可以保證本地訪問。
requirepass password //設定密碼password, 可以直接通過 -a password訪問
# requirepass password //用井號註釋掉,相當於無密碼訪問;但是保護模式開啟,可以保證本地訪問。

4.2 保護模式不起作用

    1. 不起作用1

      protected-mode yes #開啟保護模式
      bind * -::* # 全網可訪問(或者設定了別的ip)
      # requirepass password #密碼訪問
    1. 不起作用2

      protected-mode yes #開啟保護模式
      #bind * -::* # 全網可訪問
      requirepass password #密碼訪問
    1. 不起作用3

      protected-mode yes #開啟保護模式
      bind * -::* # 全網可訪問
      requirepass password #密碼訪問

5. 總結

1.如果 protected-mode yes+ 沒有 bind x.x.x.x + 沒有 requirepass xxxx 設定密碼,是起作用的,只能在執行的機器訪問;

  1. 如果 protected-mode yes了+ (設定了 bind x.x.x.x 或者 設定了 requirepass xxxx密碼):後面兩者只要有一個開啟了,就說明,你要靠你自己的bind或密碼來訪問了,它就不起作用了。

相關文章