【Linux】3、iptables

在这里插入图片描述

iptables 可在 tcp 协议栈层面限制访问,常用于解决现场的各漏洞。

场景:现场有 a、b、c 三个机器组成的 postgres 集群(端口为5432),希望只让 d、e 访问,而不让其他任何机器(如 f、g)访问。

设置步骤如下:

一、设置其他机器均无法访问 a、b、c 机器的 5432 端口

iptables -A INPUT -p tcp --dport 5432 -j DROP
# -A 为 append 一条规则到 chain 的末尾
# -p 为 指定 protocal
# -d 为 指定 destination 为 port 5432
# -j DROP 为 指定 jump 策略为 DROP(丢弃)

设置完后,可用 telnet 测试,可看到其他机器已无法访问 a、b、c 的 5432 端口了

[email protected]: telnet 192.168.2.a 5432
Trying 192.168.2.a...
^C

[email protected]: telnet 192.168.2.a 5432
Trying 192.168.2.a...
^C

[email protected]: telnet 192.168.2.a 5432
Trying 192.168.2.a...
^C

[email protected]: telnet 192.168.2.a 5432
Trying 192.168.2.a...
^C

二、设置 d、e 可访问 a、b、c 的 5432 端口

# 设置数据库集群ip(a、b、c)可以访问
# -I 为 insert 一条规则到 chain 的开头(默认行为)
# -j ACCPET 为 指定 jump 策略为 ACCEPT(畅通)
iptables -I INPUT -s 127.0.0.1     -p tcp  --dport 5432    -j ACCEPT
iptables -I INPUT -s 192.168.2.a   -p tcp  --dport 5432    -j ACCEPT
iptables -I INPUT -s 192.168.2.b   -p tcp  --dport 5432    -j ACCEPT
iptables -I INPUT -s 192.168.2.c   -p tcp  --dport 5432    -j ACCEPT
iptables -I INPUT -s 10.244.0.0/16 -p tcp  --dport 1:65535 -j ACCEPT

# 设置其他需要访问数据的ip(d、e)
iptables -I INPUT -s 192.168.2.d -p tcp --dport 5432 -j ACCEPT
iptables -I INPUT -s 192.168.2.e -p tcp --dport 5432 -j ACCEPT

设置完后,期望 d、e 允许访问 a、b、c 的 5432 端口,而 f、g 无法访问:

[email protected]: telnet 192.168.2.a 5432
Trying 192.168.2.a...
Connected to 192.168.2.a.
Escape character is '^]'

[email protected]: telnet 192.168.2.a 5432
Trying 192.168.2.a...
Connected to 192.168.2.a.
Escape character is '^]'

[email protected]: telnet 192.168.2.a 5432
Trying 192.168.2.a...
^C

[email protected]: telnet 192.168.2.a 5432
Trying 192.168.2.a...
^C

三、检查业务是否受到影响

检查 a、b、c、d、e、f、g、h 各机器和 5432 端口相关的服务,是否有报错(如网络连不上等)。

可通过如下命令查看已设置的规则:

iptables -L INPUT -n --line-numbers
# -L 为 --list 所有规则
# -n 为 --numeric,会打印 ip 和 port (默认仅显示 host name)
# --line-numbers 打印第一列的序号,方便查看有几个

猜你喜欢

转载自blog.csdn.net/jiaoyangwm/article/details/130945645
今日推荐