20 Lệnh apt cần biết với tư cách là Quản trị viên hệ thống để quản lý các gói Ubuntu hoặc Debian
APT hoặc Advanced Package Tool là tiện ích Dòng lệnh để quản lý gói trong các bản phân phối dựa trên Debian/Ubuntu.
Nó là một trong những trình quản lý gói có sẵn cùng với apt-get nhưng cũng giống như các trình quản lý gói khác, nó cho phép bạn cài đặt, sửa đổi hoặc xóa các gói khác nhau mà không cần xây dựng chúng từ mã nguồn. Nó có thể thực hiện nhiều hành động hơn liên quan đến quản lý gói mà chúng tôi sẽ đề cập trong các phần tiếp theo.
Cú pháp chung của việc sử dụng lệnh apt như sau:
$ apt [COMMAND] [PACKAGE]
Hãy nhớ sử dụng sudo với apt, vì nhiều lệnh phụ của nó sẽ yêu cầu đặc quyền để thực hiện các thay đổi ở cấp hệ thống.
Ở đây chúng ta sẽ đề cập đến các lệnh phụ khác nhau mà bạn có thể sử dụng với apt và cách sử dụng chúng.
Mục lục
Cập nhật cơ sở dữ liệu gói
apt cần một kho lưu trữ các gói với cơ sở dữ liệu của tất cả các gói có sẵn có thể được cài đặt trên hệ thống Debian/Ubuntu. Cơ sở dữ liệu kho lưu trữ này phải được cập nhật trước khi bạn bắt đầu cài đặt gói vì apt nên biết thông tin chi tiết về gói, phiên bản, kiến trúc và vị trí từ xa của gói.
Việc cập nhật cơ sở dữ liệu kho rất dễ dàng và có thể được thực hiện với:
$ sudo apt update
Thí dụ:
[email protected]:~$ sudo apt update Get:1 http://security.ubuntu.com/ubuntu focal-security InRelease [114 kB] Hit:2 http://azure.archive.ubuntu.com/ubuntu focal InRelease Get:3 http://azure.archive.ubuntu.com/ubuntu focal-updates InRelease [114 kB] Get:4 http://azure.archive.ubuntu.com/ubuntu focal-backports InRelease [101 kB] Get:5 http://azure.archive.ubuntu.com/ubuntu focal-updates/main amd64 Packages [1086 kB] Get:6 http://azure.archive.ubuntu.com/ubuntu focal-updates/universe amd64 Packages [841 kB] Fetched 2255 kB in 1s (2677 kB/s) Reading package lists... Done Building dependency tree Reading state information... Done 14 packages can be upgraded. Run 'apt list --upgradable' to see them. [email protected]:~$
Nâng cấp các gói đã cài đặt
Các gói đã cài đặt trên hệ thống có thể được nâng cấp lên phiên bản mới nhất hiện có bằng cách sử dụng:
$ sudo apt upgrade
Với lệnh nâng cấp, các gói sẽ được cài đặt nếu cần để đáp ứng các phụ thuộc, nhưng các gói hiện có sẽ không bao giờ bị xóa. Nếu nâng cấp cho một gói yêu cầu gỡ bỏ gói đã cài đặt thì nâng cấp cho gói này không được thực hiện.
Thí dụ:
[email protected]:~$ sudo apt update Get:1 http://security.ubuntu.com/ubuntu focal-security InRelease [114 kB] Hit:2 http://azure.archive.ubuntu.com/ubuntu focal InRelease Get:3 http://azure.archive.ubuntu.com/ubuntu focal-updates InRelease [114 kB] Get:4 http://azure.archive.ubuntu.com/ubuntu focal-backports InRelease [101 kB] Get:5 http://azure.archive.ubuntu.com/ubuntu focal-updates/main amd64 Packages [1086 kB] Get:6 http://azure.archive.ubuntu.com/ubuntu focal-updates/universe amd64 Packages [841 kB] Fetched 2255 kB in 1s (2677 kB/s) Reading package lists... Done Building dependency tree Reading state information... Done 14 packages can be upgraded. Run 'apt list --upgradable' to see them. [email protected]:~$ sudo apt upgrade Reading package lists... Done Building dependency tree Reading state information... Done Calculating upgrade... Done The following packages will be upgraded: apt apt-utils libapt-pkg6.0 libpython3.8 libpython3.8-minimal libpython3.8-stdlib libuv1 linux-base linux-base-sgx open-vm-tools python3-distupgrade python3.8 python3.8-minimal ubuntu-release-upgrader-core 14 upgraded, 0 newly installed, 0 to remove and 0 not upgraded. Need to get 9485 kB of archives. After this operation, 603 kB of additional disk space will be used. Do you want to continue? [Y/n] Y Get:1 http://azure.archive.ubuntu.com/ubuntu focal-updates/main amd64 libapt-pkg6.0 amd64 2.0.6 [835 kB] Get:2 http://azure.archive.ubuntu.com/ubuntu focal-updates/main amd64 apt amd64 2.0.6 [1296 kB] Get:3 http://azure.archive.ubuntu.com/ubuntu focal-updates/main amd64 apt-utils amd64 2.0.6 [216 kB] Get:4 http://azure.archive.ubuntu.com/ubuntu focal-updates/main amd64 libpython3.8 amd64 3.8.10-0ubuntu1~20.04 [1625 kB] Get:5 http://azure.archive.ubuntu.com/ubuntu focal-updates/main amd64 python3.8 amd64 3.8.10-0ubuntu1~20.04 [387 kB] Get:6 http://azure.archive.ubuntu.com/ubuntu focal-updates/main amd64 libpython3.8-stdlib amd64 3.8.10-0ubuntu1~20.04 [1675 kB] Get:7 http://azure.archive.ubuntu.com/ubuntu focal-updates/main amd64 python3.8-minimal amd64 3.8.10-0ubuntu1~20.04 [1898 kB] Get:8 http://azure.archive.ubuntu.com/ubuntu focal-updates/main amd64 libpython3.8-minimal amd64 3.8.10-0ubuntu1~20.04 [717 kB] Get:9 http://azure.archive.ubuntu.com/ubuntu focal-updates/main amd64 libuv1 amd64 1.34.2-1ubuntu1.3 [80.8 kB] Get:10 http://azure.archive.ubuntu.com/ubuntu focal-updates/main amd64 ubuntu-release-upgrader-core all 1:20.04.35 [23.9 kB] Get:11 http://azure.archive.ubuntu.com/ubuntu focal-updates/main amd64 python3-distupgrade all 1:20.04.35 [104 kB] Get:12 http://azure.archive.ubuntu.com/ubuntu focal-updates/main amd64 linux-base all 4.5ubuntu3.6 [17.8 kB] Get:13 http://azure.archive.ubuntu.com/ubuntu focal-updates/main amd64 linux-base-sgx all 4.5ubuntu3.6 [4660 B] Get:14 http://azure.archive.ubuntu.com/ubuntu focal-updates/universe amd64 open-vm-tools amd64 2:11.2.5-2ubuntu1~ubuntu20.04.1 [604 kB] Fetched 9485 kB in 0s (54.4 MB/s) Preconfiguring packages ... (Reading database ... 59511 files and directories currently installed.) Preparing to unpack .../libapt-pkg6.0_2.0.6_amd64.deb ... Unpacking libapt-pkg6.0:amd64 (2.0.6) over (2.0.5) ... Setting up libapt-pkg6.0:amd64 (2.0.6) ... (Reading database ... 59511 files and directories currently installed.) Preparing to unpack .../archives/apt_2.0.6_amd64.deb ... Unpacking apt (2.0.6) over (2.0.5) ... Setting up apt (2.0.6) ... (Reading database ... 59511 files and directories currently installed.) Preparing to unpack .../00-apt-utils_2.0.6_amd64.deb ... Unpacking apt-utils (2.0.6) over (2.0.5) ... Preparing to unpack .../01-libpython3.8_3.8.10-0ubuntu1~20.04_amd64.deb ... Unpacking libpython3.8:amd64 (3.8.10-0ubuntu1~20.04) over (3.8.5-1~20.04.3) ... Preparing to unpack .../02-python3.8_3.8.10-0ubuntu1~20.04_amd64.deb ... Unpacking python3.8 (3.8.10-0ubuntu1~20.04) over (3.8.5-1~20.04.3) ... Preparing to unpack .../03-libpython3.8-stdlib_3.8.10-0ubuntu1~20.04_amd64.deb ... Unpacking libpython3.8-stdlib:amd64 (3.8.10-0ubuntu1~20.04) over (3.8.5-1~20.04.3) ... Preparing to unpack .../04-python3.8-minimal_3.8.10-0ubuntu1~20.04_amd64.deb ... Unpacking python3.8-minimal (3.8.10-0ubuntu1~20.04) over (3.8.5-1~20.04.3) ... Preparing to unpack .../05-libpython3.8-minimal_3.8.10-0ubuntu1~20.04_amd64.deb ... Unpacking libpython3.8-minimal:amd64 (3.8.10-0ubuntu1~20.04) over (3.8.5-1~20.04.3) ... Preparing to unpack .../06-libuv1_1.34.2-1ubuntu1.3_amd64.deb ... Unpacking libuv1:amd64 (1.34.2-1ubuntu1.3) over (1.34.2-1ubuntu1.1) ... Preparing to unpack .../07-ubuntu-release-upgrader-core_1%3a20.04.35_all.deb ... Unpacking ubuntu-release-upgrader-core (1:20.04.35) over (1:20.04.33) ... Preparing to unpack .../08-python3-distupgrade_1%3a20.04.35_all.deb ... Unpacking python3-distupgrade (1:20.04.35) over (1:20.04.33) ... Preparing to unpack .../09-linux-base_4.5ubuntu3.6_all.deb ... Unpacking linux-base (4.5ubuntu3.6) over (4.5ubuntu3.1) ... Preparing to unpack .../10-linux-base-sgx_4.5ubuntu3.6_all.deb ... Unpacking linux-base-sgx (4.5ubuntu3.6) over (4.5ubuntu3.1) ... Preparing to unpack .../11-open-vm-tools_2%3a11.2.5-2ubuntu1~ubuntu20.04.1_amd64.deb ... Unpacking open-vm-tools (2:11.2.5-2ubuntu1~ubuntu20.04.1) over (2:11.0.5-4) ... Setting up libpython3.8-minimal:amd64 (3.8.10-0ubuntu1~20.04) ... Setting up apt-utils (2.0.6) ... Setting up linux-base (4.5ubuntu3.6) ... Setting up python3-distupgrade (1:20.04.35) ... Setting up libuv1:amd64 (1.34.2-1ubuntu1.3) ... Setting up linux-base-sgx (4.5ubuntu3.6) ... Setting up ubuntu-release-upgrader-core (1:20.04.35) ... Setting up python3.8-minimal (3.8.10-0ubuntu1~20.04) ... Setting up open-vm-tools (2:11.2.5-2ubuntu1~ubuntu20.04.1) ... Installing new version of config file /etc/vmware-tools/tools.conf.example ... Installing new version of config file /etc/vmware-tools/vgauth.conf ... Setting up libpython3.8-stdlib:amd64 (3.8.10-0ubuntu1~20.04) ... Setting up python3.8 (3.8.10-0ubuntu1~20.04) ... Setting up libpython3.8:amd64 (3.8.10-0ubuntu1~20.04) ... Processing triggers for libc-bin (2.31-0ubuntu9.2) ... Processing triggers for systemd (245.4-4ubuntu3.7) ... Processing triggers for man-db (2.9.1-1) ... Processing triggers for mime-support (3.64ubuntu1) ... [email protected]:~$
Đảm bảo rằng bạn đã thực hiện sudo apt update trước khi nâng cấp các gói của mình để tránh các sự cố trong quá trình nâng cấp cũng như để đảm bảo bạn nhận được các gói mới nhất hiện có từ kho lưu trữ. Nếu muốn, bạn có thể chạy các lệnh này trong một dòng dưới dạng:
$ sudo apt update && sudo apt upgrade -y
Gói nâng cấp đầy đủ
apt cũng cung cấp lệnh nâng cấp đầy đủ. Nó khác với lệnh nâng cấp vì nó sẽ xóa các gói hiện được cài đặt nếu điều này là cần thiết để nâng cấp toàn bộ hệ thống. Vì vậy, hãy cẩn thận với lệnh này và nếu có thể, hãy sử dụng lệnh nâng cấp để thay thế.
$ sudo apt full-upgrade
Cài đặt gói đơn
Việc cài đặt gói mới có thể được thực hiện với apt bằng cách sử dụng cú pháp bên dưới:
$ sudo apt install [package-name]
Thí dụ:
[email protected]:~$ sudo apt install unzip Reading package lists... Done Building dependency tree Reading state information... Done Suggested packages: zip The following NEW packages will be installed: unzip 0 upgraded, 1 newly installed, 0 to remove and 14 not upgraded. Need to get 169 kB of archives. After this operation, 593 kB of additional disk space will be used. Get:1 http://azure.archive.ubuntu.com/ubuntu focal/main amd64 unzip amd64 6.0-25ubuntu1 [169 kB] Fetched 169 kB in 0s (4040 kB/s) Selecting previously unselected package unzip. (Reading database ... 59511 files and directories currently installed.) Preparing to unpack .../unzip_6.0-25ubuntu1_amd64.deb ... Unpacking unzip (6.0-25ubuntu1) ... Setting up unzip (6.0-25ubuntu1) ... Processing triggers for mime-support (3.64ubuntu1) ... Processing triggers for man-db (2.9.1-1) ... [email protected]:~$
Cài đặt nhiều gói
Bạn có thể cài đặt nhiều gói trên một hệ thống với apt. Nó tuân theo một cú pháp tương tự như trong ví dụ trước:
$ sudo apt install [package-name-1] [package-name-2] ... [package-name-n]
Thí dụ:
[email protected]:~$ sudo apt install zip unzip Reading package lists... Done Building dependency tree Reading state information... Done The following NEW packages will be installed: unzip zip 0 upgraded, 2 newly installed, 0 to remove and 0 not upgraded. Need to get 336 kB of archives. After this operation, 1231 kB of additional disk space will be used. Get:1 http://azure.archive.ubuntu.com/ubuntu focal/main amd64 unzip amd64 6.0-25ubuntu1 [169 kB] Get:2 http://azure.archive.ubuntu.com/ubuntu focal/main amd64 zip amd64 3.0-11build1 [167 kB] Fetched 336 kB in 0s (7749 kB/s) Selecting previously unselected package unzip. (Reading database ... 59527 files and directories currently installed.) Preparing to unpack .../unzip_6.0-25ubuntu1_amd64.deb ... Unpacking unzip (6.0-25ubuntu1) ... Selecting previously unselected package zip. Preparing to unpack .../zip_3.0-11build1_amd64.deb ... Unpacking zip (3.0-11build1) ... Setting up unzip (6.0-25ubuntu1) ... Setting up zip (3.0-11build1) ... Processing triggers for man-db (2.9.1-1) ... Processing triggers for mime-support (3.64ubuntu1) ... [email protected]:~$
Nếu một trong các gói được chỉ định đã được cài đặt trên hệ thống, apt sẽ tìm kiếm bản cập nhật trong kho lưu trữ và nếu có sẽ nâng cấp gói hoặc nếu không sẽ giữ nguyên phiên bản đã cài đặt.
Cài đặt phiên bản cụ thể
Thông thường, bạn cần cài đặt một phiên bản cụ thể của một gói, đặc biệt là trên các hệ thống doanh nghiệp. apt cho phép bạn cài đặt một phiên bản nhất định của gói giả sử nó có sẵn trong kho lưu trữ bằng cách sử dụng:
$ sudo apt install [package-name]=[version]
Thí dụ:
[email protected]:~$ sudo apt install unzip=6.0-25ubuntu1 Reading package lists... Done Building dependency tree Reading state information... Done The following NEW packages will be installed: unzip 0 upgraded, 1 newly installed, 0 to remove and 0 not upgraded. Need to get 169 kB of archives. After this operation, 593 kB of additional disk space will be used. Get:1 http://azure.archive.ubuntu.com/ubuntu focal/main amd64 unzip amd64 6.0-25ubuntu1 [169 kB] Fetched 169 kB in 0s (2767 kB/s) Selecting previously unselected package unzip. (Reading database ... 59541 files and directories currently installed.) Preparing to unpack .../unzip_6.0-25ubuntu1_amd64.deb ... Unpacking unzip (6.0-25ubuntu1) ... Setting up unzip (6.0-25ubuntu1) ... Processing triggers for mime-support (3.64ubuntu1) ... Processing triggers for man-db (2.9.1-1) ... [email protected]:~$
Loại bỏ một gói
Giống như cài đặt gói, apt có thể được sử dụng để xóa gói khỏi hệ thống bằng lệnh bên dưới:
$ sudo apt remove [package-name]
Thí dụ:
[email protected]:~$ sudo apt remove unzip Reading package lists... Done Building dependency tree Reading state information... Done The following packages will be REMOVED: unzip 0 upgraded, 0 newly installed, 1 to remove and 14 not upgraded. After this operation, 593 kB disk space will be freed. Do you want to continue? [Y/n] Y (Reading database ... 59529 files and directories currently installed.) Removing unzip (6.0-25ubuntu1) ... Processing triggers for man-db (2.9.1-1) ... Processing triggers for mime-support (3.64ubuntu1) ... [email protected]:~$
Thanh lọc một gói
Giống như xóa gói, xóa gói cũng thực hiện tác vụ tương tự nhưng trong khi với lệnh xóa apt chỉ xóa các tệp nhị phân của gói để lại các tệp cấu hình như hiện tại. Mặt khác, lệnh thanh lọc đảm bảo rằng mọi thứ liên quan đến gói bao gồm các tệp nhị phân và tệp cấu hình của nó sẽ bị xóa khỏi hệ thống. Để lại các tệp cấu hình cho phép bạn sử dụng lại các tệp cấu hình tương tự nếu bạn định cài đặt lại ứng dụng.
Để xóa một gói, hãy sử dụng:
$ sudo apt purge [package-name]
Thí dụ:
[email protected]:~$ sudo apt purge unzip Reading package lists... Done Building dependency tree Reading state information... Done The following packages will be REMOVED: unzip* 0 upgraded, 0 newly installed, 1 to remove and 14 not upgraded. After this operation, 593 kB disk space will be freed. Do you want to continue? [Y/n] Y (Reading database ... 59529 files and directories currently installed.) Removing unzip (6.0-25ubuntu1) ... Processing triggers for man-db (2.9.1-1) ... Processing triggers for mime-support (3.64ubuntu1) ... (Reading database ... 59511 files and directories currently installed.) Purging configuration files for unzip (6.0-25ubuntu1) ... [email protected]:~$
Tìm kiếm các gói
apt không chỉ có thể đóng vai trò là tiện ích để cài đặt và gỡ bỏ các gói. Nó cũng có thể tìm kiếm gói cần thiết trong kho lưu trữ bằng lệnh tìm kiếm như:
$ apt search [search-text]
Thí dụ:
[email protected]:~$ apt search netstat Sorting... Done Full Text Search... Done bwm-ng/focal 0.6.2-1 amd64 small and simple console-based bandwidth monitor gnome-nettool/focal 3.8.1-3 amd64 network information tool for GNOME golang-github-aelsabbahy-gonetstat-dev/focal 0.0~git20160428.0.edf89f7-3 all Netstat implementation in Go golang-github-mwitkow-go-conntrack-dev/focal 0.0~git20190716.2f06839-1 all Go middleware for net.Conn tracking libparse-netstat-perl/focal 0.14-1 all module to parse the output of the "netstat" command net-tools/focal 1.60+git20180626.aebd88e-1ubuntu1 amd64 NET-3 networking toolkit netstat-nat/focal 1.4.10-3build1 amd64 tool that display NAT connections nicstat/focal 1.95-1build1 amd64 print network traffic statistics qnetstatview/focal 1.5.6-1 amd64 Shows detailed listings of all TCP and UDP endpoints unhide/focal 20130526-4 amd64 Forensic tool to find hidden processes and ports whichman/focal 2.4-9 amd64 Fault tolerant search utilities: whichman, ftff, ftwhich [email protected]:~$
Xem nội dung gói
Để biết thông tin chi tiết về một gói, cho dù nó đã được cài đặt hay chưa được cài đặt, bạn có thể sử dụng lệnh show như dưới đây:
$ apt show [package-name]
Thí dụ:
[email protected]:~$ apt show firefox Package: firefox Version: 90.0+build1-0ubuntu0.20.04.1 Priority: optional Section: web Origin: Ubuntu Maintainer: Ubuntu Mozilla Team <[email protected]> Bugs: https://bugs.launchpad.net/ubuntu/+filebug Installed-Size: 226 MB Provides: gnome-www-browser, iceweasel, www-browser Depends: lsb-release, libatk1.0-0 (>= 1.12.4), libc6 (>= 2.30), libcairo-gobject2 (>= 1.10.0), libcairo2 (>= 1.10.0), libdbus-1-3 (>= 1.9.14), libdbus-glib-1-2 (>= 0.78), libfontconfig1 (>= 2.12.6), libfreetype6 (>= 2.10.1), libgcc-s1 (>= 3.3), libgdk-pixbuf2.0-0 (>= 2.22.0), libglib2.0-0 (>= 2.42), libgtk-3-0 (>= 3.14), libharfbuzz0b (>= 0.6.0), libpango-1.0-0 (>= 1.14.0), libpangocairo-1.0-0 (>= 1.14.0), libpangoft2-1.0-0 (>= 1.14.0), libstdc++6 (>= 9), libx11-6, libx11-xcb1 (>= 2:1.6.9), libxcb-shm0, libxcb1, libxcomposite1 (>= 1:0.4.5), libxcursor1 (>> 1.1.2), libxdamage1 (>= 1:1.1), libxext6, libxfixes3, libxi6, libxrender1, libxt6 Recommends: xul-ext-ubufox, libcanberra0, libdbusmenu-glib4, libdbusmenu-gtk3-4 Suggests: fonts-lyx Replaces: kubuntu-firefox-installer Task: ubuntu-desktop-minimal, ubuntu-desktop, kubuntu-desktop, kubuntu-full, xubuntu-desktop, lubuntu-desktop, ubuntustudio-desktop, ubuntukylin-desktop, ubuntu-mate-core, ubuntu-mate-desktop, ubuntu-budgie-desktop Xul-Appid: {ec8030f7-c20a-464f-9b0e-13a3a9e97384} Download-Size: 57.0 MB APT-Sources: http://azure.archive.ubuntu.com/ubuntu focal-updates/main amd64 Packages Description: Safe and easy web browser from Mozilla Firefox delivers safe, easy web browsing. A familiar user interface, enhanced security features including protection from online identity theft, and integrated search let you get the most out of the web. N: There is 1 additional record. Please use the '-a' switch to see it [email protected]:~$
Liệt kê các gói đã cài đặt
Chúng ta thường cần liệt kê các gói đã cài đặt trên một hệ thống cho các mục đích khác nhau. apt cho phép bạn lấy danh sách các gói đã cài đặt chỉ với:
$ apt list --installed
Liệt kê tất cả các gói
Để liệt kê tất cả các gói có sẵn cho hệ thống của bạn, hãy sử dụng lệnh list với từ khóa –all-versions như sau:
$ apt list --all-versions
Liệt kê các gói có thể nâng cấp
Các gói cần được cập nhật vì các phiên bản mới tiếp tục phát hành với các tính năng được cải tiến hoặc bổ sung và các bản sửa lỗi bao gồm các lỗ hổng bảo mật quan trọng. apt đảm bảo bạn có thể vá hệ thống Debian/Ubuntu của mình một cách dễ dàng bằng lệnh nâng cấp như đã đề xuất trước đó trong bài viết. Để liệt kê tất cả các bản nâng cấp có sẵn cho các gói đã cài đặt trên hệ thống của bạn, hãy chạy:
$ apt list --upgradable
Thí dụ:
[email protected]:~$ apt list --upgradable Listing... Done apt-utils/focal-updates 2.0.6 amd64 [upgradable from: 2.0.5] apt/focal-updates 2.0.6 amd64 [upgradable from: 2.0.5] libapt-pkg6.0/focal-updates 2.0.6 amd64 [upgradable from: 2.0.5] libpython3.8-minimal/focal-updates,focal-security 3.8.10-0ubuntu1~20.04 amd64 [upgradable from: 3.8.5-1~20.04.3] libpython3.8-stdlib/focal-updates,focal-security 3.8.10-0ubuntu1~20.04 amd64 [upgradable from: 3.8.5-1~20.04.3] libpython3.8/focal-updates,focal-security 3.8.10-0ubuntu1~20.04 amd64 [upgradable from: 3.8.5-1~20.04.3] libuv1/focal-updates,focal-security 1.34.2-1ubuntu1.3 amd64 [upgradable from: 1.34.2-1ubuntu1.1] linux-base-sgx/focal-updates 4.5ubuntu3.6 all [upgradable from: 4.5ubuntu3.1] linux-base/focal-updates 4.5ubuntu3.6 all [upgradable from: 4.5ubuntu3.1] open-vm-tools/focal-updates 2:11.2.5-2ubuntu1~ubuntu20.04.1 amd64 [upgradable from: 2:11.0.5-4] python3-distupgrade/focal-updates 1:20.04.35 all [upgradable from: 1:20.04.33] python3.8-minimal/focal-updates,focal-security 3.8.10-0ubuntu1~20.04 amd64 [upgradable from: 3.8.5-1~20.04.3] python3.8/focal-updates,focal-security 3.8.10-0ubuntu1~20.04 amd64 [upgradable from: 3.8.5-1~20.04.3] ubuntu-release-upgrader-core/focal-updates 1:20.04.35 all [upgradable from: 1:20.04.33] abhi[email protected]:~$
Làm sạch các gói không sử dụng
Với việc sử dụng thông thường, đôi khi hệ thống của bạn có thể cài đặt các gói không còn cần thiết. Điều này có thể xảy ra vì các gói này đã được cài đặt như một phần phụ thuộc của một gói khác đã bị xóa sau đó mặc dù các gói phụ thuộc vẫn còn tồn tại trên hệ thống. Để dọn dẹp các gói như vậy và giải phóng một số dung lượng đĩa, bạn có thể sử dụng:
$ sudo apt autoremove
Thí dụ:
[email protected]:~$ sudo apt autoremove Reading package lists... Done Building dependency tree Reading state information... Done 0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded. [email protected]:~$
Hệ thống của tôi không có bất kỳ gói nào còn sót lại, vì vậy đầu ra báo cáo không có thay đổi.
Kiểm tra gói phụ thuộc
Một gói có thể có một hoặc nhiều phần phụ thuộc phải được cài đặt trên hệ thống để gói hoạt động bình thường. apt đảm bảo các phụ thuộc bắt buộc được cài đặt như một phần của quá trình cài đặt gói. Nếu bạn muốn kiểm tra các phụ thuộc của gói, hãy sử dụng:
$ apt depends [package-name]
Thí dụ:
[email protected]:~$ apt depends zip zip Depends: libbz2-1.0 Depends: libc6 (>= 2.14) Recommends: unzip [email protected]:~$
Cài đặt lại gói
Một gói có thể bị hỏng hoặc bạn có thể cần phải cài đặt lại gói đó để đảm bảo tất cả các tệp gói theo đúng thứ tự. Để cài đặt lại gói mà không gỡ bỏ gói trước, bạn có thể sử dụng:
$ sudo apt reinstall [package-name]
Thí dụ:
[email protected]:~$ sudo apt reinstall unzip Reading package lists... Done Building dependency tree Reading state information... Done 0 upgraded, 0 newly installed, 1 reinstalled, 0 to remove and 0 not upgraded. Need to get 169 kB of archives. After this operation, 0 B of additional disk space will be used. Get:1 http://azure.archive.ubuntu.com/ubuntu focal/main amd64 unzip amd64 6.0-25ubuntu1 [169 kB] Fetched 169 kB in 0s (5695 kB/s) (Reading database ... 59559 files and directories currently installed.) Preparing to unpack .../unzip_6.0-25ubuntu1_amd64.deb ... Unpacking unzip (6.0-25ubuntu1) over (6.0-25ubuntu1) ... Setting up unzip (6.0-25ubuntu1) ... Processing triggers for mime-support (3.64ubuntu1) ... Processing triggers for man-db (2.9.1-1) ... [email protected]:~$
Tải xuống một gói
Các tệp gói có thể được tải xuống hệ thống tệp cục bộ mà không cần cài đặt chúng. Để tải xuống gói có apt, hãy sử dụng:
$ apt download [package-name]
Thí dụ:
[email protected]:~$ apt download unzip Get:1 http://azure.archive.ubuntu.com/ubuntu focal/main amd64 unzip amd64 6.0-25ubuntu1 [169 kB] Fetched 169 kB in 0s (1405 kB/s) [email protected]:~$ [email protected]:~$ ls -l unzip_6.0-25ubuntu1_amd64.deb -rw-r--r-- 1 abhisheknair abhisheknair 168956 Aug 16 2019 unzip_6.0-25ubuntu1_amd64.deb [email protected]:~$
Kiểm tra nhật ký thay đổi gói
Chúng tôi có thể dễ dàng kiểm tra thay đổi về bất kỳ gói nào với apt bằng cách sử dụng:
$ apt changelog [package-name]
Chỉnh sửa nguồn
Lệnh edit-sources của apt cho phép bạn chỉnh sửa các tệp tin sources.list đồng thời cung cấp các biện pháp kiểm tra độ chính xác để đảm bảo các thay đổi nhất quán và hợp lệ. Đây là một lệnh đang được tiến hành và do đó nên được sử dụng cẩn thận. Bạn có thể chỉnh sửa sources.list thành:
$ sudo apt edit-sources
Nhận trợ giúp về APT
Để nhận thêm trợ giúp về lệnh apt, hãy sử dụng:
$ apt help
Thí dụ:
[email protected]:~$ apt help apt 2.0.6 (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 reinstall - reinstall 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 satisfy - satisfy dependency strings 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. [email protected]:~$
Bản tóm tắt
công cụ apt là một tiện ích quản lý gói dựa trên CLI tiện dụng và mạnh mẽ cho các hệ thống dựa trên Debian/Ubuntu. Công việc hàng ngày thông thường của quản trị viên hệ thống liên quan đến việc quản lý các gói trên hệ thống và biết apt cũng như các lệnh sử dụng phổ biến của nó giúp công việc này trở nên dễ dàng.
Để được trợ giúp thêm về apt, hãy tham khảo trang hướng dẫn của nó.
$ man apt