Home | 简体中文 | 繁体中文 | 杂文 | Github | 知乎专栏 | 51CTO学院 | CSDN程序员研修院 | OSChina 博客 | 腾讯云社区 | 阿里云栖社区 | Facebook | Linkedin | Youtube | 打赏(Donations) | About
知乎专栏多维度架构

第 5 章 Package Management

目录

5.1. APT 包管理
5.1.1. 搜索软件包
5.1.2. 显示软件包的详细信息
5.1.3. policy
5.1.4. 软件包的依赖关系
5.1.5. 查看所属镜像
5.1.6. Installation
5.1.7. 重新安装
5.1.8. 列出已安装软件包
5.1.9. Update
5.1.10. Remove
5.1.11. purge
5.1.12. aptitude
5.1.13. Automatic Updates
5.1.14. 更换 api 源镜像
5.1.15. dpkg
5.1.16. Upgrading
5.1.17. 制作.deb安装包
5.2. snap - Tool to interact with snaps
5.2.1. 安装 snap
5.2.2. 列出已经安装的snap包
5.2.3. 搜索要安装的snap包
5.2.4. 安装snap包
5.2.5. 更新snap包
5.2.6. 把一个包还原到以前安装的版本
5.2.7. 删除snap包
5.2.8. 查询最近做的操作日志
5.3. DNF 包管理
5.3.1. 安装 epel-release 包
5.3.2. DNF 软件库管理
5.3.3. 显示系统中可用的 DNF 软件库
5.3.4. 列出所有 RPM 包
5.3.5. 搜索软件库中的包
5.3.6. 查看软件包详情
5.3.7. 查找某一文件的提供者
5.3.8. 删除软件包
5.4. yum - Yellowdog Updater Modified 包管理
5.4.1. Yum Resource & Yum Mirror
5.4.2. yum - Yellowdog Updater Modified
5.4.3. rpm - RPM Package Manager
5.4.4. rpmbuild - Build RPM Package(s)
5.5. Homebrew
5.5.1. OpenJDK 8
5.5.2. Maven
5.5.3. 版本切换
5.6. SDKMAN
5.7. 清理安装包

5.1. APT 包管理

包管理工具

apt 命令默认从cdrom安装

注释/etc/apt/sources.list中的deb cdrom项, apt会从互联网上安装

netkiller@Linux-server:~$ sudo vi /etc/apt/sources.list
# deb cdrom:[Ubuntu-Server 6.10 _Edgy Eft_ - Release i386 (20061025.1)]/ edgy main restricted
	
apt-setup

安装是首先会下载包到/var/cache/apt/archives/目录

5.1.1. 搜索软件包

			
# apt search tcpdump
			
		

apt-cache 是早期版本,目前已经废弃

		
$ apt-cache search package
		
		

5.1.2. 显示软件包的详细信息

软件包的详细信息:

		
$ apt show package
		

列出软件包, 以及逆向依赖的软件包的详细版本信息:

$ apt showpkg package
		

5.1.3. policy

$ apt policy tcpdump
tcpdump:
  Installed: 4.0.0-6ubuntu3
  Candidate: 4.0.0-6ubuntu3
  Version table:
 *** 4.0.0-6ubuntu3 0
        500 http://us.archive.ubuntu.com/ubuntu/ lucid/main Packages
        100 /var/lib/dpkg/status

root@homeassistant:~# apt policy tcpdump
tcpdump:
  Installed: (none)
  Candidate: 4.99.0-2+deb11u1
  Version table:
     4.99.3-1~bpo11+1 100
        100 https://mirrors.tuna.tsinghua.edu.cn/debian bullseye-backports/main arm64 Packages
     4.99.0-2+deb11u1 500
        500 https://mirrors.tuna.tsinghua.edu.cn/debian bullseye/main arm64 Packages
			

5.1.4. 软件包的依赖关系

列出软件包的依赖关系:

$ apt-cache depends package
			
		
# apt-cache depends tcpdump
		

5.1.5. 查看所属镜像

			
neo@netkiller:~$ apt madison docker
    docker |      1.5-2 | http://mirrors.ustc.edu.cn/ubuntu cosmic/universe amd64 Packages
    docker |      1.5-2 | http://mirrors.ustc.edu.cn/ubuntu cosmic/universe i386 Packages			
			
		

5.1.6. Installation

$ sudo apt install package
		
		

5.1.6.1. 本地安装

		
wget https://github.com/home-assistant/supervised-installer/releases/latest/download/homeassistant-supervised.deb
apt install ./homeassistant-supervised.deb		
		
			

