APT
TimeAIssr

APT

APT (Advanced Package Tool) 是 Linux 操作系统(特别是 Debian、Ubuntu、Linux Mint 及其衍生版)中功能强大的软件包管理工具。
它极大地简化了在 Linux 系统上安装、更新、配置和删除软件的过程。

关键目录和文件

目录

  • /etc/apt/keyrings
  • /etc/apt/sources.list.d
  • /etc/apt/mirrors

文件

  • /etc/apt/sources.list.d/debian.sources
  • /etc/apt/mirrors/debian.list
  • /etc/apt/mirrors/debian-security.list

源格式

DEB822格式

使用.sources文件(多行键值对格式),而不是旧的单行 .list 格式。这是Debian13的标准格式。

1
2
3
4
5
Types: deb
URIs: https://example.com/packages/ <-- 软件源地址
Suites: stable <-- 版本代号 (如 trixie, stable)
Components: main <-- 组件
Signed-By: /etc/apt/keyrings/example.asc <-- GPG 密钥路径
1
2
3
4
5
6
7
8
9
10
11
12
~> cat /etc/apt/sources.list.d/debian.sources
Types: deb deb-src
URIs: mirror+file:///etc/apt/mirrors/debian.list
Suites: trixie trixie-updates trixie-backports
Components: main
Signed-By: /usr/share/keyrings/debian-archive-keyring.gpg

Types: deb deb-src
URIs: mirror+file:///etc/apt/mirrors/debian-security.list
Suites: trixie-security
Components: main
Signed-By: /usr/share/keyrings/debian-archive-keyring.gpg
1
2
3
4
5
6
7
~> ls -al /etc/apt/sources.list.d/
总计 20
drwxr-xr-x  2 root root 4096  1月31日 18:56 .
drwxr-xr-x 10 root root 4096  1月15日 00:07 ..
-rw-r--r--  1 root root  367  1月15日 00:05 debian.sources
-rw-r--r--  1 root root  132  1月31日 18:56 docker.sources
-rw-r--r--  1 root root  245  1月15日 00:05 google-cloud.list

Mirror隔离配置

你的.sources文件中的URIs键指向了 mirror+file:///etc/apt/mirrors/···.list。这意味着具体的源地址(如清华源、官方源)并没有写在 debian.sources 里,而是被剥离到了 /etc/apt/mirrors/ 目录下的文件中

1
2
3
4
5
6
~> ls -al /etc/apt/mirrors/
总计 16
drwxr-xr-x  2 root root 4096  1月15日 00:05 .
drwxr-xr-x 10 root root 4096  1月15日 00:07 ..
-rw-r--r--  1 root root   30  1月15日 00:05 debian.list
-rw-r--r--  1 root root   39  1月15日 00:05 debian-security.list
1
2
~> cat /etc/apt/mirrors/debian.list
https://deb.debian.org/debian
1
2
~> cat /etc/apt/mirrors/debian-security.list
https://deb.debian.org/debian-security

更换系统源

如何更换 Debian 系统源(例如换成国内源)

由于使用了 mirror+file 机制,不要直接修改 /etc/apt/sources.list.d/debian.sources 文件。你需要修改的是它指向的那个文件/etc/apt/mirrors/debian.list

  1. 查看或编辑镜像列表文件:
1
sudo nano /etc/apt/mirrors/debian.list
  1. 修改 URL:
    在这个文件中,你应该会看到类似 https://deb.debian.org/debian 的地址。将其替换为你想要的镜像源(例如清华源)。

    清华源 Trixie 地址: https://mirrors.tuna.tsinghua.edu.cn/debian/

