在Linux系统中,防火墙配置是保障服务器安全的重要一环。对于使用CentOS系统的用户来说,iptables 是一个强大且常用的工具。本文将手把手教你如何使用 CentOS iptables命令 来进行基本的 Linux防火墙设置,即使你是零基础的小白,也能轻松上手!
iptables 是 Linux 内核中用于配置 IPv4 数据包过滤和 NAT(网络地址转换)的工具。它通过一系列“规则”来决定允许或拒绝哪些网络流量。这些规则被组织在不同的“表”(tables)和“链”(chains)中。
注意:从 CentOS 7 开始,默认使用 firewalld 作为防火墙管理工具。但如果你更习惯使用 iptables,可以手动安装并禁用 firewalld。
首先,停止并禁用 firewalld:
systemctl stop firewalldsystemctl disable firewalld
然后安装 iptables-services:
yum install -y iptables-services 启动并设置开机自启:
systemctl start iptablessystemctl enable iptables
iptables -L -n -v -L 列出规则,-n 以数字形式显示 IP 和端口(更快),-v 显示详细信息。
iptables -A INPUT -i lo -j ACCEPT iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT iptables -A INPUT -p tcp --dport 22 -j ACCEPT iptables -A INPUT -p tcp --dport 80 -j ACCEPTiptables -A INPUT -p tcp --dport 443 -j ACCEPT
iptables -P INPUT DROPiptables -P FORWARD DROPiptables -P OUTPUT ACCEPT
注意:请确保先允许 SSH,否则可能把自己锁在服务器外面!
service iptables save 或者:
/sbin/iptables-save > /etc/sysconfig/iptables service iptables save,否则重启后规则会丢失。iptables -L --line-numbers 查看行号,再用 iptables -D INPUT 行号 删除。通过本教程,你已经掌握了基本的 iptables规则管理 技能。合理配置防火墙不仅能提升服务器安全性,还能防止不必要的网络攻击。记住:安全无小事,定期检查和更新你的防火墙规则是运维工作的关键一步。
关键词回顾:CentOS iptables命令、防火墙配置教程、Linux防火墙设置、iptables规则管理。
本文由主机测评网于2025-12-07发表在主机测评网_免费VPS_免费云服务器_免费独立服务器,如有疑问,请联系我们。
本文链接:https://www.vpshk.cn/2025124068.html