apt命令概述

目录

Linux软件仓库

软件源配置

apt命令

常见的命令如下:

常见的用法

示例:使用apt安装redis软件


Linux软件仓库

Ubuntu采用集中式的软件仓库机制,将各式各样 的软件包分门别类地存放在软件仓库中,进行有效地组织和管理。然后,将软件仓库置于许许多多的镜像服务器中,并保持基本一致。这样,所有的Ubuntu用 户随时都能获得最新版本的安装软件包。因此,对于用户,这些镜像服务器就是他们的软件源(Reposity)。

说白了,这里的软件仓库就类似于Apple Store一样。这样做的目的是使得Linux上安装的软件都是安全的。

由于每位用户所处的网络环境不同,不可能随意地访问各镜像站点。为了能够有选择地访问,在Ubuntu系统中,使用软件源配置文件/etc/apt/sources.list列出最合适访问的镜像站点地址。

即使这样,软件源配置文件只是告知Ubuntu系统 可以访问的镜像站点地址,但那些镜像站点具体都拥有什么软件资源并不清楚。若每安装一个软件包,就在服务器上寻找一遍,效率是很低的。因而,就有必要为这 些软件资源列个清单(建立索引文件),以便本地主机查询。这就是APT软件包管理器的工作原理。

同时,APT能够检查Ubuntu Linux系统中的软件包依赖关系,大大简化了Ubuntu用户安装和卸载软件包的过程。因而,APT成为Ubuntu Linux中最受欢迎的工具,也成为其他软件包管理工具的底层工具,例如,图形界面的软件包管理工具Synaptic就是在APT基础上工作的。

软件源配置

在安装Ubuntu Linux时,系统会根据用户所选择的国家/时区,推荐合适的软件源地址。通常,用户使用默认的配置文件就可以。不过,当用户发现更合适的软件源镜像站点时,可以重新设置APT源。

APT软件包管理器在一个文件中列出可获得软件 包的镜像站点地址,这个软件源配置文件就是/etc/apt/sources.list。这个软件源配制文件的本质就是一个普通的文本文件,可以在超级管 理员授权下,使用任何文本编辑器进行编辑。在该文件中,添加的软件源镜像站点称为一个配置项。

apt命令

apt-get,是一条linux命令,适用于deb包管理式的操作系统(例如Ubuntu系统),主要用于自动从互联网的软件仓库中搜索、安装、升级、卸载软件或操作系统。

而apt命令则是apt-get的升级版,目前各大Linux发行商都推荐使用apt命令。

常见的命令如下:

update
update 命令用于从配置的源下载包信息。update 命令应该总是在安装或升级包之前执行。

upgrade
upgrade 命令用于从配置的源安装当前系统中的所有包的可用升级。如果需要满足依赖关系,就安装新的包,但是不会删除现有的包。如果包的升级需要删除已安装的包,则不执行此包的升级。

full-upgrade
full-upgrade 命令执行升级功能,如果需要将系统升级到新的版本,则会删除当前已安装的包。

install,remove,purge
install 命令用来安装一个或多个指定的包。remove 命令用来删除包,但是会保留包的配置文件。purge 命令会在删除包的同时删除其配置文件。

autoremove
autoremove 命令用于删除自动安装的包,这些包是为了满足其他包的依赖关系而自动安装的,随着依赖关系的更改或需要它们的包已被删除,这些包现在不再需要了。

search
search 命令用于在可用包列表中搜索给定的项并显示匹配到的内容。例如,如果您正在寻找具有特定功能的包,这将非常有用。

比如我们想要安装jdk,但是又不知道具体的软件名,这时就可以使用search命令了。

parallels@ubuntu-linux-20-04-desktop:~$ sudo apt search jdk
Sorting... Done
Full Text Search... Done
crypto-policies/focal 20190816git-1 all
  unify the crypto policies used by different applications and libraries

default-jdk/focal 2:1.11-72 arm64
  Standard Java or Java compatible Development Kit

default-jdk-doc/focal 2:1.11-72 arm64
  Standard Java or Java compatible Development Kit (documentation)

default-jdk-headless/focal 2:1.11-72 arm64
  Standard Java or Java compatible Development Kit (headless)

default-jre/focal 2:1.11-72 arm64
  Standard Java or Java compatible Runtime