/etc/apt/mirrors/debian-security.list 重复上述步骤(将其换为 https://mirrors.tuna.tsinghua.edu.cn/debian-security)。*

  1. 生效:
1
sudo apt update

添加新软件源

你目前的 google-cloud.list 还是旧式的单行格式(.list),虽然能用,但为了保持系统整洁和统一,建议后续添加软件(或迁移旧配置)时使用 DEB822 (.sources) 格式。

1
2
3
~> cat /etc/apt/sources.list.d/google-cloud.list
deb [signed-by=/etc/apt/keyrings/google-keyring.gpg] https://packages.cloud.google.com/apt google-compute-engine-trixie-stable main
deb [signed-by=/etc/apt/keyrings/google-keyring.gpg] https://packages.cloud.google.com/apt cloud-sdk-trixie main

DEB822 格式模板:

假设你要添加一个名为 example 的软件源:

  1. 创建文件:
1
sudo nano /etc/apt/sources.list.d/example.sources
  1. 写入内容(标准模板):
1
2
3
4
5
Types: deb
URIs: https://example.com/packages/ <-- 软件源地址
Suites: stable <-- 版本代号 (如 trixie, stable)
Components: main <-- 组件
Signed-By: /etc/apt/keyrings/example.asc <-- GPG 密钥路径

实战案例

将你的 google-cloud.list 转换为 .sources 格式

1
2
3
~> cat /etc/apt/sources.list.d/google-cloud.list
deb [signed-by=/etc/apt/keyrings/google-keyring.gpg] https://packages.cloud.google.com/apt google-compute-engine-trixie-stable main
deb [signed-by=/etc/apt/keyrings/google-keyring.gpg] https://packages.cloud.google.com/apt cloud-sdk-trixie main

你可以删除旧的 google-cloud.list,新建 google-cloud.sources 并写入:

1
2
3
4
5
Types: deb
URIs: https://packages.cloud.google.com/apt
Suites: google-compute-engine-trixie-stable cloud-sdk-trixie
Components: main
Signed-By: /etc/apt/keyrings/google-keyring.gpg

总结

  • 修改主源: 去改 /etc/apt/mirrors/debian.list,别动 /etc/apt/sources.list.d/debian.sources
  • 添加新源: 推荐在 /etc/apt/sources.list.d/ 下新建 xxx.sources 文件,保持队形整齐。

具体示例

1
2
~> cat /etc/apt/sources.list
# See /etc/apt/sources.list.d/debian.sources
1
2
3
4
5
6
~> cat /etc/apt/sources.list.d/docker.sources
Types: deb
URIs: https://download.docker.com/linux/debian
Suites: trixie
Components: stable
Signed-By: /etc/apt/keyrings/docker.asc

目录结构

1
2
3
4
5
6
7
8
9
10
11
12
13
14
~> ls -al /etc/apt/
总计 48
drwxr-xr-x 10 root root 4096 1月15日 00:07 .
drwxr-xr-x 88 root root 4096 2月 8日 21:30 ..
drwxr-xr-x 2 root root 4096 1月31日 17:56 apt.conf.d
drwxr-xr-x 2 root root 4096 2025年 6月25日 auth.conf.d
drwxr-xr-x 2 root root 4096 1月31日 18:55 keyrings
-rw-r--r-- 1 root root 147 1月15日 00:07 listchanges.conf
drwxr-xr-x 2 root root 4096 2024年10月 7日 listchanges.conf.d
drwxr-xr-x 2 root root 4096 1月15日 00:05 mirrors
drwxr-xr-x 2 root root 4096 2025年 6月25日 preferences.d
-rw-r--r-- 1 root root 45 1月15日 00:07 sources.list
drwxr-xr-x 2 root root 4096 1月31日 18:56 sources.list.d
drwxr-xr-x 2 root root 4096 1月15日 00:07 trusted.gpg.d

你的 /etc/apt/ 目录结构非常清晰,进一步证实了这是一个高度现代化且遵循 Debian 最新规范的系统(Trixie)。

根据你提供的列表,这里有几个核心目录需要特别关注,理解它们有助于你正确管理软件源:

1. /etc/apt/mirrors/ (关键目录)

这是你配置中最独特的地方。

  • 作用: 存放纯粹的 URL 地址列表。
  • 原理: 你的 /etc/apt/sources.list.d/debian.sources 文件并不直接包含网址,而是指向这里。
  • 如何操作:
    如果你想把系统源换成清华源中科大源不需要/etc/apt/sources.list.d/debian.sources,而是应该编辑这个目录下的文件/etc/apt/mirrors/debian.list
  • 查看该目录内容:
1
2
3
4
5
6
~> ls -al /etc/apt/mirrors/
总计 16
drwxr-xr-x  2 root root 4096  1月15日 00:05 .
drwxr-xr-x 10 root root 4096  1月15日 00:07 ..
-rw-r--r--  1 root root   30  1月15日 00:05 debian.list
-rw-r--r--  1 root root   39  1月15日 00:05 debian-security.list
  • 通常里面会有 debian.listdebian-security.list。你只需把这两个文件里的 URL 改成国内镜像地址即可。
1
2
~> cat /etc/apt/mirrors/debian.list
https://deb.debian.org/debian
1
2
~> cat /etc/apt/mirrors/debian-security.list
https://deb.debian.org/debian-security

2. /etc/apt/keyrings/ (新标准)

  • 作用: 存放第三方软件的 GPG 签名密钥。
  • 现状: 这是 apt-key 命令被废弃后,Debian 官方推荐的密钥存放位置。
  • 关联: 你之前的 docker.sources 文件中有一行 Signed-By: /etc/apt/keyrings/docker.asc,指的就是这里面的文件。以后添加新软件(如 VS Code, Google Cloud),密钥都应该丢在这里。

3. /etc/apt/sources.list (空壳文件)

  • 大小: 只有 45 字节。
  • 作用: 在你的系统中,它几乎已经被废弃。里面大概率只有一行注释,告诉你去查看 .d 目录。不要在这个文件里写任何配置,保持它现在的样子即可。

4. /etc/apt/auth.conf.d/

  • 作用: 用于存放需要登录验证的软件源的账号密码(例如某些企业内部源)。
  • 注意: 如果你没有使用付费或私有仓库,这个目录通常是空的或只有默认配置,忽略即可。

其它源

Debian 的核心哲学是**“极致的稳定性”**,这就意味着在“Stable(稳定版)”发布后,官方仓库里的软件版本会被冻结,只修复安全漏洞,不再更新功能。

如果你坚持想用 apt 来管理一切,但又需要较新的软件,有以下三种进阶方案,按推荐程度从高到低排列:

1. 启用 Debian Backports (官方后备源)

Backports 仓库里包含了一些从测试版(Testing)重新编译并在稳定版(Stable)环境下运行的较新软件包(例如更新的 Linux 内核、LibreOffice 或其他工具)。

  • 特点: 官方维护,相对安全,不会破坏系统稳定性。
  • 如何设置:
  1. 你需要编辑源列表:
1
sudo nano /etc/apt/sources.list
  1. 在文件末尾添加 Backports 源(假设你当前使用的是 Debian 12 bookworm,如果是其他版本请替换代号):
1
2
deb http://deb.debian.org/debian bookworm-backports main contrib non-free

  1. 更新并安装:
    Backports 的软件默认不会自动覆盖旧版本,你必须手动指定安装:
1
2
3
sudo apt update
sudo apt install -t bookworm-backports 软件名

(例如:想安装更新的内核或 cockpit 等工具时常用此法)

2. 添加第三方官方源 (Vendor Repositories)

很多大型软件开发商(如 Docker, Google, Microsoft, HashiCorp)都知道 Debian 官方库更新慢,所以他们维护自己的 apt 仓库。

这是在 Debian 上安装 Chrome, VS Code, Docker, Spotify 等软件的标准方式。

  • 特点: 依然使用 apt updateapt upgrade 统一管理,版本最新。
  • 如何设置(通用步骤):
  1. 添加 GPG 密钥(为了验证包的安全性)。
  2. 添加源地址/etc/apt/sources.list.d/ 目录下的独立文件中。
  3. 更新并安装

举例(安装 VS Code):

1
2
3
4
5
6
7
8
9
10
11
# 1. 安装必要的工具
sudo apt install wget gpg
# 2. 下载并导入微软的 GPG Key
wget -qO- https://packages.microsoft.com/keys/microsoft.asc | gpg --dearmor > packages.microsoft.gpg
sudo install -D -o root -g root -m 644 packages.microsoft.gpg /etc/apt/keyrings/packages.microsoft.gpg
# 3. 添加源
echo "deb [arch=amd64,arm64,armhf signed-by=/etc/apt/keyrings/packages.microsoft.gpg] https://packages.microsoft.com/repos/code stable main" | sudo tee /etc/apt/sources.list.d/vscode.list > /dev/null
# 4. 安装
sudo apt update
sudo apt install code

3. 切换到 Testing 或 Unstable 分支 (仅限高级用户)

如果你觉得 Stable 的所有软件都太旧,你可以把整个系统升级到 Testing (代号 Trixie)Unstable (代号 Sid) 分支。

  • Testing: 也就是“下一个稳定版”,软件比较新,适合日常桌面使用(类似于 Ubuntu 非 LTS 版的稳定性)。
  • Sid (Unstable): 滚动更新,拥有最新鲜的软件(类似于 Arch Linux),但也更容易遇到 BUG。
  • 风险: 系统可能因为某个更新挂掉,需要你有一定的故障修复能力。
  • 如何做: 修改 /etc/apt/sources.list,把所有的 bookworm 替换成 testingsid,然后运行 sudo apt full-upgrade

4. 添加第三方非官方源 (不推荐)

有些软件没有官方 Debian 仓库,但社区或个人维护了非官方源。这种源的风险较大,可能包含过时的软件,甚至恶意软件。不建议使用,除非你非常信任这个源的维护者,并且知道如何审查和管理这些包。

例如 dariogriffo/debian.griffo.io 。下面是它的 README 内容:

Unoffical Debian packages hosted by me.

我托管的非官方 Debian 软件包。

What is this repo??

这是什么仓库??

This repository contains unofficial Debian packages (.deb) for the most up-to-date versions of popular development tools:

本仓库包含一些流行开发工具的**最新版本**的非官方 Debian 软件包(.deb):

  • Zig - General-purpose programming language and toolchain
  • Zig - 通用编程语言和工具链
  • ZLS - Language Server Protocol for Zig
  • ZLS - Zig 的语言服务器协议
  • Ghostty - Fast, feature-rich terminal emulator
  • Ghostty - 快速、功能丰富的终端模拟器
  • lazydocker - Terminal UI for Docker
  • lazydocker - Docker 的终端 UI
  • lazygit - Terminal UI for Git
  • lazygit - Git 的终端 UI
  • eza - Modern replacement for ‘ls’ command
  • eza - ‘ls’ 命令的现代替代品
  • lowfi - Minimalist lofi music player
  • lowfi - 极简的 lofi 音乐播放器
  • yazi - Terminal file manager written in Rust
  • yazi - 用 Rust 编写的终端文件管理器
  • uv - Extremely fast Python package manager
  • uv - 极快的 Python 包管理器
  • fzf - Command-line fuzzy finder
  • fzf - 命令行模糊查找器
  • zoxide - Smarter cd command
  • zoxide - 更智能的 cd 命令
  • termusic - Terminal music and podcast player
  • termusic - 终端音乐和播客播放器
  • unregistry - Lightweight container registry
  • unregistry - 轻量级容器注册表
  • uncloud - Container orchestration tool
  • uncloud - 容器编排工具
  • docker-pussh - Push Docker images over SSH
  • docker-pussh - 通过 SSH 推送 Docker 镜像
  • Ulauncher - Application launcher for Linux
  • Ulauncher - Linux 应用程序启动器
  • Bun - Fast all-in-one JavaScript runtime
  • Bun - 快速一体化 JavaScript 运行时
  • TigerBeetle - Distributed financial transactions database
  • TigerBeetle - 分布式金融交易数据库

Since Debian has a freeze and slow release policy, this repository provides the latest versions of these tools, updated automatically when new releases are available upstream.

由于 Debian 有冻结和缓慢发布的政策,此代码仓库提供了这些工具的最新版本 ,在源头上提供新版本时会自动更新。

🔧 Build Pipelines 构建流水线

Each package is built through dedicated GitHub repositories with automated CI/CD:

每个包都通过专用的 GitHub 代码仓库进行构建,并实现自动化 CI/CD:

🐧 Supported Debian Distributions 支持的 Debian 发行版
  • Bookworm (Debian 12 - oldstable)
  • Trixie (Debian 13 - stable)
  • Forky (Debian 14 - testing)
  • Sid (unstable)

This is an unofficial community project providing the most up-to-date versions of development tools as properly packaged Debian packages, filling the gap between fast-moving upstream releases and Debian’s stable packaging cycle.

这是一个非官方的社区项目,提供最新版本的开发工具作为打包好的 Debian 软件包,弥补了快速更新的上游版本与 Debian 稳定版打包周期之间的差距。

🚀 Quick Setup 快速设置

Add the repository to your sources.list 将仓库添加到您的 sources.list

1
2
3
4
5
6
7
8
# Add repository GPG key
curl -sS https://debian.griffo.io/EA0F721D231FDD3A0A17B9AC7808B4DD62C41256.asc | sudo gpg --dearmor --yes -o /etc/apt/trusted.gpg.d/debian.griffo.io.gpg

# Add repository to sources
echo "deb https://debian.griffo.io/apt $(lsb_release -sc 2>/dev/null) main" | sudo tee /etc/apt/sources.list.d/debian.griffo.io.list

# Update package list
sudo apt update

Install packages 安装软件包

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# Programming Languages & Tools
sudo apt install -y zig # Latest stable Zig
sudo apt install -y zig-master # Nightly Zig builds
sudo apt install -y zls # Zig Language Server (stable)
sudo apt install -y zls-master # Zig Language Server (nightly)

# Terminal & File Management
sudo apt install -y ghostty # Modern terminal emulator
sudo apt install -y eza # Modern ls replacement
sudo apt install -y yazi # Terminal file manager
sudo apt install -y fzf # Fuzzy finder
sudo apt install -y zoxide # Smart cd command

# Development Tools
sudo apt install -y lazygit # Git terminal UI
sudo apt install -y lazydocker # Docker terminal UI
sudo apt install -y uv # Fast Python package manager

# Media & Entertainment
sudo apt install -y lowfi # Lofi music player
sudo apt install -y termusic # Terminal music player

# Container & Cloud Tools
sudo apt install -y unregistry # Container registry
sudo apt install -y uncloud # Container orchestration
sudo apt install -y docker-pussh # Docker over SSH

# Application Launchers & Runtimes
sudo apt install -y ulauncher # Application launcher
sudo apt install -y bun # JavaScript runtime
sudo apt install -y bun-one # Bun with single binary
sudo apt install -y bun-profile # Bun with profile support

# Databases
sudo apt install -y tigerbeetle # Financial transactions database

# Install everything at once
sudo apt install -y zig ghostty lazygit lazydocker eza yazi uv fzf zoxide lowfi termusic ulauncher bun tigerbeetle
⚠️ Important Information 重要信息

Disclaimer 免责声明

This repository focuses exclusively on unofficial Debian packaging. For issues with the tools themselves, please contact the respective upstream projects. We only handle packaging-related concerns.

本仓库仅专注于非官方 Debian 打包 。有关工具本身的问题,请联系各自的上游项目。我们仅处理与打包相关的事务。

Key Features 主要特点

  • Always up-to-date - Packages updated automatically when new versions are released
  • 始终最新 - 新版本发布时自动更新软件包
  • Multiple distributions - Support for Bookworm, Trixie, and Sid
  • 支持多种发行版 - 支持 Bookworm、Trixie 和 Sid
  • Proper packaging - Follows Debian packaging standards and dependencies
  • 规范打包 - 遵循 Debian 打包标准和依赖关系
  • Nightly builds - Available for Zig and ZLS for cutting-edge features
  • 每日构建版 - Zig 和 ZLS 可用,以获取最新功能

Important Notice 重要通知

📅 March 4th, 2025 - Public key was updated. Please run the setup commands above to update your system with the new GPG key.

📅 2025 年 3 月 4 日 - 公钥已更新。请运行上述设置命令,使用新的 GPG 密钥更新您的系统。


Visit debian.griffo.io for more information and the latest updates.

访问 debian.griffo.io 获取更多信息和最新更新。


安装docker源

这段脚本是用于在 Debian 系统上设置 Docker 官方软件源的标准流程。它主要分为两个部分:添加 Docker 的 GPG 安全密钥,以及配置软件源列表。

以下是每一条命令的详细解释:

第一部分:准备工作与添加 GPG 密钥

1. 更新软件包索引

1
2
sudo apt update

  • 作用: 更新本地的软件包列表。这是为了确保接下来的安装命令能够获取到最新版本的依赖包。

2. 安装必要的依赖工具

1
2
sudo apt install ca-certificates curl

  • 作用: 安装两个关键工具:
  • ca-certificates:让系统信任各种 SSL/TLS 证书(通过 HTTPS 下载时需要)。
  • curl:用于从互联网下载文件的命令行工具。

3. 创建密钥存储目录

1
2
sudo install -m 0755 -d /etc/apt/keyrings

  • 作用: 创建用于存放第三方 GPG 密钥的目录 /etc/apt/keyrings
  • 细节:
  • -d:表示创建目录(directory)。
  • -m 0755:设置权限模式。755 意味着所有者(root)有读/写/执行权限,而其他人只有读/执行权限。这是一个安全的默认设置。
  • 注:使用 install 命令比 mkdir 更方便,因为它可以同时设定权限。

4. 下载 Docker 的 GPG 官方密钥

1
2
sudo curl -fsSL https://download.docker.com/linux/debian/gpg -o /etc/apt/keyrings/docker.asc

  • 作用: 从 Docker 官网下载 GPG 公钥并保存到刚才创建的目录中。APT 包管理器将使用此密钥来验证下载的 Docker 软件包是否被篡改。
  • 参数详解:
  • -f (fail):连接失败时不显示 HTTP 错误码,直接静默失败。
  • -s (silent):静默模式,不显示进度条。
  • -S (show-error):如果失败,显示错误信息(配合 -s 使用)。
  • -L (location):如果服务器返回重定向,自动跟随。
  • -o ...:将下载的内容输出保存为指定文件。

5. 设置密钥文件权限

1
2
sudo chmod a+r /etc/apt/keyrings/docker.asc

  • 作用: 更改密钥文件的权限,确保所有用户(a+r 即 all + read)都可以读取这个文件。如果 APT 无法读取此文件,后续更新源时会报错。

第二部分:添加软件源并更新

6. 将 Docker 仓库配置写入源列表

1
2
3
4
5
6
7
8
sudo tee /etc/apt/sources.list.d/docker.sources <<EOF
Types: deb
URIs: https://download.docker.com/linux/debian
Suites: $(. /etc/os-release && echo "$VERSION_CODENAME")
Components: stable
Signed-By: /etc/apt/keyrings/docker.asc
EOF

  • 作用: 创建一个新的源配置文件 /etc/apt/sources.list.d/docker.sources,并写入 Docker 的仓库信息。
  • 关键点详解:
  • sudo tee ...:将标准输入的内容写入文件,tee 配合 sudo 使用是为了解决重定向无法获取 root 权限的问题。
  • <<EOF ... EOF:这是一个 “Here-Document”,用于将多行文本传递给命令。
  • Suites: $(...):这是最巧妙的部分。它执行了一个子命令:
  • . /etc/os-release:读取当前系统的发行版信息文件。
  • echo "$VERSION_CODENAME":自动输出当前 Debian 版本的代号(例如 bookwormbullseye)。这样可以确保你下载的是匹配你当前系统版本的 Docker。
  • Signed-By:指明使用之前下载的那个 GPG 密钥来验证该仓库的包。

7. 再次更新软件包索引

1
sudo apt update
  • 作用: 再次刷新本地软件包缓存。
  • 目的: 这次更新会读取刚才新加的 docker.sources 文件,从 Docker 官方服务器获取最新的 Docker 软件包列表。执行完这一步后,你就可以通过 sudo apt install docker-ce 来安装 Docker 了。

安装微软源

这是在 Linux 系统(通常是 Ubuntu/Debian)上配置微软官方软件源(Repository)密钥的常用命令组合。它通常用于安装 VS Code、Edge 浏览器或 .NET Core 等软件。

这些命令通过 && 符号连接,意味着只有前一条命令成功执行,才会执行下一条

以下是详细解释:

1. sudo apt-get install wget gpg

  • 作用:安装后续步骤需要的两个基础工具。

  • wget:用于从网络下载文件的工具。

  • gpg:用于处理加密和签名的工具(GNU Privacy Guard)。

  • 注意:如果不加 -y 参数,系统可能会暂停并询问你是否确认安装,需要手动输入 y 继续。

2. wget -qO- https://packages.microsoft.com/keys/microsoft.asc | gpg --dearmor > microsoft.gpg

这一行使用了管道操作|),将两个命令串联起来:

  • wget -qO- https://.../microsoft.asc

  • -q (quiet):静默模式,不输出下载进度。

  • -O- (Output to stdout):将下载的内容直接输出到“标准输出”(屏幕/管道),而不是保存为文件。

  • 结果:它下载了微软的 ASCII 格式(文本格式)公钥,并将其“流”向下一个命令。

  • | gpg --dearmor > microsoft.gpg

  • |:管道符,接收上一个命令的输出作为输入。

  • gpg --dearmor:将输入的 ASCII 格式密钥转换为二进制格式(.gpg),这是 APT 包管理器更喜欢的格式。

  • > microsoft.gpg:将处理好的二进制数据写入当前目录下的名为 microsoft.gpg 的临时文件中。

