如何列出在 Linux 中具有可用更新的軟件包

在本文中,我將向您展示如何列出可從 Linux 系統上的命令行安裝的軟件包更新數量。 我使用了以下工具 apt、apt-get、aptitude、yum、apt-check script 和 apticron 來列出更新。

使用 apt 命令(Ubuntu 14.04 以上)

從 ubuntu 14.04 開始,我們有了一個名為“apt”的新命令。 以下命令將列出您可以更新的所有軟件包,它們的當前版本是什麼,以及新版本是什麼。

sudo apt list --upgradable
Listing... Done
gnupg/stable 1.4.18-7+deb8u2 amd64 [upgradable from: 1.4.18-7+deb8u1]
gpgv/stable 1.4.18-7+deb8u2 amd64 [upgradable from: 1.4.18-7+deb8u1]
libgcrypt20/stable 1.6.3-2+deb8u2 amd64 [upgradable from: 1.6.3-2+deb8u1]
libidn11/stable 1.29-1+deb8u2 amd64 [upgradable from: 1.29-1+deb8u1]
linux-image-3.16.0-4-amd64/stable 3.16.36-1+deb8u1 amd64 [upgradable from: 3.16.7-ckt25-2+deb8u3]

通常,這個命令應該在 apt update 之後運行。 如果有任何更新,它會顯示可以更新多少個包,並提到命令來顯示可以升級哪些包。

使用 apt-get 命令

我列出了 apt-get 命令的幾個選項來列出可用的升級包。

apt-get upgrade --dry-run
[sudo] password for user:
Reading package lists... Done
Building dependency tree
Reading state information... Done
Calculating upgrade... Done
The following packages were automatically installed and are no longer required:
  libsqlite0 pgdg-keyring
Use 'apt-get autoremove' to remove them.
The following packages have been kept back:
  linux-generic linux-headers-generic linux-image-generic
The following packages will be upgraded:
  apparmor apport apt apt-transport-https apt-utils base-files bash bind9-host
  libapt-inst1.5 libapt-pkg4.12 libbind9-90 libblkid1 libc-bin libc-dev-bin
  libc6 libc6-dev libcairo-gobject2 libcairo-script-interpreter2 libcairo2
  libcairo2-dev libcgmanager0 libcomerr2 libcurl3-gnutls libdbus-1-3
  libdbus-1-dev libdns100 libdrm-intel1 libdrm-nouveau2 libdrm-radeon1 libdrm2
  ...
  python-software-properties python-urllib3 python3-apport python3-apt
  python3-distupgrade python3-problem-report rsyslog systemd-services tcpdump
  util-linux uuid-runtime wget wpasupplicant x11-common x11proto-core-dev
  xtrans-dev
161 upgraded, 0 newly installed, 0 to remove and 3 not upgraded.
Inst base-files [7.2ubuntu5.1] (7.2ubuntu5.2 Ubuntu:14.04/trusty-updates [amd64])
Conf base-files (7.2ubuntu5.2 Ubuntu:14.04/trusty-updates [amd64])
Inst bash [4.3-7ubuntu1.4] (4.3-7ubuntu1.5 Ubuntu:14.04/trusty-updates [amd64])
...

請注意, –dry-run 允許我們查看將升級但不會進行任何更改的文件/包列表。

以下是可用的模擬選項 –just-print, -s, –simulate, , –dry-run, –recon, –no-act

幾個例子

# apt-get -s dist-upgrade | grep "^[[:digit:]]+ upgraded"
87 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.
# apt-get -s dist-upgrade | grep -Po "^[[:digit:]]+ (?=upgraded)"
87
# apt-get dist-upgrade -s --quiet=2 | grep ^Inst | wc -l
87

使用 aptitude 命令

要從“aptitude”命令獲取,請使用以下命令獲取準備升級的軟件包列表。 某些版本的 aptitude 不需要在 ~U 前後使用單引號。

