如何在Ubuntu/Debian Linux上获取已安装软件包的列表

时间:2020-01-09 10:39:30  来源:igfitidea点击:

如何获得server1上所有已安装软件包的列表,并将其安装在server2上?
如何在Ubuntu或Debian服务器上使用apt-get命令列出所有已安装的软件包?
您需要按照以下说明使用dpkg和其他命令,以查看Ubuntu或Debian Linux服务器上所有已安装软件包的列表。

只需列出所有已安装的软件包并提供简短说明

执行以下命令:

$ dpkg-query -l

输出示例:

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                           Architecture Description
+++-=============================-=================================-============-=============================================================================
==
ii  accountsservice               0.6.40-2ubuntu11.3                amd64        query and manipulate user account information
ii  acct                          6.5.5-2.1ubuntu1                  amd64        The GNU Accounting utilities for process and login accounting
ii  acl                           2.2.52-3                          amd64        Access control list utilities
ii  adduser                       3.113+nmu3ubuntu4                 all          add and remove users and groups
ii  apparmor                      2.10.95-0ubuntu2.5                amd64        user-space parser utility for AppArmor
ii  apt                           1.2.19                            amd64        commandline package manager
ii  apt-transport-https           1.2.19                            amd64        https download transport for APT
ii  apt-utils                     1.2.19                            amd64        package management related utility programs
ii  atop                          1.26-2build1                      amd64        Monitor for system resources and process activity
ii  base-files                    9.4ubuntu4.3                      amd64        Debian base system miscellaneous files
ii  base-passwd                   3.5.39                            amd64        Debian base system master password and group files
ii  bash                          4.3-14ubuntu1.1                   amd64        GNU Bourne Again SHell
ii  bash-completion               1:2.1-4.2ubuntu1.1                all          programmable completion for the bash shell
ii  bc                            1.06.95-9build1                   amd64        GNU bc arbitrary precision calculator language
ii  bind9-host                    1:9.10.3.dfsg.P4-8ubuntu1.4       amd64        Version of 'host' bundled with BIND 9.X
ii  bsdmainutils                  9.0.6ubuntu3                      amd64        collection of more utilities from FreeBSD

只想只查看软件包列表?
尝试:

$ dpkg-query -f '${binary:Package}\n' -W

输出示例:

unattended-upgrades
update-manager-core
ureadahead
usbutils
util-linux
uuid-runtime
vim
vim-common
vim-runtime
vim-tiny
virt-what
vnstat
wamerican
wbritish
wget
whiptail
whois
xauth
xdg-user-dirs
xkb-data
xml-core
xz-utils
zlib1g:amd64

如何获取已安装软件包的列表

输入以下命令

$ dpkg --get-selections | grep -v deinstall

输出示例:
显示已安装软件包的列表

另一种选择是使用apt命令,如下所示:

$ apt list --installed

或者只是使用grep命令搜索所有基于php的软件包:

$ apt list --installed | grep php

输出示例:

Listing...
accountsservice/xenial-updates,now 0.6.40-2ubuntu11.3 amd64 [installed,automatic]
acl/xenial,now 2.2.52-3 amd64 [installed,automatic]
adduser/xenial,xenial,now 3.113+nmu3ubuntu4 all [installed]
apparmor/xenial-updates,now 2.10.95-0ubuntu2.5 amd64 [installed]
apt/xenial-updates,now 1.2.19 amd64 [installed]
apt-transport-https/xenial-updates,now 1.2.19 amd64 [installed]
apt-utils/xenial-updates,now 1.2.19 amd64 [installed]
atop/xenial,now 1.26-2build1 amd64 [installed]
base-files/xenial-updates,now 9.4ubuntu4.3 amd64 [installed]
base-passwd/xenial,now 3.5.39 amd64 [installed]
bash/xenial-updates,now 4.3-14ubuntu1.1 amd64 [installed]
bash-completion/xenial-updates,xenial-updates,now 1:2.1-4.2ubuntu1.1 all [installed]
bc/xenial,now 1.06.95-9build1 amd64 [installed]
bind9-host/xenial-updates,xenial-security,now 1:9.10.3.dfsg.P4-8ubuntu1.4 amd64 [installed]
bsdmainutils/xenial,now 9.0.6ubuntu3 amd64 [installed]
bsdutils/xenial-updates,now 1:2.27.1-6ubuntu3.2 amd64 [installed]
busybox-initramfs/xenial,now 1:1.22.0-15ubuntu1 amd64 [installed]
busybox-static/xenial,now 1:1.22.0-15ubuntu1 amd64 [installed]
bzip2/xenial,now 1.0.6-8 amd64 [installed]
ca-certificates/xenial,xenial,now 20150104ubuntu1 all [installed]
command-not-found/xenial-updates,xenial-updates,now 0.3ubuntu16.04.2 all [installed]
command-not-found-data/xenial-updates,now 0.3ubuntu16.04.2 amd64 [installed]
console-setup/xenial-updates,xenial-updates,now 1.108ubuntu15.2 all [installed]
console-setup-linux/xenial-updates,xenial-updates,now 1.108ubuntu15.2 all [installed]
.....
..
...
xkb-data/xenial,xenial,now 2.16-1ubuntu1 all [installed]
xml-core/xenial,xenial,now 0.13+nmu2 all [installed,automatic]
xz-utils/xenial,now 5.1.1alpha+20120614-2ubuntu2 amd64 [installed,automatic]
zlib1g/xenial,now 1:1.2.8.dfsg-2ubuntu4 amd64 [installed]

如何将列表保存到文本文件

语法在server1上如下:

$ dpkg --get-selections | grep -v deinstall > mylist.txt

或$dpkg -l | grep ^ ii | awk {print $2}> mylist.txt现在将mylist.txt复制到名为server2的服务器:

$ scp mylist.txt user@server2:~/

如何在server2上还原列表

在server2上,从mylist.txt恢复安装,运行:

$ dpkg --clear-selections
$ sudo dpkg --set-selections < mylist.txt

或者

$ sudo apt-get install -y $(< mylist.txt)

最后,运行以下命令以删除不需要的软件包:

$ sudo apt-get autoremove

上面的命令应该从备份或其他服务器还原/安装软件包。
另一种选择是使用aptitude命令,如下所示:

### Create the list of software to a file called /tmp/packages.txt ###
dpkg --get-selections > /tmp/packages.txt
 
### copy file to another server called server1.theitroad.local ##
scp /tmp/packages.txt [email protected]:/tmp/
 
### Now that your list is copyied use apt-get/synaptic to install the packages###
 
# 1. Login to another server
ssh [email protected]
 
# 2. Restore it 
aptitude install $(cat /tmp/packages.txt | awk '{print }')