在 CentOS 中,使用 iptables 命令可以配置防火墙规则,以允许特定 IP 地址访问服务器。以下是具体步骤:
1. 查看当前的 iptables 规则
首先,查看当前的防火墙规则,以确保没有冲突的规则存在:
sudo iptables -L -n -v
2. 允许特定 IP 访问服务器
假设你希望允许 IP 地址 192.168.1.100 访问服务器的 SSH 端口(默认端口 22),可以使用以下命令:
sudo iptables -A INPUT -p tcp --dport 22 -s 192.168.1.100 -j ACCEPT
解释:
-A INPUT:将规则添加到 INPUT 链。-p tcp:指定协议为 TCP。--dport 22:指定目标端口为 22(SSH)。-s 192.168.1.100:指定源 IP 地址为192.168.1.100。-j ACCEPT:接受匹配的流量。
3. 允许多个特定 IP 访问
如果你需要允许多个特定 IP 访问,可以重复上述命令,或者使用多个 -s 参数:
sudo iptables -A INPUT -p tcp --dport 22 -m iprange --src-range 192.168.1.100-192.168.1.110 -j ACCEPT
或者:
sudo iptables -A INPUT -p tcp --dport 22 -s 192.168.1.100 -s 192.168.1.101 -j ACCEPT
4. 保存 iptables 规则
CentOS 默认不会持久化 iptables 规则,重启后规则会丢失。你可以使用 iptables-services 或 firewalld 来持久化规则。
使用 iptables-services
-
安装
iptables-services:sudo yum install iptables-services -
启动并启用
iptables服务:sudo systemctl start iptables sudo systemctl enable iptables -
保存规则:
sudo service iptables save
使用 firewalld
-
安装
firewalld:sudo yum install firewalld -
启动并启用
firewalld服务:sudo systemctl start firewalld sudo systemctl enable firewalld -
允许特定 IP 访问:
sudo firewall-cmd --permanent --zone=public --add-rich-rule='rule family="ipv4" source address="192.168.1.100" port protocol="tcp" port="22" accept' -
重新加载
firewalld配置:sudo firewall-cmd --reload
通过以上步骤,你可以在 CentOS 中使用 iptables 或 firewalld 允许特定 IP 地址访问服务器。
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,请发送邮件至 55@qq.com 举报,一经查实,本站将立刻删除。转转请注明出处:https://www.szhjjp.com/n/1453740.html