前提
Redis开放了所有IP访问,但是服务运行中,不太方便重启redis服务,此时可以通过防火墙策略保证安全
bind 0.0.0.0
添加防火墙策略
添加规则允许本地127.0.0.1和局域网的172.19.161.14连接Redis(根据自己的局域网IP更改)
iptables -A INPUT -s 127.0.0.1 -p tcp --dport 6379 -j ACCEPT
iptables -A INPUT -s 172.19.161.14 -p tcp --dport 6379 -j ACCEPT
禁用其他ip连接当前机器的6379接口,问题解决
iptables -A INPUT -p TCP --dport 6379 -j REJECT
延伸学习
查看防火墙规则
[root@izuf6barrnre7hruzsy99hz brain]
Chain INPUT (policy ACCEPT)
num target prot opt source destination
1 ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:8080
2 ACCEPT tcp -- 172.19.161.14 0.0.0.0/0 tcp dpt:6379
3 ACCEPT tcp -- 127.0.0.1 0.0.0.0/0 tcp dpt:6379
4 REJECT tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:6379 reject-with icmp-port-unreachable
Chain FORWARD (policy ACCEPT)
num target prot opt source destination
Chain OUTPUT (policy ACCEPT)
num target prot opt source destination
删除防火墙规则
[root@izuf6barrnre7hruzsy99hz brain]
Chain INPUT (policy ACCEPT)
num target prot opt source destination
1 ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:8080
2 ACCEPT tcp -- 172.19.161.14 0.0.0.0/0 tcp dpt:6379
3 ACCEPT tcp -- 127.0.0.1 0.0.0.0/0 tcp dpt:6379
4 REJECT tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:6379 reject-with icmp-port-unreachable
···
[root@izuf6barrnre7hruzsy99hz brain]
1 ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:8080
2 ACCEPT tcp -- 127.0.0.1 0.0.0.0/0 tcp dpt:6379
3 REJECT tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:6379 reject-with icmp-port-unreachable
···