5.1.6.2. dpkg 安装

*.deb

			
sudo dpkg -i *.deb
			
			

5.1.7. 重新安装

		
sudo apt reinstall package
		
		

5.1.8. 列出已安装软件包

			
root@homeassistant:~# apt list --installed
Listing... Done
adduser/oldstable,oldstable,now 3.118 all [installed]
apparmor/oldstable,now 2.13.6-10 arm64 [installed]
apt-transport-https/oldstable,oldstable,now 2.2.4 all [installed]
apt-utils/oldstable,now 2.2.4 arm64 [installed]
apt/oldstable,now 2.2.4 arm64 [installed]
armbian-bsp-cli-rk3318-box/now 22.05.0-trunk arm64 [installed,upgradable to: 23.02.2]
armbian-config/bullseye,bullseye,bullseye,now 23.02.2 all [installed]
armbian-firmware/now 22.05.0-trunk all [installed,upgradable to: 23.02.2]
base-files/now 11.1+deb11u3 arm64 [installed,upgradable to: 11.1+deb11u7]
base-passwd/oldstable,now 3.5.51 arm64 [installed]			
			
		

5.1.8.1. 列出不能更新的包

			
neo@netkiller ~ % apt list --upgradable
Listing... Done
dh-python/groovy,groovy 4.20200925 all [upgradable from: 4.20191017ubuntu7]
iptables/groovy-updates 1.8.5-3ubuntu2.20.10.1 amd64 [upgradable from: 1.8.5-3ubuntu1]
krb5-locales/groovy-updates,groovy-updates,groovy-security,groovy-security 1.17-10ubuntu0.1 all [upgradable from: 1.17-10]
libgssapi-krb5-2/groovy-updates,groovy-security 1.17-10ubuntu0.1 amd64 [upgradable from: 1.17-10]
libgssapi-krb5-2/groovy-updates,groovy-security 1.17-10ubuntu0.1 i386 [upgradable from: 1.17-10]
libip4tc2/groovy-updates 1.8.5-3ubuntu2.20.10.1 amd64 [upgradable from: 1.8.5-3ubuntu1]
libip6tc2/groovy-updates 1.8.5-3ubuntu2.20.10.1 amd64 [upgradable from: 1.8.5-3ubuntu1]
libk5crypto3/groovy-updates,groovy-security 1.17-10ubuntu0.1 amd64 [upgradable from: 1.17-10]
libk5crypto3/groovy-updates,groovy-security 1.17-10ubuntu0.1 i386 [upgradable from: 1.17-10]
libkrb5-3/groovy-updates,groovy-security 1.17-10ubuntu0.1 amd64 [upgradable from: 1.17-10]
libkrb5-3/groovy-updates,groovy-security 1.17-10ubuntu0.1 i386 [upgradable from: 1.17-10]
libkrb5support0/groovy-updates,groovy-security 1.17-10ubuntu0.1 amd64 [upgradable from: 1.17-10]
libkrb5support0/groovy-updates,groovy-security 1.17-10ubuntu0.1 i386 [upgradable from: 1.17-10]
libldap-2.4-2/groovy-updates,groovy-security 2.4.53+dfsg-1ubuntu1.2 amd64 [upgradable from: 2.4.53+dfsg-1ubuntu1.1]
libldap-common/groovy-updates,groovy-updates,groovy-security,groovy-security 2.4.53+dfsg-1ubuntu1.2 all [upgradable from: 2.4.53+dfsg-1ubuntu1.1]
libxtables12/groovy-updates 1.8.5-3ubuntu2.20.10.1 amd64 [upgradable from: 1.8.5-3ubuntu1]
linux-generic/groovy-updates 5.8.0.29.34 amd64 [upgradable from: 5.8.0.28.33]
linux-headers-generic/groovy-updates 5.8.0.29.34 amd64 [upgradable from: 5.8.0.28.33]
linux-image-generic/groovy-updates 5.8.0.29.34 amd64 [upgradable from: 5.8.0.28.33]
linux-libc-dev/groovy-updates 5.8.0-29.31 amd64 [upgradable from: 5.8.0-28.30]			
			
			

5.1.9. Update

$ apt update
$ apt upgrade
			

Smart software update

$ apt dist-upgrade
			

5.1.10. Remove

删除系统中的foo软件包

$ sudo apt remove foo
			

删除系统中的package软件包及其配置文件

$ sudo apt remove --purge package
			

5.1.11. purge

sudo apt purge package			
		
		

5.1.12. aptitude

