如何利用CentOS7 Firewalld防火墙实现IP+Port细粒度访问控制
本文参考RedHat官方文档进行实践测试
https://access.redhat.com/documentation/zh-cn/red_hat_enterprise_linux/7/html/security_guide/sec-using_firewalls
(图片可点击放大查看)
测试的CentOS7 服务器IP :192.168.31.230
firewall-cmd --permanent --zone=public --add-port=9000/tcp
firewall-cmd --permanent --zone=public --add-port=1514/udp
firewall-cmd --add-port=27017/tcp --permanent --zone=public
firewall-cmd --add-port=9200/tcp --permanent --zone=public
firewall-cmd --reload
firewall-cmd --list-all --zone=public
firewall-cmd --list-ports
下面进入正题:现在需要对9000端口进行细粒度更高的访问控制 比如只允许192.168.31.100等特定IP访问9000端口
可以通过如下两种方式来实现
firewall-cmd --permanent --zone=public --remove-port=9000/tcp
firewall-cmd --reload
(图片可点击放大查看)
在其他机器上用nc测试9000端口,可以看到已经无法访问
(图片可点击放大查看)
nc -vz 192.168.31.230 9000 -s 192.168.31.127
firewall-cmd --list-all --zone=internal
firewall-cmd --zone=internal --add-port=9000/tcp --permanent
firewall-cmd --reload
firewall-cmd --add-source=192.168.31.100/32 --permanent --zone=internal
firewall-cmd --reload
(图片可点击放大查看)
可以在192.168.31.100 Windows机器上测试
internal(内部)zone的特性
For use on internal networks when you mostly trust the other computers on the network. Only selected incoming connections are accepted.
(图片可点击放大查看)
firewall-cmd --add-source=192.168.31.127/32 --permanent --zone=internal
firewall-cmd --reload
firewall-cmd --list-all --zone=internal
添加前和添加后的对比测试截图如下
(图片可点击放大查看)
(图片可点击放大查看)
firewall-cmd --set-log-denied=all
firewall-cmd --add-source=192.168.31.127/32 --permanent --zone=internal
firewall-cmd --reload
firewall-cmd --set-log-denied=off
(图片可点击放大查看)
当然也支持如下语法
firewall-cmd --add-source={192.168.31.230/32,192.168.31.100/32} --permanent --zone=internal
firewall-cmd --remove-port=9000/tcp --permanent --zone=public
firewall-cmd --reload
firewall-cmd --add-rich-rule='rule family="ipv4" source address="192.168.31.100/32" port port="9000" protocol="tcp" log prefix="graylog9000" level="info" accept' --permanent
firewall-cmd --add-rich-rule='rule family="ipv4" source address="192.168.31.127/32" port port="9000" protocol="tcp" log prefix="graylog9000" level="info" accept' --permanent
firewall-cmd --reload
nc -vz 192.168.31.230 9000 -s 192.168.31.127
(图片可点击放大查看)
当然也可以不加日志打印
firewall-cmd --add-rich-rule='rule family="ipv4" source address="192.168.31.100/32" port port="9000" protocol="tcp" accept' --permanent
firewall-cmd --reload
参考官方文档
https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/7/html/security_guide/sec-managing_icmp_requests
firewall-cmd --set-target=DROP --zone=public --permanent
firewall-cmd --add-icmp-block-inversion
firewall-cmd --reload
本文分享自 WalkingCloud 微信公众号,前往查看
如有侵权,请联系 cloudcommunity@tencent.com 删除。
本文参与 腾讯云自媒体同步曝光计划 ,欢迎热爱写作的你一起参与!