{"id":201333,"date":"2025-05-10T08:51:17","date_gmt":"2025-05-10T00:51:17","guid":{"rendered":"https:\/\/server.hk\/cnblog\/201333\/"},"modified":"2025-05-10T08:51:17","modified_gmt":"2025-05-10T00:51:17","slug":"redis%e5%af%86%e7%a0%81%e8%ae%be%e7%bd%ae%e4%b8%8e%e8%ae%bf%e9%97%ae%e9%99%90%e5%88%b6%e5%ae%9e%e7%8e%b0%e6%96%b9%e6%b3%95","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/201333\/","title":{"rendered":"Redis\u5bc6\u7801\u8bbe\u7f6e\u4e0e\u8bbf\u95ee\u9650\u5236\u5b9e\u73b0\u65b9\u6cd5"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>Redis\u5bc6\u7801\u8bbe\u7f6e\u4e0e\u8bbf\u95ee\u9650\u5236\u5b9e\u73b0\u65b9\u6cd5<\/h1>\n<p><span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u672c\u7bc7\u6587\u7ae0\u4e3b\u8981\u662f\u7ed3\u5408\u6211\u4e4b\u524d\u9762\u8bd5\u7684\u5404\u79cd\u7ecf\u5386\u548c\u5b9e\u6218\u5f00\u53d1\u4e2d\u9047\u5230\u7684\u95ee\u9898\u89e3\u51b3\u7ecf\u9a8c\u6574\u7406\u7684\uff0c\u5e0c\u671b\u8fd9\u7bc7\u300aRedis\u5bc6\u7801\u8bbe\u7f6e\u4e0e\u8bbf\u95ee\u9650\u5236\u5b9e\u73b0\u65b9\u6cd5\u300b\u5bf9\u4f60\u6709\u5f88\u5927\u5e2e\u52a9\uff01\u6b22\u8fce\u6536\u85cf\uff0c\u5206\u4eab\u7ed9\u66f4\u591a\u7684\u9700\u8981\u7684\u670b\u53cb\u5b66\u4e60~<\/p>\n<p>\u73b0\u5728\u7528redis\u7f13\u5b58\u70ed\u6570\u636e\u8d8a\u6765\u8d8a\u5e38\u89c1\u4e86\uff0c\u751a\u81f3\u4e00\u4e9b\u914d\u7f6e\uff0c\u5f00\u5173\u7b49\u7b49\u7684\u4e1c\u897f\u4e5f\u5199\u5230redis\u91cc\u3002\u539f\u56e0\u5c31\u662fredis\u7b80\u5355\u9ad8\u6548\u3002redis\u91cc\u7684\u6570\u636e\u4e5f\u8d8a\u6765\u8d8a\u91cd\u8981\u4e86\uff0c\u4f8b\u5982\u4e00\u4e9b\u4e1a\u52a1\u7684\u4e2d\u95f4\u6570\u636e\u4f1a\u6682\u65f6\u5b58\u653e\u5728redis\u91cc\uff0c\u6240\u4ee5\u9650\u5236redis\u7684\u8bbf\u95ee\u8fd8\u662f\u5f88\u6709\u5fc5\u8981\u3002<\/p>\n<p>\u672c\u6587\u901a\u8fc7\u51e0\u4e2a\u624b\u6bb5\u8bf4\u4e00\u4e0b\u751f\u4ea7\u73af\u5883\u4e2dredis\u7684\u8bbf\u95ee\u6743\u9650\u63a7\u5236\u3002<\/p>\n<p><strong>1\u3001\u7ed1\u5b9a\u7f51\u5361bind<\/strong><\/p>\n<p>redis\u7684\u914d\u7f6e\u6587\u4ef6redis.conf\u4e2d\u5bf9\u4e8e\u7f51\u7edc\u5b89\u5168\u90e8\u5206\u6709\u8fd9\u6837\u4e00\u6bb5\u8bdd<\/p>\n<blockquote>\n<p>################################## NETWORK #####################################<\/p>\n<p># By default, if no &#8220;bind&#8221; configuration directive is specified, Redis listens<br \/> # for connections from all the network interfaces available on the server.<br \/> # It is possible to listen to just one or multiple selected interfaces using<br \/> # the &#8220;bind&#8221; configuration directive, followed by one or more IP addresses.<br \/> #<br \/> # ~~~ WARNING ~~~ If the computer running Redis is directly exposed to the<br \/> # internet, binding to all the interfaces is dangerous and will expose the<br \/> # instance to everybody on the internet. So by default we uncomment the<br \/> # following bind directive, that will force Redis to listen only into<br \/> # the IPv4 lookback interface address (this means Redis will be able to<br \/> # accept connections only from clients running into the same computer it<br \/> # is running).<\/p>\n<\/blockquote>\n<p>\u8fd9\u6bb5\u8bdd\u7684\u610f\u601d\u9053\u51fa\u4e86bind\u7684\u6df1\u610f\uff1abind\u7684\u610f\u601d\u662f\u4f60\u7684redis\u5b9e\u4f8b\u7ed1\u5b9a\u5728\u54ea\u4e2ainterface\u4e0a\uff0c\u53ef\u4ee5\u7406\u89e3\u6210\u7ed1\u5b9a\u5728\u54ea\u4e2a\u7f51\u5361\u4e0a\u3002\u90a3\u4e48\u6211\u4eec\u6709\u51e0\u4e2a\u7f51\u5361\u5462\uff1f\u53ef\u4ee5\u770b\u4e00\u4e0b\u3002<\/p>\n<blockquote>\n<p>$ ifconfig <br \/> eth0 Link encap:Ethernet HWaddr 6C:92:BF:22:D7:FC <br \/> inet addr:10.93.84.53 Bcast:10.93.84.127 Mask:255.255.255.128<\/p>\n<p>lo Link encap:Local Loopback <br \/> inet addr:127.0.0.1 Mask:255.0.0.0<\/p>\n<\/blockquote>\n<p>\u8fd9\u91cc\u5c31\u4e24\u4e2a\uff0c\u4e00\u4e2a\u662feth0\u4ee5\u592a\u7f51\u5361(10.93.84.53)\uff0c\u4e00\u4e2a\u662f\u672c\u5730\u56de\u8deflo(127.0.0.1)\u3002<\/p>\n<p>\u90a3\u4e48\u4f1a\u6709\u8fd9\u4e24\u79cd\u60c5\u51b5\uff1a<\/p>\n<p>1) bind 10.93.84.53 #\u540c\u4e00\u7f51\u6bb5\u7684\u6240\u6709\u4e3b\u673a\u90fd\u53ef\u4ee5\u8fde\u63a5redis<\/p>\n<p>2) bind 127.0.0.1 #\u672c\u5730\u56de\u8def\uff0c\u90a3\u4e48\u53ea\u6709\u4f60redis\u5b9e\u4f8b\u6240\u5728\u7684\u4e3b\u673a\u80fd\u8bbf\u95eeredis<\/p>\n<p>\u4f60\u53ef\u4ee5\u6839\u636e\u4f60\u7684\u9700\u8981\u8fdb\u884c\u4f7f\u7528:<\/p>\n<p>1) \u5982\u679c\u4f60\u7684\u673a\u5668\u76f4\u63a5\u66b4\u9732\u7ed9\u4e92\u8054\u7f51\uff0c\u90a3\u4e48\u4f60\u8fd8\u662f\u614e\u91cd\u7684\u5c06bind\u8bbe\u7f6e\u4e3a127.0.0.1\u5427\uff0c\u5426\u5219\u4f60\u76f8\u5f53\u4e8e\u66b4\u9732\u4e86\u4f60\u7684redis\u7ed9\u5916\u90e8\u6240\u6709\u653b\u51fb\u3002<\/p>\n<p>2) \u5982\u679c\u4f60\u518d\u751f\u4ea7\u73af\u5883\u4e2d\uff0c\u90a3\u4e48\u4f60\u4e00\u822c\u4f1a\u9700\u8981\u7ed1\u5728\u7f51\u5361\u4e0a\uff0c\u4ee5\u4fbf\u5176\u4ed6\u4e3b\u673a\u4e5f\u80fd\u8bbf\u95eeredis\uff0c\u90a3\u4e48\u6211\u4eec\u4f1a\u6709\u4e00\u4e9b\u5176\u4ed6\u7684\u65b9\u5f0f\u4fdd\u8bc1redis\u6570\u636e\u7684\u5b89\u5168\u3002<\/p>\n<p><strong>2\u3001\u8bbe\u7f6e\u5bc6\u7801requirepass<\/strong><\/p>\n<p>redis.conf\u91cc\u6709\u8fd9\u6837\u7684\u914d\u7f6e\uff0c\u8bbe\u7f6e\u4e86\u5bc6\u7801\u4e4b\u540e\u3002<\/p>\n<blockquote>\n<p>#requirepass <br \/> requirepass mypass  <\/p>\n<\/blockquote>\n<p>\u91cd\u542fredis\u670d\u52a1\u540e\uff0c\u4f60\u7684\u5ba2\u6237\u7aef\u90fd\u9700\u8981\u901a\u8fc7\u5bc6\u7801\u7684\u65b9\u5f0f\u8bbf\u95eeredis<\/p>\n<blockquote>\n<p># \u5bc6\u7801\u6b63\u786e<br \/> $ redis-cli -h 10.93.84.53 -p 6379 -a mypass ping<br \/> PONG<br \/> # \u5bc6\u7801\u9519\u8bef<br \/> $ redis-cli -h 10.93.84.53 -p 6379 -a hehe ping <br \/> (error) NOAUTH Authentication required.<\/p>\n<\/blockquote>\n<p><strong>3\u3001nologin\u964d\u4f4e\u8d26\u53f7\u6743\u9650<\/strong><\/p>\n<p>\u4ee5\u8f83\u4f4e\u6743\u9650\u8d26\u53f7\u8fd0\u884cRedis\u670d\u52a1\uff0c\u4e14\u7981\u7528\u8be5\u8d26\u53f7\u7684\u767b\u5f55\u6743\u9650\u3002\u53e6\u5916\u53ef\u4ee5\u9650\u5236\u653b\u51fb\u8005\u5f80\u654f\u611f\u5199\u5165\u6587\u4ef6\uff0c\u4f46\u662fRedis\u6570\u636e\u8fd8\u662f\u80fd\u88ab\u9ed1\u5ba2\u8bbf\u95ee\u5230\uff0c\u6216\u8005\u88ab\u9ed1\u5ba2\u6076\u610f\u5220\u9664\u3002<\/p>\n<p>\u7981\u6b62Linux\u7528\u6237\u767b\u5f55\u7684\u65b9\u6cd5\uff0c\u4e00\u822c\u662f\u4fee\u6539\u7528\u6237\u7684shell\u7c7b\u578b\u4e3a\/sbin\/nologin<\/p>\n<p>\u8fd9\u79cd\u65b9\u5f0f\u4f1a\u66f4\u52a0\u4eba\u6027\u5316\u4e00\u70b9\uff0c\u56e0\u4e3a\u4e0d\u4ec5\u53ef\u4ee5\u7981\u6b62\u7528\u6237\u767b\u5f55\uff0c\u8fd8\u53ef\u4ee5\u5728\u7981\u7528\u767b\u9646\u65f6\u7ed9\u63d0\u793a\u544a\u8bc9\u5b83\u8fd9\u4e48\u505a\u7684\u539f\u56e0\u3002<br \/> \u4fee\u6539\/etc\/nologin.txt\uff0c\u6ca1\u6709\u7684\u8bdd\u5c31\u624b\u52a8\u65b0\u5efa\u4e00\u4e2a\uff0c\u5728\u91cc\u9762\u6dfb\u52a0\u7ed9\u88ab\u7981\u6b62\u7528\u6237\u7684\u63d0\u793a(\u8fd9\u79cd\u65b9\u5f0f\u7684\u6240\u6709\u7528\u6237\u7684\u9501\u5b9a\u4fe1\u606f\u90fd\u5728\u8fd9\u4e2a\u6587\u4ef6\u4e2d\uff0c\u5728\u767b\u9646\u65f6\u7ed9\u4e0e\u63d0\u793a)\u3002<\/p>\n<p>\u5176\u5b9e\u5c31\u662f\u628a\/etc\/passwd\u6587\u4ef6\u91cc\u7684\/bin\/bash\u5bf9\u5e94\u6539\u6210\/sbin\/nologin<\/p>\n<blockquote>\n<p>[root@host-192-168-1-117 ~]# useradd wangshibo<br \/> [root@host-192-168-1-117 ~]# echo &#8220;123456&#8221;|passwd &#8211;stdin wangshibo<br \/> Changing password for user wangshibo.<br \/> passwd: all authentication tokens updated successfully.<br \/> [root@host-192-168-1-117 ~]# cat \/etc\/passwd|grep wangshibo<br \/> wangshibo:x:500:500::\/home\/wangshibo:\/bin\/bash<br \/> [root@host-192-168-1-117 ~]# sed -i &#8216;s#\/home\/wangshibo:\/bin\/bash#\/home\/wangshibo:\/sbin\/nologin#g&#8217; \/etc\/passwd<br \/> [root@host-192-168-1-117 ~]# cat \/etc\/passwd|grep wangshibo<br \/> wangshibo:x:500:500::\/home\/wangshibo:\/sbin\/nologin<\/p>\n<p>[root@host-192-168-1-117 ~]# touch \/etc\/nologin.txt<br \/> [root@host-192-168-1-117 ~]# cat \/etc\/nologin.txt<br \/> In order to protect the system security, this type of user is locked!<\/p>\n<\/blockquote>\n<p><strong>4\u3001iptables\u8bbe\u7f6e\u9632\u706b\u5899<\/strong><\/p>\n<p>\u5728\u751f\u4ea7\u73af\u5883\u4e2d\u8bbe\u7f6e\u9632\u706b\u5899\u8fd8\u662f\u5f88\u6709\u5fc5\u8981\u7684\u3002\u5982\u679c\u6b63\u5e38\u4e1a\u52a1\u4e2dRedis\u670d\u52a1\u9700\u8981\u88ab\u5176\u4ed6\u670d\u52a1\u5668\u6765\u8bbf\u95ee\uff0c\u53ef\u4ee5\u8bbe\u7f6eiptables\u7b56\u7565\u4ec5\u5141\u8bb8\u6307\u5b9a\u7684IP\u6765\u8bbf\u95eeRedis\u670d\u52a1\u3002<\/p>\n<p>\u5728\u4f60redis\u5b9e\u4f8b\u6240\u5728\u7684\u4e3b\u673a\uff0c\u6267\u884c\u5982\u4e0b\u547d\u4ee4\u3002<\/p>\n<blockquote>\n<p># \u67e5\u770biptables\u89c4\u5219\u914d\u7f6e\u7684\u89c4\u5219<br \/> $ iptables -nL &#8211;line-number<br \/> # \u7981\u6b62\u6240\u6709\u7684\u4e3b\u673a\u8bbf\u95ee\u672c\u673a6379\u7aef\u53e3<br \/> $ iptables -I INPUT -p TCP &#8211;dport 6379 -j DROP<br \/> # \u6253\u5f00\u5982\u4e0b\u7684\u673a\u5668-s\u6307\u5b9a\uff0c\u8fd9\u4e9b\u673a\u5668\u53ef\u4ee5\u8bbf\u95ee\u672c\u673a\u76846379\u7aef\u53e3<br \/> $ iptables -I INPUT -s 10.93.21.21 -p tcp &#8211;dport 6379 -j ACCEPT<br \/> $ iptables -I INPUT -s 10.93.18.34 -p tcp &#8211;dport 6379 -j ACCEPT<br \/> $ iptables -I INPUT -s 10.93.18.35 -p tcp &#8211;dport 6379 -j ACCEPT<br \/> $ iptables -I INPUT -s 10.93.84.53 -p tcp &#8211;dport 6379 -j ACCEPT<br \/> # \u4fdd\u5b58iptables\u914d\u7f6e<br \/> $ service iptables save<br \/> iptables: Saving firewall rules to \/etc\/sysconfig\/iptables:[ OK ]<br \/> # \u91cd\u542f\u9632\u706b\u5899 <br \/> $ service iptables restart<br \/> iptables: Setting chains to policy ACCEPT: filter [ OK ]<br \/> iptables: Flushing firewall rules: [ OK ]<br \/> iptables: Unloading modules: [ OK ]<br \/> iptables: Applying firewall rules: [ OK ]<\/p>\n<\/blockquote>\n<p>\u8bbe\u7f6e\u6210\u529f\u540e\uff0c\u53ea\u6709\u914d\u7f6e\u7684\u90a3\u56db\u53f0\u673a\u5668\u53ef\u4ee5\u8bbf\u95eeredis\u5b9e\u4f8b\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Redis\u5bc6\u7801\u8bbe\u7f6e\u4e0e\u8bbf\u95ee\u9650\u5236\u5b9e\u73b0&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101],"tags":[],"class_list":["post-201333","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/201333","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=201333"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/201333\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=201333"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=201333"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=201333"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}