CentOS 防护墙配置与优化指南

CentOS 作为一款广泛使用的开源操作系统,其安全性一直是用户关注的焦点,防火墙作为系统安全的第一道防线,对于保护系统免受外部攻击至关重要,本文将详细介绍如何在 CentOS 系统上配置和优化防火墙。
防火墙是一种网络安全设备,用于监控和控制进出网络的数据流,在 CentOS 系统中,我们可以使用 iptables 或 firewalld 来配置防火墙。
使用 iptables 配置防火墙
- 安装
iptables
sudo yum install iptables
- 启动
iptables
sudo systemctl start iptables
设置默认策略
sudo iptables -P INPUT DROP sudo iptables -P FORWARD DROP sudo iptables -P OUTPUT ACCEPT
允许特定端口
sudo iptables -A INPUT -p tcp --dport 80 -j ACCEPT sudo iptables -A INPUT -p tcp --dport 443 -j ACCEPT
保存配置
sudo iptables-save > /etc/sysconfig/iptables
使用 firewalld 配置防火墙

- 安装
firewalld
sudo yum install firewalld
- 启动
firewalld
sudo systemctl start firewalld
添加规则
sudo firewall-cmd --permanent --add-port=80/tcp sudo firewall-cmd --permanent --add-port=443/tcp
重新加载防火墙
sudo firewall-cmd --reload
防火墙优化
开启 IP Forwarding
echo 'net.ipv4.ip_forward = 1' | sudo tee -a /etc/sysctl.conf sudo sysctl -p
开启状态跟踪
sudo iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
开启连接跟踪
sudo iptables -A INPUT -p tcp -m tcp --syn -j ACCEPT
FAQs

Q1:如何查看防火墙规则?
A1:
- 使用
iptables查看:
sudo iptables -L
- 使用
firewalld查看:
sudo firewall-cmd --list-all
Q2:如何永久删除防火墙规则?
A2:
- 使用
iptables删除:
sudo iptables -D INPUT -p tcp --dport 80 -j ACCEPT
- 使用
firewalld删除:
sudo firewall-cmd --permanent --remove-port=80/tcp sudo firewall-cmd --reload