3. sudo install -D -o root -g root -m 644 microsoft.gpg /usr/share/keyrings/microsoft.gpg

  • 作用:将刚才生成的临时密钥文件安全地安装到系统目录中。
  • 参数详解
  • install:类似于 cp (复制) 命令,但能同时设置文件属性。
  • -D:如果目标目录不存在,自动创建它(虽然 /usr/share/keyrings 通常已存在)。
  • -o root -g root:设置文件的所有者(Owner)和所属组(Group)均为 root,确保安全性。
  • -m 644:设置文件权限。644 表示所有者可读写,其他人只读。
  • 路径:将文件移动到 /usr/share/keyrings/。这是现代 Linux 发行版推荐存放第三方密钥的地方(比旧的 /etc/apt/trusted.gpg 更安全)。

4. rm -f microsoft.gpg

  • 作用:清理现场。
  • 详解
  • 删除第 2 步在当前目录下生成的那个临时文件 microsoft.gpg,因为我们已经把它安装到系统目录里了,不再需要这个副本。
  • -f (force):强制删除,如果文件不存在也不会报错。

总结

这套组合拳的逻辑是:“下载工具 -> 下载并转换密钥 -> 安全地安装到系统目录 -> 删除临时垃圾”

执行完这一步后,你通常还需要创建对应的源文件(例如 /etc/apt/sources.list.d/vscode.list),引用这个密钥,然后运行 sudo apt update 才能安装软件。


