Linux apt 簡單使用教程
apt是一個Linux的軟件包管理工具。通過apt您可以很方便的 安裝、更新、卸載軟件。
apt和yum差不多,都可以算是Linux 上的一個應用商店。不過apt主要是在Debian和Ubuntu這一類 Debian系的Linux上使用,而yum主要是在CentOS和Fedora這一類RedHat系的Linux上使用。
安裝軟件
安裝軟件可以使用:
apt install 軟件名稱
下面安裝vim:
apt install -y vim
其中的-y就是無需手動確認安裝,如果不加-y的話,安裝每一個依賴程序都需要手動輸入y確認。
如果要一次安裝多個軟件可以這樣寫:
apt install zip rar
上面的zip和rar都是軟件名稱。
在默認情況下,apt安裝的軟件都是最新版本的。如果需要安裝老版本的軟件可以在軟件名稱后面加 =版本 ,如下:
apt install php=5.6
更新軟件
如果要查看可更新的軟件可以使用:
apt list --upgradeable
列表如下:
apt/oldstable,oldstable 1.4.9 amd64 [可從該版本升級:1.4.6]
apt-utils/oldstable,oldstable 1.4.9 amd64 [可從該版本升級:1.4.6]
base-files/oldstable 9.9+deb9u11 amd64 [可從該版本升級:9.9]
bind9-host/oldstable,oldstable 1:9.10.3.dfsg.P4-12.3+deb9u5 amd64 [可從該版本升級:1:9.10.3.dfsg.P4-12.3]
bsdutils/oldstable,oldstable 1:2.29.2-1+deb9u1 amd64 [可從該版本升級:1:2.29.2-1]
cron/oldstable 3.0pl1-128+deb9u1 amd64 [可從該版本升級:3.0pl1-128+b1]
dbus/oldstable,oldstable 1.10.28-0+deb9u1 amd64 [可從該版本升級:1.10.18-1]
debian-archive-keyring/oldstable,oldstable-updates 2017.5+deb9u1 all [可從該版本升級:2017.5]
discover/oldstable 2.1.2-7.1+deb9u1 amd64 [可從該版本升級:2.1.2-7.1]
dpkg/oldstable 1.18.25 amd64 [可從該版本升級:1.18.24]
e2fslibs/oldstable 1.43.4-2+deb9u1 amd64 [可從該版本升級:1.43.4-2]
e2fsprogs/oldstable 1.43.4-2+deb9u1 amd64 [可從該版本升級:1.43.4-2]
file/oldstable 1:5.30-1+deb9u3 amd64 [可從該版本升級:1:5.30-1]
gcc-6-base/oldstable,oldstable 6.3.0-18+deb9u1 amd64 [可從該版本升級:6.3.0-18]
gettext-base/oldstable 0.19.8.1-2+deb9u1 amd64 [可從該版本升級:0.19.8.1-2]
gnupg/oldstable 2.1.18-8~deb9u4 amd64 [可從該版本升級:2.1.18-6]
gnupg-agent/oldstable 2.1.18-8~deb9u4 amd64 [可從該版本升級:2.1.18-6]
gpgv/oldstable 2.1.18-8~deb9u4 amd64 [可從該版本升級:2.1.18-6]
如果需要更新所有已安裝的軟件可以使用:
apt upgradable
如果需要更新apt的軟件數據庫可以使用:
apt update
如果要更新單個軟件可以直接使用安裝命令,當apt檢測到軟件已安裝時就會更新軟件。
卸載軟件
刪除軟件可以使用:
apt remove 軟件名稱
如果要徹底卸載軟件可以使用:
apt purge 軟件名稱
apt remove只會刪除軟件本體,對於軟件的配置文件之類的是不會刪除的。apt purge可以直接刪除軟件和相關的配置文件。
查找軟件
搜索軟件可以使用:
apt search 軟件名稱
apt search會列出所有和搜索詞相關的軟件。
下面搜索vsftpd:
root@localhost:~# apt search vsftpd
正在排序... 完成
全文搜索... 完成
ccze/oldstable 0.2.1-3 amd64
robust, modular log coloriser
ftpd/oldstable 0.17-36 amd64
File Transfer Protocol (FTP) server
resource-agents/oldstable 1:4.0.0~rc1-4 amd64
Cluster Resource Agents
vsftpd/oldstable 3.0.3-8+b1 amd64
lightweight, efficient FTP server written for security
vsftpd-dbg/oldstable 3.0.3-8+b1 amd64
lightweight, efficient FTP server written for security (debug)
yasat/oldstable 848-1 all
simple stupid audit tool
要查看一個軟件的詳細信息可以使用:
apt show 軟件名稱
下面查看 vim 的詳細信息:
pi@raspberrypi:/ $ apt show vim
Package: vim
Version: 2:8.0.0197-4+deb9u3
Priority: optional
Section: editors
Maintainer: Debian Vim Maintainers <pkg-vim-maintainers@lists.alioth.debian.org>
Installed-Size: 1,925 kB
Provides: editor
Depends: vim-common (= 2:8.0.0197-4+deb9u3), vim-runtime (= 2:8.0.0197-4+deb9u3), libacl1 (>= 2.2.51-8), libc6 (>= 2.15), libgpm2 (>= 1.20.4), libselinux1 (>= 1.32), libtinfo5 (>= 6)
Suggests: ctags, vim-doc, vim-scripts
Homepage: http://www.vim.org/
Download-Size: 791 kB
APT-Manual-Installed: yes
APT-Sources: http://raspbian.raspberrypi.org/raspbian stretch/main armhf 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).
如果要查看已安裝的軟件可以使用:
apt list --installed
更改 Debian apt 鏡像源
Debian官方的apt鏡像源在國內使用速度可能會比較慢。國內的阿里、網易、清華大學 等 都提供Debian的apt鏡像源。
下面以網易為例,簡單寫一下Debian更改apt為國內鏡像源的方法。
首先需要查看 Debian 的版本,可以使用:
cat /etc/debian_version
查詢結果如下:
pi@raspberrypi:/ $ cat /etc/debian_version
9.13
pi@raspberrypi:/ $ cat /etc/os-release
PRETTY_NAME="Raspbian GNU/Linux 9 (stretch)"
NAME="Raspbian GNU/Linux"
VERSION_ID="9"
VERSION="9 (stretch)"
VERSION_CODENAME=stretch
ID=raspbian
ID_LIKE=debian
HOME_URL="http://www.raspbian.org/"
SUPPORT_URL="http://www.raspbian.org/RaspbianForums"
BUG_REPORT_URL="http://www.raspbian.org/RaspbianBugs"
注意其中的stretch,這是版本代號。
Debian的鏡像源配置文件在/etc/apt/sources.list,為防止出現意外可以先備份一下默認的配置文件:
cp /etc/apt/sources.list /etc/apt/sources.list-backup
刪除默認的配置文件:
rm /etc/apt/sources.list
進入網易的Debian鏡像下載頁面,下載和系統版本代號對應的配置文件。
這里以stretch版為例,把stretch版的配置文件下載到 /etc/apt目錄:
wget -P /etc/apt https://mirrors.163.com/.help/sources.list.stretch
下載完成后去除多余的后綴:
mv /etc/apt/sources.list.stretch /etc/apt/sources.list
上面只是以網易的鏡像源為例,不一定適用於其它鏡像源。有的鏡像源需要復制配置信息到默認配置文件中粘貼。
無論是網易還是其它的鏡像源,主要都是更改配置文件,只是更改的方式不一樣。
