ubuntu中自带的ufw防火墙

2023-11-05

ufw是一个基于主机的(host-based)iptable防火墙。我使用的操作系统是Ubuntu10.04,ufw防火墙是默认安装的。下面是我用到的一些ufw命令(根据man文档整理):
1. 查看防火墙的状态:sudo ufw status. 状态显示是“inactive”关闭状态。
(status: show status of firewall and ufw managed rules.)
2. 开启或禁用ufw防火墙: sudo ufw enable/disable/reload.
(enable: reload firewall and enables firewall on boot)
(disable: unload firewall and disables firewall on boot)
(reload: reloads firewall)
3. 配置默认策略。默认配置包括数据的流入(incoming)和流出(outgoing),设置包括拒绝和接受两种。
sudo ufw default allow/deny/reject [incoming/outgoing]
(change the default policy for traffic going direction.)
4. 日志的开启和关闭:sudo ufw logging on/off
(toggle logging. Logged packets use the LOG_KERN syslog facility. Specifying a LEVEL turns logging on for the specified LEVEL. The default log level is ‘low’)

过滤规则的语法(RULE SYNTAX):
用户可以通过简单语法或者复杂语法指定过滤规则。简单的语法仅需要指定主机的端口和可选的协议就可以进行接受或拒绝设置。例如:
(Users can specify rules using a simple syntax or a full syntax.The simple syntax only specifies the port and optionally the protocol to be allowed or denied on the host. For example:)

sudo ufw allow 53     这条规则允许tcp和udp协议的53端口去访问任何地址。
(This rule will allow tcp and udp port 53 to any address on this host.)
sudo ufw allow 53/tcp
这条规则只允许主机tcp协议的53端口访问任何地址。 
(This will allow tcp port 53 to any address on this host.)
ufw也可以通过服务名来设置规则,例如:
(ufw will also check /ect/services for the port and protocol if specifying a service by name. Eg:)
sudo ufw allow smtp
ufw支持输入和输出过滤,用户可以使用in或out来指定过滤的方向。如果没有指定方向,默认的是对输入流进行过滤。例如:
(ufw supports both ingress and egress filtering and users may optionally specify a direction of either in or out for either incoming or outgoing traffic. If no direction is supplied, the rule applies to incoming traffic. Eg:)
sudo ufw allow in http
sudo ufw reject out smtp
用户可以使用更加复杂的语法来指定源和目的地址和端口。这个语法是基于OpenBSD的PF语法。例如:
(Users can also use a fuller syntax, specifying the source and destination addresses and ports. This syntax is based on OpenBSD's PF syntax. For example:)
sudo ufw deny proto tcp to any port 80   这条规则会拒绝所有访问本机tcp协议80端口的网络流。
(This will deny all traffic to tcp port 80 on this host.)
sudo ufw deny proto tcp from 10.0.0.0/8 to 192.168.0.1 port 25   这条规则会拒绝从10.0.0.0/8来访问192.168.0.1的端口25。
(This will deny all traffic from the 10.0.0.0/8 to tcp port 25 with the address 192.168.0.1)
sudo ufw allow proto tcp from any to any port 80,443,8080:8090  这会允许对tcp协议的80、443、8080-8090端口进行访问。当指定多个端口时,端口列表必须是数字而且不能有空格。不能一次指定超过15个端口。
(This will allow all traffic to tcp ports 80, 443, and 8080-8090 inclusive.)

ufw支持链接速率限制,这对于蛮力攻击(bruteforce attacks)是有用的。如果一个IP地址在30秒之内初始超过6次的链接就会被拒绝。 例如:
(ufw supports connection rate limiting, which is useful for protecting against brute-force login attacks. ufw will deny connections if an IP address has attempted 6 or more connections in the last 30 seconds. Typical usage is:)
sudo ufw limit ssh/tcp

可以在原始规则前加delete来删除一条规则,例如,原始规则是:
sudo ufw deny 80/tcp
使用这条语句删除:
sudo ufw delete deny 80/tcp

例子
sudo ufw deny 53 拒绝所有连向53端口的链接。
sudo ufw allow 80/tcp 允许链接tcp协议的80端口
sudo ufw allow from 10.0.0.0/8 允许10.0.0.0/8网段的主机访问本机
sudo ufw deny proto udp port 514 from host 1.2.3.4 拒绝1.2.3.4访问udp协议的514端口
sudo ufw allow proto udp from 1.2.3.5 port 5469 to 1.2.3.4 port 5469 允许主机1.2.3.5的端口5469访问主机1.2.3.4的5469端口

注意:
规则的顺序是很重要的,第一条匹配的规则起作用。因此,当添加新规则时,把特殊的规则放到通用的规则前面。查看防火墙的详细状态使用命令"sudo ufw show raw".

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

ubuntu中自带的ufw防火墙 的相关文章

随机推荐