密钥

这两种安装方式虽然最终目的相同(都是为了让 apt 信任第三方软件源),但在处理密钥的格式存放路径上采用了不同的流派。

这种差异主要源于 Linux 规范的演变 以及 不同厂商的历史习惯

下面我为你详细拆解这两者的核心区别:

1. 密钥格式的区别:文本 (ASCII) vs 二进制 (Binary)

这是最明显的区别。

  • Docker 的做法(现代流派):直接使用 .asc

  • curl ... -o docker.asc

  • Docker 下载的是 ASCII Armored 格式的密钥(类似于文本文件,用记事本打开能看到 -----BEGIN PGP PUBLIC KEY BLOCK-----)。

  • 为什么: 较新版本的 apt(Debian 11+ / Ubuntu 20.04+)已经可以直接识别 ASCII 格式的密钥,不需要转换。这种方式更简单直观。

  • 微软 的做法(传统流派):转换为 .gpg

  • gpg --dearmor

  • 微软先下载 ASCII 格式,然后通过管道传给 gpg --dearmor 命令。这个命令的作用是**“去甲”**(De-armor),即把文本格式转换成机器更容易读取的 二进制格式

  • 为什么: 以前旧版本的 apt 只认二进制格式。微软为了保证兼容性(兼容旧系统),习惯沿用这种写法。

