一个更友好的 ss / netstat 替代品,适合人类使用。通过简洁的 TUI 或格式化表格检查网络连接。

brew install snitch
感谢 @bevanjkay 将 snitch 添加到 homebrew-core
go install github.com/karol-broda/snitch@latest
nix-env -iA nixpkgs.snitch
感谢 @DieracDelta 将 snitch 添加到 nixpkgs
# try it
nix run github:karol-broda/snitch
# install to profile
nix profile install github:karol-broda/snitch
# or add to flake inputs
{
inputs.snitch.url = "github:karol-broda/snitch";
}
# then use: inputs.snitch.packages.${system}.default
将 snitch 添加到你的 flake inputs 并导入 home-manager 模块:
{
inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
home-manager.url = "github:nix-community/home-manager";
snitch.url = "github:karol-broda/snitch";
};
outputs = { nixpkgs, home-manager, snitch, ... }: {
homeConfigurations."user" = home-manager.lib.homeManagerConfiguration {
pkgs = nixpkgs.legacyPackages.x86_64-linux;
modules = [
snitch.homeManagerModules.default
{
programs.snitch = {
enable = true;
# optional: use the flake's package instead of nixpkgs
# package = snitch.packages.x86_64-linux.default;
settings = {
defaults = {
theme = "catppuccin-mocha";
interval = "2s";
resolve = true;
};
};
};
}
];
};
};
}
可用主题:ansi, catppuccin-mocha, catppuccin-macchiato, catppuccin-frappe, catppuccin-latte, gruvbox-dark, gruvbox-light, dracula, nord, tokyo-night, tokyo-night-storm, tokyo-night-light, solarized-dark, solarized-light, one-dark,
# with yay
yay -S snitch-bin
# with paru
paru -S snitch-bin
curl -sSL https://raw.githubusercontent.com/karol-broda/snitch/master/install.sh | sh
默认安装到 ~/.local/bin(如果可用),否则安装到 /usr/local/bin。可通过以下方式覆盖:
curl -sSL https://raw.githubusercontent.com/karol-broda/snitch/master/install.sh | INSTALL_DIR=~/bin sh
macos: 安装脚本会自动移除二进制文件上的隔离属性(
com.apple.quarantine),使其无需 gatekeeper 警告即可运行。若要禁用此行为,请设置KEEP_QUARANTINE=1。
预构建的 OCI 镜像可从 GitHub 容器镜像仓库获取:
# pull from ghcr.io
docker pull ghcr.io/karol-broda/snitch:latest # alpine (default)
docker pull ghcr.io/karol-broda/snitch:latest-alpine # alpine (~17MB)
docker pull ghcr.io/karol-broda/snitch:latest-scratch # minimal, binary only (~9MB)
docker pull ghcr.io/karol-broda/snitch:latest-debian # debian trixie
docker pull ghcr.io/karol-broda/snitch:latest-ubuntu # ubuntu 24.04
# or use a specific version
docker pull ghcr.io/karol-broda/snitch:0.2.0-alpine
或者,通过 nix flake 在本地构建:
nix build github:karol-broda/snitch#snitch-alpine
docker load < result
运行容器:
# basic usage - sees host sockets but not process names
docker run --rm --net=host snitch:latest ls
# full info - includes PID, process name, user
docker run --rm --net=host --pid=host --cap-add=SYS_PTRACE snitch:latest ls
| flag | 用途 |
|---|---|
--net=host | 共享宿主机的网络命名空间(查看宿主机连接所必需) |
--pid=host | 共享宿主机的 PID 命名空间(获取进程信息所需) |
--cap-add=SYS_PTRACE | 从 /proc/<pid> 读取进程详细信息 |
注意: 不需要
CAP_NET_ADMIN和CAP_NET_RAW。snitch 从/proc/net/*读取数据,这不需要特殊的网络能力。
从 releases 下载:
snitch_<version>_linux_<arch>.tar.gz 或 .deb/.rpm/.apksnitch_<version>_darwin_<arch>.tar.gztar xzf snitch_*.tar.gz
sudo mv snitch /usr/local/bin/
macos: 如果被提示“无法打开,因为无法验证开发者”,请运行:
xattr -d com.apple.quarantine /usr/local/bin/snitch
snitch # launch interactive tui
snitch -l # tui showing only listening sockets
snitch ls # print styled table and exit
snitch ls -l # listening sockets only
snitch ls -t -e # tcp established connections
snitch ls -p # plain output (parsable)
snitch / snitch top带有实时更新连接列表的交互式 TUI。
snitch # all connections
snitch -l # listening only
snitch -t # tcp only
snitch -e # established only
snitch -i 2s # 2 second refresh interval
快捷键:
j/k, ↑/↓ navigate
g/G top/bottom
t/u toggle tcp/udp
l/e/o toggle listen/established/other
s/S cycle sort / reverse
w watch/monitor process (highlight)
W clear all watched
K kill process (with confirmation)
/ search
enter connection details
? help
q quit
snitch ls一次性表格输出。如果输出超过终端高度,会自动使用分页器。
snitch ls # styled table (default)
snitch ls -l # listening only
snitch ls -t -l # tcp listeners
snitch ls -e # established only
snitch ls -p # plain/parsable output
snitch ls -o json # json output
snitch ls -o csv # csv output
snitch ls -n # numeric (no dns resolution)
snitch ls --no-headers # omit headers
snitch json用于脚本编写的 JSON 输出。
snitch json
snitch json -l
snitch watch按固定间隔输出 JSON 数据帧。
snitch watch -i 1s | jq '.count'
snitch watch -l -i 500ms
snitch upgrade检查更新并就地升级。
snitch upgrade # check for updates
snitch upgrade --yes # upgrade automatically
snitch upgrade -v 0.1.7 # install specific version
以下快捷标志适用于所有命令:
-t, --tcp tcp only
-u, --udp udp only
-l, --listen listening sockets
-e, --established established connections
-4, --ipv4 ipv4 only
-6, --ipv6 ipv6 only
DNS 与服务名称解析选项:
--resolve-addrs resolve ip addresses to hostnames (default: true)
--resolve-ports resolve port numbers to service names
--no-cache disable dns caching (force fresh lookups)
DNS 查询会并行执行并缓存以提高性能。在调试或地址频繁变化时,可使用 --no-cache 绕过缓存。
如需更精确的过滤,可结合 ls 使用 key=value 语法:
snitch ls proto=tcp state=listen
snitch ls pid=1234
snitch ls proc=nginx
snitch ls lport=443
snitch ls contains=google
带样式的表格(默认):
╭─────────────────┬───────┬───────┬─────────────┬─────────────────┬────────╮
│ PROCESS │ PID │ PROTO │ STATE │ LADDR │ LPORT │
├─────────────────┼───────┼───────┼─────────────┼─────────────────┼────────┤
│ nginx │ 1234 │ tcp │ LISTEN │ * │ 80 │
│ postgres │ 5678 │ tcp │ LISTEN │ 127.0.0.1 │ 5432 │
╰─────────────────┴───────┴───────┴─────────────┴─────────────────┴────────╯
2 connections
纯文本输出(-p):
PROCESS PID PROTO STATE LADDR LPORT
nginx 1234 tcp LISTEN * 80
postgres 5678 tcp LISTEN 127.0.0.1 5432
可选配置文件位于 ~/.config/snitch/snitch.toml:
[defaults]
numeric = false # disable name resolution
dns_cache = true # cache dns lookups (set to false to disable)
theme = "auto" # color theme: auto, dark, light, mono
[tui]
remember_state = false # remember view options between sessions
当 remember_state = true 时,TUI 将保存并恢复:
状态保存到 $XDG_STATE_HOME/snitch/tui.json(默认为 ~/.local/state/snitch/tui.json)。
CLI 标志始终优先于已保存的状态。
SNITCH_THEME=dark # set default theme
SNITCH_RESOLVE=0 # disable dns resolution
SNITCH_DNS_CACHE=0 # disable dns caching
SNITCH_NO_COLOR=1 # disable color output
SNITCH_CONFIG=/path/to # custom config file path
/proc/net/* 读取数据,需要 root 或 CAP_NET_ADMIN 才能获取完整进程信息mono