systemctl 是CentOS7的服务管理工具中主要的工具,它融合之前 service 和 chkconfig 的功能于一体。service 管理服务,chkconfig 管理启动项
systemctl 和 service 命令格式不同
service firewalld status
systemctl status firewalld
CentOS7 默认的防火墙不是 firewall,所以前半部分是学习 firewall 后半部分是 iptables
firewall 防火墙
命令后 .service 不加也可以
基本使用
# 启动 systemctl start firewalld.service # 停止 systemctl stop firewalld.service # 重启 systemctl restart firewalld.service # 查看状态 systemctl status firewalld.service # 启用, 开机自启服务 systemctl enable firewalld.service # 禁用, 开机自启服务 systemctl disable firewalld.service # 查看是否开机启动 systemctl is-enabled firewalld.service # 查看已启动的服务列表 systemctl list-unit-files | grep enabled # 查看启动失败的服务列表 systemctl --failed
基本配置 firewalld-cmd
# 查看版本 firewall-cmd --version # 查看帮助 firewall-cmd --help # 显示状态 firewall-cmd --state # 开发一个端口 firewall-cmd --zone=public --permanent --add-port=端口/tcp 参数:--permanent永久生效,没有此参数重启后失效 --add-port 端口 # 更新防火墙规则 firewall-cmd --reload
# 删除端口
firewall-cmd --zone=public --permanent --remove-port=端口/tcp
# 查看端口是否开放 firewall-cmd --zone=public --query-port=端口/tcp # 查看所有打开的端口 firewall-cmd --zone=public --list-ports
# 查看防火墙规则
firewall-cmd --list-all
# 查看区域信息 firewall-cmd --get-active-zones # 查看指定接口所属区域 firewall-cmd --get-zone-of-interface=eth0 # 拒绝所有包 firewall-cmd --panic-on # 取消拒绝状态 firewall-cmd --panic-off # 查看是否拒绝 firewall-cmd --query-panic
iptables 防火墙
安装 iptables
# 检查状态 systemctl status iptables.service # 停止firewall systemctl stop firewalld.service # 禁用开机启动firewall systemctl disable firewalld.service # 安装 iptables yum install -y iptables iptables-services # 启动 iptables systemctl start iptables.service # 启用开机启动 iptables systemctl enable iptables.service
设置规则
参数:
-t<表>:指定要操纵的表 -A:向规则链中添加条目 -D:从规则链中删除条目 -i:向规则链中插入条目 -R:替换规则链中的条目 -L:显示规则链中已有的条目 -F:清除规则链中已有的条目 -Z:清空规则链中的数据包计算器和字节计数器 -N:创建新的用户自定义规则链 -P:定义规则链中的默认目标 -h:显示帮助信息 -p:指定要匹配的数据包协议类型 -s:指定要匹配的数据包源ip地址 -j<目标>:指定要跳转的目标 -i<网络接口>:指定数据包进入本机的网络接口 -o<网络接口>:指定数据包要离开本机所使用的网络接口
# 查看iptables现有规则 iptables -L -n # 先允许所有,不然有可能会杯具 iptables -P INPUT ACCEPT # 清空所有默认规则 iptables -F # 清空所有自定义规则 iptables -X # 所有计数器归0 iptables -Z # 允许来自于lo接口的数据包(本地访问) iptables -A INPUT -i lo -j ACCEPT 开放端口 iptables -A INPUT -p tcp -s IP地址 --dport 端口 -j ACCEPT # 保存规则 service iptables save # 允许ping iptables -A INPUT -p icmp --icmp-type 8 -j ACCEPT # 允许接受本机请求之后的返回数据 RELATED,是为FTP设置的 iptables -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT # 其他入站一律丢弃 iptables -P INPUT DROP # 所有出站一律绿灯 iptables -P OUTPUT ACCEPT # 所有转发一律丢弃 iptables -P FORWARD DROP # 如果要添加内网ip信任 iptables -A INPUT -p tcp -s 45.96.174.68 -j ACCEPT # 过滤所有非以上规则的请求 iptables -P INPUT DROP # 要封停一个IP,使用下面这条命令: iptables -I INPUT -s IP地址 -j DROP # 要解封一个IP,使用下面这条命令: iptables -D INPUT -s IP地址 -j DROP
常用端口
# http 80 # https 443 # https 443 # mysql 3306 # mongodb 27017 # postgre 5432 # elasticsearch 9200 # redis 6379 # rabbitmq 15672, 5672 # consul 8500 # nacos 8848 # ftp 21 # ssh 22 # telnet 23 # smtp 25
firewall 实例
# 开放 mysql 端口 firewall-cmd --zone=public --permanent --add-port=3306/tcp # 重新载入规则 firewall-cmd --reload # 删除 mysql 端口 firewall-cmd --zone=public --permanent --remove-port=3306/tcp
iptables 实列
开放端口 iptables -A INPUT -p tcp --dport 3306 -j ACCEPT # 保存规则 service iptables save 删除端口 iptables -D INPUT -p tcp --dport 3306 -j ACCEPT