apt-key
用于管理Debian Linux系统中的软件包密钥。每个发布的deb包,都是通过密钥认证的,apt-key用来管理密钥。
-
apt-key list
列出已保存在系统中key。包括
/etc/apt/trusted.gpg
和/etc/apt/trusted.gpg.d/
目录下的密钥 -
apt-key add <keyname >
把下载的key添加到本地trusted数据库中,使用描述性名称,以gpg或asc作为文件扩展名
-
apt-key del <keyname>
从本地trusted数据库删除key。
-
apt-key update (弃用,直接删除和添加)
更新本地trusted数据库,删除过期没用的key。
-
apt-key adv --recv-key
下载并添加到受信任密钥环中(不做任何检查,有风险)
http://manpages.ubuntu.com/manpages/bionic/en/man8/apt-key.8.html
目录、文件
/var/cache/apt/archives
已经下载到的.deb
软件包都放在这里(用 apt-get install 安装软件时,软件包的临时存放路径)
/var/lib/apt/lists
存放已安装和未安装的软件列表
使用apt-get update命令会从/etc/apt/sources.list
指定的源更新软件列表,并保存到该目录
/etc/apt
sources.list 官方软件源地址(配置为阿里源)
souces.list.d 目录下是第三方软件源地址,里面的文件必须以.list
结尾
/etc/apt
trusted.gpg: local trusted keys, new keys will be added here
trusted.gpg.d:additional keyrings can be stored here (by other packages or the administrator)
/usr/bin/
通过 apt 安装的软件,命令存放在 /usr/bin/ 目录下
apt-secure
参考
http://manpages.ubuntu.com/manpages/bionic/en/man8/apt-secure.8.html
基础元素
Release 文件
Release文件包含分发元数据和索引文件的校验值
apt 要求随 Release 文件一起发布一个 Relesase.gpg 的签名文件,用来验证安装包提供者的信息
InRelease 文件
InRelease文件内联gpg签名(数据和签名在一个 InRelease 文件中)
lfp@legion:/var/lib/apt/lists$ ls
...
# 两个文件
deb.nodesource.com_node%5f12.x_dists_bionic_InRelease
deb.nodesource.com_node%5f12.x_dists_bionic_main_binary-amd64_Packages
...
# 三个文件
dl.google.com_linux_chrome_deb_dists_stable_main_binary-amd64_Packages
dl.google.com_linux_chrome_deb_dists_stable_Release
dl.google.com_linux_chrome_deb_dists_stable_Release.gpg
...
区别:在下载时避免竞争情况
The only difference to Release is that the signature is not detached, but within the file. This is a first step towards getting rid of race conditions when updating Packages/Sources files and mirror updates
runninghttps://lists.debian.org/debian-devel-announce/2009/11/msg00001.html
校验值MD5
保护apt安全的基础
debian archive 包含一个Release文件,随安装包一起更新,里面包含了分发元数据和Package的MD5
Package文件里包含安装信息以及安装文件的MD5
验证链
1)验证签名文件
签名文件用来保证Package文件的正确性
-
如果无法下载 Release 文件或 Release.gpg 签名无效,则报错
W: GPG 错误......下列签名无效 EXPKEYSIG......
E: 仓库......没有数字签名
N: 无法安全地用该源进行更新,所以默认禁用该源
缺少公钥
-
apt 使用 gpg 来验证签名文件
-
获取密钥
默认情况下,Debian 系统会预先安装一些 Debian Archieve 的公钥,保存在
/etc/apt/trusted.gpg
文件中,第三方软件密钥需要通过apt-key add [.gpg]
安装到/etc/apt/sources.list.d/
目录下一旦将密钥添加到apt的密钥环中,就相当于告诉apt信任该密钥签名的一切东西
如果公钥丢失,可以通过下面的命令到公钥服务器上寻找
apt-key adv --keyserver <server_url> --recv-key <keyId>
- 如果更新失败可能是防火墙端口问题,尝试指定常规HTTP端口80
-
如果连接的是公司的代理服务器,可以尝试下面的方法
--keyserver-options http-proxy=<myProxy> --keyserver keyserver.ubuntu.com
-
通过
gpgv
签名验证工具来验证签名的有效性详细介绍参见博文GPG配置、命令、实例与apt-key密钥测试
gpgv 认为apt密钥环中的密钥都是可信的,不会检查其是否过期或被吊销
-
通过
--keyring [.gpg file]
指定密钥环,-v
可以显示更多信息apt 密钥环保存在 /etc/apt/trusted.gpg 或 /etc/apt/trusted.gpg.d/xxx.gpg 中
-
单独签名验证
gpgv --keyring /etc/apt/trusted.gpg [Release.gpg file] [Release file]
-
内联签名验证
gpgv --keyring /etc/apt/trusted.gpg [InRelease file]
-
2)验证Package文件
Package 文件中包含软件不同版本的信息,用来保证deb文件的正确性
-
从Release文件或InRelease文件中提取Package文件的MD5
sed -n "s,main/binary-amd64/Packages$,,p" [Release / InRelease file]
-
计算Package文件的MD5
md5sum [Packages file]
3)验证安装包
-
从Package文件中提取deb文件的MD5
sed -n "s/MD5sum: //p" [Packages file]
-
从apt缓存中提取软件的MD5
apt-cache show [package_name] | sed -n "s/MD5sum: //p"
-
计算本地已下载deb文件的MD5
md5sum [.deb file]
验证测试
chrome 验证
特点
- chrome 是Release文件和签名文件[.gpg]分离的
- 使用本地密钥环验证(保存在 /etc/apt/trusted.gpg)
官方声明
https://www.google.com/linuxrepositories/
Release.gpg文件
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1
iQIcBAABCAAGBQJeqbVjAAoJEHi9ZUc8s70TgUIP/RzWWeDxvtGrmLoWt0csiD+O
wrAr86yDSSzFasjKPcS+SQzs5FnCamFdTT7KD2C6thwRgCLR3oumHMuKC5hnb9/4
GP7qMCDYQEMR2IQcWfKPoT2fAX1eKDKJtv5qsAEdSb3uIW27zkdvUA4j4N6w4toA
RA24VV/VSK1p3T4j3HQzN6fOta0wA3onN9bPrcXZAig7Tm78SKbjYEzd1jxIeQQE
aTKP6AfHPnn8UFNkVyifigsd1Usaex3BJumzHq+jLhTtJDcLjqQNQdcKs48xY0Ek
lZJHY1w/p8e06Y16fXxO/Mh6+Kmu+ZBOKEo3VjshBOISASkMXG/JPEjWadP62A8S
lprRALXaWLcF5P5RYjdqhatCxH37SQr3iqqQmdC/PSCDq/Z5cYiVIElyUeHnMZ6i
X6wYvOd1n9p64VgUAINpbY0NeWZc0Kj1pMXaL+bohUnH8YWDfIhFdQDdQbd0DxBY
xgSTAuUn4DkMKZvtqVEsAIZk5VrYjWykdvdaZad8DdAhVxuHzl1xVEXRDyDhxvUN
IE2oOMv1N5MrXKHtGJLITlv0SAtbZRSaez91dudr9eoln8bZ+oFI9VrHO0xKO2/W
/VRMExkQC51OHCEtZKfsqqSAEG0sctvagq5MElCElZkmD/P72MuznRBgjbfeKs/B
JMSaAmp1mus5Mo7BZND6
=/bSj
-----END PGP SIGNATURE-----
验证签名
lfp@legion:/var/lib/apt/lists$ gpgv --keyring /etc/apt/trusted.gpg dl.google.com_linux_chrome_deb_dists_stable_Release.gpg dl.google.com_linux_chrome_deb_dists_stable_Release
gpgv: 签名建立于 2020年04月30日 星期四 01时12分03秒 CST
gpgv: 使用 RSA 密钥 78BD65473CB3BD13
gpgv: 完好的签名,来自于“Google Inc. (Linux Packages Signing Authority) <linux-packages-keymaster@google.com>”
Release文件
包含Package文件的MD5
Origin: Google LLC
Label: Google
Suite: stable
Codename: stable
Version: 1.0
Date: Wed, 29 Apr 2020 17:11:57 UTC
Architectures: amd64
Components: main
Description: Google chrome-linux software repository
MD5Sum:
2e55673e5a00d8837090d0922e198520 4599 main/binary-amd64/Packages
eafbe9cc415e53d2280c86a0d64be27d 1133 main/binary-amd64/Packages.gz
156e5ea7a0c6bed5973a68a45e546dc9 151 main/binary-amd64/Release
SHA1:
9525687fab2b772c511c9e9ae5c7c7b6d8b92e2a 4599 main/binary-amd64/Packages
c364469ff8578e7c7323b030ad3e459b9192a4ea 1133 main/binary-amd64/Packages.gz
0f4348c2d4d7cc1f8e59b5934d87f1ca872f6e34 151 main/binary-amd64/Release
SHA256:
667d27f55652d51c57c0eaab074dd2d365e373ebd5b6e1277b18606cc5177c1b 4599 main/binary-amd64/Packages
7dc589a54517f36e7786b101555e9f1d2c6e2058b1b3743c575eb8c165094620 1133 main/binary-amd64/Packages.gz
c1e3c9318381862306adcdc4fd4fe2d85be8aa4c4f3dcbb40fce80413f588286 151 main/binary-amd64/Release
提取Package文件的MD5
lfp@legion:/var/lib/apt/lists$ sed -n "s,main/binary-amd64/Packages$,,p" dl.google.com_linux_chrome_deb_dists_stable_Release
2e55673e5a00d8837090d0922e198520 4599
9525687fab2b772c511c9e9ae5c7c7b6d8b92e2a 4599
667d27f55652d51c57c0eaab074dd2d365e373ebd5b6e1277b18606cc5177c1b 4599
计算Package文件的MD5
lfp@legion:/var/lib/apt/lists$ md5sum dl.google.com_linux_chrome_deb_dists_stable_main_binary-amd64_Packages
2e55673e5a00d8837090d0922e198520 dl.google.com_linux_chrome_deb_dists_stable_main_binary-amd64_Packages
Package文件可信
2e55673e5a00d8837090d0922e198520 一致
Package文件
包含deb文件的MD5
# 包含各种版本的chrome
Package: google-chrome-beta
...
Package: google-chrome-stable
Version: 81.0.4044.129-1
Architecture: amd64
Maintainer: Chrome Linux Team <chromium-dev@chromium.org>
Installed-Size: 229948
Pre-Depends: dpkg (>= 1.14.0)
Depends: ca-certificates, fonts-liberation, libappindicator3-1, libasound2 (>= 1.0.16), libatk-bridge2.0-0 (>= 2.5.3), libatk1.0-0 (>= 2.2.0), libatspi2.0-0 (>= 2.9.90), libc6 (>= 2.16), libcairo2 (>= 1.6.0), libcups2 (>= 1.4.0), libdbus-1-3 (>= 1.5.12), libdrm2 (>= 2.4.38), libexpat1 (>= 2.0.1), libgbm1 (>= 8.1~0), libgcc1 (>= 1:3.0), libgdk-pixbuf2.0-0 (>= 2.22.0), libglib2.0-0 (>= 2.39.4), libgtk-3-0 (>= 3.9.10), libnspr4 (>= 2:4.9-2~), libnss3 (>= 2:3.22), libpango-1.0-0 (>= 1.14.0), libpangocairo-1.0-0 (>= 1.14.0), libx11-6 (>= 2:1.4.99.1), libx11-xcb1, libxcb-dri3-0, libxcb1 (>= 1.6), libxcomposite1 (>= 1:0.3-1), libxcursor1 (>> 1.1.2), libxdamage1 (>= 1:1.1), libxext6, libxfixes3 (>= 1:5.0), libxi6 (>= 2:1.2.99.4), libxrandr2 (>= 2:1.2.99.3), libxrender1, libxss1, libxtst6, wget, xdg-utils (>= 1.0.2)
Recommends: libu2f-udev, libvulkan1
Provides: www-browser
Priority: optional
Section: web
Filename: pool/main/g/google-chrome-stable/google-chrome-stable_81.0.4044.129-1_amd64.deb
Size: 67137920
SHA256: fe140112304b243240a5f6b287105fd5b7d6e48c6ff682194a62c8d08fd0ed5b
SHA1: f5f984d1a1419b803a7a26dbda1d04fb8313c4b3
# md5
MD5sum: 3705bb8b32a9b4cfcc4440c14966acbc
Description: The web browser from Google
Google Chrome is a browser that combines a minimal design with sophisticated technology to make the web faster, safer, and easier.
Package: google-chrome-unstable
...
提取deb文件的MD5
lfp@legion:/var/lib/apt/lists$ sed -n "s/MD5sum: //p" dl.google.com_linux_chrome_deb_dists_stable_main_binary-amd64_Packages
9c6634a7bbda0cedb2d218410c0a06c2
3705bb8b32a9b4cfcc4440c14966acbc
fe9bc72b7cb12549a69187c0e393f930
从apt缓存中提取chrome信息
lfp@legion:/var/lib/apt/lists$ apt-cache show chromium-browser | sed -n "s/MD5sum: //p"
# 没有匹配的md5,打开浏览器,查看chrome的版本信息是:版本 81.0.4044.129(正式版本) (64 位)
# apt-cache show chromium-browser 显示信息如下,没有找到同一个版本,于是从Google下载了一个最新的安装包
# Package: chromium-browser
# Filename: pool/universe/c/chromium-browser/chromium-browser_80.0.3987.163-0ubuntu0.18.04.1_amd64.deb
6dcd58431410a691c847a709765f7248
dfd394ff98654f1e0a97d204f7343ab1
计算deb文件的MD5
从Google那里下载了一个deb安装包
lfp@legion:~/Downloads$ md5sum google-chrome-stable_current_amd64.deb
3705bb8b32a9b4cfcc4440c14966acbc google-chrome-stable_current_amd64.deb
deb文件可信
3705bb8b32a9b4cfcc4440c14966acbc 一致
nodejs验证
特点
- nodejs 是InRelease文件,内联签名
- 使用本地密钥环验证(保存在 /etc/apt/trusted.gpg)
InRelease文件
包含Package文件的MD5
验证签名
lfp@legion:/var/lib/apt/lists$ gpgv --keyring /etc/apt/trusted.gpg deb.nodesource.com_node%5f12.x_dists_bionic_InRelease
gpgv: 签名建立于 2020年04月30日 星期四 00时53分13秒 CST
gpgv: 使用 RSA 密钥 9FD3B784BC1C6FC31A8A0A1C1655A0AB68576280
gpgv: 完好的签名,来自于“NodeSource <gpg@nodesource.com>”
lfp@legion:/var/lib/apt/lists$ vim deb.nodesource.com_node%5f12.x_dists_bionic_InRelease
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA512
Origin: Node Source
Label: Node Source
Codename: bionic
Date: Wed, 29 Apr 2020 16:53:13 UTC
Architectures: i386 amd64 armhf arm64
Components: main
Description: Apt Repository for the Node.JS 12.x Branch
MD5Sum:
d41d8cd98f00b204e9800998ecf8427e 0 main/binary-i386/Packages
7029066c27ac6f5ef18d660d5741979a 20 main/binary-i386/Packages.gz
cf52b42ebdc37bfabc86a5db93fcbdbc 130 main/binary-i386/Release
# amd64
6d2cd675d3c647d51a8ee0349754a976 1195 main/binary-amd64/Packages
608cc59026b960ec64b97bcbeaa68003 765 main/binary-amd64/Packages.gz
049fa528953b36ae91d8fe360618d46f 131 main/binary-amd64/Release
14ce3c619a83d518ee3e433dedbdf26a 1216 main/binary-armhf/Packages
219c6a1d7d300d409d4bb8249911d58f 775 main/binary-armhf/Packages.gz
8064ccb91382a3c1cbade0c462ee18b3 131 main/binary-armhf/Release
45ad97bad6053d65a462c352219fa962 1195 main/binary-arm64/Packages
8afb33e583bf54aabaeb9b3378c3ca26 766 main/binary-arm64/Packages.gz
670d019ad65bf455298c252afc334bff 131 main/binary-arm64/Release
d41d8cd98f00b204e9800998ecf8427e 0 main/source/Sources
7029066c27ac6f5ef18d660d5741979a 20 main/source/Sources.gz
e4627d3fe224f8b3c07d9a69c88bedd2 132 main/source/Release
提取Package文件的MD5
sed -n "s,main/binary-amd64/Packages$,,p" deb.nodesource.com_node%5f12.x_dists_bionic_InRelease
6d2cd675d3c647d51a8ee0349754a976 1195
4615cf89691b8c95c052a84b09a1d24079268403 1195
8ec2d3674dc82a29ca759a2cf59cfe67a2b6c3a42106c523b11f93791a1e538e 1195
计算Package文件的MD5
lfp@legion:/var/lib/apt/lists$ md5sum deb.nodesource.com_node%5f12.x_dists_bionic_main_binary-amd64_Packages
6d2cd675d3c647d51a8ee0349754a976 deb.nodesource.com_node%5f12.x_dists_bionic_main_binary-amd64_Packages
Package文件可信
6d2cd675d3c647d51a8ee0349754a976 一致
Package文件
包含deb文件的MD5
Package: nodejs
Version: 12.16.3-1nodesource1
Architecture: amd64
Maintainer: Chris Lea <chl@nodesource.com>
Installed-Size: 87857
Depends: libc6 (>= 2.17), libgcc1 (>= 1:3.4), libstdc++6 (>= 4.8), python-minimal, ca-certificates
Conflicts: nodejs-dev, nodejs-legacy, npm
Replaces: nodejs-dev (<= 0.8.22), nodejs-legacy, npm (<= 1.2.14)
Provides: nodejs-dev, nodejs-legacy, npm
Homepage: https://nodejs.org
Priority: optional
Section: web
Filename: pool/main/n/nodejs/nodejs_12.16.3-1nodesource1_amd64.deb
Size: 17989662
SHA256: b2d1a6327f5a34c097d7fb5eeed8357d9758c09b30e356f45dfa01cc24103108
SHA1: de90a1776ee9995b3121ab68f49fef3cb110ce65
MD5sum: 9f87646d2782a572da1f965cf96f974f
Description: Node.js event-based server-side javascript engine
Node.js is similar in design to and influenced by systems like
Ruby's Event Machine or Python's Twisted.
.
It takes the event model a bit further - it presents the event
loop as a language construct instead of as a library.
.
Node.js is bundled with several useful libraries to handle server tasks :
System, Events, Standard I/O, Modules, Timers, Child Processes, POSIX,
HTTP, Multipart Parsing, TCP, DNS, Assert, Path, URL, Query Strings.
提取deb文件的MD5
lfp@legion:/var/lib/apt/lists$ sed -n "s/MD5sum: //p" deb.nodesource.com_node%5f12.x_dists_bionic_main_binary-amd64_Packages
9f87646d2782a572da1f965cf96f974f
从apt缓存中提取nodejs信息
lfp@legion:/var/lib/apt/lists$ apt-cache show nodejs | sed -n "s/MD5sum: //p"
# 包含不同版本的信息
9f87646d2782a572da1f965cf96f974f
0e6643fbe872255dbfaebd5449813d8f
02d7a42a30a7d72b78d9bc4a7ceb5a5a
3930b41c309e69cc0bd3737cfc1e7d31
计算deb文件的md5
lfp@legion:/var/lib/apt/lists$ md5sum /var/cache/apt/archives/nodejs_12.16.3-1nodesource1_amd64.deb
9f87646d2782a572da1f965cf96f974f /var/cache/apt/archives/nodejs_12.16.3-1nodesource1_amd64.deb
deb文件可信
9f87646d2782a572da1f965cf96f974f 一致
smplayer 验证
特点
- smplayer 是InRelease文件,内联签名
- 使用第三方密钥环去验证(保存在/etc/apt/trusted.gpg.d目录中)
验证流程
-
签名
lfp@legion:/var/lib/apt/lists$ gpgv --keyring /etc/apt/trusted.gpg.d/rvm_ubuntu_smplayer.gpg ppa.launchpad.net_rvm_smplayer_ubuntu_dists_bionic_InRelease
gpgv: 签名建立于 2020年04月13日 星期一 23时45分47秒 CST
gpgv: 使用 RSA 密钥 A7E13D78E4A4F4F4
gpgv: 完好的签名,来自于“Launchpad PPA named smplayer for rvm” -
Package
MD5 7aa109a3525c661e783e9b943e4b46fa
lfp@legion:/var/lib/apt/lists$ sed -n "s,main/binary-amd64/Packages$,,p" ppa.launchpad.net_rvm_smplayer_ubuntu_dists_bionic_InRelease
7aa109a3525c661e783e9b943e4b46fa 2909
29ca94a4f3a57c328b31789bce66cd6bbaa819e2 2909
6586e6ef8389cddb47ae0f7f7761ddbfedab35ed3ffbb3b10b4a1f91264577ae 2909lfp@legion:/var/lib/apt/lists$ md5sum ppa.launchpad.net_rvm_smplayer_ubuntu_dists_bionic_main_binary-amd64_Packages
7aa109a3525c661e783e9b943e4b46fa ppa.launchpad.net_rvm_smplayer_ubuntu_dists_bionic_main_binary-amd64_Packages -
deb
MD5 601afc2fe220b608acb1e5b920afca96
lfp@legion:/var/lib/apt/lists$ sed -n "s/MD5sum: //p" ppa.launchpad.net_rvm_smplayer_ubuntu_dists_bionic_main_binary-amd64_Packages
601afc2fe220b608acb1e5b920afca96
b569cc540016f0b04fae5dd15a1434eb
4eb1111c66b5087e7489cf7526321a9e
45a466ca713b566f920d9e6414212552lfp@legion:/etc/apt/trusted.gpg.d$ apt-cache show smplayer | grep -E 'MD5|Filename'
Filename: pool/main/s/smplayer/smplayer_20.4.2-1~bionic1_amd64.deb
MD5sum: 601afc2fe220b608acb1e5b920afca96
Filename: pool/universe/s/smplayer/smplayer_18.2.2~ds0-1_amd64.deb
MD5sum: 7fdfc2f64d835cf5f7a38035523379a2lfp@legion:/var/cache/apt/archives$ md5sum smplayer_20.4.2-1~bionic1_amd64.deb
601afc2fe220b608acb1e5b920afca96 smplayer_20.4.2-1~bionic1_amd64.deb
没有公钥或签名无效测试
本地没有该公钥
-
本地公钥过期
猜测:此时软件发布者应该会创建一个新的子密钥来签名,而本地公钥是过期的,情况类似于用一个错误的密钥验证签名文件
# 使用错误的密钥去验证签名文件
lfp@legion:~$ gpgv --keyring /etc/apt/trusted.gpg.d/sogou-archive-keyring.gpg /var/lib/apt/lists/typora.io_linux_._InRelease
gpgv: 签名建立于 2020年03月04日 星期三 00时11分02秒 CST
gpgv: 使用 RSA 密钥 4AC441BE68B4ADAB7439FBF9BA300B7755AFCFAE
gpgv: issuer "abner@typora.io"
gpgv: 无法检查签名:没有公钥
相关问题
EXPKEYSIG 没有数字签名
问题:执行apt update
出现如下错误
W: GPG 错误:https://dl.yarnpkg.com/debian stable InRelease: 下列签名无效: EXPKEYSIG 23E7166788B63E1E Yarn Packaging yarn@dan.cx
E: 仓库 “https://dl.yarnpkg.com/debian stable InRelease” 没有数字签名。
N: 无法安全地用该源进行更新,所以默认禁用该源
原因:
安装第三方软件的时候会同时安装软件仓库地址以及密钥,上述问题是因为本地的密钥过期了,需要更新
办法:更新密钥
找到该软件安装方法中添加密钥的方式(如yarn的安装步骤一),再次执行即可
-
直接搜索密钥添加到密钥列表
apt-key adv --keyserver <server_url> --recv-key <keyId>
-
到服务器找密钥手动安装
-
获取 pub_key的ID
23E7166788B63E1E
-
到密钥服务器上以十六进制形式搜索
0x23E7166788B63E1E
-
单击 pub链接,复制密钥内容并保存到本地,以txt格式
key.txt
密钥内容
-
终端添加密钥
sudo apt-key add key.txt
ok
-
更新
sudo apt update
-