Redis安全的前提是:Redis运行在可信环境,在安全方面。Redis没有做太多的工作,而是尽最大可能去优化高性能和易用性。这意味着将Redis实例直接暴露在网络上或者让不可信用户可以直接访问Redis的tcp端口或Unix套接字,是不安全的。也就是说,在生产环境中,不允许外界直接连上Redis。
1、Redis配置文件中的bind参数可限制访问服务器的地址(redis服务器的ip)
127.0.0.1:6379> config get bind
1) "bind"
2) ""
这个参数没有设置值的时候,表示所有ip都是允许访问redis的。这个参数不能通过config set 命令动态设置,也就意味着如果你需要修改这个值,就必须去修改配置文件。
步骤:关闭redis服务
[root@localhost src]# ps -ef|grep redis
root 8109 1 0 21:22 ? 00:00:00 ./redis-server 127.0.0.1:6379
root 8121 7895 0 21:25 pts/0 00:00:00 grep redis
[root@localhost src]# kill -9 8109
[root@localhost src]# ps -ef|grep redis
root 8123 7895 0 21:26 pts/0 00:00:00 grep redis
使用 vi redis.conf,打开文件,使用?bind找到配置项,然后按i,进入编辑模式,修改完ip后按esc,再用:wq,保存修改并退出,
在src目录下使用./redis-server ../redis.conf &命令启动服务,然后再到redis命令窗口查看bind的配置就可以了。
[root@localhost redis-5.0.4]# ls
00-RELEASENOTES BUGS CONTRIBUTING COPYING deps INSTALL Makefile MANIFESTO README.md redis.conf runtest runtest-cluster runtest-sentinel sentinel.conf src tests utils
[root@localhost redis-5.0.4]# vim redis.conf
[root@localhost src]# ./redis-cli
127.0.0.1:6379> config get bind
1) "bind"
2) "127.0.0.1 192.168.136.128"
这样,本机可以使用127.0.0.1访问redis,外部网络也可以通过192.168.136.128访问redis服务。
2、可以通过设置密码,直接修改配置或者修改完配置文件然后再启动。
127.0.0.1:6379> config get requirepass
1) "requirepass"
2) ""
127.0.0.1:6379> config set requirepass password
OK
127.0.0.1:6379> config get requirepass
(error) NOAUTH Authentication required.
127.0.0.1:6379> auth password
OK
127.0.0.1:6379> config get requirepass
1) "requirepass"
2) "password"
127.0.0.1:6379> exit
[root@localhost src]# ./redis-cli -a password
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
127.0.0.1:6379> config get requirepass
1) "requirepass"
2) "password"
如果你想通过设置密码来保证redis的安全,那么你设置的密码就应该足够复杂。原因是Redis速度非常快,外部用户可以在每秒尝试多达150k个密码。可以看下https://blog.youkuaiyun.com/suchenbin/article/details/87070953 674~690行
3、设置防火墙访问规则
系统防火墙配置地址:/etc/sysconfig/下,执行 vim /etc/sysconfig/iptables,加上一条防火墙规则
-A INPUT -p tcp --dport 6379 -j ACCEPT
重新启动就可以了
重启:service iptables restart
如果有写的不对的地方,请大家多多批评指正,非常感谢!