当前位置: 移动技术网 > IT编程>数据库>Redis > Redis密码设置与访问限制实现方法

Redis密码设置与访问限制实现方法

2020年11月24日  | 移动技术网IT编程  | 我要评论
现在用redis缓存热数据越来越常见了,甚至一些配置,开关等等的东西也写到redis里。原因就是redis简单高效。redis里的数据也越来越重要了,例如一些业务的中间数据会暂时存放在redis里,所

现在用redis缓存热数据越来越常见了,甚至一些配置,开关等等的东西也写到redis里。原因就是redis简单高效。redis里的数据也越来越重要了,例如一些业务的中间数据会暂时存放在redis里,所以限制redis的访问还是很有必要。

本文通过几个手段说一下生产环境中redis的访问权限控制。

1、绑定网卡bind

redis的配置文件redis.conf中对于网络安全部分有这样一段话

################################## network #####################################

# by default, if no "bind" configuration directive is specified, redis listens
# for connections from all the network interfaces available on the server.
# it is possible to listen to just one or multiple selected interfaces using
# the "bind" configuration directive, followed by one or more ip addresses.
#
# ~~~ warning ~~~ if the computer running redis is directly exposed to the
# internet, binding to all the interfaces is dangerous and will expose the
# instance to everybody on the internet. so by default we uncomment the
# following bind directive, that will force redis to listen only into
# the ipv4 lookback interface address (this means redis will be able to
# accept connections only from clients running into the same computer it
# is running).

这段话的意思道出了bind的深意:bind的意思是你的redis实例绑定在哪个interface上,可以理解成绑定在哪个网卡上。那么我们有几个网卡呢?可以看一下。

$ ifconfig
eth0 link encap:ethernet hwaddr 6c:92:bf:22:d7:fc
inet addr:10.93.84.53 bcast:10.93.84.127 mask:255.255.255.128

lo link encap:local loopback
inet addr:127.0.0.1 mask:255.0.0.0

这里就两个,一个是eth0以太网卡(10.93.84.53),一个是本地回路lo(127.0.0.1)。

那么会有这两种情况:

1) bind 10.93.84.53 #同一网段的所有主机都可以连接redis

2) bind 127.0.0.1 #本地回路,那么只有你redis实例所在的主机能访问redis

你可以根据你的需要进行使用:

1) 如果你的机器直接暴露给互联网,那么你还是慎重的将bind设置为127.0.0.1吧,否则你相当于暴露了你的redis给外部所有攻击。

2) 如果你再生产环境中,那么你一般会需要绑在网卡上,以便其他主机也能访问redis,那么我们会有一些其他的方式保证redis数据的安全。

2、设置密码requirepass

redis.conf里有这样的配置,设置了密码之后。

#requirepass <yourpassword>
requirepass mypass

重启redis服务后,你的客户端都需要通过密码的方式访问redis

# 密码正确
$ redis-cli -h 10.93.84.53 -p 6379 -a mypass ping
pong
# 密码错误
$ redis-cli -h 10.93.84.53 -p 6379 -a hehe ping
(error) noauth authentication required.

3、nologin降低账号权限

以较低权限账号运行redis服务,且禁用该账号的登录权限。另外可以限制攻击者往敏感写入文件,但是redis数据还是能被黑客访问到,或者被黑客恶意删除。

禁止linux用户登录的方法,一般是修改用户的shell类型为/sbin/nologin

这种方式会更加人性化一点,因为不仅可以禁止用户登录,还可以在禁用登陆时给提示告诉它这么做的原因。
修改/etc/nologin.txt,没有的话就手动新建一个,在里面添加给被禁止用户的提示(这种方式的所有用户的锁定信息都在这个文件中,在登陆时给与提示)。

其实就是把/etc/passwd文件里的/bin/bash对应改成/sbin/nologin

[root@host-192-168-1-117 ~]# useradd wangshibo
[root@host-192-168-1-117 ~]# echo "123456"|passwd --stdin wangshibo
changing password for user wangshibo.
passwd: all authentication tokens updated successfully.
[root@host-192-168-1-117 ~]# cat /etc/passwd|grep wangshibo
wangshibo:x:500:500::/home/wangshibo:/bin/bash
[root@host-192-168-1-117 ~]# sed -i 's#/home/wangshibo:/bin/bash#/home/wangshibo:/sbin/nologin#g' /etc/passwd
[root@host-192-168-1-117 ~]# cat /etc/passwd|grep wangshibo
wangshibo:x:500:500::/home/wangshibo:/sbin/nologin

[root@host-192-168-1-117 ~]# touch /etc/nologin.txt
[root@host-192-168-1-117 ~]# cat /etc/nologin.txt
in order to protect the system security, this type of user is locked!

4、iptables设置防火墙

在生产环境中设置防火墙还是很有必要的。如果正常业务中redis服务需要被其他服务器来访问,可以设置iptables策略仅允许指定的ip来访问redis服务。

在你redis实例所在的主机,执行如下命令。

# 查看iptables规则配置的规则
$ iptables -nl --line-number
# 禁止所有的主机访问本机6379端口
$ iptables -i input -p tcp --dport 6379 -j drop
# 打开如下的机器-s指定,这些机器可以访问本机的6379端口
$ iptables -i input -s 10.93.21.21 -p tcp --dport 6379 -j accept
$ iptables -i input -s 10.93.18.34 -p tcp --dport 6379 -j accept
$ iptables -i input -s 10.93.18.35 -p tcp --dport 6379 -j accept
$ iptables -i input -s 10.93.84.53 -p tcp --dport 6379 -j accept
# 保存iptables配置
$ service iptables save
iptables: saving firewall rules to /etc/sysconfig/iptables:[ ok ]
# 重启防火墙
$ service iptables restart
iptables: setting chains to policy accept: filter [ ok ]
iptables: flushing firewall rules: [ ok ]
iptables: unloading modules: [ ok ]
iptables: applying firewall rules: [ ok ]

设置成功后,只有配置的那四台机器可以访问redis实例。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网