Wireshark 安装与初始化实战
Ubuntu 24.04 从零安装 Wireshark 4.2.2 全家桶双方案实测(在线仓库/离线 deb 308 包),dumpcap 权限初始化与非 root 抓包排障实录(dpkg-reconfigure 授权、wireshark 组、重装失效复现),tshark 验证主线全程回显。
Wireshark 安装与初始化实战
目标:在一台 Ubuntu 24.04 服务器上从零完成 Wireshark 全家桶(wireshark/tshark/dumpcap 及 editcap/mergecap/capinfos/text2pcap 等周边工具)的安装与初始化,重点打通「非 root 用户抓包」的 dumpcap 权限链路,并给出在线仓库与离线 deb 两种安装方案的完整实测过程与排障实录。本文全部命令与回显均在真实环境逐条实测(Ubuntu 24.04 LTS,2026-10-27)。
一、环境信息
- 操作系统:Ubuntu 24.04 LTS(实测环境为 24.04.5,x86_64)
- 内核:Linux 7.0.0-38-generic(
uname -r可查,tshark 版本输出同显) - 网卡:eth0(内网 IP 10.89.63.2,网关 10.89.63.1),另有 lo 回环接口
- Wireshark 版本:4.2.2-1.1build3(Ubuntu 24.04 noble/universe 仓库实装,apt 索引查询日期 2026-10-27)
- 官方最新稳定版:4.6.9(wireshark.org/download.html,查询日期 2026-10-27,选型说明见第三章)
- 抓包底层库:libpcap 1.10.4(
tshark --version输出实测) - 演示普通用户:admin(口令 q1w2e3r4,用于非 root 抓包实测)
- 内存:19949 MB(
tshark --version输出同显)
二、前置条件
安装前确认以下四项(每项给出验证命令与实测回显):
- 具备 sudo 或 root 权限(安装与授权环节需要):
whoami # 确认当前身份
实测回显(root 身份执行):
root
- 系统能访问软件源(在线方案需要;离线方案跳过本项):
apt-get update -qq && echo APT-OK # 静默刷新索引,成功输出 APT-OK
实测回显:
APT-OK
- 确认目标版本在仓库中可见:
apt-cache policy wireshark tshark
实测回显(节选):
wireshark:
Installed: (none)
Candidate: 4.2.2-1.1build3
Version table:
4.2.2-1.1build3 500
500 http://archive.ubuntu.com/ubuntu noble/universe amd64 Packages
tshark:
Installed: (none)
Candidate: 4.2.2-1.1build3
Version table:
4.2.2-1.1build3 500
500 http://archive.ubuntu.com/ubuntu noble/universe amd64 Packages
💡 说明:
wireshark与tshark候选版本一致(4.2.2-1.1build3),说明两者同源同版本发布——tshark是 CLI 前端、wireshark是 Qt GUI 前端,共享同一个wireshark-common解析核心包。
- 创建用于非 root 抓包实测的普通用户(第二章演示账号,口令 q1w2e3r4):
useradd -m -s /bin/bash admin # 创建演示用户 admin
echo 'admin:q1w2e3r4' | chpasswd # 设置演示口令
实测回显(无输出即成功):
id admin
uid=1001(admin) gid=1001(admin) groups=1001(admin)
三、版本选型说明
Wireshark 的版本获取渠道有三类,实测对比后的选择依据如下:
| 渠道 | 版本 | 获取方式 | 适用 |
|---|---|---|---|
| Ubuntu 24.04 仓库 | 4.2.2-1.1build3 | apt install | 服务器默认选择:安全更新随发行版走、依赖自动解决 |
| 官方上游稳定版 | 4.6.9(2026-10-27 官网查询) | 官网安装包/PPA/源码编译 | 需要最新协议支持时 |
| 官方旧稳定维护线 | 4.4.19 | 官网下载页 Old Stable 区 | 生产环境锁旧大版本、只跟补丁 |
本文选 Ubuntu 24.04 仓库的 4.2.2-1.1build3 做主线实测,理由:服务器运维的第一诉求是「装得上、更新稳、好回滚」,发行版仓库版本的协议解析能力覆盖绝大多数排障场景;且该版本与官方 4.2.x 上游同源(包版本号直接标注 packaged as 4.2.2-1.1build3)。需要 4.6.x 新特性(如最新协议 dissector)时再评估官方渠道,两者安装与初始化流程一致。
四、方案 A:在线仓库安装(推荐主线)
4.1 方案说明
通过 apt 从 Ubuntu 官方仓库安装 wireshark 与 tshark 两个包。wireshark 元包会连带安装 GUI 前端(wireshark-qt)与解析核心(wireshark-common、wireshark-data);tshark 安装 CLI 前端。无界面的服务器上 GUI 装了也用不上,但统一安装便于后续把 pcap 文件拷回桌面端用同版本 GUI 分析,版本对齐可避免解析行为差异。
4.2 安装步骤
DEBIAN_FRONTEND=noninteractive apt-get install -y wireshark tshark
实测回显(安装日志较长,节选首尾;共下载 331 个包):
Reading package lists...
Building dependency tree...
Reading state information...
The following additional packages will be installed:
adwaita-icon-theme at-spi2-common at-spi2-core dbus dbus-bin dbus-daemon
dbus-session-bus-common dbus-system-bus-common dbus-user-session
...(输出节选,中间省略依赖列表与下载进度行)
Setting up libqt6waylandeglcompositorhwintegration6:amd64 (6.4.2-5build3) ...
Setting up libqt6waylandeglclienthwintegration6:amd64 (6.4.2-5build3) ...
Setting up qt6-wayland:amd64 (6.4.2-5build3) ...
Setting up adwaita-icon-theme (46.0-1) ...
Setting up libgtk-3-0t64:amd64 (3.24.41-4ubuntu1.3) ...
Processing triggers for libc-bin (2.39-0ubuntu8.9) ...
Processing triggers for libgdk-pixbuf-2.0-0:amd64 (2.42.10+dfsg-2ubuntu3.3) ...
⚠️ 注意交互式安装的弹窗:在带终端的服务器上直接执行
sudo apt install wireshark,安装wireshark-common时会弹出一个全屏 debconf 对话框,问句原文为 "Should non-superusers be able to capture packets?"(默认<No>)。选Yes即在安装阶段完成 dumpcap 特权授权;选No或像本文一样以 noninteractive 方式安装(自动取默认No),则安装后必须按第七章手动补授权——这是 Ubuntu 服务器上安装 Wireshark 最常见的「装完抓不了包」根因。
4.3 安装结果验证
tshark --version | head -4
实测回显:
TShark (Wireshark) 4.2.2 (Git v4.2.2 packaged as 4.2.2-1.1build3).
Copyright 1998-2024 Gerald Combs <gerald@wireshark.org> and contributors.
Licensed under the terms of the GNU General Public License (version 2 or later).
which tshark dumpcap editcap mergecap capinfos text2pcap rawshark
实测回显(全家桶七个工具全部就位):
/usr/bin/tshark
/usr/bin/dumpcap
/usr/bin/editcap
/usr/bin/mergecap
/usr/bin/capinfos
/usr/bin/text2pcap
/usr/bin/rawshark
五、方案 B:离线 deb 包安装(断网服务器)
5.1 方案说明
服务器无法联网时,在一台同版本、可联网的机器(相同发行版与代号,本文即另一台 Ubuntu 24.04)上用 apt-get install --download-only 把目标包与全部依赖下载成 deb 文件集,打包带到目标服务器,用 dpkg -i 离线安装。本节在实测环境中完整走通「下载 → 离线安装 → 修复依赖」三步(先 purge 清空环境模拟断网初始状态)。
5.2 第一步:在可联网机器上下载全量 deb
apt-get clean
DEBIAN_FRONTEND=noninteractive apt-get install --download-only -y wireshark tshark
实测回显(节选首尾):
Reading package lists...
Building dependency tree...
Reading state information...
The following additional packages will be installed:
adwaita-icon-theme at-spi2-common at-spi2-core dbus dbus-bin dbus-daemon
...(输出节选,中间省略依赖列表)
Get:308 http://archive.ubuntu.com/ubuntu noble/universe amd64 wireshark amd64 4.2.2-1.1build3 [4621 kB]
Fetched 188 MB in 38s (4893 kB/s)
Download complete and in download only mode
确认 deb 文件集已落在 apt 缓存目录:
ls /var/cache/apt/archives/*.deb | wc -l
实测回显:
308
把这 308 个 deb 打包带走(示例):
cd /var/cache/apt && tar czf /opt/wireshark-4.2.2-debs.tar.gz archives/*.deb
# 将 /opt/wireshark-4.2.2-debs.tar.gz 经 U 盘/内网传至断网服务器后解包到任意目录
5.3 第二步:目标服务器 dpkg 离线安装
在断网服务器上(deb 文件所在目录)一次性安装全部 deb:
cd /var/cache/apt/archives && dpkg -i *.deb
实测回显(节选;308 个包中 292 个配置完成,末尾出现 3 个依赖顺序报错):
Setting up tshark:amd64 (4.2.2-1.1build3) ...
Setting up wireshark-common:amd64 (4.2.2-1.1build3) ...
Setting up wireshark:amd64 (4.2.2-1.1build3) ...
...(输出节选,中间省略大量 Setting up 行,共 292 行)
dpkg: error processing archive python3-minimal_3.12.3-0ubuntu2.1_amd64.deb (--install):
package python3-minimal is already installed and configured
dpkg: error processing archive python3_3.12.3-0ubuntu2.1_amd64.deb (--install):
package python3 is already installed and configured
dpkg: error processing archive systemd-sysv_255.4-1ubuntu8.18_amd64.deb (--install):
package systemd-sysv is already installed and configured
Errors were encountered while processing:
python3-minimal python3 systemd-sysv
💡 说明:
dpkg -i单遍安装不处理包间依赖顺序,少数包(此处 python3 系与 systemd-sysv,属于系统既装包的版本交叉)报already installed and configured属正常现象,不影响 Wireshark 本体——上一步回显中 tshark/wireshark-common/wireshark 均已 Setting up 成功。按下一小节用 apt 收尾即可清账。
5.4 第三步:apt 修复依赖收尾
DEBIAN_FRONTEND=noninteractive apt-get install -f -y
实测回显(节选尾部):
Setting up at-spi2-core (2.52.0-1build1) ...
Setting up libgtk-3-bin (3.24.41-4ubuntu1.3) ...
Setting up qt6-gtk-platformtheme:amd64 (6.4.2+dfsg-21.1build5) ...
Processing triggers for libc-bin (2.39-0ubuntu8.9) ...
验证无未完成配置的包(dpkg --audit 无输出即干净):
dpkg -l | grep -E '^ii.*(wireshark|tshark)'
实测回显:
ii tshark 4.2.2-1.1build3 amd64 network traffic analyzer - command-line sniffer
ii wireshark 4.2.2-1.1build3 amd64 network traffic analyzer - meta-package
ii wireshark-common 4.2.2-1.1build3 amd64 network traffic analyzer - common binaries
ii wireshark-data 4.2.2-1.1build3 all network traffic analyzer - data files
ii wireshark-qt 4.2.2-1.1build3 amd64 network traffic analyzer - Qt version
dpkg --audit
实测回显:无输出(安装账目干净)。
5.5 离线安装结果验证
tshark --version | head -1
实测回显:
TShark (Wireshark) 4.2.2 (Git v4.2.2 packaged as 4.2.2-1.1build3).
六、方案对比与选择建议
| 维度 | 方案 A:在线仓库 | 方案 B:离线 deb |
|---|---|---|
| 是否需联网 | 需要 | 仅下载机联网,目标机断网可用 |
| 依赖处理 | apt 全自动 | dpkg 单遍装 + apt -f 收尾(可能少量顺序报错) |
| 版本 | 仓库当前版 4.2.2-1.1build3 | 与下载机仓库版本一致 |
| 操作复杂度 | 一条命令 | 下载/传输/安装三步 |
| 适用场景 | 可联网服务器(默认选择) | 断网/隔离区服务器、批量部署同版本 |
选择建议:能联网一律方案 A;隔离区服务器用方案 B,且下载机必须与目标机同发行版同代号(否则依赖集不一致,dpkg -i 报错面会大很多)。两个方案装完后的 dumpcap 权限初始化完全相同,见下章。
七、dumpcap 权限初始化(非 root 抓包)
7.1 权限模型说明
Wireshark 的抓包特权收敛在 dumpcap 一个程序上(GUI/tshark 实时抓包都是调它)。Ubuntu 包的授权机制:把 /usr/bin/dumpcap 的属组设为 wireshark 组、并赋予 cap_net_admin 与 cap_net_raw 文件能力,任何被加入 wireshark 组的用户即可抓包,无需 root。是否执行这套授权由 debconf 问题 wireshark-common/install-setuid(问句 "Should non-superusers be able to capture packets?",默认 false)控制——noninteractive 安装或选了 No 的读者,装完即是未授权状态。
先看当前状态(未授权):
debconf-show wireshark-common | grep setuid
实测回显:
wireshark-common/install-setuid: false
7.2 复现失败现象(排障实录第一步)
用演示用户 admin 尝试抓包。先确认接口枚举正常:
su - admin -c 'tshark -D'
实测回显(-D 只枚举接口,不需要抓包特权):
1. eth0
2. any
3. lo (Loopback)
4. bluetooth-monitor
5. nflog
6. nfqueue
7. dbus-system
8. dbus-session
9. ciscodump (Cisco remote capture)
10. dpauxmon (DisplayPort AUX channel monitor capture)
11. randpkt (Random packet generator)
12. sdjournal (systemd Journal Export)
13. sshdump (SSH remote capture)
14. udpdump (UDP Listener remote capture)
15. wifidump (Wi-Fi remote capture)
真正抓包立刻失败:
su - admin -c 'tshark -i any -c 2'
实测回显(错误信息原文,tshark 自带修复指引):
Capturing on 'any'
tshark: You do not have permission to capture on device "any".
(socket: Operation not permitted)
Please check to make sure you have sufficient permissions.
On Debian and Debian derivatives such as Ubuntu, if you have installed Wireshark from a package, try running
sudo dpkg-reconfigure wireshark-common
selecting "<Yes>" in response to the question
Should non-superusers be able to capture packets?
adding yourself to the "wireshark" group by running
sudo usermod -a -G wireshark {your username}
and then logging out and logging back in again.
If you did not install Wireshark from a package, ensure that Dumpcap has the needed CAP_NET_RAW and CAP_NET_ADMIN capabilities by running
sudo setcap cap_net_raw,cap_net_admin=ep {path/to/}dumpcap
and then restarting Wireshark.
0 packets captured
7.3 修复第一步:dpkg-reconfigure 授权
交互式终端上直接回答 sudo dpkg-reconfigure wireshark-common 并选 <Yes> 即可。无交互终端(脚本/自动化)时用 debconf 预设,等效选 Yes:
echo 'wireshark-common wireshark-common/install-setuid boolean true' | debconf-set-selections
DEBIAN_FRONTEND=noninteractive dpkg-reconfigure -f noninteractive wireshark-common
验证授权已生效:
getcap /usr/bin/dumpcap
ls -l /usr/bin/dumpcap
getent group wireshark
实测回显(三件套齐了:文件能力 + wireshark 属组 + 组存在):
/usr/bin/dumpcap cap_net_admin,cap_net_raw=eip
-rwxr-xr-- 1 root wireshark 229112 Apr 16 2024 /usr/bin/dumpcap
wireshark:x:103:
⚠️ 排障注记:给
debconf-set-selections喂的预设行必须是四段式包名 题名 boolean true。写成wireshark-common/install-setuid select true会报warning: Unknown type true, skipping line(实测踩坑),dpkg-reconfigure 后授权仍是 false——boolean 型问题要用boolean关键字。
7.4 修复第二步:用户入组并重新登录
usermod -aG wireshark admin
groups admin
实测回显:
admin : admin wireshark
⚠️ 注意:组关系在登录会话建立时注入。已在登录中的用户必须退出重登(或
newgrp wireshark临时切换)才拿到新组——「加了组还不行」九成是这个原因。
7.5 修复结果验证:admin 抓包成功
在 lo 回环口上产生 ICMP 流量并抓 4 个报文(ping 127.0.0.1 后台运行,admin 身份抓包):
ping -c 3 -i 0.2 127.0.0.1 > /dev/null &
su - admin -c 'timeout 10 tshark -i lo -c 4'
实测回显(抓包成功,ICMP 请求/应答逐条列出):
1 0.000000000 127.0.0.1 → 127.0.0.1 ICMP 98 Echo (ping) request id=0x0055, seq=2/512, ttl=64
2 0.000009461 127.0.0.1 → 127.0.0.1 ICMP 98 Echo (ping) reply id=0x0055, seq=2/512, ttl=64 (request in 1)
3 0.207068088 127.0.0.1 → 127.0.0.1 ICMP 98 Echo (ping) request id=0x0055, seq=3/768, ttl=64
4 0.207076300 127.0.0.1 → 127.0.0.1 ICMP 98 Echo (ping) reply id=0x0055, seq=3/768, ttl=64 (request in 3)
root 身份抓外网口 eth0 同样验证(root 无需入组):
ping -c 3 -i 0.3 10.89.63.1 > /dev/null &
timeout 12 tshark -i eth0 -c 4
实测回显:
1 0.000000000 10.89.63.2 → 10.89.63.1 ICMP 98 Echo (ping) request id=0x0056, seq=2/512, ttl=64
2 0.000023655 10.89.63.1 → 10.89.63.2 ICMP 98 Echo (ping) reply id=0x0056, seq=2/512, ttl=64 (request in 1)
3 0.302922714 10.89.63.2 → 10.89.63.1 ICMP 98 Echo (ping) request id=0x0056, seq=3/768, ttl=64
4 0.302942441 10.89.63.1 → 10.89.63.2 ICMP 98 Echo (ping) reply id=0x0056, seq=3/768, ttl=64 (request in 3)
7.6 手动 setcap 方式(非包安装场景)
若 Wireshark 不是从发行版包安装(如源码编译部署),按 7.2 错误信息中的官方指引直接给 dumpcap 赋能力即可:sudo setcap cap_net_raw,cap_net_admin=ep /usr/local/bin/dumpcap(路径按实际安装位置)。包管理安装的服务器推荐 7.3 的标准方式,避免绕过包的状态管理。
八、无界面服务器的 tshark 验证主线
服务器上没有图形界面,日常抓包走 tshark 主线。三个最常用的健康检查命令:
- 版本与编译信息(确认版本与 libpcap 就绪):
tshark --version | grep -E 'TShark|Running on' | head -3
实测回显(节选):
TShark (Wireshark) 4.2.2 (Git v4.2.2 packaged as 4.2.2-1.1build3).
Running on Linux 7.0.0-38-generic, with Intel(R) Core(TM) i7-14700KF (with
SSE4.2), with 19949 MB of physical memory, with GLib 2.80.0, with libpcap 1.10.4
- 接口枚举(确认能看到网卡):
tshark -D | head -3
实测回显:
1. eth0
2. any
3. lo (Loopback)
- 最小抓包(
-c 5抓满 5 个报文自动退出,作为捕获链路冒烟测试):
timeout 15 tshark -i any -c 5
实测回显(ping 网关期间执行,5 个报文抓满自动退出):
1 0.000000000 10.89.63.2 → 10.89.63.1 ICMP 100 Echo (ping) request id=0x0057, seq=2/512, ttl=64
2 0.000023642 10.89.63.1 → 10.89.63.2 ICMP 100 Echo (ping) reply id=0x0057, seq=2/512, ttl=64 (request in 1)
3 0.303976417 10.89.63.2 → 10.89.63.1 ICMP 100 Echo (ping) request id=0x0057, seq=3/768, ttl=64
4 0.303999677 10.89.63.1 → 10.89.63.2 ICMP 100 Echo (ping) reply id=0x0057, seq=3/768, ttl=64 (request in 3)
5 0.608076230 10.89.63.2 → 10.89.63.1 ICMP 100 Echo (ping) request id=0x0057, seq=4/1024, ttl=64
tshark 的完整参数体系(写入文件、过滤器、字段提取、统计)见本专栏 《Wireshark 常用 CLI 命令详解(tshark 工具链)》 篇;过滤器语法体系见 《Wireshark 配置与过滤器体系详解》 篇。
九、常见问题(FAQ)
Q1:安装完成,tshark -D 正常,但抓包报 "You do not have permission to capture"?
按第七章顺序处理:dpkg-reconfigure wireshark-common 选 Yes(或 debconf 预设)→ usermod -aG wireshark 你的用户名 → 退出重新登录 → getcap /usr/bin/dumpcap 应看到 cap_net_admin,cap_net_raw=eip。三步缺一不可,最常见遗漏是第三步重新登录。
Q2:用 debconf-set-selections 预设时报 "Unknown type true, skipping line"?
预设行格式错误。install-setuid 是 boolean 题型,必须写 wireshark-common wireshark-common/install-setuid boolean true 四段式;写成 select true 会跳过该行(7.3 排障注记实测复现)。
Q3:dpkg -i 离线安装报 "already installed and configured" 错误?
单遍 dpkg 不排依赖顺序,既装系统包与新 deb 交叉时会报此类错。只要目标包(wireshark/tshark)已 Setting up 成功,补一条 apt-get install -f -y 即清账(5.3/5.4 实测)。
Q4:purge 重装后,之前能抓包的用户又不能抓了?
purge 会连同删除 wireshark 组本身(组成员关系随组消失),重装后授权也被重置为默认 false。实测重装后 groups admin 只剩 admin : admin、getcap 无输出、抓包再次报权限错误——必须完整重做 7.3(授权)与 7.4(入组)。这是生产上「重装 Wireshark 后权限失效」的根因。
Q5:交互式安装时错过了那个 Yes/No 对话框,怎么补选?
sudo dpkg-reconfigure wireshark-common 会再次弹出同一对话框(问句 "Should non-superusers be able to capture packets?"),选 Yes 即等效补授权;随后用户入组并重新登录。
十、卸载与清理
完整卸载(含全局配置与不再需要的依赖):
apt-get purge -y 'wireshark*' tshark
实测回显(节选):
Removing wireshark (4.2.2-1.1build3) ...
Removing wireshark-common (4.2.2-1.1build3) ...
Removing tshark (4.2.2-1.1build3) ...
Purging configuration files for wireshark-common (4.2.2-1.1build3) ...
apt-get autoremove -y --purge
实测回显(节选,连带清理 GUI 依赖链):
The following packages will be REMOVED:
adwaita-icon-theme libgtk-3-common libqt6core6t64 qt6-wayland ...
Purging configuration files for libgtk-3-common (3.24.41-4ubuntu1.3) ...
验证卸载干净:
dpkg -l | grep -c wireshark
实测回显:
0
⚠️ 注意两点:其一,purge 会删除
wireshark组,如 FAQ Q4 所述重装后需重做授权;其二,autoremove --purge会连带清理作为依赖被拉入的包,若同一台服务器上还有其他软件依赖这些包(如 GTK 库),autoremove 的清单里确认无误再确认执行。
十一、参考链接
- 官方下载页(版本渠道):https://www.wireshark.org/download.html
- Ubuntu 包索引 wireshark 页:https://packages.ubuntu.com/noble/wireshark
- 官方平台安装文档(Wireshark Wiki - CaptureSetup/CapturePrivileges):https://wiki.wireshark.org/CaptureSetup/CapturePrivileges
- 官方 tshark 手册页:https://www.wireshark.org/docs/man-pages/tshark.html
- 本专栏姊妹篇:《Wireshark 介绍与选型指南》(架构与选型边界)、《Wireshark 配置与过滤器体系详解》(过滤语法与 profile)、《Wireshark 常用 CLI 命令详解(tshark 工具链)》(CLI 全量实战)