2. 存放目录的区别:/etc vs /usr/share

  • Docker:放在 /etc/apt/keyrings

  • /etc 目录通常用于存放系统管理员(也就是你) 手动配置的系统文件。

  • 这是目前 Debian/Docker 官方推荐的做法,语义上更准确:表示这是“管理员添加的第三方密钥”。

  • 微软:放在 /usr/share/keyrings

  • /usr/share 通常用于存放系统自带软件安装包自带的只读数据。

  • 虽然放在这里也能用,但在 Linux 的目录规范(FHS)中,管理员手动修改 /usr 下的文件是不太规范的(尽管在添加密钥这个场景下被广泛接受)。

3. 安全逻辑的共同点(关键!)

虽然过程不同,但请注意,这两个脚本都抛弃了过时的 apt-key add 方式

  • 旧方式(不安全): 以前大家直接运行 sudo apt-key add key.gpg。这会把密钥放入系统的“主钥匙串”,导致这个厂商的密钥可以签名系统里的任何软件,存在安全隐患。
  • 这两个脚本的新方式(安全):
  • 它们都把密钥单独存放在一个隔离的文件夹里(keyrings)。
  • 后续在配置 sources.list 时,都会加上 Signed-By 字段指向这个特定的密钥文件。
  • 结果: 微软的密钥只能验证微软的软件,Docker 的密钥只能验证 Docker 的软件,互不干涉。