管理软件包

neo@kerberos:~$ tasksel
neo@kerberos:~$ aptitude
		

5.1.13. Automatic Updates

sudo apt-get install unattended-upgrades
		

/etc/apt/apt.conf.d/50unattended-upgrades


		

Notifications

sudo apt-get install apticron
		

/etc/apticron/apticron.conf

EMAIL="root@example.com"
		

5.1.13.1. 升级过程中链接中断怎么办?

Ubuntu 16.04 升级到 16.10 过程中SSH中断

我猜测do-release-upgrade 一定会有恢复方案,应该是screen. 经过查看果然是 screen

开始尝试恢复screen 提示

neo@netkiller:~$ screen -ls
No Sockets found in /var/run/screen/S-neo.

后来想到应该是root而不是当前用户,再次查看

neo@netkiller:~$ sudo screen -ls
[sudo] password for neo:
There is a screen on:
 1955.ubuntu-release-upgrade-screen-window (11/25/2016 07:44:50 PM)       (Detached)
1 Socket in /var/run/screen/S-root.

的确如猜测一样,现在回复窗口吧。

neo@netkiller:~$ sudo screen -r 1955

继续			
			

5.1.14. 更换 api 源镜像

备份 /etc/apt/sources.list 文件,然后覆盖即可

		
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic main restricted universe multiverse
deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-updates main restricted universe multiverse
deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-updates main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-backports main restricted universe multiverse
deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-backports main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-security main restricted universe multiverse
deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-security main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-proposed main restricted universe multiverse
deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-proposed main restricted universe multiverse		
		
		

5.1.14.1. Ubuntu 18.04

			
sudo cp /etc/apt/sources.list /etc/apt/sources.list.backup
cat > /etc/apt/sources.list << EOF 
deb http://mirrors.aliyun.com/ubuntu/ xenial main 
deb-src http://mirrors.aliyun.com/ubuntu/ xenial main 
deb http://mirrors.aliyun.com/ubuntu/ xenial-updates main 
deb-src http://mirrors.aliyun.com/ubuntu/ xenial-updates main 
deb http://mirrors.aliyun.com/ubuntu/ xenial universe 
deb-src http://mirrors.aliyun.com/ubuntu/ xenial universe 
deb http://mirrors.aliyun.com/ubuntu/ xenial-updates universe 
deb-src http://mirrors.aliyun.com/ubuntu/ xenial-updates universe 
deb http://mirrors.aliyun.com/ubuntu/ xenial-security main 
deb-src http://mirrors.aliyun.com/ubuntu/ xenial-security main 
deb http://mirrors.aliyun.com/ubuntu/ xenial-security universe 
deb-src http://mirrors.aliyun.com/ubuntu/ xenial-security universe 
EOF			
			
			

5.1.15. dpkg

5.1.15.1. -i|--install 安装.deb包

$ sudo dpkg -i netkiller-1.0.deb
			

5.1.15.2. -r|--remove 卸载.deb包

$ sudo dpkg -r netkiller
			