aptitude search '~U'
...
i   scudcloud                                                                                                      - ScudCloud is a non official desktop client for Slack                                                                     
i   shim-signed                                                                                                    - Secure Boot chain-loading bootloader (Microsoft-signed binary)                                                           
i   smbclient                                                                                                      - command-line SMB/CIFS clients for Unix                                                                                   
i   strongswan                                                                                                     - IPsec VPN solution metapackage                                                                                           
i A strongswan-ike                                                                                                 - strongSwan Internet Key Exchange (v2) daemon                                                                             
i A strongswan-plugin-openssl                                                                                      - strongSwan plugin for OpenSSL                                                                                            
i A strongswan-starter                                                                                             - strongSwan daemon starter and configuration file parser                                                                  
i   sudo                                                                                                           - Provide limited super user privileges to specific users   
...

注意:它不會在線搜索,只會在您的系統本地搜索。

如何使用 apt-check 腳本進行檢查

我能夠使用這個腳本在 ubuntu 14.04 上獲得整潔的輸出。

# /usr/lib/update-notifier/apt-check -p
bind9-host
python3-problem-report
liblwres90
linux-headers-generic
libdns100
libisccfg90
...

# /usr/lib/update-notifier/apt-check –human-readable
33個包可以更新。
30 個更新是安全更新。

使用 yum 和 up2date 命令

Yum 是一個軟件包管理器,可在基於 RPM 的系統上安裝、更新和刪除軟件包。 它會自動計算依賴關係並找出安裝包時應該發生的事情。 列出可用於已安裝軟件包的更新:

yum list updates
Loaded plugins: fastestmirror, refresh-packagekit, security
Loading mirror speeds from cached hostfile
 * base: centos.hyve.com
 * epel: mirrors.coreix.net
 * extras: centos.hyve.com
 * rpmforge: www.mirrorservice.org
 * updates: mirror.sov.uk.goscomb.net

kernel.x86_64                                                     2.6.32-504.3.3.el6  
...

下一個實用程序是 up2date。 該實用程序從 RHN(紅帽網絡)下載軟件包並將它們安裝到您的系統上。 使用 up2date 更新/安裝軟件包的優點是它會自動解析依賴項並下載您可能需要的任何其他軟件包。 它有 -l 選項來顯示哪些軟件包更新可供下載和安裝。

up2date -l

Apticron 工具 – 通過電子郵件將更新列表發送給您

如果您需要您的機器在新軟件包可用時自動提醒您 – apticron 可能正是您正在尋找的軟件包。 Apticron 是一個簡單的 shell 腳本,每天從 cron 調用一次。 但是你需要先安裝它:

sudo apt-get install apticron

並配置:

vim /etc/apticron/apticron.conf
EMAIL="[email protected]"

現在,apticron 會在有新軟件包可用時每天發送一次電子郵件。 電子郵件示例如下所示:

apticron report [Mon, 10 Jul 2017 10:42:01 -0800] ======================================================================== apticron has detected that some packages need upgrading on: [ 1.2.3.4 ] The following packages are currently pending an upgrade: xfree86-common 4.3.0.dfsg.1-14sarge3 libice6 4.3.0.dfsg.1-14sarge3 libsm6 4.3.0.dfsg.1-14sarge3 xlibs-data 4.3.0.dfsg.1-14sarge3 libx11-6 4.3.0.dfsg.1-14sarge3 libxext6 4.3.0.dfsg.1-14sarge3 libxpm4 4.3.0.dfsg.1-14sarge3 ======================================================================== Package Details: Reading changelogs... --- Changes for xfree86 (xfree86-common libice6 libsm6 xlibs-data libx11-6 libxext6 libxpm4) --- xfree86 (4.3.0.dfsg.1-14sarge3) stable-security; urgency=high * Non-maintainer update by the Security Team: Fixes several vulnerabilities reported by iDefense (CVE-2006-6101, CVE-2006-6102, CVE-2006-6103) -- Moritz Muehlenhoff Sun, 9 Jul 2017 13:31:35 +0000 ======================================================================== You can perform the upgrade by issuing the command: apt-get dist-upgrade as root on faustus.example.com It is recommended that you simulate the upgrade first to confirm that the actions that would be taken are reasonable. The upgrade may be simulated by issuing the command: apt-get -s dist-upgrade -- apticron 

使計算機軟件保持最新是保護系統的最重要任務。 Ubuntu 可以提醒您掛起的更新,也可以配置為自動應用更新。 請注意,更新可能會重新啟動您服務器上的服務,因此這可能不適用於所有環境。