• 用于包管理的基本命令APT-GET和APT-CACHE


    这篇文章解释你如何快速学习从命令行使用apt-get和apt-cache安装、移除、升级和搜索软件包。这篇文章提供一些有用命令,它们将帮助你在基于Debian/Ubuntu的系统中处理包管理。

    apt-get是什么?

    apt-get工具是一个强大并且免费的包管理命令行程序,它用于与Ubuntu的APT库(高级包工具)一起使用来执行新软件包的安装,移除已有软件包,升级已有软件包甚至用于升级整个操作系统。

    apt-cache是什么?

    apt-cache命令行工具用于搜索apt软件包缓存。简单说,这个工具用于搜索软件包,收集包的信息并且也用于搜索什么可用包准备在基于Debian或Ubuntu系统上安装。

    1、如何列出所有可用包?

    要列出所有可用包,输入以下命令。

    1. root@gyl-huawei:~# apt-cache pkgnames
    2. libdatrie-doc
    3. libfstrcmp0-dbg
    4. librime-data-sampheng
    5. task-estonian
    6. python-pyao-dbg
    7. fonts-georgewilliams
    8. python3-doc8
    9. ...

    2、如何查找包名和软件描述?

    要在安装前查找包名及其描述,使用'search'标记。apt-cache和"search"一起使用将显示一个匹配包和短描述的列表。例如,查找包"vsftpd"的描述,则命令是:

    1. root@gyl-huawei:~# apt-cache search vsftpd
    2. ccze - robust, modular log coloriser
    3. resource-agents - Cluster Resource Agents
    4. vsftpd - lightweight, efficient FTP server written for security
    5. vsftpd-dbg - lightweight, efficient FTP server written for security (debug)
    6. yasat - simple stupid audit tool

    要查找和列出所有以'vsftpd'开头的包,你可以使用以下命令。

    1. root@gyl-huawei:~# apt-cache pkgnames vsftpd
    2. vsftpd
    3. vsftpd-dbg

    3、如何检测包信息

    例如,如果你想要检测包信息和其短描述(版本号,校验和,尺寸,安装尺寸,目录等)。按如下使用'show'子命令。

    1. root@gyl-huawei:~# apt-cache show netcat
    2. Package: netcat
    3. Version: 1.10-41.1
    4. Installed-Size: 16
    5. Maintainer: Ruben Molina
    6. Architecture: all
    7. Depends: netcat-traditional (>= 1.10-39)
    8. Description-en: TCP/IP swiss army knife -- transitional package
    9. This is a "dummy" package that depends on lenny's default version of
    10. netcat, to ease upgrades. It may be safely removed.
    11. Description-md5: 1353f8c1d079348417c2180319bdde09
    12. Tag: implemented-in::c, interface::commandline, network::client,
    13. protocol::ip, protocol::tcp, protocol::udp, role::program,
    14. scope::utility, use::downloading, use::transmission
    15. Section: net
    16. Priority: optional
    17. Filename: pool/main/n/netcat/netcat_1.10-41.1_all.deb
    18. Size: 9034
    19. MD5sum: b2029ae3dbefc4154642127503d2e57f
    20. SHA256: 26e2a9720cbd234ba3a2f769e59deb506e08356bfb08c71cd32b1f09c1e9cf42

    4、如何检测特定包的依赖关系

    使用'showpkg'子命令来检测特定软件包的依赖关系。那些依赖包是否安装了。例如,与包名一起使用'showpkg'命令。

    1. root@gyl-huawei:~# apt-cache showpkg vsftpd
    2. Package: vsftpd
    3. Versions:
    4. 3.0.3-12 (/var/lib/apt/lists/repo.huaweicloud.com_debian_dists_buster_main_binary-amd64_Packages)
    5. Description Language:
    6. File: /var/lib/apt/lists/repo.huaweicloud.com_debian_dists_buster_main_binary-amd64_Packages
    7. MD5: 81386f72ac91a5ea48f8db0b023f3f9b
    8. Description Language: en
    9. File: /var/lib/apt/lists/repo.huaweicloud.com_debian_dists_buster_main_i18n_Translation-en
    10. MD5: 81386f72ac91a5ea48f8db0b023f3f9b
    11. Reverse Depends:
    12. vsftpd-dbg,vsftpd 3.0.3-12
    13. Dependencies:
    14. 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))
    15. Provides:
    16. 3.0.3-12 - ftp-server (= )
    17. Reverse Provides:

    5、如何检测缓存的统计数据

    'stat'子命令将显示有关缓存的整体统计数据。例如,以下命令所有包名是在缓存中找到的包数。

    1. root@gyl-huawei:~# apt-cache stats
    2. Total package names: 84159 (1,683 k)
    3. Total package structures: 84372 (3,712 k)
    4. Normal packages: 58736
    5. Pure virtual packages: 923
    6. Single virtual packages: 14226
    7. Mixed virtual packages: 623
    8. Missing: 9864
    9. Total distinct versions: 63391 (5,071 k)
    10. Total distinct descriptions: 123109 (2,955 k)
    11. Total dependencies: 424554/106361 (10.2 M)
    12. Total ver/file relations: 65154 (1,564 k)
    13. Total Desc/File relations: 57573 (1,382 k)
    14. Total Provides mappings: 19451 (467 k)
    15. Total globbed strings: 182238 (4,074 k)
    16. Total slack space: 21.9 k
    17. Total space accounted for: 31.5 M
    18. Total buckets in PkgHashTable: 50503
    19. Unused: 9524
    20. Used: 40979
    21. Utilization: 81.1417%
    22. Average entries: 2.05891
    23. Longest: 59
    24. Shortest: 1
    25. Total buckets in GrpHashTable: 50503
    26. Unused: 9524
    27. Used: 40979
    28. Utilization: 81.1417%
    29. Average entries: 2.05371
    30. Longest: 9
    31. Shortest: 1

    6、如何升级系统包

    'update'命令用于与在/etc/apt/sources.list文件中指定的它们的源重新同步包索引文件。update命令从它们的位置获取这些包并且更新包到新的版本。

    1. root@gyl-huawei:~# apt-get update
    2. Get:1 http://repo.huaweicloud.com/debian buster InRelease [122 kB]
    3. Get:2 http://repo.huaweicloud.com/debian buster-updates InRelease [56.6 kB]
    4. ...
    5. Get:133 http://repo.huaweicloud.com/debian buster-proposed-updates/main amd64 Packages [99.9 kB]
    6. Get:134 http://repo.huaweicloud.com/debian buster-proposed-updates/main Translation-en [66.1 kB]
    7. Fetched 23.8 MB in 5s (4,410 kB/s)
    8. Reading package lists... Done
    9. N: Repository 'http://repo.huaweicloud.com/debian buster InRelease' changed its 'Version' value from '10.11' to '10.12'

    7、如何升级软件包

    'upgrade'命令用于升级在系统上所有当前安装的软件包。在任何情况下,不移除当前安装的包或者不获取和安装还未安装的包来满足升级依赖关系。

    1. root@gyl-huawei:~# apt-get upgrade
    2. Reading package lists... Done
    3. Building dependency tree
    4. Reading state information... Done
    5. Calculating upgrade... Done
    6. The following packages have been kept back:
    7. base-files linux-image-amd64
    8. The following packages will be upgraded:
    9. bind9-host chrony dirmngr distro-info-data dpkg dpkg-dev gnupg gnupg-l10n gnupg-utils gpg gpg-agent gpg-wks-client
    10. gpg-wks-server gpgconf gpgsm gpgv grub-common grub-pc grub-pc-bin grub2-common gzip libbind9-161 libc-bin libc-dev-bin
    11. libc-l10n libc6 libc6-dev libdns-export1104 libdns1104 libdpkg-perl libexpat1 libexpat1-dev libfreetype6 libgnutls30
    12. libisc-export1100 libisc1100 libisccc161 libisccfg163 libldap-2.4-2 libldap-common liblwres161 liblzma5 libpcap0.8
    13. libsasl2-2 libsasl2-modules libsasl2-modules-db libssl1.1 libxml2 linux-libc-dev locales openssl rsyslog tzdata vim
    14. vim-common vim-runtime vim-tiny xxd xz-utils zlib1g
    15. 60 upgraded, 0 newly installed, 0 to remove and 2 not upgraded.
    16. Need to get 50.0 MB of archives.
    17. After this operation, 1,967 kB of additional disk space will be used.
    18. Do you want to continue? [Y/n] Y
    19. ...

    如果你想要升级,不关注是否添加或移除软件包来满足依赖关系,使用'dist-upgrade'子命令。

    1. root@gyl-huawei:~# apt-get dist-upgrade
    2. Reading package lists... Done
    3. Building dependency tree
    4. Reading state information... Done
    5. Calculating upgrade... Done
    6. The following NEW packages will be installed:
    7. linux-image-4.19.0-21-amd64
    8. The following packages have been kept back:
    9. base-files
    10. The following packages will be upgraded:
    11. linux-image-amd64
    12. 1 upgraded, 1 newly installed, 0 to remove and 1 not upgraded.
    13. Need to get 48.5 MB of archives.
    14. After this operation, 270 MB of additional disk space will be used.
    15. Do you want to continue? [Y/n]
    16. ...

    8、如何安装或升级特定软件包

    'install'子命令用于安装或升级。

    1. root@gyl-huawei:~# apt-get install netcat
    2. Reading package lists... Done
    3. Building dependency tree
    4. Reading state information... Done
    5. The following NEW packages will be installed:
    6. netcat
    7. 0 upgraded, 1 newly installed, 0 to remove and 2 not upgraded.
    8. Need to get 9,034 B of archives.
    9. After this operation, 16.4 kB of additional disk space will be used.
    10. Get:1 http://repo.huaweicloud.com/debian buster/main amd64 netcat all 1.10-41.1 [9,034 B]
    11. Fetched 9,034 B in 0s (69.6 kB/s)
    12. Selecting previously unselected package netcat.
    13. (Reading database ... 42001 files and directories currently installed.)
    14. Preparing to unpack .../netcat_1.10-41.1_all.deb ...
    15. Unpacking netcat (1.10-41.1) ...
    16. Setting up netcat (1.10-41.1) ...

    9、如何安装多个包

    为了同时安装多个包,你可以在命令中添加多个包名称。例如,以下命令将安装包'nethogs'和'goaccess'。

    root@gyl-huawei:~# apt-get install nethogs goaccess

    10、如何使用通配符安装若干包

    你可以用正则表达式用一个字符串添加若干包。例如,使用*通配符安装包含'*name*'字符串的若干包,名称将是'package-name'。

    root@gyl-huawei:~# apt-get install '*name*'

    11、如何安装没有升级的包

    使用子'--no-upgrade'命令将阻止已经安装的包升级。

    root@gyl-huawei:~# apt-get install packageName --no-upgrade

    12、如何只升级特定包

    '--only-upgrade'命令不安装新包,而它只升级已经安装的包并且禁用包的新安装。

    root@gyl-huawei:~# apt-get install packageName --only-upgrade

    13、如何安装特定包版本?

    只想要安装包的特定版本,只要与包名一起使用'='并且跟上所要版本。

    1. root@gyl-huawei:~# apt-get install vsftpd=3.0.3-12
    2. Reading package lists... Done
    3. Building dependency tree
    4. Reading state information... Done
    5. The following NEW packages will be installed:
    6. vsftpd
    7. 0 upgraded, 1 newly installed, 0 to remove and 2 not upgraded.
    8. Need to get 0 B/153 kB of archives.
    9. After this operation, 357 kB of additional disk space will be used.
    10. Preconfiguring packages ...
    11. Selecting previously unselected package vsftpd.
    12. (Reading database ... 42050 files and directories currently installed.)
    13. Preparing to unpack .../vsftpd_3.0.3-12_amd64.deb ...
    14. Unpacking vsftpd (3.0.3-12) ...
    15. Setting up vsftpd (3.0.3-12) ...
    16. [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.
    17. Processing triggers for man-db (2.8.5-2) ...
    18. Processing triggers for systemd (241-7~deb10u8) ...

    14、如何移除包但不移除配置

    要卸载软件包,而不移除它们的配置文件(为了之后再使用相同配置)。按以下使用'remove'。

    1. root@gyl-huawei:~# apt-get remove vsftpd
    2. Reading package lists... Done
    3. Building dependency tree
    4. Reading state information... Done
    5. The following package was automatically installed and is no longer required:
    6. ssl-cert
    7. Use 'apt autoremove' to remove it.
    8. The following packages will be REMOVED:
    9. vsftpd
    10. 0 upgraded, 0 newly installed, 1 to remove and 2 not upgraded.
    11. After this operation, 357 kB disk space will be freed.
    12. Do you want to continue? [Y/n] Y
    13. (Reading database ... 42103 files and directories currently installed.)
    14. Removing vsftpd (3.0.3-12) ...
    15. Processing triggers for man-db (2.8.5-2) ...

    15、如何完全移除包

    要移除包括其配置文件的软件包,按如下使用'purge'子命令。

    root@gyl-huawei:~# apt-get purge vsftpd

    另外,你可以按如下组合两个命令一起。

    root@gyl-huawei:~# apt-get remove --purge vsftpd

    16、如何清理磁盘空间

    'clean'命令用于通过从本地仓库清理获取的(下载的).deb文件(包)释放磁盘空间。

    root@gyl-huawei:~# apt-get clean

    17、如何只下载包的源代码

    只下载特定包的源代码,与包名一起使用选项'--download-only source'。

    1. root@gyl-huawei:~# apt-get --download-only source vsftpd
    2. Reading package lists... Done
    3. Need to get 234 kB of source archives.
    4. Get:1 http://repo.huaweicloud.com/debian buster/main vsftpd 3.0.3-12 (dsc) [1,856 B]
    5. Get:2 http://repo.huaweicloud.com/debian buster/main vsftpd 3.0.3-12 (tar) [197 kB]
    6. Get:3 http://repo.huaweicloud.com/debian buster/main vsftpd 3.0.3-12 (diff) [35.2 kB]
    7. Fetched 234 kB in 0s (541 kB/s)
    8. Download complete and in download only mode

    18、如何下载和解压一个包

    要下载和解压一个包的源代码到一个特定目录,输入以下命令。

    1. root@gyl-huawei:~# apt-get source vsftpd
    2. Reading package lists... Done
    3. Skipping already downloaded file 'vsftpd_3.0.3-12.dsc'
    4. Skipping already downloaded file 'vsftpd_3.0.3.orig.tar.gz'
    5. Skipping already downloaded file 'vsftpd_3.0.3-12.debian.tar.xz'
    6. Need to get 0 B of source archives.
    7. dpkg-source: info: extracting vsftpd in vsftpd-3.0.3
    8. dpkg-source: info: unpacking vsftpd_3.0.3.orig.tar.gz
    9. dpkg-source: info: unpacking vsftpd_3.0.3-12.debian.tar.xz
    10. dpkg-source: info: using patch list from debian/patches/series
    11. dpkg-source: info: applying 0020-manpage-hyphen.patch
    12. ...

    19、如何下,解包和编译一个包

    你也可以同时下载,解包和编译源代码,使用选项'--compile'。

     apt-get --compile source goaccess

    20、如何下一个包,但不安装

    使用'download'选项,你可以下载任何指定的包,而不安装它。例如,以下命令只下载'nethogs'包到当前工作目录。

    1. root@gyl-huawei:~# apt-get download nethogs
    2. Get:1 http://repo.huaweicloud.com/debian buster/main amd64 nethogs amd64 0.8.5-2+b1 [30.9 kB]
    3. Fetched 30.9 kB in 0s (1,347 kB/s)
    4. root@gyl-huawei:~# ls
    5. nethogs_0.8.5-2+b1_amd64.deb

    21、如何检测包的更改日志?

    ’changelog‘标记下载一个包的change-log并且显示被安装的包版本。

    root@gyl-huawei:~# apt-get changelog vsftpd

    22、如何检查损坏的依赖关系?

    'check'命令是一个诊断工具。它用于更新包缓存并且检查损坏的依赖关系。

    1. root@gyl-huawei:~# apt-get check
    2. Reading package lists... Done
    3. Building dependency tree
    4. Reading state information... Done

    23、如何搜索和构建依赖关系?

    'build-dep'命令在系统中搜索本地仓库并且为包安装构建依赖关系。如果在本地仓库中不存在包,它将返回一个错误代码。

    root@gyl-huawei:~# apt-get build-dep netcat

    24、如何自动清理Apt-Get缓存?

    ’autoclean‘命令删除来自/var/cache/apt/archives的所有.deb文件来释放显著的磁盘空间量。

    1. root@gyl-huawei:~# apt-get autoclean
    2. Reading package lists... Done
    3. Building dependency tree
    4. Reading state information... Done
    5. root@gyl-huawei:~# apt-get autoclean
    6. Reading package lists... Done
    7. Building dependency tree
    8. Reading state information... Done

    25、如何自动移除安装的包?

    'autoremove'子命令用于自动移除被安装来满足其它包依赖关系但现在不再需要的包。例如,以下命令将移除一个安装的包和其依赖关系。

    1. root@gyl-huawei:~# apt-get autoremove vsftpd
    2. Reading package lists... Done
    3. Building dependency tree
    4. Reading state information... Done
    5. The following packages will be REMOVED:
    6. ssl-cert vsftpd
    7. 0 upgraded, 0 newly installed, 2 to remove and 2 not upgraded.
    8. After this operation, 422 kB disk space will be freed.
    9. Do you want to continue? [Y/n] Y
    10. (Reading database ... 43307 files and directories currently installed.)
    11. Removing ssl-cert (1.0.39) ...
    12. Removing vsftpd (3.0.3-12) ...
    13. Processing triggers for man-db (2.8.5-2) ...

    26、检查一个包的所有依赖关系

    这将帮助你显示有关你指定的一个特定包依赖关系的信息。

    1. root@gyl-huawei:~# apt depends glances
    2. glances
    3. Depends: adduser
    4. Depends: lsb-base (>= 3.0-6)
    5. Depends: node-normalize.css
    6. Depends: python3-pkg-resources
    7. Depends: python3-psutil (>= 2.0.0)
    8. Depends: <python3:any>
    9. python3
    10. Recommends: hddtemp
    11. Recommends: lm-sensors
    12. Recommends: python3-bottle
    13. Recommends: python3-docker
    14. Recommends: python3-influxdb
    15. Recommends: python3-matplotlib
    16. Recommends: python3-netifaces
    17. Recommends: python3-pysnmp4
    18. Recommends: python3-pystache
    19. Suggests: glances-doc

     

    27、搜索一个包

    search选项搜索指定包名并且显示所有匹配的包。

    1. root@gyl-huawei:~# apt search apache2
    2. Sorting... Done
    3. Full Text Search... Done
    4. apache2/oldstable-proposed-updates 2.4.38-3+deb10u8 amd64
    5. Apache HTTP Server
    6. ...

    28、查看有关包的信息

    这将帮助你显示有关包的信息,通过指定你想要显示有关信息的所有包运行这个命令。

    1. root@gyl-huawei:~# apt show nmap
    2. Package: nmap
    3. Version: 7.70+dfsg1-6+deb10u2
    4. Priority: optional
    5. Section: net
    6. Maintainer: Debian Security Tools @tracker.debian.org>
    7. Installed-Size: 4,622 kB
    8. 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)
    9. Suggests: ncat, ndiff, zenmap
    10. Homepage: https://nmap.org/
    11. Tag: admin::monitoring, implemented-in::c, interface::commandline,
    12. network::scanner, role::program, scope::utility, use::scanning
    13. Download-Size: 1,894 kB
    14. APT-Sources: http://repo.huaweicloud.com/debian buster/main amd64 Packages
    15. Description: The Network Mapper
    16. Nmap is a utility for network exploration or security auditing. It
    17. supports ping scanning (determine which hosts are up), many port
    18. scanning techniques, version detection (determine service protocols
    19. and application versions listening behind ports), and TCP/IP
    20. fingerprinting (remote host OS or device identification). Nmap also
    21. offers flexible target and port specification, decoy/stealth scanning,
    22. sunRPC scanning, and more. Most Unix and Windows platforms are
    23. supported in both GUI and commandline modes. Several popular handheld
    24. devices are also supported, including the Sharp Zaurus and the iPAQ.
  • 相关阅读:
    SpringCloud微服务实践之六 Feign最佳实践(抽取)
    8.19 Day43---面试补充
    Codeforces Round #813 (Div. 2)
    考察程序员功力的代码
    金纳米粒子/钯纳米粒子/纳米银颗粒修饰聚苯乙烯微球/表面合成CdSe纳米晶制备过程
    【组成原理-处理器】处理器的相关概念
    Redis事务
    Linux下线程同步(带你了解什么是互斥锁、死锁、读写锁、条件变量、信号量等)
    mapreduce序列化(Hadoop)
    内核实战教程第二期|带你揭开数据库存储结构的神秘面纱
  • 原文地址:https://blog.csdn.net/yuyuyuliang00/article/details/126227696