centos 防火墙通过 firewalld 工具设置,包括查看状态、启用防火墙、允许端口和来自特定 ip 的连接。端口开放方法有:使用 netstat 查看已开放端口、使用 lsof 查看进程端口、使用 systemctl 启动服务、使用 firewall-cmd 和 iptables 开放端口。
Centos 防火墙设置与端口开放
一、防火墙设置
Centos 自带的防火墙工具为 firewalld。它提供了一个简便的方法来管理防火墙规则。
1. 查看当前防火墙状态
firewall-cmd --state
2. 启用防火墙
firewall-cmd --permanent --zone=public --add-interface=eth0 firewall-cmd --reload
其中,eth0 为网络接口名。
3. 允许特定的端口
firewall-cmd --permanent --zone=public --add-port=80/tcp firewall-cmd --reload
4. 允许来自特定 IP 地址的连接
firewall-cmd --permanent --zone=public --add-rich-rule="rule family=ipv4 source address=192.168.1.1/32 accept" firewall-cmd --reload
二、端口开放
在设置防火墙后,还需要手动开放要使用的端口。
1. 使用 netstat 命令查看已开放的端口
netstat -ntlp
2. 使用 lsof 命令查看进程正在使用的端口
lsof -i -P
3. 使用 systemctl 命令启动需要开放端口的服务
systemctl start 服务名称
例如,开放 80 端口的 HTTP 服务:
systemctl start httpd
4. 使用 firewall-cmd 命令开放端口
firewall-cmd --permanent --zone=public --add-port=80/tcp firewall-cmd --reload
5. 使用 iptables 命令开放端口
iptables -I INPUT -p tcp --dport 80 -j ACCEPT