Ubuntu之ufw安装和使用
一、ufw简介
ufw是Ubuntu系列发行版自带的类似iptables的防火墙管理软件,底层也是基于netfilter的。ufw是Uncomplicated FireWall的简称,真正地简化了 iptables,它从出现的这几年,已经成为 Ubuntu 和 Debian 等系统上的默认防火墙。而且 ufw 出乎意料的简单,这对新管理员来说是一个福音,否则他们可能需要投入大量时间来学习防火墙管理。ufw默认关闭,开放所有端口。环境说明:如下操作命令和示例均在Ubuntu20.04系统中测试。
二、使用示例
ufw命令需要通过root或者具有sudo权限的用户执行。
1、获取ufw命令帮助
root@s162:~# ufw --help
2、版本查看
root@s162:~# ufw version
ufw 0.36
Copyright 2008-2015 Canonical Ltd.
3、防火墙服务启停管理
- 查看防火墙状态
root@s162:~# ufw status
Status: activeTo Action From
– ------ ----
80/tcp ALLOW Anywhere
80/tcp (v6) ALLOW Anywhere (v6)88/tcp DENY OUT Anywhere
88/tcp (v6) DENY OUT Anywhere (v6)
- 查看防火墙状态详细信息
root@s162:~# ufw status verbose
Status: active
Logging: on (full)
Default: deny (incoming), allow (outgoing), disabled (routed)
New profiles: skipTo Action From
– ------ ----
80/tcp ALLOW IN Anywhere
80/tcp (v6) ALLOW IN Anywhere (v6)88/tcp DENY OUT Anywhere
88/tcp (v6) DENY OUT Anywhere (v6)
- 启动防火墙并设置开机自启动
root@s162:~# ufw enable
Command may disrupt existing ssh connections. Proceed with operation (y|n)? y
Firewall is active and enabled on system startup
- 停止防火墙并取消开机自启动
root@s162:~# ufw disable
Firewall stopped and disabled on system startup
- 重启防火墙
root@s162:~# ufw reload
Firewall reloaded
- 重置防火墙
root@s162:~# ufw reset
Resetting all rules to installed defaults. This may disrupt existing ssh
connections. Proceed with operation (y|n)? y
Backing up ‘user.rules’ to ‘/etc/ufw/user.rules.20210914_061746’
Backing up ‘before.rules’ to ‘/etc/ufw/before.rules.20210914_061746’
Backing up ‘after.rules’ to ‘/etc/ufw/after.rules.20210914_061746’
Backing up ‘user6.rules’ to ‘/etc/ufw/user6.rules.20210914_061746’
Backing up ‘before6.rules’ to ‘/etc/ufw/before6.rules.20210914_061746’
Backing up ‘after6.rules’ to ‘/etc/ufw/after6.rules.20210914_061746’
4、防火墙日志管理
- 关闭防火墙日志
root@s162:~# ufw logging off
Logging disabled
- 开启防火墙日志
root@s162:~# ufw logging on
Logging enabled
- 设置防火墙日志级别为low
root@s162:~# ufw logging low
Logging enabled
- 设置防火墙日志级别为medium
root@s162:~# ufw logging medium
Logging enabled
- 设置防火墙日志级别为high
root@s162:~# ufw logging high
Logging enabled
- 设置防火墙日志级别为full
root@s162:~# ufw logging full
Logging enabled
5、添加防火墙策略
如下示例以添加放行策略为例,示例同理适用allow、deny、reject,添加策略默认添加到入方向,如果需要添加到出方向使用关键词out。
- 添加放行一个端口策略
root@s162:~# ufw allow 8080
Rules updated
Rules updated (v6)
- 添加放行一个协议端口策略
root@s162:~# ufw allow 8081/tcp
Rules updated
Rules updated (v6)
root@s162:~# ufw allow 8082/udp
Rules updated
Rules updated (v6)
- 添加放行一个应用协议策略
root@s162:~# ufw allow http
Rule added
Rule added (v6)
- 添加放行一个端口策略
root@s162:~# ufw allow proto tcp to any port 81
Rule added
Rule added (v6)
- 允许指定源地址访问指定端口
root@s162:~# ufw allow proto tcp from 192.168.0.0/24 to 192.168.0.162 port 88
Rule added
- 运行指定源地址端口访问指定地址指定端口
root@s162:~# ufw allow proto udp from 1.2.3.5 port 5469 to 1.2.3.4 port 5469
Rule added
- 添加出方向安全策略
root@s162:~# ufw deny out 88/tcp
Rule added
Rule added (v6)
6、删除防火墙策略
- 查看策略编号
root@s162:~# ufw status numbered
Status: activeTo Action From
– ------ ----
[ 1] 80/tcp ALLOW IN Anywhere
[ 2] 80/tcp (v6) ALLOW IN Anywhere (v6)
- 删除指定编号策略
root@s162:~# ufw delete 2
Deleting:
allow 80/tcp
Proceed with operation (y|n)? y
Rule deleted (v6)
- 删除指定规则策略
root@s162:~# ufw delete allow 80/tcp
Rule deleted
Could not delete non-existent rule (v6)
7、插入安全策略
root@s162:~# ufw insert 1 deny proto tcp from 192.168.0.1 to any port 80
Rule inserted
root@s162:~# ufw insert 2 deny from 192.168.0.1 to any port 80
Rule inserted
8、添加默认安全策略
以下示例使用–dry-run参数模拟执行展示。
- 设置默认入策略为允许
root@s162:~# ufw --dry-run default allow
running ufw-init
running ufw-init
Default incoming policy changed to ‘allow’
(be sure to update your rules accordingly)
- 设置默认出策略为拒绝
root@s162:~# ufw --dry-run default deny outgoing
running ufw-init
running ufw-init
Default outgoing policy changed to ‘deny’
(be sure to update your rules accordingly)
9、添加自定义应用
- 首先自定义一个应用
root@s162:/etc/ufw/applications.d# cat test
[test]
title=test
description=this is a test.
ports=81/tcp
- 添加策略时调用应用名称
root@s162:/etc/ufw/applications.d# ufw allow test
Rule added
Rule added (v6)
- 查看防火墙策略
root@s162:/etc/ufw/applications.d# ufw status
Status: activeTo Action From
– ------ ----
80/tcp DENY 192.168.0.1
80 DENY 192.168.0.1
80/tcp REJECT Anywhere
test ALLOW Anywhere
80/tcp (v6) REJECT Anywhere (v6)
test (v6) ALLOW Anywhere (v6)88/tcp DENY OUT Anywhere
88/tcp (v6) DENY OUT Anywhere (v6)
三、ufw常用及默认规则说明
- ufw默认关闭
- ufw默认拒绝入流量,放行出流量
- ufw日志级别默认为low
- ufw添加策略默认为in放行,需要在出方向添加安全策略使用关键词out
- ufw添加策略为指定协议是默认为IP协议,包含TCP和UDP
- ufw使用–dry-run参数模拟执行
四、ufw使用语法
ufw [–dry-run] enable|disable|reload
ufw [–dry-run] default allow|deny|reject [incoming|outgoing|routed]
ufw [–dry-run] logging on|off|LEVEL
ufw [–dry-run] reset
ufw [–dry-run] status [verbose|numbered]
ufw [–dry-run] show REPORT
ufw [–dry-run] [delete] [insert NUM] allow|deny|reject|limit [in|out] [log|log-all]
PORT[/PROTOCOL]
ufw [–dry-run] [rule] [delete] [insert NUM] allow|deny|reject|limit [in|out [on
INTERFACE]] [log|log-all] [proto PROTOCOL] [from ADDRESS [port PORT]] [to ADDRESS [port
PORT]]
ufw [–dry-run] route [delete] [insert NUM] allow|deny|reject|limit [in|out on INTERFACE]
[log|log-all] [proto PROTOCOL] [from ADDRESS [port PORT]] [to ADDRESS [port PORT]]
ufw [–dry-run] delete NUM
ufw [–dry-run] app list|info|default|update
五、重要文件说明
- 参数配置文件/etc/ufw/ufw.conf
- 防火墙日志文件/var/log/ufw.log
- 路由转发参数配置文件/etc/ufw/sysctl.conf
- 应用定义文件目录/etc/ufw/applications.d
Ubuntu之ufw安装和使用相关推荐
- ubuntu系统怎么安装ssh服务器配置,如何在Ubuntu 20.04 LTS上安装SSH服务器
Terminal Linux Logo 在 步骤1.首先,通过apt在终端中运行以下以下命令,确保所有系统软件包都是最新的. sudo apt update sudo apt upgrade 步骤2. ...
- Ubuntu 18.04 安装 MySQL 5.7【解决普通用户登录、密码修改、远程访问等问题】
之前在Ubuntu 16.04安装 MySQL的时候很顺利,这次在 Ubuntu 18.04 中安装 MySQL 5.7.23 中,遇到一些坑,折腾了好久,这里做一个记录. 1. 安装数据库 # 安装 ...
- ubuntu之ufw防火墙
UFW 是Ubuntu下的一个主机端的iptables类防火墙配置工具(底层调用iptables来处理).这个工具的目的是提供给用户一个可以轻松驾驭的界面,就像包集成和动态检测开放的端口一样.虽然功能 ...
- Ubuntu 防火墙 ufw
UbuntuHelp:UFW :http://wiki.ubuntu.org.cn/UbuntuHelp:UFW Ufw使用指南:http://wiki.ubuntu.org.cn/Ufw使用指南 u ...
- lighttpd安装_如何在Ubuntu服务器上安装Lighttpd
lighttpd安装 In this tutorial, we will walk you through how to install Lighttpd on Ubuntu Server. Ligh ...
- Ubuntu防火墙UFW
防火墙是用于监控和过滤传入和传出网络流量的工具. 它通过定义一组确定是允许还是阻止特定流量的安全规则来工作. Ubuntu 22.04安装了一个称为UFW的防火墙配置工具.它是用于管理iptables ...
- ubuntu下ufw学习
ubuntu下ufw学习 安装 命令 状态 打开特定端口 允许某主机通信某端口 关闭端口 安装 apt-get install ufw ufw enable ufw default deny 命令 状 ...
- ubuntu 18.10安装ssh软件
ubuntu 18.10 ssh软件安装步骤: 输入 Ctrl + Alt + T 打开命令终端: 输入 sudo apt-get update 更新源列表: 在输入命令后,显示的是输入密码操作,之后 ...
- Ubuntu 20.04 安装配置 GitLab
Ubuntu 20.04 安装配置 GitLab GitLab GitLab 官网:https://about.gitlab.com/ GitLab CE 或 Community Edition 是一 ...
最新文章
- day22 案例 发送邮箱激活码 购物车 分析
- ×××S 2008 实用小技巧
- 监督学习和无监督学习_让半监督学习再次伟大!谷歌最新无监督数据增强研究,全面超越现有半监督学习方法...
- Spring事务嵌套引发的问题--Transaction rolled back because it has been marked as rollback-only...
- 虚函数和虚表指针的例子
- 最短路 dijkstra模板
- android自定义队列,Android 消息机制(一)消息队列的创建与循环的开始 Looper与MessageQueue...
- python线程同步
- 利用监听器实现在线人数统计
- 另一种阶乘 函数法!
- Linux驱动学习--HCI框架及源码分析
- DNF的SPK文件解析笔记
- yum 碰到的问题与解决办法
- coding码市管理项目
- FPGA实现 贪吃蛇
- GoldWave 音频截取工具
- (转)Native Extensions for Silverlight (NESL)?
- “住过一晚两万的ICU后,我还是建议你不要轻易买保险”
- 50多个有用的Docker工具集
- TreeView三角替换+-符号的问题