在CentOS 7中,默认使用firewalld作为防火墙。但是,许多系统管理员仍然偏好使用iptables due to its flexibility and powerful功能。本文将指导您如何在CentOS 7上安装和配置iptables。
安装iptables
打开终端,输入以下命令安装iptables:
sudo yum install iptables-services
停用firewalld
为避免冲突,需要停用并禁用firewalld:
sudo systemctl stop firewalld
sudo systemctl disable firewalld
启用iptables服务
使用以下命令启用并启动iptables服务:
sudo systemctl enable iptables
sudo systemctl start iptables
配置iptables规则
编辑iptables配置文件:
sudo nano /etc/sysconfig/iptables
添加所需的规则,例如:
-A INPUT -p tcp --dport 22 -j ACCEPT
-A INPUT -p tcp --dport 80 -j ACCEPT
-A INPUT -j DROP
保存并应用规则
保存文件后,重启iptables服务以应用新规则:
sudo systemctl restart iptables
验证iptables状态
检查iptables是否正常运行:
sudo systemctl status iptables
通过以上步骤,您已成功在CentOS 7上安装和配置了iptables防火墙。请记住定期更新规则以确保持续的服务器安全。