Do you understand the details of the apt command in Linux mint

1 概述

  Linux Mint 18.0 系统中我们经常发现apt命令与Ubuntu 16.04 LTS系统中有很大差别,今天我们就一起解密一下这其中的差异。

为了方便后续描述,我们先做一个约定,这里假定 :

系统主机名说明
Ubuntu 16.04 LTSubuntu虚拟机
Linux Mint 18.0 LTSqwfys我的电脑

  首先,我们来看一下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.list
  autoclean        - Erase old downloaded archive files
  autoremove       - Remove automatically all unused packages
  build            - Build binary or source packages from sources
  build-dep        - Configure build-dependencies for source packages
  changelog        - View a package's changelog
  check            - Verify that there are no broken dependencies
  clean            - Erase downloaded archive files
  contains         - List packages containing a file
  content          - List files contained in a package
  deb              - Install a .deb package
  depends          - Show raw dependency information for a package
  dist-upgrade     - Upgrade the system by removing/installing/upgrading packages
  download         - Download the .deb file for a package
  edit-sources     - Edit /etc/apt/sources.list with your preferred text editor
  dselect-upgrade  - Follow dselect selections
  full-upgrade     - Same as 'dist-upgrade'
  held             - List all held packages
  help             - Show help for a command
  hold             - Hold a package
  install          - Install/upgrade packages
  list             - List packages based on package names
  policy           - Show policy settings
  purge            - Remove packages and their configuration files
  recommends       - List missing recommended packages for a particular package
  rdepends         - Show reverse dependency information for a package
  reinstall        - Download and (possibly) reinstall a currently installed package
  remove           - Remove packages
  search           - Search for a package by name and/or expression
  show             - Display detailed information about a package
  showhold         - Same as 'held'
  source           - Download source archives
  sources          - Same as 'edit-sources'
  unhold           - Unhold a package
  update           - Download lists of new/upgradable packages
  upgrade          - Perform a safe upgrade
  version          - Show the installed version of a package

lwk@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/advantage

27 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] command

apt 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 names
  search - search in package descriptions
  show - show package details
  install - install packages
  remove - remove packages
  autoremove - Remove automatically all unused packages
  update - update list of available packages
  upgrade - upgrade the system by installing/upgrading packages
  full-upgrade - upgrade the system by removing/installing/upgrading packages
  edit-sources - edit the source information file

See 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/python3

import sys
import os
import subprocess

def 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 = False

rows, columns = os.popen('stty size', 'r').read().split()

if argcommand == "help":
    if len(sys.argv) < 3:
        usage()
    show_help = True
    argcommand = 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"):
    # apt
    command = "/usr/bin/apt %s %s" % (argcommand, argoptions)
elif argcommand in ("clean", "dselect-upgrade", "build-dep", "check", "autoclean", "source", "moo"):
    # apt-get
    command = "apt-get %s %s" % (argcommand, argoptions)
elif argcommand in ("changelog", "reinstall"):
    # aptitude
    command = "aptitude %s %s" % (argcommand, argoptions)
elif argcommand in ("stats", "depends", "rdepends", "policy"):
    # apt-cache
    command = "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-mark
    command = "apt-mark %s %s" % (argcommand, argoptions)
elif argcommand in ("markauto", "markmanual"):
    # apt-mark
    command = "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 = True

if 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命令就可以满足日常系统管理工作了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

qwfys200

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值