防火墙是Linux系统安全的第一道防线,通过控制网络流量的进出,保护系统免受未授权访问。本章将详细介绍Linux防火墙的基本概念和配置方法。
防火墙:网络安全系统,监控和控制网络流量。
防火墙功能:
东巴文理解:防火墙就像城堡的城门,检查进出的每一个人,只允许授权的人通过。
主要防火墙工具:
| 工具 | 说明 | 适用系统 |
|---|---|---|
| iptables | 底层防火墙工具 | 所有Linux |
| firewalld | 动态防火墙管理 | CentOS 7+、Fedora |
| UFW | 简易防火墙配置 | Ubuntu、Debian |
| nftables | 新一代防火墙 | 新版Linux |
东巴文提示:不同Linux发行版使用不同的防火墙工具,选择适合您系统的工具。
数据包过滤流程:
入站数据包 → 防火墙规则检查 → 允许/拒绝 → 系统
出站数据包 → 防火墙规则检查 → 允许/拒绝 → 网络
过滤依据:
默认策略:
东巴文最佳实践:生产环境建议使用DROP作为默认策略,更安全。
iptables结构:
表(Tables)
├── filter表(过滤)
├── nat表(地址转换)
├── mangle表(修改)
└── raw表(原始)
链(Chains)
├── INPUT(入站)
├── OUTPUT(出站)
├── FORWARD(转发)
├── PREROUTING(路由前)
└── POSTROUTING(路由后)
东巴文理解:iptables就像一个多层过滤器,数据包经过不同的表和链,逐层检查。
查看规则:
# 查看所有规则
iptables -L
# 查看指定链的规则
iptables -L INPUT
# 显示规则编号
iptables -L --line-numbers
# 显示详细信息
iptables -L -v
# 显示规则编号和详细信息
iptables -L -v --line-numbers
# 查看NAT规则
iptables -t nat -L
# 输出示例:
# Chain INPUT (policy ACCEPT)
# num target prot opt source destination
# 1 ACCEPT all -- 0.0.0.0/0 0.0.0.0/0 state RELATED,ESTABLISHED
# 2 ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:22
# 3 ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:80
# 4 DROP all -- 0.0.0.0/0 0.0.0.0/0
清除规则:
# 清除所有规则
iptables -F
# 清除指定链的规则
iptables -F INPUT
# 清除NAT规则
iptables -t nat -F
# 删除所有用户自定义链
iptables -X
# 重置计数器
iptables -Z
基本语法:
iptables -A 链名 规则选项 -j 动作
常用选项:
| 选项 | 说明 |
|---|---|
| -A | 追加规则到链末尾 |
| -I | 插入规则到链开头 |
| -I 链名 编号 | 插入规则到指定位置 |
| -D | 删除规则 |
| -R | 替换规则 |
| -p | 指定协议(tcp/udp/icmp) |
| -s | 指定源地址 |
| -d | 指定目标地址 |
| --sport | 指定源端口 |
| --dport | 指定目标端口 |
| -i | 指定入站接口 |
| -o | 指定出站接口 |
| -j | 指定动作(ACCEPT/DROP/REJECT) |
添加规则示例:
# 允许SSH连接
iptables -A INPUT -p tcp --dport 22 -j ACCEPT
# 允许HTTP连接
iptables -A INPUT -p tcp --dport 80 -j ACCEPT
# 允许HTTPS连接
iptables -A INPUT -p tcp --dport 443 -j ACCEPT
# 允许特定IP访问
iptables -A INPUT -s 192.168.1.100 -j ACCEPT
# 允许特定网段访问
iptables -A INPUT -s 192.168.1.0/24 -j ACCEPT
# 拒绝特定IP访问
iptables -A INPUT -s 192.168.1.200 -j DROP
# 允许本地回环
iptables -A INPUT -i lo -j ACCEPT
# 允许已建立的连接
iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
# 允许ICMP(ping)
iptables -A INPUT -p icmp -j ACCEPT
# 拒绝所有其他入站流量
iptables -A INPUT -j DROP
东巴文提示:规则的顺序很重要,iptables从上到下匹配,匹配到第一条规则就停止。
插入规则:
# 在链开头插入规则
iptables -I INPUT -p tcp --dport 8080 -j ACCEPT
# 在指定位置插入规则
iptables -I INPUT 2 -p tcp --dport 3306 -j ACCEPT
# 在第3行插入规则
iptables -I INPUT 3 -s 192.168.1.100 -j ACCEPT
删除规则:
# 按编号删除规则
iptables -D INPUT 1
# 按规则内容删除
iptables -D INPUT -p tcp --dport 80 -j ACCEPT
# 删除所有规则
iptables -F INPUT
协议匹配:
# TCP协议
iptables -A INPUT -p tcp --dport 80 -j ACCEPT
# UDP协议
iptables -A INPUT -p udp --dport 53 -j ACCEPT
# ICMP协议
iptables -A INPUT -p icmp -j ACCEPT
# 多端口匹配
iptables -A INPUT -p tcp -m multiport --dports 80,443,8080 -j ACCEPT
# 端口范围
iptables -A INPUT -p tcp --dport 8000:9000 -j ACCEPT
IP地址匹配:
# 单个IP
iptables -A INPUT -s 192.168.1.100 -j ACCEPT
# 网段
iptables -A INPUT -s 192.168.1.0/24 -j ACCEPT
# 多个IP(使用iprange模块)
iptables -A INPUT -m iprange --src-range 192.168.1.100-192.168.1.200 -j ACCEPT
# 目标地址
iptables -A INPUT -d 192.168.1.100 -j ACCEPT
接口匹配:
# 入站接口
iptables -A INPUT -i eth0 -j ACCEPT
# 出站接口
iptables -A OUTPUT -o eth0 -j ACCEPT
# 多个接口
iptables -A INPUT -i eth0 -i eth1 -j ACCEPT
连接状态匹配:
# 允许已建立的连接
iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
# 允许新连接
iptables -A INPUT -m state --state NEW -j ACCEPT
# 允许无效连接(不推荐)
iptables -A INPUT -m state --state INVALID -j ACCEPT
东巴文理解:连接状态匹配就像识别"老朋友",已建立的连接可以直接通过。
SNAT(源地址转换):
# 启用IP转发
echo 1 > /proc/sys/net/ipv4/ip_forward
# 或永久启用
vim /etc/sysctl.conf
net.ipv4.ip_forward = 1
# 应用配置
sysctl -p
# SNAT配置
iptables -t nat -A POSTROUTING -s 192.168.1.0/24 -o eth0 -j SNAT --to-source 10.0.0.1
# MASQUERADE(动态IP)
iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
DNAT(目标地址转换):
# 端口转发
iptables -t nat -A PREROUTING -p tcp --dport 80 -j DNAT --to-destination 192.168.1.100:80
# 端口映射
iptables -t nat -A PREROUTING -p tcp --dport 8080 -j DNAT --to-destination 192.168.1.100:80
# IP映射
iptables -t nat -A PREROUTING -d 10.0.0.1 -j DNAT --to-destination 192.168.1.100
东巴文应用:NAT常用于内网服务器对外提供服务。
本地端口转发:
# 将80端口转发到8080
iptables -t nat -A PREROUTING -p tcp --dport 80 -j REDIRECT --to-port 8080
远程端口转发:
# 将外部80端口转发到内网服务器
iptables -t nat -A PREROUTING -p tcp -d 10.0.0.1 --dport 80 -j DNAT --to-destination 192.168.1.100:80
iptables -t nat -A POSTROUTING -p tcp -d 192.168.1.100 --dport 80 -j MASQUERADE
保存规则:
# Debian/Ubuntu
# 安装iptables-persistent
sudo apt install iptables-persistent
# 保存规则
sudo netfilter-persistent save
# 规则保存位置
# /etc/iptables/rules.v4
# /etc/iptables/rules.v6
# CentOS/RHEL
# 安装iptables-services
sudo yum install iptables-services
# 保存规则
sudo service iptables save
# 规则保存位置
# /etc/sysconfig/iptables
手动保存规则:
# 保存IPv4规则
iptables-save > /etc/iptables/rules.v4
# 保存IPv6规则
ip6tables-save > /etc/iptables/rules.v6
# 恢复规则
iptables-restore < /etc/iptables/rules.v4
ip6tables-restore < /etc/iptables/rules.v6
#!/bin/bash
# Web服务器防火墙配置脚本
# 清除所有规则
iptables -F
iptables -X
iptables -t nat -F
iptables -t nat -X
# 设置默认策略
iptables -P INPUT DROP
iptables -P FORWARD DROP
iptables -P OUTPUT ACCEPT
# 允许本地回环
iptables -A INPUT -i lo -j ACCEPT
# 允许已建立的连接
iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
# 允许SSH
iptables -A INPUT -p tcp --dport 22 -j ACCEPT
# 允许HTTP
iptables -A INPUT -p tcp --dport 80 -j ACCEPT
# 允许HTTPS
iptables -A INPUT -p tcp --dport 443 -j ACCEPT
# 允许ICMP
iptables -A INPUT -p icmp -j ACCEPT
# 记录被拒绝的连接
iptables -A INPUT -m limit --limit 5/min -j LOG --log-prefix "iptables denied: " --log-level 7
# 拒绝其他所有入站流量
iptables -A INPUT -j DROP
# 保存规则
# Debian/Ubuntu
netfilter-persistent save
# CentOS/RHEL
service iptables save
echo "防火墙配置完成"
东巴文最佳实践:先允许必要的服务,最后拒绝其他所有流量。
#!/bin/bash
# 防DDoS攻击配置
# 限制SYN连接速率
iptables -A INPUT -p tcp --syn -m limit --limit 1/s --limit-burst 3 -j ACCEPT
# 限制ICMP速率
iptables -A INPUT -p icmp -m limit --limit 1/s --limit-burst 3 -j ACCEPT
# 限制SSH连接速率
iptables -A INPUT -p tcp --dport 22 -m connlimit --connlimit-above 3 -j DROP
# 限制HTTP连接速率
iptables -A INPUT -p tcp --dport 80 -m connlimit --connlimit-above 50 -j DROP
# 防止SYN Flood攻击
iptables -A INPUT -p tcp --syn -m recent --name synflood --set
iptables -A INPUT -p tcp --syn -m recent --name synflood --rcheck --seconds 60 --hitcount 3 -j DROP
# 防止端口扫描
iptables -A INPUT -m recent --name portscan --rcheck --seconds 86400 -j DROP
iptables -A INPUT -m recent --name portscan --remove
echo "防DDoS配置完成"
firewalld:动态防火墙管理工具。
firewalld特点:
东巴文理解:firewalld就像智能门禁系统,可以根据不同区域设置不同的访问规则。
预定义区域:
| 区域 | 说明 |
|---|---|
| drop | 丢弃所有入站流量 |
| block | 拒绝所有入站流量 |
| public | 公共区域,默认区域 |
| external | 外部区域,支持NAT |
| dmz | 非军事区 |
| work | 工作区域 |
| home | 家庭区域 |
| internal | 内部区域 |
| trusted | 信任所有流量 |
查看区域:
# 查看所有区域
firewall-cmd --get-zones
# 输出示例:
# block dmz drop external home internal public trusted work
# 查看默认区域
firewall-cmd --get-default-zone
# 输出示例:
# public
# 查看活动区域
firewall-cmd --get-active-zones
# 输出示例:
# public
# interfaces: eth0
# 查看区域详细信息
firewall-cmd --zone=public --list-all
# 输出示例:
# public (active)
# target: default
# icmp-block-inversion: no
# interfaces: eth0
# sources:
# services: ssh dhcpv6-client
# ports:
# protocols:
# forward: no
# masquerade: no
# forward-ports:
# source-ports:
# icmp-blocks:
# rich rules:
# 启动firewalld
sudo systemctl start firewalld
# 停止firewalld
sudo systemctl stop firewalld
# 重启firewalld
sudo systemctl restart firewalld
# 查看状态
sudo systemctl status firewalld
# 设置开机自启
sudo systemctl enable firewalld
# 禁用开机自启
sudo systemctl disable firewalld
# 检查是否运行
firewall-cmd --state
# 输出示例:
# running
设置默认区域:
# 设置默认区域
sudo firewall-cmd --set-default-zone=home
# 查看默认区域
firewall-cmd --get-default-zone
# 输出示例:
# home
接口绑定:
# 将接口绑定到区域
sudo firewall-cmd --zone=public --change-interface=eth0
# 查看接口所属区域
firewall-cmd --get-zone-of-interface=eth0
# 输出示例:
# public
# 将接口添加到区域
sudo firewall-cmd --zone=trusted --add-interface=eth1
# 从区域移除接口
sudo firewall-cmd --zone=trusted --remove-interface=eth1
源地址绑定:
# 将源地址绑定到区域
sudo firewall-cmd --zone=trusted --add-source=192.168.1.0/24
# 查看区域的源地址
firewall-cmd --zone=trusted --list-sources
# 移除源地址
sudo firewall-cmd --zone=trusted --remove-source=192.168.1.0/24
# 查看所有可用服务
firewall-cmd --get-services
# 输出示例:
# RH-Satellite-6 amanda-client amanda-k5-client amqp amqps apcupsd audit bacula bacula-client bgp bitcoin bitcoin-rpc bitcoin-testnet bitcoin-testnet-rpc ceph ceph-mon cfengine condor-collector ctdb dhcp dhcpv6 dhcpv6-client distcc dns docker-registry docker-swarm dropbox-lansync elasticsearch etcd-client etcd-server finger freeipa-ldap freeipa-ldaps freeipa-replication freeipa-trust ftp ganglia-client ganglia-master git gre high-availability http https imap imaps ipp ipp-client ipsec irc ircs iscsi-target isns jenkins kadmin kerberos kibana klogin kpasswd kprop kshell ldap ldaps libvirt libvirt-tls lightning-network llmnr managesieve matrix mdns minidlna mongodb mosh mountd mqtt mqtt-tls ms-wbt mssql murmur mysql nfs nfs3 nmea-0183 nrpe ntp nut openvpn ovirt-imageio ovirt-storageconsole ovirt-vmconsole plex pmcd pmproxy pmwebapi pmwebapis pop3 pop3s postgresql privoxy proxy-dhcp ptp pulseaudio puppetmaster quassel radius redis rpc-bind rsh rsyncd rtsp salt-master samba samba-client samba-dc sane sip sips slp smtp smtp-submission smtps snmp snmptrap spideroak-lansync squid ssh steam-streaming svdrp svn syncthing syncthing-gui synergy syslog syslog-tls telnet tftp tftp-client tinc tor-socks transmission-client upnp-client vdsm vnc-server wbem-http wbem-https wsman wsmans xdmcp xmpp-bosh xmpp-client xmpp-local xmpp-server zabbix-agent zabbix-server
# 查看区域开放的服务
firewall-cmd --zone=public --list-services
# 输出示例:
# ssh dhcpv6-client
# 查看服务详细信息
firewall-cmd --info-service=http
# 输出示例:
# http
# ports: 80/tcp
# protocols:
# source-ports:
# modules:
# destination:
临时添加服务:
# 添加服务(临时)
sudo firewall-cmd --zone=public --add-service=http
# 添加多个服务
sudo firewall-cmd --zone=public --add-service={http,https,ssh}
# 移除服务
sudo firewall-cmd --zone=public --remove-service=http
永久添加服务:
# 添加服务(永久)
sudo firewall-cmd --permanent --zone=public --add-service=http
# 添加多个服务
sudo firewall-cmd --permanent --zone=public --add-service={http,https,ssh}
# 移除服务
sudo firewall-cmd --permanent --zone=public --remove-service=http
# 重新加载配置
sudo firewall-cmd --reload
东巴文提示:临时规则立即生效但重启后失效,永久规则需要reload才能生效但重启后保留。
创建服务文件:
# 复制现有服务文件
sudo cp /usr/lib/firewalld/services/http.xml /etc/firewalld/services/myapp.xml
# 编辑服务文件
sudo vim /etc/firewalld/services/myapp.xml
# 文件内容
<?xml version="1.0" encoding="utf-8"?>
<service>
<short>MyApp</short>
<description>My Application Service</description>
<port protocol="tcp" port="8080"/>
</service>
# 重新加载配置
sudo firewall-cmd --reload
# 使用自定义服务
sudo firewall-cmd --permanent --zone=public --add-service=myapp
sudo firewall-cmd --reload
# 开放端口(临时)
sudo firewall-cmd --zone=public --add-port=80/tcp
# 开放多个端口
sudo firewall-cmd --zone=public --add-port={80/tcp,443/tcp,8080/tcp}
# 开放端口范围
sudo firewall-cmd --zone=public --add-port=8000-9000/tcp
# 开放UDP端口
sudo firewall-cmd --zone=public --add-port=53/udp
# 开放端口(永久)
sudo firewall-cmd --permanent --zone=public --add-port=80/tcp
sudo firewall-cmd --reload
# 移除端口(临时)
sudo firewall-cmd --zone=public --remove-port=80/tcp
# 移除端口(永久)
sudo firewall-cmd --permanent --zone=public --remove-port=80/tcp
sudo firewall-cmd --reload
# 查看开放的端口
firewall-cmd --zone=public --list-ports
# 输出示例:
# 80/tcp 443/tcp 8080/tcp
# 查看所有配置
firewall-cmd --zone=public --list-all
富规则语法:
rule [family="ipv4|ipv6"]
source [NOT] address="address[/mask]" [mac="mac-address"]
destination [NOT] address="address[/mask]"
service name="service-name"
port port="port-number" protocol="tcp|udp"
protocol value="protocol-value"
icmp-block name="icmp-type"
masquerade
forward-port port="port-number" protocol="tcp|udp" to-port="port-number" to-addr="address"
log [prefix="prefix-text"] [level="emerg|alert|crit|err|warn|notice|info|debug"] [limit value="rate/duration"]
audit [limit value="rate/duration"]
accept|reject [type="reject-type"]|drop
富规则示例:
# 允许特定IP访问SSH
sudo firewall-cmd --permanent --zone=public --add-rich-rule='rule family="ipv4" source address="192.168.1.100" service name="ssh" accept'
# 拒绝特定IP访问
sudo firewall-cmd --permanent --zone=public --add-rich-rule='rule family="ipv4" source address="192.168.1.200" reject'
# 允许特定IP访问特定端口
sudo firewall-cmd --permanent --zone=public --add-rich-rule='rule family="ipv4" source address="192.168.1.0/24" port protocol="tcp" port="3306" accept'
# 限制连接速率
sudo firewall-cmd --permanent --zone=public --add-rich-rule='rule service name="ssh" limit value="3/m" accept'
# 记录被拒绝的连接
sudo firewall-cmd --permanent --zone=public --add-rich-rule='rule family="ipv4" source address="0.0.0.0/0" log prefix="firewalld: " level="notice" limit value="3/m" reject'
# 端口转发
sudo firewall-cmd --permanent --zone=public --add-rich-rule='rule family="ipv4" forward-port port="80" protocol="tcp" to-port="8080"'
# 查看富规则
firewall-cmd --zone=public --list-rich-rules
# 移除富规则
sudo firewall-cmd --permanent --zone=public --remove-rich-rule='rule family="ipv4" source address="192.168.1.100" service name="ssh" accept'
# 重新加载配置
sudo firewall-cmd --reload
东巴文理解:富规则就像精细的访问控制列表,可以实现复杂的访问控制。
启用NAT:
# 启用IP转发
sudo sysctl -w net.ipv4.ip_forward=1
# 永久启用
sudo vim /etc/sysctl.conf
net.ipv4.ip_forward = 1
# 应用配置
sudo sysctl -p
# 启用伪装
sudo firewall-cmd --permanent --zone=public --add-masquerade
# 端口转发
sudo firewall-cmd --permanent --zone=public --add-forward-port=port=80:proto=tcp:toport=8080:toaddr=192.168.1.100
# 重新加载配置
sudo firewall-cmd --reload
UFW(Uncomplicated Firewall):Ubuntu简易防火墙。
UFW特点:
东巴文理解:UFW就像简化版的防火墙,让新手也能轻松配置。
# 安装UFW
sudo apt install ufw
# 查看状态
sudo ufw status
# 输出示例:
# Status: inactive
# 查看详细状态
sudo ufw status verbose
# 输出示例:
# Status: inactive
# 启用UFW
sudo ufw enable
# 输出示例:
# Command may disrupt existing ssh connections. Proceed with operation (y|n)? y
# Firewall is active and enabled on system startup
# 禁用UFW
sudo ufw disable
# 重置UFW
sudo ufw reset
# 设置默认入站策略
sudo ufw default deny incoming
# 设置默认出站策略
sudo ufw default allow outgoing
# 查看默认策略
sudo ufw status verbose
# 输出示例:
# Status: active
# Logging: on (low)
# Default: deny (incoming), allow (outgoing), disabled (routed)
# New profiles: skip
允许服务:
# 允许SSH
sudo ufw allow ssh
# 或使用端口号
sudo ufw allow 22
# 允许HTTP
sudo ufw allow http
# 或使用端口号
sudo ufw allow 80
# 允许HTTPS
sudo ufw allow https
# 或使用端口号
sudo ufw allow 443
# 允许特定端口和协议
sudo ufw allow 80/tcp
# 允许端口范围
sudo ufw allow 8000:9000/tcp
# 允许特定IP访问
sudo ufw allow from 192.168.1.100
# 允许特定IP访问特定端口
sudo ufw allow from 192.168.1.100 to any port 22
# 允许特定网段访问
sudo ufw allow from 192.168.1.0/24
# 允许特定接口
sudo ufw allow in on eth0 to any port 80
拒绝服务:
# 拒绝SSH
sudo ufw deny ssh
# 拒绝特定端口
sudo ufw deny 80
# 拒绝特定IP访问
sudo ufw deny from 192.168.1.200
# 拒绝特定IP访问特定端口
sudo ufw deny from 192.168.1.200 to any port 22
# 查看规则编号
sudo ufw status numbered
# 输出示例:
# Status: active
#
# To Action From
# -- ------ ----
# [ 1] 22/tcp ALLOW IN Anywhere
# [ 2] 80/tcp ALLOW IN Anywhere
# [ 3] 443/tcp ALLOW IN Anywhere
# [ 4] 22/tcp (v6) ALLOW IN Anywhere (v6)
# [ 5] 80/tcp (v6) ALLOW IN Anywhere (v6)
# [ 6] 443/tcp (v6) ALLOW IN Anywhere (v6)
# 按编号删除规则
sudo ufw delete 2
# 按规则内容删除
sudo ufw delete allow 80
# 删除拒绝规则
sudo ufw delete deny 80
查看可用应用:
# 查看可用应用
sudo ufw app list
# 输出示例:
# Available applications:
# Apache
# Apache Full
# Apache Secure
# OpenSSH
# 查看应用信息
sudo ufw app info "Apache Full"
# 输出示例:
# Profile: Apache Full
# Title: Web Server (HTTP,HTTPS)
# Description: Apache v2 is the next generation of the omnipresent Apache web server.
#
# Ports:
# 80,443/tcp
使用应用配置:
# 允许应用
sudo ufw allow "Apache Full"
# 允许OpenSSH
sudo ufw allow "OpenSSH"
# 查看应用规则
sudo ufw status
创建应用配置文件:
# 创建配置文件
sudo vim /etc/ufw/applications.d/myapp
# 文件内容
[MyApp]
title=My Application
description=My custom application
ports=8080/tcp
# 更新应用列表
sudo ufw app update MyApp
# 使用自定义应用
sudo ufw allow MyApp
# 启用日志
sudo ufw logging on
# 设置日志级别
sudo ufw logging low # 低级别
sudo ufw logging medium # 中级别
sudo ufw logging high # 高级别
# 禁用日志
sudo ufw logging off
# 查看日志
sudo tail -f /var/log/ufw.log
# 输出示例:
# Jan 1 10:00:00 server kernel: [UFW BLOCK] IN=eth0 OUT= MAC=00:0c:29:12:34:56:00:0c:29:ab:cd:ef:08:00 SRC=192.168.1.100 DST=192.168.1.200 LEN=60 TOS=0x00 PREC=0x00 TTL=64 ID=12345 DF PROTO=TCP SPT=54321 DPT=22 WINDOW=29200 RES=0x00 SYN URGP=0
# 查看被阻止的连接
sudo grep "UFW BLOCK" /var/log/ufw.log
# 统计被阻止的IP
sudo grep "UFW BLOCK" /var/log/ufw.log | awk '{print $11}' | sort | uniq -c | sort -nr
# 查看特定IP的日志
sudo grep "192.168.1.100" /var/log/ufw.log
#!/bin/bash
# Web服务器UFW配置脚本
# 重置UFW
sudo ufw reset
# 设置默认策略
sudo ufw default deny incoming
sudo ufw default allow outgoing
# 允许SSH
sudo ufw allow ssh
# 允许HTTP
sudo ufw allow http
# 允许HTTPS
sudo ufw allow https
# 允许特定IP访问MySQL
sudo ufw allow from 192.168.1.100 to any port 3306
# 启用日志
sudo ufw logging medium
# 启用UFW
sudo ufw enable
# 查看状态
sudo ufw status verbose
echo "UFW配置完成"
#!/bin/bash
# 邮件服务器UFW配置脚本
# 重置UFW
sudo ufw reset
# 设置默认策略
sudo ufw default deny incoming
sudo ufw default allow outgoing
# 允许SSH
sudo ufw allow ssh
# 允许SMTP
sudo ufw allow 25/tcp
# 允许SMTPS
sudo ufw allow 465/tcp
# 允许Submission
sudo ufw allow 587/tcp
# 允许IMAP
sudo ufw allow 143/tcp
# 允许IMAPS
sudo ufw allow 993/tcp
# 允许POP3
sudo ufw allow 110/tcp
# 允许POP3S
sudo ufw allow 995/tcp
# 启用日志
sudo ufw logging medium
# 启用UFW
sudo ufw enable
# 查看状态
sudo ufw status verbose
echo "邮件服务器UFW配置完成"
✅ 理解防火墙基本概念 ✅ 掌握iptables配置与管理 ✅ 学会firewalld配置与管理 ✅ 熟练使用UFW防火墙 ✅ 了解防火墙安全策略
完成本章学习后,请确认您能够:
东巴文(db-w.cn) - 让Linux学习更简单