redis.conf中使用requirepass不生效的原因及解決方法
requirepass字段介紹
requirepass字段是redis.conf中的一個字段,可以看下redis.conf中的注釋
# IMPORTANT NOTE: starting with Redis 6 "requirepass" is just a compatibility # layer on top of the new ACL system. The option effect will be just setting # the password for the default user. Clients will still authenticate using # AUTH <password> as usually, or more explicitly with AUTH default <password> # if they follow the new protocol: both will work. # # The requirepass is not compatable with aclfile option and the ACL LOAD # command, these will cause requirepass to be ignored. # # requirepass foobared
即這個字段是用來設(shè)置默認用戶default的密碼的,用戶可以通過auth 或者auth default 來認證,同時說明了不能跟aclfile兼容,如果啟動acl,則該字段會被忽略,會使用acl文件中的default用戶,如果沒有配置default用戶,則會新建一個nopass的default用戶并使用,哈哈,這就是為什么redis.conf配置了requirepass而不生效的原因,提前說了。
如何啟用requirepass
- 啟用redis.conf中的requirepass,改為自己的密碼password,同時啟用logfile,注意不要啟用aclfile,否則會不生效
- 啟動redis-server ./redis.conf
- redis-cli -h localhost -p 6379訪問,發(fā)現(xiàn)需要進行認證,輸入auth password或者auth default password即可進行訪問了,默認登錄用戶就是default用戶
- default用戶的密碼就是requirepass配置的密碼,在initServer中會調(diào)用ACLUpdateDefaultUserPassword(server.requirepass)函數(shù)設(shè)置default用戶的密碼
至于為什么啟用aclfile時會不生效,請繼續(xù)看
啟用requirepass時requirepass不生效?
現(xiàn)象
requirepass是default用戶的密碼,配置密碼后,aclfile也啟用時,修改redis.conf配置后重啟redis后,redis-cli -h localhost -p port 無需認證仍然可以訪問,即沒有生效
看下redis.conf中注釋可以知道跟aclfile是不兼容的,啟用aclfile時,會忽略requirepass
原因
redis.conf中同時啟用requirepass和aclfile,redis在加載配置時,會讀取aclfile,重新新建全局Users對象,調(diào)用ACLInitDefaultUser函數(shù)重新新建nopass的default用戶,先前已加載的defaultUser對象(密碼從requirepass來)不會被用到,即default用戶是nopass的,但是如果acl文件中配置了default用戶以及配置了密碼,則還是需要認證的
sds ACLLoadFromFile(const char *filename) { ... /* The default user pointer is referenced in different places: instead * of replacing such occurrences it is much simpler to copy the new * default user configuration in the old one. */ user *new_default = ACLGetUserByName("default",7); if (!new_default) { new_default = ACLCreateDefaultUser(); // nopass的default用戶 } ACLCopyUser(DefaultUser,new_default); ACLFreeUser(new_default); raxInsert(Users,(unsigned char*)"default",7,DefaultUser,NULL); raxRemove(old_users,(unsigned char*)"default",7,NULL); ACLFreeUsersSet(old_users); sdsfree(errors); return NULL; ... }
解決方法
- 不啟用aclfile,只使用requirepass,即只有default用戶了
- 啟用aclfile,redis-cli登錄后,用config set requirepass xxx,會生效,然后重新redis-cli登錄訪問即可,如果需要重啟redis也生效,則進行acl save(會寫default的user規(guī)則到aclfile中)
注意點:config set requirepass xxx會調(diào)用updateRequirePass函數(shù),該函數(shù)會繼續(xù)調(diào)用ACLUpdateDefaultUserPassword更新default用戶的密碼(nopass變?yōu)橛忻艽a狀態(tài)),注意redis最新版本(7.0以上)只會在更新的內(nèi)容發(fā)生變化時才會調(diào)用到updateRequirePass函數(shù),如下面的sdsConfigSet函數(shù),在內(nèi)容有變化時才返回1
configSetCommand -> performInterfaceSet -> sdsConfigSet函數(shù)
performInterfaceSet會將sdsConfigSet函數(shù)返回值作為自己的返回值,
configSetCommand函數(shù)判斷performInterfaceSet返回值,如果為1,則
會調(diào)用到updateRequirePass函數(shù)
- 使用上還是直接使用aclfile即可,將requirepass注釋掉,登錄后新增用戶,然后acl save
總結(jié)
本文主要介紹了如何啟用requirepass,以及啟用requirepass為什么不會生效,從代碼層面分析了不生效的原因,是因為同時啟用了aclfile導(dǎo)致requirepass中的密碼不會被用到,最后介紹了解決方法,建議使用上直接使用aclfile即可。
到此這篇關(guān)于redis.conf中使用requirepass不生效的原因及解決方法的文章就介紹到這了,更多相關(guān)redis.conf中使用requirepass不生效內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
一文搞懂Redis中的慢查詢?nèi)罩竞捅O(jiān)視器
我們都知道MySQL有慢查詢?nèi)罩?但Redis也有慢查詢?nèi)罩?可用于監(jiān)視和優(yōu)化查詢,本文給大家詳細介紹了Redis中的慢查詢?nèi)罩竞捅O(jiān)視器,文章通過代碼示例講解的非常詳細,需要的朋友可以參考下2024-04-04Redis常用的數(shù)據(jù)結(jié)構(gòu)及實際應(yīng)用場景
本文介紹了Redis中常用的數(shù)據(jù)結(jié)構(gòu),包括字符串、列表、集合、哈希表、有序集合和Bitmap,并詳細說明了它們在各種場景下的使用,需要的朋友可以參考下2024-05-05