linux设置iptables防火墙的详细步骤(centos防火墙设置方法)

CentOS中设置iptables防火墙的详细步骤如下:

1. 安装iptables防火墙

CentOS系统默认安装了iptables,所以这一步可以跳过。如果重新安装系统,则运行:

yum install iptables-services

2. 启用iptables服务

systemctl enable iptables
systemctl enable ip6tables

3. 禁用firewalld(如果已安装)

CentOS 7 默认使用firewalld作为防火墙,需要先禁用:

systemctl disable firewalld

4. 配置iptables规则

iptables规则存储在/etc/sysconfig/iptables文件中。例如一个简单的允许HTTP和SSH访问的规则:

*filter  
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
-A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
-A INPUT -p icmp -j ACCEPT
-A INPUT -i lo -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT
-A INPUT -j REJECT --reject-with icmp-host-prohibited
-A FORWARD -j REJECT --reject-with icmp-host-prohibited
COMMIT

保存并退出。

5. 重启iptables服务

systemctl restart iptables 
systemctl restart ip6tables

6. 设置iptables开机启动

systemctl enable iptables
systemctl enable ip6tables

7. 配置SELinux放行80端口(如果已启用SELinux)

setsebool -P httpd_can_network_connect  1

至此,iptables防火墙已经配置完成,并且会在系统启动时自动启动。您可以根据实际需求设置更严格或更开放的iptables规则。

如果在配置iptables防火墙的过程中遇到任何问题,欢迎在这里提问。我会详细解释每一步并排除故障,帮助您成功配置CentOS的iptables防火墙

© 版权声明
THE END
喜欢就支持一下吧
点赞15 分享
评论 抢沙发

请登录后发表评论