总结:哪种更好?

特性 Docker 方式 (直接存 .asc) 微软方式 (dearmor 转 .gpg)
可读性 高 (文件可直接查看) 低 (二进制乱码)
兼容性 现代系统 (Ubuntu 20.04+) 极高 (包含古老的系统)
简洁性 简单,命令少 繁琐,需要管道转换
推荐度 ⭐⭐⭐⭐ (未来趋势) ⭐⭐⭐ (保守稳健)

我的建议:
如果你是在维护现代服务器(Debian 11/12, Ubuntu 20.04/22.04/24.04),Docker 的写法更加现代和优雅。不需要调用 gpg 命令进行转换,减少了依赖,脚本也更易读。

你完全可以用 Docker 的那种写法去安装微软的源(只要把 URL 换成微软的,文件名改一下即可),效果是一模一样的。

转换

这是一个非常好的想法!使用 Docker 的那种“现代化写法”(即 直接使用 ASCII 密钥 + Deb822 格式源文件)来配置微软源,不仅更清晰,而且维护起来也更规范。

这里以安装 VS Code 为例(这是最常见的微软 Linux 软件),完全套用 Docker 的逻辑重写一遍。

现代化写法(Docker 风格)

我们将原本复杂的管道转换命令 (gpg --dearmor) 丢掉,直接下载 .asc 文件,并使用更易读的 .sources 格式。