5.1.15.3. -L|--listfiles <package> ... List files `owned' by package(s). 列出包中的文件

$ dpkg -L netkiller|more
/.
/opt
/opt/neo
/opt/neo/netkiller-1.0
/opt/neo/netkiller-1.0/linux
/opt/neo/netkiller-1.0/linux/docbook.css
/opt/neo/netkiller-1.0/linux/apas03.html
/opt/neo/netkiller-1.0/linux/shell
			

5.1.15.4. -l|--list [<pattern> ...] List packages concisely. 列出.deb包

			
$ sudo dpkg -l netkiller
$ dpkg -l netkiller
Desired=Unknown/Install/Remove/Purge/Hold
| Status=Not/Inst/Conf-files/Unpacked/halF-conf/Half-inst/trig-aWait/Trig-pend
|/ Err?=(none)/Reinst-required (Status,Err: uppercase=bad)
||/ Name           Version        Description
+++-==============-==============-============================================
un  netkiller      <none>         (no description available)
			
			

5.1.15.5. Status

系统上装了哪些软件包

要查看 Debian 系统上安装的所有软件包的状态, 运行

     dpkg --list

输出每个软件包的一行简单介绍, 2字符的状态标志, 包名, 所安装版本, 和简要描述.

查看以 "foo" 开头的软件包的状态, 执行:

     dpkg --list 'foo*'

要得到某个软件包的更详细信息, 执行:

     dpkg --status packagename
			

List of installed software packages

$ dpkg-query -W
			

Description of installed software packages

$ dpkg -l
			

找出一个文件的归属包

dpkg --search cachemgr
squid3-cgi: /usr/lib/cgi-bin/cachemgr3.cgi
squid3-cgi: /usr/share/man/man8/cachemgr3.cgi.8.gz
squid3-cgi: /etc/squid3/cachemgr.conf
			

5.1.15.6. dpkg-deb - Debian package archive (.deb) manipulation tool

-X, --vextract archive directory Extract and display the filenames contained by a package
$ dpkg -X dmd_2.057-0_amd64.deb dmd_2.057-0_amd64
				
-e, --control archive [directory] Extracts the control information files from a package archive into the specified directory.
$ dpkg -e dmd_2.057-0_amd64.deb

$ find DEBIAN/
DEBIAN/
DEBIAN/conffiles
DEBIAN/md5sum
DEBIAN/control
				
-b, --build directory [archive|directory]

在你的目录下创建DEBIAN目录与control文件

				
mkdir DEBIAN/

cat >> DEBIAN/control <<EOF
Package: netkiller
Version: 1.0-0
Architecture: amd64
Maintainer: Neo Chen <netkiller@msn.com>
Installed-Size: 51196
Depends: libc6-dev, gcc, gcc-multilib, libc6 (>= 2.11), libgcc1 (>= 1:4.1.1), libstdc++6 (>= 4.1.1)
Section: devel
Priority: optional
Description: Netkiller ebook
 .
 Main designer: Neo Chen
 .
 Homepage: http://netkiller.github.com/
 .
EOF
				
				
				
$ dpkg -b dlang dlang.deb
dpkg-deb: building package `netkiller' in `dlang.deb'.		

$ dpkg --info dlang.deb
 new debian package, version 2.0.
 size 263266 bytes: control archive= 371 bytes.
     354 bytes,    14 lines      control              
 Package: netkiller
 Version: 1.0-0
 Architecture: amd64
 Maintainer: Neo Chen <netkiller@msn.com>
 Installed-Size: 51196
 Depends: libc6-dev, gcc, gcc-multilib, libc6 (>= 2.11), libgcc1 (>= 1:4.1.1), libstdc++6 (>= 4.1.1)
 Section: devel
 Priority: optional
 Description: Netkiller ebook
  .
  Main designer: Neo Chen
  .
  Homepage: http://netkiller.github.com/
  .

$ dpkg --contents dlang.deb
drwxr-xr-x neo/neo           0 2012-02-06 11:22 ./
-rw-r--r-- neo/neo         144 2012-02-01 16:35 ./hello.lst
-rwxr-xr-x neo/neo         321 2012-01-08 21:25 ./test.d
-rw-r--r-- neo/neo         207 2012-02-01 15:57 ./d4py.d
-rwxr-xr-x neo/neo      919366 2012-02-01 16:28 ./hello
-rw-r--r-- neo/neo        6452 2012-02-01 16:28 ./hello.o
-rwxr--r-- neo/neo          80 2012-01-08 21:28 ./hello.d

				
				

5.1.15.7. dpkg-reconfigure

$ sudo dpkg-reconfigure package
			

所有未完成的配置步骤,主要是你在安装过程中出现中断后,可以使用下面命令补救。

$ sudo dpkg --configure -a			
			

5.1.16. Upgrading

升级到最新开发版

5.1.16.1. GUI

update-manager --devel-release
			

5.1.16.2. CLI

$ sudo do-release-upgrade

$ lsb_release -a
			

升级到最新开发版

vim /etc/update-manager/release-upgrades 文件,把里面的
Prompt=lts
改为
Prompt=normal
			
sudo do-release-upgrade -d
			

5.1.16.3. CDROM

$ sudo mount -t iso9660 -o loop ~/maverick-alternate-i386.iso /cdrom
$ sudo /cdrom/cdromupgrade
			

5.1.17. 制作.deb安装包

5.1.17.1. checkinstall — Track installation of local software, and produce a binary manageable with your package management software.

$ tar xxx.tar.gz
$ cd xxx
$ ./configure
$ make			
			
$ sudo apt-get install checkinstall		
			

5.1.17.2. dh_make - prepare Debian packaging for an original source archive

http://www.debian.org/doc/manuals/maint-guide/index.zh-cn.html

http://www.debian.org/doc/manuals/maint-guide/

5.1.17.3. control

Architecture: any | amd64 | i386 The generated binary package is an architecture dependent one usually in a compiled language.

Architecture: all The generated binary package is an architecture independent one usually consisting of text, images, or scripts in an interpreted language.