default-jre-headless/focal 2:1.11-72 arm64
  Standard Java or Java compatible Runtime (headless)

golang-github-jdkato-prose-dev/focal 1.1.1-1 all
  Golang library for text processing

golang-github-jdkato-syllables-dev/focal 0.1.0+git20170409.10.8961fa0-1 all
  Go syllable counter

japitools/focal 0.9.7-1 all
  Java API compatibility testing tools

jattach/focal 1.5-2 arm64
  JVM Dynamic Attach utility all in one jmap jstack jcmd jinfo

show
show 命令显示关于给定包的信息,包括它的依赖关系、安装和下载大小、包的来源、包内容的描述等等。比如,在删除一个包或搜索要安装的新包之前查看这些信息是很有帮助的。

list
list 命令可以显示满足特定条件的包列表,默认列出所有的包。可以通过 --installed 选项列出已安装的包,--upgrade 选项列出可以升级的包。

edit-sources
edit-sources 命令用来编辑 /etc/apt/source.list 文件

常见的用法

更新包索引文件

$ sudo apt update

安装包

$ sudo apt install nginx

安装指定版本的包

$ sudo apt install vim=2:8.0.1453-1ubuntu1

安装本地的 deb 包文件

$ sudo apt install name.deb

这种方法会自动下载并安装依赖的包。

安装系统中有更新的包
upgrade 命令会升级已安装的包,同但不移除任何包。它的目标是确保可能的侵入式升级最小化:

$ sudo apt update
$ sudo apt upgrade

删除包
通过下面两个命令可以删除使用 apt install 安装的包:

$ sudo apt remove nmap
$ sudo apt purge nmap

其中 remove 命令会保留配置文件,而 purge 命令会把配置文件一起删除。

查看包的信息

$ apt show vim

parallels@ubuntu-linux-20-04-desktop:/etc/apt$ sudo apt show vim
Package: vim
Version: 2:8.1.2269-1ubuntu5.7
Priority: optional
Section: editors
Origin: Ubuntu
Maintainer: Ubuntu Developers <[email protected]>
Original-Maintainer: Debian Vim Maintainers <[email protected]>
Bugs: https://bugs.launchpad.net/ubuntu/+filebug
Installed-Size: 3,143 kB
Provides: editor
Depends: vim-common (= 2:8.1.2269-1ubuntu5.7), vim-runtime (= 2:8.1.2269-1ubuntu5.7), libacl1 (>= 2.2.23), libc6 (>= 2.29), libcanberra0 (>= 0.2), libgpm2 (>= 1.20.7), libpython3.8 (>= 3.8.2), libselinux1 (>= 1.32), libtinfo6 (>= 6)
Suggests: ctags, vim-doc, vim-scripts
Homepage: https://www.vim.org/
Task: server, cloud-image, lubuntu-desktop
Download-Size: 1,135 kB
APT-Manual-Installed: yes
APT-Sources: http://ports.ubuntu.com/ubuntu-ports focal-updates/main arm64 Packages
Description: Vi IMproved - enhanced vi editor
 Vim is an almost compatible version of the UNIX editor Vi.
 .
 Many new features have been added: multi level undo, syntax
 highlighting, command line history, on-line help, filename
 completion, block operations, folding, Unicode support, etc.
 .
 This package contains a version of vim compiled with a rather
 standard set of features.  This package does not provide a GUI
 version of Vim.  See the other vim-* packages if you need more
 (or less).

N: There is 1 additional record. Please use the '-a' switch to see it

示例:使用apt安装redis软件

1  首先执行命令 sudo apt update

