linux中25个有用的apt-get和apt-cache基本命令

什么是 apt-get?

apt-get是一个功能强大且免费的包管理命令行程序,用于与Ubuntu’s APT(Advanced Packaging Tool) 库执行安装新软件包、删除现有软件包、升级现有软件包甚至用于升级整个操作系统。

什么是 apt-cache?

apt-cache命令行工具用于搜索 apt 软件包缓存。简单来说,这个工具用于搜索软件包,收集软件包信息,也用于搜索可以安装的可用软件包Debian或者Ubuntu`基于系统。

1. 如何列出所有可用的包?

要列出所有可用的包,请键入以下命令。

  1. $ apt-cache pkgnames
  1. esseract-ocr-epo
  2. pipenightdreams
  3. mumudvb
  4. tbb-examples
  5. libsvm-java
  6. libmrpt-hmtslam0.9
  7. libboost-timer1.50-dev
  8. kcm-touchpad
  9. g++-4.5-multilib
  10. ...

2. 如何找到软件的包名和描述?

要在安装之前找出包名称及其描述,请使用 search 命令。使用searchapt-cache将显示带有简短描述的匹配包列表。假设你想了解包的描述 vsftpd,那么命令就是。

  1. $ apt-cache search vsftpd
  1. vsftpd - lightweight, efficient FTP server written for security
  2. ccze - A robust, modular log coloriser
  3. ftpd - File Transfer Protocol (FTP) server
  4. yasat - simple stupid audit tool

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

  1. $ apt-cache pkgnames vsftpd
  1. vsttpd

3. 如何查看包信息?

例如,如果你想检查软件包信息及其简短说明,请说(版本号、校验和、大小、安装大小、类别等)。用 show 子命令如下所示。

  1. $ apt-cache show netcat
  1. Package: netcat
  2. Priority: optional
  3. Section: universe/net
  4. Installed-Size: 30
  5. Maintainer: Ubuntu Developers <ubuntu-devel-discuss@lists.ubuntu.com>
  6. Original-Maintainer: Ruben Molina <rmolina@udea.edu.co>
  7. Architecture: all
  8. Version: 1.10-40
  9. Depends: netcat-traditional (>= 1.10-39)
  10. Filename: pool/universe/n/netcat/netcat_1.10-40_all.deb
  11. Size: 3340
  12. MD5sum: 37c303f02b260481fa4fc9fb8b2c1004
  13. SHA1: 0371a3950d6967480985aa014fbb6fb898bcea3a
  14. SHA256: eeecb4c93f03f455d2c3f57b0a1e83b54dbeced0918ae563784e86a37bcc16c9
  15. Description-en: TCP/IP swiss army knife -- transitional package
  16. This is a "dummy" package that depends on lenny's default version of
  17. netcat, to ease upgrades. It may be safely removed.
  18. Description-md5: 1353f8c1d079348417c2180319bdde09
  19. Bugs: https://bugs.launchpad.net/ubuntu/+filebug
  20. Origin: Ubuntu

4. 如何检查特定包的依赖关系?

使用 showpkg 子命令来检查特定软件包的依赖关系。是否安装了这些依赖包。例如,使用 showpkg 命令和包名。

  1. $ apt-cache showpkg vsftpd
  1. Package: vsftpd
  2. Versions:
  3. 2.3.5-3ubuntu1 (/var/lib/apt/lists/in.archive.ubuntu.com_ubuntu_dists_quantal_main_binary-i386_Packages)
  4. Description Language:
  5. File: /var/lib/apt/lists/in.archive.ubuntu.com_ubuntu_dists_quantal_main_binary-i386_Packages
  6. MD5: 81386f72ac91a5ea48f8db0b023f3f9b
  7. Description Language: en
  8. File: /var/lib/apt/lists/in.archive.ubuntu.com_ubuntu_dists_quantal_main_i18n_Translation-en
  9. MD5: 81386f72ac91a5ea48f8db0b023f3f9b
  10. Reverse Depends:
  11. ubumirror,vsftpd
  12. harden-servers,vsftpd
  13. Dependencies:
  14. 2.3.5-3ubuntu1 - debconf (18 0.5) debconf-2.0 (0 (null)) upstart-job (0 (null)) libc6 (2 2.15) libcap2 (2 2.10) libpam0g (2 0.99.7.1) libssl1.0.0 (2 1.0.0) libwrap0 (2 7.6-4~) adduser (0 (null)) libpam-modules (0 (null)) netbase (0 (null)) logrotate (0 (null)) ftp-server (0 (null)) ftp-server (0 (null))
  15. Provides:
  16. 2.3.5-3ubuntu1 - ftp-server
  17. Reverse Provides:

5. 如何查看缓存的统计信息

stats 子命令将显示有关缓存的整体统计信息。例如,以下命令将显示 Total package names 是在缓存中找到的包数。

  1. $ apt-cache stats
  1. Total package names: 51868 (1,037 k)
  2. Total package structures: 51868 (2,490 k)
  3. Normal packages: 39505
  4. Pure virtual packages: 602
  5. Single virtual packages: 3819
  6. Mixed virtual packages: 1052
  7. Missing: 6890
  8. Total distinct versions: 43015 (2,753 k)
  9. Total distinct descriptions: 81048 (1,945 k)
  10. Total dependencies: 252299 (7,064 k)
  11. Total ver/file relations: 45567 (729 k)
  12. Total Desc/File relations: 81048 (1,297 k)
  13. Total Provides mappings: 8228 (165 k)
  14. Total globbed strings: 286 (3,518 )
  15. Total dependency version space: 1,145 k
  16. Total slack space: 62.6 k
  17. Total space accounted for: 13.3 M

6. 如何更新系统包

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

  1. $ sudo apt-get update
  1. [sudo] password for tecmint:
  2. Ign http://security.ubuntu.com quantal-security InRelease
  3. Get:1 http://security.ubuntu.com quantal-security Release.gpg [933 B]
  4. Get:2 http://security.ubuntu.com quantal-security Release [49.6 kB]
  5. Ign http://in.archive.ubuntu.com quantal InRelease
  6. Ign http://in.archive.ubuntu.com quantal-updates InRelease
  7. Get:3 http://repo.varnish-cache.org precise InRelease [13.7 kB]
  8. Ign http://in.archive.ubuntu.com quantal-backports InRelease
  9. Hit http://in.archive.ubuntu.com quantal Release.gpg
  10. Get:4 http://security.ubuntu.com quantal-security/main Sources [34.8 kB]
  11. Get:5 http://in.archive.ubuntu.com quantal-updates Release.gpg [933 B]
  12. ...

7. 如何升级软件包

upgrade 命令用于升级系统上所有当前安装的软件包。在任何情况下,当前安装的软件包都不会被删除,或者尚未安装的软件包也不会被检索和安装以满足升级依赖性。

  1. $ sudo apt-get upgrade
  1. Reading package lists... Done
  2. Building dependency tree
  3. Reading state information... Done
  4. The following packages have been kept back:
  5. linux-headers-generic linux-image-generic wine1.5 wine1.5-i386
  6. The following packages will be upgraded:
  7. activity-log-manager-common activity-log-manager-control-center adium-theme-ubuntu alacarte
  8. alsa-base app-install-data-partner appmenu-gtk appmenu-gtk3 apport apport-gtk apt
  9. apt-transport-https apt-utils aptdaemon aptdaemon-data at-spi2-core bamfdaemon base-files bind9-host
  10. ...

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

  1. $ sudo apt-get dist-upgrade

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

install子命令被一个或多个希望安装或升级的软件包跟踪。

  1. $ sudo apt-get install netcat
  1. Reading package lists... Done
  2. Building dependency tree
  3. Reading state information... Done
  4. The following extra packages will be installed:
  5. netcat-traditional
  6. The following NEW packages will be installed:
  7. netcat netcat-traditional
  8. 0 upgraded, 2 newly installed, 0 to remove and 328 not upgraded.
  9. Need to get 67.1 kB of archives.
  10. After this operation, 186 kB of additional disk space will be used.
  11. Do you want to continue [Y/n]? y
  12. Get:1 http://in.archive.ubuntu.com/ubuntu/ quantal/universe netcat-traditional i386 1.10-40 [63.8 kB]
  13. Get:2 http://in.archive.ubuntu.com/ubuntu/ quantal/universe netcat all 1.10-40 [3,340 B]
  14. Fetched 67.1 kB in 1s (37.5 kB/s)
  15. Selecting previously unselected package netcat-traditional.
  16. (Reading database ... 216118 files and directories currently installed.)
  17. Unpacking netcat-traditional (from .../netcat-traditional_1.10-40_i386.deb) ...
  18. Selecting previously unselected package netcat.
  19. Unpacking netcat (from .../netcat_1.10-40_all.deb) ...
  20. Processing triggers for man-db ...
  21. Setting up netcat-traditional (1.10-40) ...
  22. Setting up netcat (1.10-40) ...

9. 如何安装多个包?

你可以在命令中添加多个软件包名称,以便同时安装多个软件包。例如,以下命令将安装包[nethogs][goaccess]

  1. $ sudo apt-get install nethogs goaccess
  1. Reading package lists... Done
  2. Building dependency tree
  3. Reading state information... Done
  4. goaccess is already the newest version.
  5. nethogs is already the newest version.
  6. 0 upgraded, 0 newly installed, 0 to remove and 328 not upgraded.

10. 如何使用通配符安装多个包

借助正则表达式,你可以使用一个字符串添加多个包。例如,我们使用*通配符来安装几个包含 name字符串,名称将是package-name

  1. $ sudo apt-get install '*name*'

11. 如何在不升级的情况下安装软件包

使用 sub –no-upgrade 命令将阻止已安装的软件包升级。

  1. $ sudo apt-get install packageName --no-upgrade
  1. Reading package lists... Done
  2. Building dependency tree
  3. Reading state information... Done
  4. Skipping vsftpd, it is already installed and upgrade is not set.
  5. 0 upgraded, 0 newly installed, 0 to remove and 328 not upgraded.

12. 如何只升级特定的包

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

  1. $ sudo apt-get install packageName --only-upgrade
  1. Reading package lists... Done
  2. Building dependency tree
  3. Reading state information... Done
  4. vsftpd is already the newest version.
  5. 0 upgraded, 0 newly installed, 0 to remove and 328 not upgraded.

13. 如何安装特定的软件包版本?

假设你只想安装特定版本的软件包,只需使用=与包名并附加所需的版本。

  1. $ sudo apt-get install vsftpd=2.3.5-3ubuntu1
  1. Reading package lists... Done
  2. Building dependency tree
  3. Reading state information... Done
  4. vsftpd is already the newest version.
  5. 0 upgraded, 0 newly installed, 0 to remove and 328 not upgraded.

14. 如何在没有配置的情况下删除包

卸载软件包而不删除它们的配置文件(以便以后重新使用相同的配置)。使用remove 命令如图所示。

  1. $ sudo apt-get remove vsftpd
  1. [sudo] password for tecmint:
  2. Reading package lists... Done
  3. Building dependency tree
  4. Reading state information... Done
  5. The following packages will be REMOVED:
  6. vsftpd
  7. 0 upgraded, 0 newly installed, 1 to remove and 328 not upgraded.
  8. After this operation, 364 kB disk space will be freed.
  9. Do you want to continue [Y/n]? y
  10. (Reading database ... 216156 files and directories currently installed.)
  11. Removing vsftpd ...
  12. vsftpd stop/waiting
  13. Processing triggers for ureadahead ...
  14. Processing triggers for man-db ...

15. 我如何完全删除包

要删除软件包,包括其配置文件,请使用 purge 子命令如下所示。

  1. $ sudo apt-get purge vsftpd
  1. Reading package lists... Done
  2. Building dependency tree
  3. Reading state information... Done
  4. The following packages will be REMOVED:
  5. vsftpd*
  6. 0 upgraded, 0 newly installed, 1 to remove and 328 not upgraded.
  7. After this operation, 0 B of additional disk space will be used.
  8. Do you want to continue [Y/n]? y
  9. (Reading database ... 216107 files and directories currently installed.)
  10. Removing vsftpd ...
  11. Purging configuration files for vsftpd ...
  12. Processing triggers for ureadahead ...

或者,你可以将这两个命令组合在一起,如下所示。

  1. $ sudo apt-get remove --purge vsftpd
  1. Reading package lists... Done
  2. Building dependency tree
  3. Reading state information... Done
  4. The following packages will be REMOVED:
  5. vsftpd*
  6. 0 upgraded, 0 newly installed, 1 to remove and 328 not upgraded.
  7. After this operation, 364 kB disk space will be freed.
  8. Do you want to continue [Y/n]? y
  9. (Reading database ... 216156 files and directories currently installed.)
  10. Removing vsftpd ...
  11. vsftpd stop/waiting
  12. Purging configuration files for vsftpd ...
  13. Processing triggers for ureadahead ...
  14. Processing triggers for man-db ...

16. 如何清理磁盘空间

clean 命令用于通过清理检索(下载)来释放磁盘空间.deb来自本地存储库的文件(包)。

  1. $ sudo apt-get clean

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

要仅下载特定包的源代码,请使用选项 –download-only source带有’package-name’,如图所示。

  1. $ sudo apt-get --download-only source vsftpd
  1. Reading package lists... Done
  2. Building dependency tree
  3. Reading state information... Done
  4. Need to get 220 kB of source archives.
  5. Get:1 http://in.archive.ubuntu.com/ubuntu/ quantal/main vsftpd 2.3.5-3ubuntu1 (dsc) [1,883 B]
  6. Get:2 http://in.archive.ubuntu.com/ubuntu/ quantal/main vsftpd 2.3.5-3ubuntu1 (tar) [188 kB]
  7. Get:3 http://in.archive.ubuntu.com/ubuntu/ quantal/main vsftpd 2.3.5-3ubuntu1 (diff) [30.5 kB]
  8. Fetched 220 kB in 4s (49.1 kB/s)
  9. Download complete and in download only mode

18. 如何下载和解压包

要将包的源代码下载并解压到特定目录,请键入以下命令。

  1. $ sudo apt-get source vsftpd
  1. Reading package lists... Done
  2. Building dependency tree
  3. Reading state information... Done
  4. Need to get 220 kB of source archives.
  5. Get:1 http://in.archive.ubuntu.com/ubuntu/ quantal/main vsftpd 2.3.5-3ubuntu1 (dsc) [1,883 B]
  6. Get:2 http://in.archive.ubuntu.com/ubuntu/ quantal/main vsftpd 2.3.5-3ubuntu1 (tar) [188 kB]
  7. Get:3 http://in.archive.ubuntu.com/ubuntu/ quantal/main vsftpd 2.3.5-3ubuntu1 (diff) [30.5 kB]
  8. Fetched 220 kB in 1s (112 kB/s)
  9. gpgv: Signature made Thursday 24 May 2012 02:35:09 AM IST using RSA key ID 2C48EE4E
  10. gpgv: Can't check signature: public key not found
  11. dpkg-source: warning: failed to verify signature on ./vsftpd_2.3.5-3ubuntu1.dsc
  12. dpkg-source: info: extracting vsftpd in vsftpd-2.3.5
  13. dpkg-source: info: unpacking vsftpd_2.3.5.orig.tar.gz
  14. dpkg-source: info: unpacking vsftpd_2.3.5-3ubuntu1.debian.tar.gz
  15. dpkg-source: info: applying 01-builddefs.patch
  16. dpkg-source: info: applying 02-config.patch
  17. dpkg-source: info: applying 03-db-doc.patch
  18. dpkg-source: info: applying 04-link-local.patch
  19. dpkg-source: info: applying 05-whitespaces.patch
  20. dpkg-source: info: applying 06-greedy.patch
  21. dpkg-source: info: applying 07-utf8.patch
  22. dpkg-source: info: applying 08-manpage.patch
  23. dpkg-source: info: applying 09-s390.patch
  24. dpkg-source: info: applying 10-remote-dos.patch
  25. dpkg-source: info: applying 11-alpha.patch
  26. dpkg-source: info: applying 09-disable-anonymous.patch
  27. dpkg-source: info: applying 12-ubuntu-use-snakeoil-ssl.patch

19. 如何下载、解压和编译一个包

你也可以同时下载、解压和编译源代码,使用选项–compile 如下所示。

  1. $ sudo apt-get --compile source goaccess
  1. [sudo] password for tecmint:
  2. Reading package lists... Done
  3. Building dependency tree
  4. Reading state information... Done
  5. Need to get 130 kB of source archives.
  6. Get:1 http://in.archive.ubuntu.com/ubuntu/ quantal/universe goaccess 1:0.5-1 (dsc) [1,120 B]
  7. Get:2 http://in.archive.ubuntu.com/ubuntu/ quantal/universe goaccess 1:0.5-1 (tar) [127 kB]
  8. Get:3 http://in.archive.ubuntu.com/ubuntu/ quantal/universe goaccess 1:0.5-1 (diff) [2,075 B]
  9. Fetched 130 kB in 1s (68.0 kB/s)
  10. gpgv: Signature made Tuesday 26 June 2012 09:38:24 AM IST using DSA key ID A9FD4821
  11. gpgv: Can't check signature: public key not found
  12. dpkg-source: warning: failed to verify signature on ./goaccess_0.5-1.dsc
  13. dpkg-source: info: extracting goaccess in goaccess-0.5
  14. dpkg-source: info: unpacking goaccess_0.5.orig.tar.gz
  15. dpkg-source: info: unpacking goaccess_0.5-1.debian.tar.gz
  16. dpkg-buildpackage: source package goaccess
  17. dpkg-buildpackage: source version 1:0.5-1
  18. dpkg-buildpackage: source changed by Chris Taylor <ctaylor@debian.org>
  19. dpkg-buildpackage: host architecture i386
  20. dpkg-source --before-build goaccess-0.5
  21. dpkg-checkbuilddeps: Unmet build dependencies: debhelper (>= 9) autotools-dev libncurses5-dev libglib2.0-dev libgeoip-dev autoconf
  22. dpkg-buildpackage: warning: build dependencies/conflicts unsatisfied; aborting
  23. dpkg-buildpackage: warning: (Use -d flag to override.)
  24. ...

20. 如何在不安装的情况下下载软件包

使用 download 选项,你可以下载任何给定的软件包而无需安装它。例如,下面的命令只会下载nethogs打包到当前工作目录。

  1. $ sudo apt-get download nethogs
  1. Get:1 Downloading nethogs 0.8.0-1 [27.1 kB]
  2. Fetched 27.1 kB in 3s (7,506 B/s)

21. 如何查看包的变更日志?

changelog标志下载软件包更改日志并显示已安装的软件包版本。

  1. $ sudo apt-get changelog vsftpd
  1. vsftpd (2.3.5-3ubuntu1) quantal; urgency=low
  2. * Merge from Debian testing (LP: #1003644). Remaining changes:
  3. + debian/vsftpd.upstart: migrate vsftpd to upstart.
  4. + Add apport hook (LP: #513978):
  5. - debian/vsftpd.apport: Added.
  6. - debian/control: Build-depends on dh-apport.
  7. - debian/rules: Add --with apport.
  8. + Add debian/watch file.
  9. + debian/patches/09-disable-anonymous.patch: Disable anonymous login
  10. by default. (LP: #528860)
  11. * debian/patches/12-ubuntu-us-snakeoil-ssl.patch: Use snakeoil SSL
  12. certificates and key.
  13. -- Andres Rodriguez <andreserl@ubuntu.com> Wed, 23 May 2012 16:59:36 -0400
  14. ...

22. 如何检查已损坏的依赖项?

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

  1. $ sudo apt-get check
  1. [sudo] password for tecmint:
  2. Reading package lists... Done
  3. Building dependency tree
  4. Reading state information... Done

23. 如何搜索和构建依赖?

这个build-dep命令搜索系统中的本地存储库并安装package.json的构建依赖项。如果本地存储库中不存在该包,它将返回错误代码。

  1. $ sudo apt-get build-dep netcat
  1. The following NEW packages will be installed:
  2. debhelper dh-apparmor html2text po-debconf quilt
  3. 0 upgraded, 5 newly installed, 0 to remove and 328 not upgraded.
  4. Need to get 1,219 kB of archives.
  5. After this operation, 2,592 kB of additional disk space will be used.
  6. Do you want to continue [Y/n]? y
  7. Get:1 http://in.archive.ubuntu.com/ubuntu/ quantal/main html2text i386 1.3.2a-15build1 [91.4 kB]
  8. Get:2 http://in.archive.ubuntu.com/ubuntu/ quantal/main po-debconf all 1.0.16+nmu2ubuntu1 [210 kB]
  9. Get:3 http://in.archive.ubuntu.com/ubuntu/ quantal/main dh-apparmor all 2.8.0-0ubuntu5 [9,846 B]
  10. Get:4 http://in.archive.ubuntu.com/ubuntu/ quantal/main debhelper all 9.20120608ubuntu1 [623 kB]
  11. Get:5 http://in.archive.ubuntu.com/ubuntu/ quantal/main quilt all 0.60-2 [285 kB]
  12. Fetched 1,219 kB in 4s (285 kB/s)
  13. ...

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

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

  1. $ sudo apt-get autoclean
  1. Reading package lists... Done
  2. Building dependency tree
  3. Reading state information... Done

25. 如何自动删除已安装的软件包?

autoremove sub 命令用于自动删除那些肯定安装以满足其他包的依赖关系但现在不再需要的包。例如,以下命令将删除已安装的包及其依赖项。

  1. $ sudo apt-get autoremove vsftpd
  1. Reading package lists... Done
  2. Building dependency tree
  3. Reading state information... Done
  4. Package 'vsftpd' is not installed, so not removed
  5. 0 upgraded, 0 newly installed, 0 to remove and 328 not upgraded.

返回笔记列表
入门小站