1 概述
??Linux Mint 18.0 系统中我们经常发现apt命令与Ubuntu 16.04 LTS系统中有很大差别,今天我们就一起解密一下这其中的差异。
为了方便后续描述,我们先做一个约定,这里假定 :
系统 主机名 说明 Ubuntu 16.04 LTS ubuntu 虚拟机 Linux Mint 18.0 LTS qwfys 我的电脑
??首先,我们来看一下Linux Mint 18.0上的apt命令:
lwk@qwfys ~ $ lsb_release -a
No LSB modules are available.
Distributor ID: LinuxMint
Description: Linux Mint 18.3 Sylvia
Release: 18.3
Codename: sylvia
lwk@qwfys ~ $
lwk@qwfys ~ $ apt
apt
Usage: apt command [options]apt help command [options]Commands:add-repository - Add entries to apt sources.listautoclean - Erase old downloaded archive filesautoremove - Remove automatically all unused packagesbuild - Build binary or source packages from sourcesbuild-dep - Configure build-dependencies for source packageschangelog - View a package's changelogcheck - Verify that there are no broken dependenciesclean - Erase downloaded archive filescontains - List packages containing a filecontent - List files contained in a packagedeb - Install a .deb packagedepends - Show raw dependency information for a packagedist-upgrade - Upgrade the system by removing/installing/upgrading packagesdownload - Download the .deb file for a packageedit-sources - Edit /etc/apt/sources.list with your preferred text editordselect-upgrade - Follow dselect selectionsfull-upgrade - Same as 'dist-upgrade'held - List all held packageshelp - Show help for a commandhold - Hold a packageinstall - Install/upgrade packageslist - List packages based on package namespolicy - Show policy settingspurge - Remove packages and their configuration filesrecommends - List missing recommended packages for a particular packagerdepends - Show reverse dependency information for a packagereinstall - Download and (possibly) reinstall a currently installed packageremove - Remove packagessearch - Search for a package by name and/or expressionshow - Display detailed information about a packageshowhold - Same as 'held'source - Download source archivessources - Same as 'edit-sources'unhold - Unhold a packageupdate - Download lists of new/upgradable packagesupgrade - Perform a safe upgradeversion - Show the installed version of a packagelwk@qwfys ~ $
??接下来,我们看看在Ubuntu Server 16.04 LTS上的apt命令:
lwk@qwfys ~ $ ssh lwk@192.168.56.101
Welcome to Ubuntu 16.04.4 LTS (GNU/Linux 4.4.0-116-generic x86_64)* Documentation: https://help.ubuntu.com* Management: https://landscape.canonical.com* Support: https://ubuntu.com/advantage27 packages can be updated.
0 updates are security updates.Last login: Fri Apr 27 10:42:27 2018 from 192.168.56.1
lwk@ubuntu:~$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 16.04.4 LTS
Release: 16.04
Codename: xenial
lwk@ubuntu:~$ apt
apt 1.2.25 (amd64)
Usage: apt [options] commandapt is a commandline package manager and provides commands for
searching and managing as well as querying information about packages.
It provides the same functionality as the specialized APT tools,
like apt-get and apt-cache, but enables options more suitable for
interactive use by default.Most used commands:list - list packages based on package namessearch - search in package descriptionsshow - show package detailsinstall - install packagesremove - remove packagesautoremove - Remove automatically all unused packagesupdate - update list of available packagesupgrade - upgrade the system by installing/upgrading packagesfull-upgrade - upgrade the system by removing/installing/upgrading packagesedit-sources - edit the source information fileSee apt(8) for more information about the available commands.
Configuration options and syntax is detailed in apt.conf(5).
Information about how to configure sources can be found in sources.list(5).
Package and version choices can be expressed via apt_preferences(5).
Security details are available in apt-secure(8).This APT has Super Cow Powers.
lwk@ubuntu:~$
??那么二者之间有什么差异呢?接下来,我们一起分析一下。
2 apt 探密
??这里我们一起来做一个实验,用whereis与which命令来查看一下apt在上述两个系统上的差异。
??在Linux Mint 18.0上,输入如下命令:
lwk@qwfys ~ $ whereis apt
apt: /usr/bin/apt /usr/lib/apt /etc/apt /usr/local/bin/apt /usr/share/man/man8/apt.8.gz
lwk@qwfys ~ $ which apt
/usr/local/bin/apt
lwk@qwfys ~ $
??在Ubuntu 16.04 LTS上,输入如下命令
lwk@ubuntu:~$ whereis apt
apt: /usr/bin/apt /usr/lib/apt /etc/apt /usr/share/man/man8/apt.8.gz
lwk@ubuntu:~$ which apt
/usr/bin/apt
lwk@ubuntu:~$
??我们发现Linux Mint 18.0系统上,优先执行的是文件/usr/local/bin/apt,那么这个文件有什么奇特之处呢,我们来一起看一下:
lwk@qwfys ~ $ ll /usr/local/bin/apt
-rwxr-xr-x 1 root root 6482 Nov 23 17:49 /usr/local/bin/apt*
lwk@qwfys ~ $ file /usr/local/bin/apt
/usr/local/bin/apt: Python script, ASCII text executable
lwk@qwfys ~ $
??我们发现该文件是一个python文件,我们来看一下这个文件的内容:
lwk@qwfys ~ $ cat /usr/local/bin/apt
#!/usr/bin/python3import sys
import os
import subprocessdef usage():print("apt")print("Usage: apt command [options]")print(" apt help command [options]")print("")print("Commands:")print(" add-repository - Add entries to apt sources.list")print(" autoclean - Erase old downloaded archive files")print(" autoremove - Remove automatically all unused packages")print(" build - Build binary or source packages from sources")print(" build-dep - Configure build-dependencies for source packages")print(" changelog - View a package's changelog")print(" check - Verify that there are no broken dependencies")print(" clean - Erase downloaded archive files")print(" contains - List packages containing a file")print(" content - List files contained in a package")print(" deb - Install a .deb package")print(" depends - Show raw dependency information for a package")print(" dist-upgrade - Upgrade the system by removing/installing/upgrading packages")print(" download - Download the .deb file for a package")print(" edit-sources - Edit /etc/apt/sources.list with your preferred text editor")print(" dselect-upgrade - Follow dselect selections")print(" full-upgrade - Same as 'dist-upgrade'")print(" held - List all held packages")print(" help - Show help for a command")print(" hold - Hold a package")print(" install - Install/upgrade packages")print(" list - List packages based on package names")print(" policy - Show policy settings")print(" purge - Remove packages and their configuration files")print(" recommends - List missing recommended packages for a particular package")print(" rdepends - Show reverse dependency information for a package")print(" reinstall - Download and (possibly) reinstall a currently installed package")print(" remove - Remove packages")print(" search - Search for a package by name and/or expression")print(" show - Display detailed information about a package")print(" showhold - Same as 'held'")print(" source - Download source archives")print(" sources - Same as 'edit-sources'")print(" unhold - Unhold a package")print(" update - Download lists of new/upgradable packages")print(" upgrade - Perform a safe upgrade")print(" version - Show the installed version of a package")print("")sys.exit(1)aliases = {
"dist-upgrade": "full-upgrade","sources": "edit-sources","held": "showhold"
}if len(sys.argv) < 2:usage()argcommand = sys.argv[1]
argoptions = " ".join(sys.argv[2:])command = ""show_help = False
sort = False
highlight = Falserows, columns = os.popen('stty size', 'r').read().split()if argcommand == "help":if len(sys.argv) < 3:usage()show_help = Trueargcommand = sys.argv[2]argoptions = " ".join(sys.argv[3:])if argcommand in aliases.keys():argcommand = aliases[argcommand]if argcommand in ("autoremove", "list", "show", "install", "remove", "purge", "update", "upgrade", "full-upgrade", "edit-sources"):# aptcommand = "/usr/bin/apt %s %s" % (argcommand, argoptions)
elif argcommand in ("clean", "dselect-upgrade", "build-dep", "check", "autoclean", "source", "moo"):# apt-getcommand = "apt-get %s %s" % (argcommand, argoptions)
elif argcommand in ("changelog", "reinstall"):# aptitudecommand = "aptitude %s %s" % (argcommand, argoptions)
elif argcommand in ("stats", "depends", "rdepends", "policy"):# apt-cachecommand = "apt-cache %s %s" % (argcommand, argoptions)
elif argcommand in ("recommends", ):command = "/usr/lib/linuxmint/mintsystem/mint-apt-recommends.py " + argoptions
elif argcommand in ("showhold", "hold", "unhold"):# apt-markcommand = "apt-mark %s %s" % (argcommand, argoptions)
elif argcommand in ("markauto", "markmanual"):# apt-markcommand = "apt-mark %s %s" % (argcommand[4:], argoptions)
elif argcommand == "contains":command = "dpkg -S %s" % argoptions
elif argcommand == "content":command = "dpkg -L %s" % argoptions
elif argcommand == "deb":command = "dpkg -i %s" % argoptions
elif argcommand == "build":command = "dpkg-buildpackage %s" % argoptions
elif argcommand == "version":command = "/usr/lib/linuxmint/common/version.py %s" % argoptions
elif argcommand == "download":command = "/usr/lib/linuxmint/mintsystem/mint-apt-download.py " + argoptions
elif argcommand == "add-repository":command = "add-apt-repository %s" % argoptions
elif argcommand == "search":command = "aptitude -w %s %s %s" % (columns, argcommand, argoptions)
else:usage()sys.exit(1)# Sudo prefix
if os.getuid() != 0 and argcommand in ("autoremove", "install", "remove", "purge", "update", "upgrade", "full-upgrade", "edit-sources", "clean", "dselect-upgrade", "build-dep", "check", "autoclean", "reinstall", "deb", "hold", "unhold", "add-repository", "markauto", "markmanual"):command = "sudo %s" % command# Color highlighting
if argcommand in ("content", "version", "policy", "depends", "rdepends", "search") and len(argoptions.strip()) > 1:highlight = True# Sorting
if argcommand in ("content", "contains"):sort = Trueif show_help:print("\"apt " + argcommand + " " + argoptions + "\" is equivalent to \"" + command + "\"")
else:command = command.split()if sort and highlight:ps1 = subprocess.Popen(command, stdout=subprocess.PIPE)ps2 = subprocess.Popen(('sort'), stdin=ps1.stdout, stdout=subprocess.PIPE)ps3 = subprocess.Popen(('highlight', argoptions), stdin=ps2.stdout)ps3.wait()elif sort:ps1 = subprocess.Popen(command, stdout=subprocess.PIPE)ps2 = subprocess.Popen(('sort'), stdin=ps1.stdout)ps2.wait()elif highlight:ps1 = subprocess.Popen(command, stdout=subprocess.PIPE)ps2 = subprocess.Popen(('highlight', argoptions), stdin=ps1.stdout)ps2.wait()else:return_code = subprocess.call(command)sys.exit(return_code)
lwk@qwfys ~ $
??由此可见,文件/usr/local/bin/apt是对如下几个命令的封装:
/usr/bin/apt
/usr/bin/apt-get
/usr/bin/aptitude
/usr/bin/apt-mark
/usr/bin/dpkg
/usr/lib/linuxmint/common/version.py
/usr/lib/linuxmint/mintsystem/mint-apt-download.py
??对我们来说,只要熟知Linux Mint 18.0系统中的apt命令就可以满足日常系统管理工作了。