这篇文章解释你如何快速学习从命令行使用apt-get和apt-cache安装、移除、升级和搜索软件包。这篇文章提供一些有用命令,它们将帮助你在基于Debian/Ubuntu的系统中处理包管理。
apt-get工具是一个强大并且免费的包管理命令行程序,它用于与Ubuntu的APT库(高级包工具)一起使用来执行新软件包的安装,移除已有软件包,升级已有软件包甚至用于升级整个操作系统。
apt-cache命令行工具用于搜索apt软件包缓存。简单说,这个工具用于搜索软件包,收集包的信息并且也用于搜索什么可用包准备在基于Debian或Ubuntu系统上安装。
要列出所有可用包,输入以下命令。
- root@gyl-huawei:~# apt-cache pkgnames
- libdatrie-doc
- libfstrcmp0-dbg
- librime-data-sampheng
- task-estonian
- python-pyao-dbg
- fonts-georgewilliams
- python3-doc8
- ...
要在安装前查找包名及其描述,使用'search'标记。apt-cache和"search"一起使用将显示一个匹配包和短描述的列表。例如,查找包"vsftpd"的描述,则命令是:
- root@gyl-huawei:~# apt-cache search vsftpd
- ccze - robust, modular log coloriser
- resource-agents - Cluster Resource Agents
- vsftpd - lightweight, efficient FTP server written for security
- vsftpd-dbg - lightweight, efficient FTP server written for security (debug)
- yasat - simple stupid audit tool
要查找和列出所有以'vsftpd'开头的包,你可以使用以下命令。
- root@gyl-huawei:~# apt-cache pkgnames vsftpd
- vsftpd
- vsftpd-dbg
例如,如果你想要检测包信息和其短描述(版本号,校验和,尺寸,安装尺寸,目录等)。按如下使用'show'子命令。
- root@gyl-huawei:~# apt-cache show netcat
- Package: netcat
- Version: 1.10-41.1
- Installed-Size: 16
- Maintainer: Ruben Molina
- Architecture: all
- Depends: netcat-traditional (>= 1.10-39)
- Description-en: TCP/IP swiss army knife -- transitional package
- This is a "dummy" package that depends on lenny's default version of
- netcat, to ease upgrades. It may be safely removed.
- Description-md5: 1353f8c1d079348417c2180319bdde09
- Tag: implemented-in::c, interface::commandline, network::client,
- protocol::ip, protocol::tcp, protocol::udp, role::program,
- scope::utility, use::downloading, use::transmission
- Section: net
- Priority: optional
- Filename: pool/main/n/netcat/netcat_1.10-41.1_all.deb
- Size: 9034
- MD5sum: b2029ae3dbefc4154642127503d2e57f
- SHA256: 26e2a9720cbd234ba3a2f769e59deb506e08356bfb08c71cd32b1f09c1e9cf42
使用'showpkg'子命令来检测特定软件包的依赖关系。那些依赖包是否安装了。例如,与包名一起使用'showpkg'命令。
- root@gyl-huawei:~# apt-cache showpkg vsftpd
- Package: vsftpd
- Versions:
- 3.0.3-12 (/var/lib/apt/lists/repo.huaweicloud.com_debian_dists_buster_main_binary-amd64_Packages)
- Description Language:
- File: /var/lib/apt/lists/repo.huaweicloud.com_debian_dists_buster_main_binary-amd64_Packages
- MD5: 81386f72ac91a5ea48f8db0b023f3f9b
- Description Language: en
- File: /var/lib/apt/lists/repo.huaweicloud.com_debian_dists_buster_main_i18n_Translation-en
- MD5: 81386f72ac91a5ea48f8db0b023f3f9b
-
-
- Reverse Depends:
- vsftpd-dbg,vsftpd 3.0.3-12
- Dependencies:
- 3.0.3-12 - debconf (18 0.5) debconf-2.0 (0 (null)) libc6 (2 2.28) libcap2 (2 1:2.10) libpam0g (2 0.99.7.1) libssl1.1 (2 1.1.0) libwrap0 (2 7.6-4~) adduser (0 (null)) libpam-modules (0 (null)) lsb-base (2 3.0-6) netbase (0 (null)) procps (0 (null)) ftp-server (0 (null)) logrotate (0 (null)) ssl-cert (0 (null)) ftp-server (0 (null))
- Provides:
- 3.0.3-12 - ftp-server (= )
- Reverse Provides:
'stat'子命令将显示有关缓存的整体统计数据。例如,以下命令所有包名是在缓存中找到的包数。
- root@gyl-huawei:~# apt-cache stats
- Total package names: 84159 (1,683 k)
- Total package structures: 84372 (3,712 k)
- Normal packages: 58736
- Pure virtual packages: 923
- Single virtual packages: 14226
- Mixed virtual packages: 623
- Missing: 9864
- Total distinct versions: 63391 (5,071 k)
- Total distinct descriptions: 123109 (2,955 k)
- Total dependencies: 424554/106361 (10.2 M)
- Total ver/file relations: 65154 (1,564 k)
- Total Desc/File relations: 57573 (1,382 k)
- Total Provides mappings: 19451 (467 k)
- Total globbed strings: 182238 (4,074 k)
- Total slack space: 21.9 k
- Total space accounted for: 31.5 M
- Total buckets in PkgHashTable: 50503
- Unused: 9524
- Used: 40979
- Utilization: 81.1417%
- Average entries: 2.05891
- Longest: 59
- Shortest: 1
- Total buckets in GrpHashTable: 50503
- Unused: 9524
- Used: 40979
- Utilization: 81.1417%
- Average entries: 2.05371
- Longest: 9
- Shortest: 1
'update'命令用于与在/etc/apt/sources.list文件中指定的它们的源重新同步包索引文件。update命令从它们的位置获取这些包并且更新包到新的版本。
- root@gyl-huawei:~# apt-get update
- Get:1 http://repo.huaweicloud.com/debian buster InRelease [122 kB]
- Get:2 http://repo.huaweicloud.com/debian buster-updates InRelease [56.6 kB]
- ...
- Get:133 http://repo.huaweicloud.com/debian buster-proposed-updates/main amd64 Packages [99.9 kB]
- Get:134 http://repo.huaweicloud.com/debian buster-proposed-updates/main Translation-en [66.1 kB]
- Fetched 23.8 MB in 5s (4,410 kB/s)
- Reading package lists... Done
- N: Repository 'http://repo.huaweicloud.com/debian buster InRelease' changed its 'Version' value from '10.11' to '10.12'
'upgrade'命令用于升级在系统上所有当前安装的软件包。在任何情况下,不移除当前安装的包或者不获取和安装还未安装的包来满足升级依赖关系。
- root@gyl-huawei:~# apt-get upgrade
- Reading package lists... Done
- Building dependency tree
- Reading state information... Done
- Calculating upgrade... Done
- The following packages have been kept back:
- base-files linux-image-amd64
- The following packages will be upgraded:
- bind9-host chrony dirmngr distro-info-data dpkg dpkg-dev gnupg gnupg-l10n gnupg-utils gpg gpg-agent gpg-wks-client
- gpg-wks-server gpgconf gpgsm gpgv grub-common grub-pc grub-pc-bin grub2-common gzip libbind9-161 libc-bin libc-dev-bin
- libc-l10n libc6 libc6-dev libdns-export1104 libdns1104 libdpkg-perl libexpat1 libexpat1-dev libfreetype6 libgnutls30
- libisc-export1100 libisc1100 libisccc161 libisccfg163 libldap-2.4-2 libldap-common liblwres161 liblzma5 libpcap0.8
- libsasl2-2 libsasl2-modules libsasl2-modules-db libssl1.1 libxml2 linux-libc-dev locales openssl rsyslog tzdata vim
- vim-common vim-runtime vim-tiny xxd xz-utils zlib1g
- 60 upgraded, 0 newly installed, 0 to remove and 2 not upgraded.
- Need to get 50.0 MB of archives.
- After this operation, 1,967 kB of additional disk space will be used.
- Do you want to continue? [Y/n] Y
- ...
如果你想要升级,不关注是否添加或移除软件包来满足依赖关系,使用'dist-upgrade'子命令。
- root@gyl-huawei:~# apt-get dist-upgrade
- Reading package lists... Done
- Building dependency tree
- Reading state information... Done
- Calculating upgrade... Done
- The following NEW packages will be installed:
- linux-image-4.19.0-21-amd64
- The following packages have been kept back:
- base-files
- The following packages will be upgraded:
- linux-image-amd64
- 1 upgraded, 1 newly installed, 0 to remove and 1 not upgraded.
- Need to get 48.5 MB of archives.
- After this operation, 270 MB of additional disk space will be used.
- Do you want to continue? [Y/n]
- ...
'install'子命令用于安装或升级。
- root@gyl-huawei:~# apt-get install netcat
- Reading package lists... Done
- Building dependency tree
- Reading state information... Done
- The following NEW packages will be installed:
- netcat
- 0 upgraded, 1 newly installed, 0 to remove and 2 not upgraded.
- Need to get 9,034 B of archives.
- After this operation, 16.4 kB of additional disk space will be used.
- Get:1 http://repo.huaweicloud.com/debian buster/main amd64 netcat all 1.10-41.1 [9,034 B]
- Fetched 9,034 B in 0s (69.6 kB/s)
- Selecting previously unselected package netcat.
- (Reading database ... 42001 files and directories currently installed.)
- Preparing to unpack .../netcat_1.10-41.1_all.deb ...
- Unpacking netcat (1.10-41.1) ...
- Setting up netcat (1.10-41.1) ...
为了同时安装多个包,你可以在命令中添加多个包名称。例如,以下命令将安装包'nethogs'和'goaccess'。
root@gyl-huawei:~# apt-get install nethogs goaccess
你可以用正则表达式用一个字符串添加若干包。例如,使用*通配符安装包含'*name*'字符串的若干包,名称将是'package-name'。
root@gyl-huawei:~# apt-get install '*name*'
使用子'--no-upgrade'命令将阻止已经安装的包升级。
root@gyl-huawei:~# apt-get install packageName --no-upgrade
'--only-upgrade'命令不安装新包,而它只升级已经安装的包并且禁用包的新安装。
root@gyl-huawei:~# apt-get install packageName --only-upgrade
13、如何安装特定包版本?
只想要安装包的特定版本,只要与包名一起使用'='并且跟上所要版本。
- root@gyl-huawei:~# apt-get install vsftpd=3.0.3-12
- Reading package lists... Done
- Building dependency tree
- Reading state information... Done
- The following NEW packages will be installed:
- vsftpd
- 0 upgraded, 1 newly installed, 0 to remove and 2 not upgraded.
- Need to get 0 B/153 kB of archives.
- After this operation, 357 kB of additional disk space will be used.
- Preconfiguring packages ...
- Selecting previously unselected package vsftpd.
- (Reading database ... 42050 files and directories currently installed.)
- Preparing to unpack .../vsftpd_3.0.3-12_amd64.deb ...
- Unpacking vsftpd (3.0.3-12) ...
- Setting up vsftpd (3.0.3-12) ...
- [vsftpd.conf:1] Line references path below legacy directory /var/run/, updating /var/run/vsftpd/empty → /run/vsftpd/empty; please update the tmpfiles.d/ drop-in file accordingly.
- Processing triggers for man-db (2.8.5-2) ...
- Processing triggers for systemd (241-7~deb10u8) ...
要卸载软件包,而不移除它们的配置文件(为了之后再使用相同配置)。按以下使用'remove'。
- root@gyl-huawei:~# apt-get remove vsftpd
- Reading package lists... Done
- Building dependency tree
- Reading state information... Done
- The following package was automatically installed and is no longer required:
- ssl-cert
- Use 'apt autoremove' to remove it.
- The following packages will be REMOVED:
- vsftpd
- 0 upgraded, 0 newly installed, 1 to remove and 2 not upgraded.
- After this operation, 357 kB disk space will be freed.
- Do you want to continue? [Y/n] Y
- (Reading database ... 42103 files and directories currently installed.)
- Removing vsftpd (3.0.3-12) ...
- Processing triggers for man-db (2.8.5-2) ...
要移除包括其配置文件的软件包,按如下使用'purge'子命令。
root@gyl-huawei:~# apt-get purge vsftpd
另外,你可以按如下组合两个命令一起。
root@gyl-huawei:~# apt-get remove --purge vsftpd
'clean'命令用于通过从本地仓库清理获取的(下载的).deb文件(包)释放磁盘空间。
root@gyl-huawei:~# apt-get clean
只下载特定包的源代码,与包名一起使用选项'--download-only source'。
- root@gyl-huawei:~# apt-get --download-only source vsftpd
- Reading package lists... Done
- Need to get 234 kB of source archives.
- Get:1 http://repo.huaweicloud.com/debian buster/main vsftpd 3.0.3-12 (dsc) [1,856 B]
- Get:2 http://repo.huaweicloud.com/debian buster/main vsftpd 3.0.3-12 (tar) [197 kB]
- Get:3 http://repo.huaweicloud.com/debian buster/main vsftpd 3.0.3-12 (diff) [35.2 kB]
- Fetched 234 kB in 0s (541 kB/s)
- Download complete and in download only mode
要下载和解压一个包的源代码到一个特定目录,输入以下命令。
- root@gyl-huawei:~# apt-get source vsftpd
- Reading package lists... Done
- Skipping already downloaded file 'vsftpd_3.0.3-12.dsc'
- Skipping already downloaded file 'vsftpd_3.0.3.orig.tar.gz'
- Skipping already downloaded file 'vsftpd_3.0.3-12.debian.tar.xz'
- Need to get 0 B of source archives.
- dpkg-source: info: extracting vsftpd in vsftpd-3.0.3
- dpkg-source: info: unpacking vsftpd_3.0.3.orig.tar.gz
- dpkg-source: info: unpacking vsftpd_3.0.3-12.debian.tar.xz
- dpkg-source: info: using patch list from debian/patches/series
- dpkg-source: info: applying 0020-manpage-hyphen.patch
- ...
你也可以同时下载,解包和编译源代码,使用选项'--compile'。
apt-get --compile source goaccess
使用'download'选项,你可以下载任何指定的包,而不安装它。例如,以下命令只下载'nethogs'包到当前工作目录。
- root@gyl-huawei:~# apt-get download nethogs
- Get:1 http://repo.huaweicloud.com/debian buster/main amd64 nethogs amd64 0.8.5-2+b1 [30.9 kB]
- Fetched 30.9 kB in 0s (1,347 kB/s)
- root@gyl-huawei:~# ls
- nethogs_0.8.5-2+b1_amd64.deb
’changelog‘标记下载一个包的change-log并且显示被安装的包版本。
root@gyl-huawei:~# apt-get changelog vsftpd
'check'命令是一个诊断工具。它用于更新包缓存并且检查损坏的依赖关系。
- root@gyl-huawei:~# apt-get check
- Reading package lists... Done
- Building dependency tree
- Reading state information... Done
'build-dep'命令在系统中搜索本地仓库并且为包安装构建依赖关系。如果在本地仓库中不存在包,它将返回一个错误代码。
root@gyl-huawei:~# apt-get build-dep netcat
’autoclean‘命令删除来自/var/cache/apt/archives的所有.deb文件来释放显著的磁盘空间量。
- root@gyl-huawei:~# apt-get autoclean
- Reading package lists... Done
- Building dependency tree
- Reading state information... Done
- root@gyl-huawei:~# apt-get autoclean
- Reading package lists... Done
- Building dependency tree
- Reading state information... Done
'autoremove'子命令用于自动移除被安装来满足其它包依赖关系但现在不再需要的包。例如,以下命令将移除一个安装的包和其依赖关系。
- root@gyl-huawei:~# apt-get autoremove vsftpd
- Reading package lists... Done
- Building dependency tree
- Reading state information... Done
- The following packages will be REMOVED:
- ssl-cert vsftpd
- 0 upgraded, 0 newly installed, 2 to remove and 2 not upgraded.
- After this operation, 422 kB disk space will be freed.
- Do you want to continue? [Y/n] Y
- (Reading database ... 43307 files and directories currently installed.)
- Removing ssl-cert (1.0.39) ...
- Removing vsftpd (3.0.3-12) ...
- Processing triggers for man-db (2.8.5-2) ...
这将帮助你显示有关你指定的一个特定包依赖关系的信息。
- root@gyl-huawei:~# apt depends glances
- glances
- Depends: adduser
- Depends: lsb-base (>= 3.0-6)
- Depends: node-normalize.css
- Depends: python3-pkg-resources
- Depends: python3-psutil (>= 2.0.0)
- Depends: <python3:any>
- python3
- Recommends: hddtemp
- Recommends: lm-sensors
- Recommends: python3-bottle
- Recommends: python3-docker
- Recommends: python3-influxdb
- Recommends: python3-matplotlib
- Recommends: python3-netifaces
- Recommends: python3-pysnmp4
- Recommends: python3-pystache
- Suggests: glances-doc
search选项搜索指定包名并且显示所有匹配的包。
- root@gyl-huawei:~# apt search apache2
- Sorting... Done
- Full Text Search... Done
- apache2/oldstable-proposed-updates 2.4.38-3+deb10u8 amd64
- Apache HTTP Server
- ...
这将帮助你显示有关包的信息,通过指定你想要显示有关信息的所有包运行这个命令。
- root@gyl-huawei:~# apt show nmap
- Package: nmap
- Version: 7.70+dfsg1-6+deb10u2
- Priority: optional
- Section: net
- Maintainer: Debian Security Tools
@tracker.debian.org> - Installed-Size: 4,622 kB
- Depends: nmap-common (= 7.70+dfsg1-6+deb10u2), libc6 (>= 2.15), libgcc1 (>= 1:3.0), liblinear3 (>= 2.01+dfsg), liblua5.3-0, libpcap0.8 (>= 0.9.8), libpcre3, libssh2-1 (>= 1.2.9), libssl1.1 (>= 1.1.0), libstdc++6 (>= 5.2), zlib1g (>= 1:1.1.4)
- Suggests: ncat, ndiff, zenmap
- Homepage: https://nmap.org/
- Tag: admin::monitoring, implemented-in::c, interface::commandline,
- network::scanner, role::program, scope::utility, use::scanning
- Download-Size: 1,894 kB
- APT-Sources: http://repo.huaweicloud.com/debian buster/main amd64 Packages
- Description: The Network Mapper
- Nmap is a utility for network exploration or security auditing. It
- supports ping scanning (determine which hosts are up), many port
- scanning techniques, version detection (determine service protocols
- and application versions listening behind ports), and TCP/IP
- fingerprinting (remote host OS or device identification). Nmap also
- offers flexible target and port specification, decoy/stealth scanning,
- sunRPC scanning, and more. Most Unix and Windows platforms are
- supported in both GUI and commandline modes. Several popular handheld
- devices are also supported, including the Sharp Zaurus and the iPAQ.