parallels@ubuntu-linux-20-04-desktop:/etc/apt$ sudo apt update
Ign:1 https://repo.mongodb.org/apt/ubuntu xenial/mongodb-org/3.6 InRelease
Hit:2 http://ports.ubuntu.com/ubuntu-ports focal InRelease
Hit:3 https://repo.mongodb.org/apt/ubuntu xenial/mongodb-org/3.6 Release
Get:5 http://ports.ubuntu.com/ubuntu-ports focal-updates InRelease [114 kB]
Get:6 http://ports.ubuntu.com/ubuntu-ports focal-backports InRelease [108 kB]
Get:7 http://ports.ubuntu.com/ubuntu-ports focal-security InRelease [114 kB]                                                                                                
Get:8 http://ports.ubuntu.com/ubuntu-ports focal-updates/main arm64 Packages [1,203 kB]                                                                                     
Get:9 http://ports.ubuntu.com/ubuntu-ports focal-updates/main arm64 DEP-11 Metadata [278 kB]                                                                                
Get:10 http://ports.ubuntu.com/ubuntu-ports focal-updates/universe arm64 Packages [868 kB]                                                                                  
Get:11 http://ports.ubuntu.com/ubuntu-ports focal-updates/universe arm64 DEP-11 Metadata [389 kB]                                                                           
Get:12 http://ports.ubuntu.com/ubuntu-ports focal-backports/main arm64 DEP-11 Metadata [9,568 B]                                                                            
Get:13 http://ports.ubuntu.com/ubuntu-ports focal-backports/universe arm64 DEP-11 Metadata [30.8 kB]                                                                        
Get:14 http://ports.ubuntu.com/ubuntu-ports focal-security/main arm64 DEP-11 Metadata [40.7 kB]                                                                             
Get:15 http://ports.ubuntu.com/ubuntu-ports focal-security/universe arm64 DEP-11 Metadata [66.3 kB]                                                                         
Fetched 3,222 kB in 1min 3s (51.3 kB/s)                                                                                                                                     
Reading package lists... Done
Building dependency tree       
Reading state information... Done
62 packages can be upgraded. Run 'apt list --upgradable' to see them.
parallels@ubuntu-linux-20-04-desktop:/etc/apt$ sudo apt list --upgradable 
Listing... Done
alsa-ucm-conf/focal-updates 1.2.2-1ubuntu0.13 all [upgradable from: 1.2.2-1ubuntu0.11]
apport-gtk/focal-updates 2.20.11-0ubuntu27.23 all [upgradable from: 2.20.11-0ubuntu27.21]
...

2 查询redis软件:sudo apt search redis

parallels@ubuntu-linux-20-04-desktop:/etc/apt$ sudo apt search redis
Sorting... Done
Full Text Search... Done
babeld/focal 1.9.1-1 arm64
  loop-free distance-vector routing protocol

bird/focal 1.6.8-1 arm64
  Internet Routing Daemon

bird-bgp/focal 1.6.8-1 all
  Internet Routing Daemon [transitional package]

bird-doc/focal 1.6.8-1 all
  Internet Routing Daemon - documentation

bird2/focal 2.0.7-2 arm64
  Internet Routing Daemon

bird2-doc/focal 2.0.7-2 all
  Internet Routing Daemon - documentation

3 可以先查看下redis软件:sudo apt show redis

parallels@ubuntu-linux-20-04-desktop:/etc/apt$ sudo apt show redis
Package: redis
Version: 5:5.0.7-2ubuntu0.1
Priority: optional
Section: universe/database
Origin: Ubuntu
Maintainer: Ubuntu Developers <[email protected]>
Original-Maintainer: Chris Lamb <[email protected]>
Bugs: https://bugs.launchpad.net/ubuntu/+filebug
Installed-Size: 63.5 kB
Depends: redis-server (<< 5:5.0.7-2ubuntu0.1.1~), redis-server (>= 5:5.0.7-2ubuntu0.1)
Homepage: https://redis.io/
Download-Size: 3,064 B
APT-Sources: http://ports.ubuntu.com/ubuntu-ports focal-updates/universe arm64 Packages
Description: Persistent key-value database with network interface (metapackage)
 Redis is a key-value database in a similar vein to memcache but the dataset
 is non-volatile. Redis additionally provides native support for atomically
 manipulating and querying data structures such as lists and sets.
 .
 The dataset is stored entirely in memory and periodically flushed to disk.

N: There is 1 additional record. Please use the '-a' switch to see it

 4 安装软件:sudo apt install redis

parallels@ubuntu-linux-20-04-desktop:~$ sudo apt install redis
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following additional packages will be installed:
  libhiredis0.14 libjemalloc2 liblua5.1-0 lua-bitop lua-cjson redis-server redis-tools
Suggested packages:
  ruby-redis
The following NEW packages will be installed:
  libhiredis0.14 libjemalloc2 liblua5.1-0 lua-bitop lua-cjson redis redis-server redis-tools
