centos

CentOS防火墙如何使用iptables

小樊
47
2025-03-21 14:13:32
栏目: 智能运维

在CentOS系统中,iptables是一个用于配置内核防火墙(netfilter)规则的用户空间工具。以下是如何使用iptables的基本步骤:

1. 查看当前防火墙规则

sudo iptables -L -n -v

2. 允许特定IP访问

sudo iptables -A INPUT -s 192.168.1.100 -j ACCEPT

3. 允许特定端口访问

sudo iptables -A INPUT -p tcp --dport 80 -j ACCEPT

4. 阻止特定IP访问

sudo iptables -A INPUT -s 192.168.1.100 -j DROP

5. 阻止特定端口访问

sudo iptables -A INPUT -p tcp --dport 80 -j DROP

6. 保存防火墙规则

CentOS 7及以上版本使用firewalld作为默认防火墙管理工具,但你可以手动保存iptables规则:

sudo service iptables save

或者使用firewall-cmd

sudo firewall-cmd --runtime-to-permanent

7. 重启防火墙服务

sudo systemctl restart iptables

或者使用firewall-cmd

sudo systemctl restart firewalld

8. 查看防火墙状态

sudo systemctl status iptables

或者使用firewall-cmd

sudo firewall-cmd --state

注意事项

通过以上步骤,你可以在CentOS系统中使用iptables来配置和管理防火墙规则。

0
看了该问题的人还看了