1. 准备工作:安装依赖并创建目录

和 Docker 脚本的第一步完全一样。

1
2
3
sudo apt update
sudo apt install ca-certificates curl
sudo install -m 0755 -d /etc/apt/keyrings
2. 下载微软 GPG 密钥(直接保存为 .asc)

核心变化: 不需要 gpg --dearmor,也不用管道。直接把微软的 .asc 文本密钥下载到 keyrings 目录。

1
2
sudo curl -fsSL https://packages.microsoft.com/keys/microsoft.asc -o /etc/apt/keyrings/microsoft.asc
sudo chmod a+r /etc/apt/keyrings/microsoft.asc
3. 写入软件源配置(使用 .sources 格式)

核心变化: 我们不再创建 .list 文件(单行格式),而是创建 .sources 文件(多行键值对格式),这与 Docker 的脚本保持一致。

1
2
3
4
5
6
7
sudo tee /etc/apt/sources.list.d/vscode.sources <<EOF
Types: deb
URIs: https://packages.microsoft.com/repos/code
Suites: stable
Components: main
Signed-By: /etc/apt/keyrings/microsoft.asc
EOF

注意:这里的 Suites 填的是 stable,因为 VS Code 的仓库结构比较简单,统一使用 stable 分支,不像 Docker 那样需要根据系统代号(如 bookworm)来区分。

4. 更新并安装

现在 apt 就能识别刚才的配置了。

1
2
sudo apt update
sudo apt install code

为什么这样写更好?
  1. 去除了 gpg 依赖
    原来的写法必须安装 gpg 工具才能进行格式转换。现在的写法只需要 curl,依赖更少,出错概率更低。
  2. 格式统一
    你现在查看 /etc/apt/sources.list.d/ 目录时,docker.sourcesvscode.sources 的格式是一模一样的。强迫症表示非常舒适。
  3. 权限分离
    Signed-By: /etc/apt/keyrings/microsoft.asc 明确指定了只有这个密钥能为 VS Code 签名,它无法影响你系统里的其他软件(如 Docker 或系统内核),安全性满分。
适用范围提示

这种写法适用于 Debian 11+Ubuntu 20.04+ 等较新的系统。如果你的系统非常古老(比如 Ubuntu 16.04),apt 可能还不支持 .asc 后缀的密钥,那时候才需要退回到微软官方那种“转二进制”的老写法。

由 Hexo 驱动 & 主题 Keep
本站由 提供部署服务
访问量