0 upgraded, 8 newly installed, 0 to remove and 62 not upgraded.
Need to get 815 kB of archives.
After this operation, 3,804 kB of additional disk space will be used.
Do you want to continue? [Y/n] y
Get:1 http://ports.ubuntu.com/ubuntu-ports focal/universe arm64 libhiredis0.14 arm64 0.14.0-6 [28.2 kB]
Get:2 http://ports.ubuntu.com/ubuntu-ports focal/universe arm64 libjemalloc2 arm64 5.2.1-1ubuntu1 [172 kB]
Get:3 http://ports.ubuntu.com/ubuntu-ports focal/universe arm64 liblua5.1-0 arm64 5.1.5-8.1build4 [92.0 kB]
Get:4 http://ports.ubuntu.com/ubuntu-ports focal/universe arm64 lua-bitop arm64 1.0.2-5 [6,196 B]
Get:5 http://ports.ubuntu.com/ubuntu-ports focal/universe arm64 lua-cjson arm64 2.1.0+dfsg-2.1 [15.6 kB]                                                                    
Get:6 http://ports.ubuntu.com/ubuntu-ports focal-updates/universe arm64 redis-tools arm64 5:5.0.7-2ubuntu0.1 [461 kB]                                                       
Get:7 http://ports.ubuntu.com/ubuntu-ports focal-updates/universe arm64 redis-server arm64 5:5.0.7-2ubuntu0.1 [37.4 kB]                                                     
Get:8 http://ports.ubuntu.com/ubuntu-ports focal-updates/universe arm64 redis all 5:5.0.7-2ubuntu0.1 [3,064 B]                                                              
Fetched 815 kB in 11s (76.6 kB/s)                                                                                                                                           
Selecting previously unselected package libhiredis0.14:arm64.
(Reading database ... 185226 files and directories currently installed.)
Preparing to unpack .../0-libhiredis0.14_0.14.0-6_arm64.deb ...
Unpacking libhiredis0.14:arm64 (0.14.0-6) ...
Selecting previously unselected package libjemalloc2:arm64.
Preparing to unpack .../1-libjemalloc2_5.2.1-1ubuntu1_arm64.deb ...
Unpacking libjemalloc2:arm64 (5.2.1-1ubuntu1) ...
Selecting previously unselected package liblua5.1-0:arm64.
Preparing to unpack .../2-liblua5.1-0_5.1.5-8.1build4_arm64.deb ...
Unpacking liblua5.1-0:arm64 (5.1.5-8.1build4) ...
Selecting previously unselected package lua-bitop:arm64.
Preparing to unpack .../3-lua-bitop_1.0.2-5_arm64.deb ...
...

5 再次查看redis软件:sudo apt show redis

parallels@ubuntu-linux-20-04-desktop:~$ sudo apt show redis
Package: redis
Version: 5:5.0.7-2ubuntu0.1
Priority: optional
Section: universe/database
Origin: Ubuntu
Maintainer: Ubuntu Developers <[email protected]>
Original-Maintainer: Chris Lamb <[email protected]>
Bugs: https://bugs.launchpad.net/ubuntu/+filebug
Installed-Size: 63.5 kB
Depends: redis-server (<< 5:5.0.7-2ubuntu0.1.1~), redis-server (>= 5:5.0.7-2ubuntu0.1)
Homepage: https://redis.io/
Download-Size: 3,064 B
APT-Manual-Installed: yes
APT-Sources: http://ports.ubuntu.com/ubuntu-ports focal-updates/universe arm64 Packages
Description: Persistent key-value database with network interface (metapackage)
 Redis is a key-value database in a similar vein to memcache but the dataset
 is non-volatile. Redis additionally provides native support for atomically
 manipulating and querying data structures such as lists and sets.
 .
 The dataset is stored entirely in memory and periodically flushed to disk.

N: There is 1 additional record. Please use the '-a' switch to see it

6 此时redis服务会自动启动

parallels@ubuntu-linux-20-04-desktop:~$ ps -e | grep redis
 134624 ?        00:00:00 redis-server

7 测试

parallels@ubuntu-linux-20-04-desktop:~$ redis-cli
127.0.0.1:6379> ping
PONG
127.0.0.1:6379> 

×

猜你喜欢

转载自blog.csdn.net/daida2008/article/details/124641493
今日推荐