Các thao tác cơ bản với yum Image Các thao tác cơ bản với yum

Bài viết này giới thiệu với các bạn về thao tác cơ bản với yum trên hệ điều hành Linux qua các cách sử dụng lệnh yum dưới đây giúp cho các bạn có thể tự tìm hiểu và tự học Linux cơ bản dễ dàng hơn.

1. Giới thiệu về lệnh yum

Lệnh yum (Yellowdog Updater, Modified) trình quản lý gói dựa trên RPM, được sử dụng để cài đặt, cập nhật, gỡ bỏ hoặc tìm kiếm các gói phần mềm trong các bản phân phối Linux khác nhau bao gồm CentOS, RHEL và Fedora. yum sử dụng nhiều kho lưu trữ của bên thứ ba để cài đặt các gói tự động bằng cách giải quyết các vấn đề phụ thuộc của chúng.

2. Sử dụng lệnh yum

2.1. Cài đặt gói bằng yum install

Để cài đặt gói chúng ta sử dụng lệnh yum install packagename. Điều này sẽ xác định các phụ thuộc tự động và cài đặt chúng.

Ví dụ sau đây cài đặt gói httpd.

[root@test1 ~]# yum install httpd
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.123host.vn
 * extras: mirrors.123host.vn
 * updates: mirrors.123host.vn
base                                                                                                                | 3.6 kB  00:00:00
extras                                                                                                              | 3.4 kB  00:00:00
updates                                                                                                             | 3.4 kB  00:00:00
Resolving Dependencies
--> Running transaction check
---> Package httpd.x86_64 0:2.4.6-89.el7.centos will be installed
--> Finished Dependency Resolution

Dependencies Resolved

===========================================================================================================================================
 Package                     Arch                         Version                                      Repository                     Size
===========================================================================================================================================
Installing:
 httpd                       x86_64                       2.4.6-89.el7.centos                          updates                       2.7 M

Transaction Summary
===========================================================================================================================================
Install  1 Package

Total download size: 2.7 M
Installed size: 9.4 M
Is this ok [y/d/N]: y
Downloading packages:
httpd-2.4.6-89.el7.centos.x86_64.rpm                                                                                | 2.7 MB  00:00:02
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : httpd-2.4.6-89.el7.centos.x86_64                                                                                        1/1
  Verifying  : httpd-2.4.6-89.el7.centos.x86_64                                                                                        1/1

Installed:
  httpd.x86_64 0:2.4.6-89.el7.centos

Complete!

Theo mặc định yum install, sẽ nhắc chúng ta chấp nhận hoặc từ chối trước khi cài đặt các gói. Nếu bạn muốn yum cài đặt tự động mà không yum hỏi xác nhận lại, hãy sử dụng tùy chọn -y như sau:

yum -y install packagename

2.2. Gỡ cài đặt gói bằng yum remove

Để xóa gói (cùng với tất cả các phụ thuộc của gói), sử dụng yum remove như bên dưới:

[root@test1 ~]# yum remove httpd
Loaded plugins: fastestmirror
Resolving Dependencies
--> Running transaction check
---> Package httpd.x86_64 0:2.4.6-89.el7.centos will be erased
--> Finished Dependency Resolution

Dependencies Resolved

===========================================================================================================================================
 Package                     Arch                         Version                                     Repository                      Size
===========================================================================================================================================
Removing:
 httpd                       x86_64                       2.4.6-89.el7.centos                         @updates                       9.4 M

Transaction Summary
===========================================================================================================================================
Remove  1 Package

Installed size: 9.4 M
Is this ok [y/N]: y
Downloading packages:
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Erasing    : httpd-2.4.6-89.el7.centos.x86_64                                                                                        1/1
  Verifying  : httpd-2.4.6-89.el7.centos.x86_64                                                                                        1/1

Removed:
  httpd.x86_64 0:2.4.6-89.el7.centos

Complete!

2.3. Cập nhật gói bằng yum remove

Giả sử bạn đã cài phiên bản cũ của một gói và bạn muốn cập nhật lên phiên bản ổn định mới nhất. Chỉ cần chạy lệnh sau, nó sẽ tự động giải quyết tất cả các vấn đề phụ thuộc và cài đặt chúng.

[root@test1 ~]# yum update httpd

2.4. Liệt kê gói bằng yum list

Sử dụng yum list để tìm kiếm gói cụ thể có tên.

Ví dụ: Để tìm kiếm một gói openssh, sử dụng lệnh.

[root@test1 ~]# yum list openssh
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.123host.vn
 * extras: mirrors.123host.vn
 * updates: mirrors.123host.vn
Installed Packages
openssh.x86_64                                                      7.4p1-16.el7                                                      @base

2.5. Tìm kiếm gói bằng yum seach

Nếu bạn không biết chính xác tên gói cần cài đặt, hãy sử dụng yum seach, sẽ giúp chúng ta tìm kiếm tất cả các gói khớp với từ khóa chúng ta cần tìm và hiển thị nó.

👉 Ví dụ: Tìm kiếm kho yum cho tất cả các gói phù hợp với từ khóa firefox và liệt kê các gói có sẵn.

[root@test1 ~]# yum search firefox
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.123host.vn
 * extras: mirrors.123host.vn
 * updates: mirrors.123host.vn
=========================================================== N/S matched: firefo ===========================================================
firefox.i686 : Mozilla Firefox Web browser
firefox.x86_64 : Mozilla Firefox Web browser

  Name and summary matches only, use "search all" for everything.

2.6. Nhận thông tin về gói yum

Nếu chúng ta muốn biết thông tin của gói trước khi cài đặt. Để có được thông tin của một gói chỉ cần chạy lệnh sau:

[root@test1 ~]# yum info firefox
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.123host.vn
 * extras: mirrors.123host.vn
 * updates: mirrors.123host.vn
Installed Packages
Name        : firefox
Arch        : x86_64
Version     : 60.6.1
Release     : 1.el7.centos
Size        : 207 M
Repo        : installed
From repo   : updates
Summary     : Mozilla Firefox Web browser
URL         : https://www.mozilla.org/firefox/
License     : MPLv1.1 or GPLv2+ or LGPLv2+
Description : Mozilla Firefox is an open-source web browser, designed for standards
            : compliance, performance and portability.

Available Packages
Name        : firefox
Arch        : i686
Version     : 60.6.1
Release     : 1.el7.centos
Size        : 93 M
Repo        : updates/7/x86_64
Summary     : Mozilla Firefox Web browser
URL         : https://www.mozilla.org/firefox/
License     : MPLv1.1 or GPLv2+ or LGPLv2+
Description : Mozilla Firefox is an open-source web browser, designed for standards
            : compliance, performance and portability.

2.7. Liệt kê tất cả các gói có sẵn bằng yum

Lệnh sau sẽ liệt kê tất cả các gói có sẵn trong cơ sở dữ liệu yum:

[root@test1 ~]# yum list | less

2.8. Liệt kê tất cả các gói đã cài đặt bằng yum

Để xem tất cả các gói được cài đặt trên hệ thống của bạn, thực hiện như sau:

[root@test1 ~]# yum list installed | less

2.9. Tập tin thuộc về gói nào? - Sử dụng yum provides

Yum provides được sử dụng để tìm gói nào thuộc về một tệp cụ thể.

👉 Ví dụ: Nếu bạn muốn biết tên của gói có /etc/httpd/conf/httpd.conf.

[root@test1 ~]# yum provides /etc/httpd/conf/httpd.conf
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.123host.vn
 * extras: mirrors.123host.vn
 * updates: mirrors.123host.vn
httpd-2.4.6-88.el7.centos.x86_64 : Apache HTTP Server
Repo        : base
Matched from:
Filename    : /etc/httpd/conf/httpd.conf



httpd-2.4.6-89.el7.centos.x86_64 : Apache HTTP Server
Repo        : updates
Matched from:
Filename    : /etc/httpd/conf/httpd.conf



httpd-2.4.6-89.el7.centos.x86_64 : Apache HTTP Server
Repo        : @updates
Matched from:
Filename    : /etc/httpd/conf/httpd.conf

2.10. Kiểm tra cập nhật có sẵn bằng yum

Để kiểm tra xem có bao nhiêu gói đã cài đặt trên hệ thống của bạn có sẵn các bản cập nhật, chúng ta chạy lệnh sau:

[root@test1 ~]# yum check-update

2.11. Cập nhật hệ thống bằng yum

Để cập nhật hệ thống của bạn tất cả các gói có bản cập nhật mới chúng ta chạy lệnh sau. Nó sẽ cài đặt tất cả các bản vá mới nhất và cập nhật bảo mật cho hệ thống của bạn.

[root@test1 ~]# yum update

2.12. Liệt kê các nhóm phần mềm có sẵn bằng cách sử dụng yum grouplist

Đối với lệnh yum một số gói liên quan được nhóm lại với nhau trong một nhóm cụ thể. Thay vì tìm kiếm và cài đặt tất cả các gói riêng lẻ thuộc về một chức năng cụ thể, bạn chỉ cần cài đặt nhóm, sẽ cài đặt tất cả các gói thuộc về nhóm.

Để xem tất cả các nhóm phần mềm có sẵn chúng ta chạy lệnh sau yum grouplist. Thực hiện như bên dưới:

👉 Ví dụ: Để liệt kê tất cả các nhóm có sẵn, chỉ cần chạy ra lệnh sau.

[root@test1 ~]# yum grouplist
Loaded plugins: fastestmirror
There is no installed groups file.
Maybe run: yum groups mark convert (see man yum)
Loading mirror speeds from cached hostfile
 * base: mirrors.123host.vn
 * extras: mirrors.123host.vn
 * updates: mirrors.123host.vn
Available Environment Groups:
   Minimal Install
   Compute Node
   Infrastructure Server
   File and Print Server
   Basic Web Server
   Virtualization Host
   Server with GUI
   GNOME Desktop
   KDE Plasma Workspaces
   Development and Creative Workstation
Available Groups:
   Compatibility Libraries
   Console Internet Tools
   Development Tools
   Graphical Administration Tools
   Legacy UNIX Compatibility
   Scientific Support
   Security Tools
   Smart Card Support
   System Administration Tools
   System Management
Done

2.13. Cài đặt gói nhóm

Cài đặt một nhóm phần mềm cụ thể bằng cách sử dụng yum groupinstall.

👉 Ví dụ: Chúng ta cần cài nhóm DNS Name Server.

[root@test1 ~]# yum groupinstall 'MySQL Database'
Loaded plugins: fastestmirror
There is no installed groups file.
Maybe run: yum groups mark convert (see man yum)
Loading mirror speeds from cached hostfile
 * base: mirrors.123host.vn
 * extras: mirrors.123host.vn
 * updates: mirrors.123host.vn
Warning: group MySQL Database does not exist.
Maybe run: yum groups mark install (see man yum)
Error: No packages in any requested group available to install or update
[root@test1 ~]# yum groupinstall 'DNS Name Server'
Loaded plugins: fastestmirror
There is no installed groups file.
Maybe run: yum groups mark convert (see man yum)
Loading mirror speeds from cached hostfile
 * base: mirrors.123host.vn
 * extras: mirrors.123host.vn
 * updates: mirrors.123host.vn
Resolving Dependencies
--> Running transaction check
---> Package bind.x86_64 32:9.9.4-73.el7_6 will be installed
--> Processing Dependency: python-ply for package: 32:bind-9.9.4-73.el7_6.x86_64
---> Package bind-chroot.x86_64 32:9.9.4-73.el7_6 will be installed
--> Running transaction check
---> Package python-ply.noarch 0:3.4-11.el7 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

===========================================================================================================================================
 Package                          Arch                        Version                                   Repository                    Size
===========================================================================================================================================
Installing for group install "DNS Name Server":
 bind                             x86_64                      32:9.9.4-73.el7_6                         updates                      1.8 M
 bind-chroot                      x86_64                      32:9.9.4-73.el7_6                         updates                       88 k
Installing for dependencies:
 python-ply                       noarch                      3.4-11.el7                                base                         123 k

Transaction Summary
===========================================================================================================================================
Install  2 Packages (+1 Dependent package)

Total download size: 2.0 M
Installed size: 5.1 M
Is this ok [y/d/N]: y
Downloading packages:
(1/3): bind-chroot-9.9.4-73.el7_6.x86_64.rpm                                                                        |  88 kB  00:00:00
(2/3): python-ply-3.4-11.el7.noarch.rpm                                                                             | 123 kB  00:00:00
(3/3): bind-9.9.4-73.el7_6.x86_64.rpm                                                                               | 1.8 MB  00:00:00
-------------------------------------------------------------------------------------------------------------------------------------------
Total                                                                                                      2.7 MB/s | 2.0 MB  00:00:00
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : python-ply-3.4-11.el7.noarch                                                                                            1/3
  Installing : 32:bind-9.9.4-73.el7_6.x86_64                                                                                           2/3
  Installing : 32:bind-chroot-9.9.4-73.el7_6.x86_64                                                                                    3/3
OSError: No such file or directory
ValueError: SELinux policy is not managed or store cannot be accessed.
warning: %posttrans(bind-32:9.9.4-73.el7_6.x86_64) scriptlet failed, exit status 1
Non-fatal POSTTRANS scriptlet failure in rpm package 32:bind-9.9.4-73.el7_6.x86_64
  Verifying  : 32:bind-chroot-9.9.4-73.el7_6.x86_64                                                                                    1/3
  Verifying  : python-ply-3.4-11.el7.noarch                                                                                            2/3
  Verifying  : 32:bind-9.9.4-73.el7_6.x86_64                                                                                           3/3

Installed:
  bind.x86_64 32:9.9.4-73.el7_6                                    bind-chroot.x86_64 32:9.9.4-73.el7_6

Dependency Installed:
  python-ply.noarch 0:3.4-11.el7

Complete!

2.14. Xóa gói nhóm

Để xóa bất kỳ nhóm cài đặt hiện có khỏi hệ thống, chúng ta thực hiện như sau:

[root@test1 ~]# yum groupremove 'DNS Name Server'
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.123host.vn
 * extras: mirrors.123host.vn
 * updates: mirrors.123host.vn
No environment named DNS Name Server exists
Resolving Dependencies
--> Running transaction check
---> Package bind.x86_64 32:9.9.4-73.el7_6 will be erased
---> Package bind-chroot.x86_64 32:9.9.4-73.el7_6 will be erased
--> Finished Dependency Resolution

Dependencies Resolved

===========================================================================================================================================
 Package                          Arch                        Version                                  Repository                     Size
===========================================================================================================================================
Removing:
 bind                             x86_64                      32:9.9.4-73.el7_6                        @updates                      4.5 M
 bind-chroot                      x86_64                      32:9.9.4-73.el7_6                        @updates                      3.5 k

Transaction Summary
===========================================================================================================================================
Remove  2 Packages

Installed size: 4.5 M
Is this ok [y/N]: y
Downloading packages:
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Erasing    : 32:bind-chroot-9.9.4-73.el7_6.x86_64                                                                                    1/2
  Erasing    : 32:bind-9.9.4-73.el7_6.x86_64                                                                                           2/2
ValueError: SELinux policy is not managed or store cannot be accessed.
warning: %postun(bind-32:9.9.4-73.el7_6.x86_64) scriptlet failed, exit status 1
Non-fatal POSTUN scriptlet failure in rpm package 32:bind-9.9.4-73.el7_6.x86_64
  Verifying  : 32:bind-chroot-9.9.4-73.el7_6.x86_64                                                                                    1/2
  Verifying  : 32:bind-9.9.4-73.el7_6.x86_64                                                                                           2/2

Removed:
  bind.x86_64 32:9.9.4-73.el7_6                                    bind-chroot.x86_64 32:9.9.4-73.el7_6

Complete!

2.15. Cập nhật gói nhóm

Để cập nhật bất kỳ gói nhóm được cài đặt, chúng ta chạy lệnh bên dưới:

[root@test1 ~]# yum groupupdate 'DNS Name Server'

2.16. Danh sách kho yum được kích hoạt

Để liệt kê tất cả các kho yum được kích hoạt trong hệ thống của bạn, thực hiện như sau:

[root@test1 ~]# yum repolist
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.123host.vn
 * extras: mirrors.123host.vn
 * updates: mirrors.123host.vn
repo id                                                          repo name                                                           status
base/7/x86_64                                                    CentOS-7 - Base                                                     10,019
extras/7/x86_64                                                  CentOS-7 - Extras                                                      387
updates/7/x86_64                                                 CentOS-7 - Updates                                                   1,840
repolist: 12,246

2.17. Liệt kê tất cả các kho yum được kích hoạt và vô hiệu hóa

Lệnh sau sẽ hiển thị tất cả các kho yum được kích hoạt và vô hiệu hóa trên hệ thống của bạn:

[root@test1 ~]# yum repolist all
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.123host.vn
 * extras: mirrors.123host.vn
 * updates: mirrors.123host.vn
repo id                                                      repo name                                                      status
C7.0.1406-base/x86_64                                        CentOS-7.0.1406 - Base                                         disabled
C7.0.1406-centosplus/x86_64                                  CentOS-7.0.1406 - CentOSPlus                                   disabled
C7.0.1406-extras/x86_64                                      CentOS-7.0.1406 - Extras                                       disabled
C7.0.1406-fasttrack/x86_64                                   CentOS-7.0.1406 - Fasttrack                                    disabled
C7.0.1406-updates/x86_64                                     CentOS-7.0.1406 - Updates                                      disabled
C7.1.1503-base/x86_64                                        CentOS-7.1.1503 - Base                                         disabled
C7.1.1503-centosplus/x86_64                                  CentOS-7.1.1503 - CentOSPlus                                   disabled
C7.1.1503-extras/x86_64                                      CentOS-7.1.1503 - Extras                                       disabled
C7.1.1503-fasttrack/x86_64                                   CentOS-7.1.1503 - Fasttrack                                    disabled
C7.1.1503-updates/x86_64                                     CentOS-7.1.1503 - Updates                                      disabled
C7.2.1511-base/x86_64                                        CentOS-7.2.1511 - Base                                         disabled
C7.2.1511-centosplus/x86_64                                  CentOS-7.2.1511 - CentOSPlus                                   disabled
C7.2.1511-extras/x86_64                                      CentOS-7.2.1511 - Extras                                       disabled
C7.2.1511-fasttrack/x86_64                                   CentOS-7.2.1511 - Fasttrack                                    disabled
C7.2.1511-updates/x86_64                                     CentOS-7.2.1511 - Updates                                      disabled
...

2.18. Cài đặt gói từ Kho lưu trữ cụ thể

Theo mặc định, lệnh yum chỉ cài đặt từ kho được kích hoạt. Vì một số lý do bạn muốn cài đặt một gói từ kho lưu trữ bị vô hiệu hóa, bạn có thể sử dụng tùy chọn enenablerepo trong yum install như sau.

Ví dụ: Để cài đặt gói PhpMyAdmin 3.5.2 thực hiện như sau:

[root@test1 ~]# yum --enablerepo=base install phpmyadmin

2.19. Thực thi các lệnh yum tương tác bằng Yum Shell

Lệnh yum cung cấp shell tương tác để chạy nhiều lệnh như bên dưới:

[root@test1 ~]# yum shell
Loaded plugins: fastestmirror
> info samba.x86_64
Loading mirror speeds from cached hostfile
 * base: mirrors.123host.vn
 * extras: mirrors.123host.vn
 * updates: mirrors.123host.vn
Available Packages
Name        : samba
Arch        : x86_64
Version     : 4.8.3
Release     : 4.el7
Size        : 680 k
Repo        : base/7/x86_64
Summary     : Server and Client software to interoperate with Windows machines
URL         : http://www.samba.org/
License     : GPLv3+ and LGPLv3+
Description : Samba is the standard Windows interoperability suite of programs for Linux and
            : Unix.

> update httpd
...

2.20. Clean bộ nhớ cache bằng yum

Để xóa tất cả các tệp được lưu trong bộ nhớ cache được kích hoạt, bạn cần chạy lệnh sau để dọn sạch tất cả bộ đệm. Chúng ta thực hiện như bên dưới:

[root@test1 ~]# yum clean all
Loaded plugins: fastestmirror
Cleaning repos: base extras updates
Cleaning up list of fastest mirrors

2.21. Xem Lịch sử của Yum

Để xem tất cả các lệnh yum trong quá khứ, chúng ta chạy lệnh sau.

[root@test1 ~]# yum history list
Loaded plugins: fastestmirror
ID     | Command line             | Date and time    | Action(s)      | Altered
-------------------------------------------------------------------------------
    27 | groupremove DNS Name Ser | 2019-05-02 23:42 | Erase          |    2 EE
    26 | groupinstall DNS Name Se | 2019-05-02 23:38 | Install        |    3 EE
    25 | update                   | 2019-05-02 23:27 | I, U           |   18
    24 | install httpd            | 2019-05-02 21:45 | Install        |    1
    23 | remove httpd             | 2019-05-01 09:15 | Erase          |    1
    22 | install firefox          | 2019-05-01 09:14 | Install        |   84
    21 | install httpd            | 2019-05-01 09:10 | Install        |    5
    20 | install bind-utils       | 2019-04-15 14:54 | Install        |    2
    19 | -y update                | 2019-04-11 21:35 | Update         |    6
    18 | install lsof             | 2019-04-07 12:45 | Install        |    1
    17 | install traceroute -y    | 2019-04-06 15:22 | Install        |    1
    16 | install git              | 2019-04-05 20:30 | Install        |    4
    15 | -y install mkisofs       | 2019-03-29 11:16 | Install        |    2
    14 | install psmisc           | 2019-03-28 23:17 | Install        |    1
    13 | install udisks2          | 2019-03-28 22:18 | Install        |   22
    12 | -y install mc            | 2019-03-28 00:30 | Install        |   29
    11 | install rsync            | 2019-03-23 23:22 | Install        |    1
    10 | update                   | 2019-03-22 22:27 | I, O, U        |  167 EE
     9 | install at               | 2019-03-21 21:20 | Install        |    1
     8 | install acpid            | 2018-05-29 16:51 | Install        |    1
history list

2.22. Tải xuống tệp RPM gói

Thay vì cài đặt gói từ kho lưu trữ như đã trình bày trước đây, chúng tôi có thể tùy chọn tải xuống tệp .rpm từ kho lưu trữ bằng lệnh yumdownloader.

Lệnh yumdownloader xuất phát từ gói yum-utils, vì vậy chúng tôi cài đặt gói này trước. Thực hiện như sau:

[root@test1 ~]# yum install yum-utils -y
Loaded plugins: fastestmirror
Determining fastest mirrors
 * base: mirrors.123host.vn
 * extras: mirrors.123host.vn
 * updates: mirrors.123host.vn
base                                                                                                                | 3.6 kB  00:00:00
extras                                                                                                              | 3.4 kB  00:00:00
updates                                                                                                             | 3.4 kB  00:00:00
(1/4): base/7/x86_64/group_gz                                                                                       | 166 kB  00:00:00
(2/4): extras/7/x86_64/primary_db                                                                                   | 201 kB  00:00:00
(3/4): base/7/x86_64/primary_db                                                                                     | 6.0 MB  00:00:02
(4/4): updates/7/x86_64/primary_db                                                                                  | 4.2 MB  00:00:02
Resolving Dependencies
--> Running transaction check
---> Package yum-utils.noarch 0:1.1.31-50.el7 will be installed
--> Processing Dependency: python-kitchen for package: yum-utils-1.1.31-50.el7.noarch
--> Processing Dependency: libxml2-python for package: yum-utils-1.1.31-50.el7.noarch
--> Running transaction check
---> Package libxml2-python.x86_64 0:2.9.1-6.el7_2.3 will be installed
---> Package python-kitchen.noarch 0:1.1.1-5.el7 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

===========================================================================================================================================
 Package                              Arch                         Version                                Repository                  Size
===========================================================================================================================================
Installing:
 yum-utils                            noarch                       1.1.31-50.el7                          base                       121 k
Installing for dependencies:
 libxml2-python                       x86_64                       2.9.1-6.el7_2.3                        base                       247 k
 python-kitchen                       noarch                       1.1.1-5.el7                            base                       267 k

Transaction Summary
===========================================================================================================================================
Install  1 Package (+2 Dependent packages)

Total download size: 634 k
Installed size: 3.2 M
Downloading packages:
(1/3): yum-utils-1.1.31-50.el7.noarch.rpm                                                                           | 121 kB  00:00:00
(2/3): python-kitchen-1.1.1-5.el7.noarch.rpm                                                                        | 267 kB  00:00:00
(3/3): libxml2-python-2.9.1-6.el7_2.3.x86_64.rpm                                                                    | 247 kB  00:00:00
-------------------------------------------------------------------------------------------------------------------------------------------
Total                                                                                                      1.0 MB/s | 634 kB  00:00:00
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : python-kitchen-1.1.1-5.el7.noarch                                                                                       1/3
  Installing : libxml2-python-2.9.1-6.el7_2.3.x86_64                                                                                   2/3
  Installing : yum-utils-1.1.31-50.el7.noarch                                                                                          3/3
  Verifying  : libxml2-python-2.9.1-6.el7_2.3.x86_64                                                                                   1/3
  Verifying  : python-kitchen-1.1.1-5.el7.noarch                                                                                       2/3
  Verifying  : yum-utils-1.1.31-50.el7.noarch                                                                                          3/3

Installed:
  yum-utils.noarch 0:1.1.31-50.el7

Dependency Installed:
  libxml2-python.x86_64 0:2.9.1-6.el7_2.3                                python-kitchen.noarch 0:1.1.1-5.el7

Complete!

Sau khi cài đặt yum-util chúng ta có thể tải tệp RPM về hệ thống. Chạy lệnh sau:

[root@test1 ~]# yumdownloader unbound
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.123host.vn
 * extras: mirrors.123host.vn
 * updates: mirrors.123host.vn
unbound-1.6.6-1.el7.x86_64.rpm                                                                                      | 673 kB  00:00:00
[root@test1 ~]# ls -la unbound-1.6.6-1.el7.x86_64.rpm
-rw-r--r-- 1 root root 689232 Apr 25  2018 unbound-1.6.6-1.el7.x86_64.rpm

2.23. Cài đặt tệp RPM cục bộ

Các tệp .rpm có thể được cài đặt bằng lệnh rpm, nhưng sử dụng yum có lợi thế là tự động phát hiện và cài đặt bất kỳ phụ thuộc gói nào có thể được yêu cầu. Chúng ta sẽ cài đặt tệp .rpm mà chúng ta vừa tải xuống bằng lệnh yumdownloader. Chúng ta chạy lệnh bên dưới:

[root@test1 ~]# yum localinstall unbound-1.6.6-1.el7.x86_64.rpm
Loaded plugins: fastestmirror
Examining unbound-1.6.6-1.el7.x86_64.rpm: unbound-1.6.6-1.el7.x86_64
Marking unbound-1.6.6-1.el7.x86_64.rpm to be installed
Resolving Dependencies
--> Running transaction check
---> Package unbound.x86_64 0:1.6.6-1.el7 will be installed
--> Processing Dependency: unbound-libs(x86-64) = 1.6.6-1.el7 for package: unbound-1.6.6-1.el7.x86_64
Loading mirror speeds from cached hostfile
 * base: mirrors.123host.vn
 * extras: mirrors.123host.vn
 * updates: mirrors.123host.vn
--> Processing Dependency: libevent-2.0.so.5()(64bit) for package: unbound-1.6.6-1.el7.x86_64
--> Processing Dependency: libunbound.so.2()(64bit) for package: unbound-1.6.6-1.el7.x86_64
--> Running transaction check
---> Package libevent.x86_64 0:2.0.21-4.el7 will be installed
---> Package unbound-libs.x86_64 0:1.6.6-1.el7 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

===========================================================================================================================================
 Package                       Arch                    Version                          Repository                                    Size
===========================================================================================================================================
Installing:
 unbound                       x86_64                  1.6.6-1.el7                      /unbound-1.6.6-1.el7.x86_64                  2.4 M
Installing for dependencies:
 libevent                      x86_64                  2.0.21-4.el7                     base                                         214 k
 unbound-libs                  x86_64                  1.6.6-1.el7                      base                                         405 k

Transaction Summary
===========================================================================================================================================
Install  1 Package (+2 Dependent packages)

Total size: 3.0 M
Total download size: 619 k
Installed size: 4.2 M
Is this ok [y/d/N]: y
Downloading packages:
(1/2): unbound-libs-1.6.6-1.el7.x86_64.rpm                                                                          | 405 kB  00:00:00
(2/2): libevent-2.0.21-4.el7.x86_64.rpm                                                                             | 214 kB  00:00:00
-------------------------------------------------------------------------------------------------------------------------------------------
Total                                                                                                      1.2 MB/s | 619 kB  00:00:00
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : libevent-2.0.21-4.el7.x86_64                                                                                            1/3
  Installing : unbound-libs-1.6.6-1.el7.x86_64                                                                                         2/3
  Installing : unbound-1.6.6-1.el7.x86_64                                                                                              3/3
  Verifying  : libevent-2.0.21-4.el7.x86_64                                                                                            1/3
  Verifying  : unbound-1.6.6-1.el7.x86_64                                                                                              2/3
  Verifying  : unbound-libs-1.6.6-1.el7.x86_64                                                                                         3/3

Installed:
  unbound.x86_64 0:1.6.6-1.el7

Dependency Installed:
  libevent.x86_64 0:2.0.21-4.el7                                     unbound-libs.x86_64 0:1.6.6-1.el7

Complete!

2.24. Thêm kho lưu trữ mới

Chúng ta có thể chỉ định kho lưu trữ mới bằng cách chỉnh sửa thủ công hoặc tạo tệp .repo mới trong thư mục /etc/yum.repos.d, chúng ta có thể tạo kho lưu trữ liên tục hơn bằng lệnh yum-config-manager. Lệnh này xuất phát từ gói yum-utils mà chúng ta đã cài đặt trước đó, vì vậy nếu bạn muốn sử dụng yum-config-manager, trước tiên phải cài đặt yum-utils.

[root@test1 ~]# yum-config-manager --add-repo="https://mirror.aarnet.edu.au/pub/centos/7"
Loaded plugins: fastestmirror
adding repo from: https://mirror.aarnet.edu.au/pub/centos/7

[mirror.aarnet.edu.au_pub_centos_7]
name=added from: https://mirror.aarnet.edu.au/pub/centos/7
baseurl=https://mirror.aarnet.edu.au/pub/centos/7
enabled=1

Chúng ta chỉ cung cấp một URL kho lưu trữ đã biết trong tùy chọn –add-repo và tạo ra một tệp .repo dựa trên tên của URL kho lưu trữ được chỉ định. Trong ví dụ trên thì /etc/yum.repos.d/mirror.aarnet.edu.au_pub_centos_7.repo đã được tạo với nội dung được chỉ định ở đầu ra của lệnh. Bây giờ chúng ta có thể cài đặt hoặc cập nhật các gói từ kho lưu trữ mới này.

3. Tệp /etc/yum.conf

Tệp cấu hình /etc/yum.conf chứa một phần [main] một số key-value trong phần này ảnh hưởng đến cách yum vận hành và các phần khác ảnh hưởng đến cách yum xử lý các kho lưu trữ. Bạn có thể thêm nhiều tùy chọn bổ sung dưới [main] trong /etc/yum.conf.

Một tệp cấu hình /etc/yum.conf:

[root@test1 ~]# cat /etc/yum.conf
[main]
cachedir=/var/cache/yum/$basearch/$releasever
keepcache=0 
debuglevel=2
logfile=/var/log/yum.log 
exactarch=1
obsoletes=1 
gpgcheck=1
plugins=1
installonly_limit=5
bugtracker_url=http://bugs.centos.org/set_project.php?project_id=23&ref=http://bugs.centos.org/bug_report_page.php?category=yum
distroverpkg=centos-release


#  This is the default, if you make this bigger yum won't see if the metadata
# is newer on the remote and so you'll "gain" the bandwidth of not having to
# download the new metadata and "pay" for it by yum not having correct
# information.
#  It is esp. important, to have correct metadata, for distributions like
# Fedora which don't keep old packages around. If you don't like this checking
# interupting your command line usage, it's much better to have something
# manually check the metadata once an hour (yum-updatesd will do this).
# metadata_expire=90m

# PUT YOUR REPOS HERE OR IN separate files named file.repo
# in /etc/yum.repos.d

Các tùy chọn được sử dụng phổ biến nhất trong [main]:

1️. cachedir= Thư mục

Thư mục là một đường dẫn tuyệt đối đến thư mục nơi mà yum sẽ lưu trữ các bộ nhớ cache và cơ sở dữ liệu của nó. Theo mặc định, thư mục bộ nhớ cache của yum là /var/cache/yum/$basearch/$releasever.

2️. keepcache= Giá trị

  • 0: Không giữ lại bộ nhớ cache của các tiêu đề và gói sau khi cài đặt thành công.
  • 1: Giữ lại bộ nhớ cache sau khi cài đặt thành công.

3️. debuglevel= Giá trị

Giá trị là một số nguyên nằm giữa 1 và 10. Đặt debuglevel giá trị cao thì yum hiển thị đầu ra gỡ lỗi chi tiết hơn. debuglevel=0 vô hiệu hóa đầu ra gỡ lỗi, theo mặt định thì debuglevel=2.

4️. logfile= file_name

file_name là một đường dẫn tuyệt đối tệp yum sẽ ghi đầu ra ghi nhật ký của nó. Theo mặc định, file_name là /var/log/yum.log.

5️. exactarch= Giá trị

  • 0: Không tính kiến ​​trúc khi cập nhật các gói.
  • 1: Xem xét kiến ​​trúc chính xác khi cập nhật các gói.

6️. obsoletes= Giá trị

  • 0: Vô hiệu hóa yumlogic xử lý lỗi khi thực hiện cập nhật.
  • 1: Kích hoạt yumlogic xử lý lỗi khi thực hiện cập nhật.

7️. gpgcheck= Giá trị

  • 0: Vô hiệu hóa kiểm tra chữ ký GPG trên các gói trong tất cả các kho, bao gồm cả cài đặt gói cục bộ.
  • 1: Cho phép kiểm tra chữ ký GPG trên tất cả các gói trong tất cả các kho, bao gồm cả cài đặt gói cục bộ. Theo mặc định thì gpgcheck=1

8️. plugins= Giá trị

  • 0: Vô hiệu hóa tất cả các plugin yum.
  • 1: Kích hoạt tất cả các plugin yum.

9️. installonly_limit= Giá trị

Giá trị là một số nguyên biểu thị số lượng phiên bản tối đa có thể được cài đặt đồng thời cho bất kỳ gói nào được liệt kê trong lệnh installonlypkgs.

Mặc định lệnh installonlypkgs bao gồm một số gói kernel khác nhau, do đó việc thay đổi giá trị installonly_limit sẽ ảnh hưởng đến số lượng phiên bản cài đặt tối đa của bất kỳ gói kernel nào.

1️0️. assumeyes= Giá trị

  • 0: yum sẽ nhắc xác nhận các hành động quan trọng mà nó thực hiện. Giá trị 0 là mặc định của assumeyes.
  • 1: yum sẽ không nhắc xác nhận các hành động quan trọng . Nếu assumeyes có giá trị bằng 1 thì lệnh yum sẽ tự động mà không hỏi xác nhận lại giống như khi chúng ta sử dụng tùy chọn -y trong lệnh yum.

1️1️. exclude= Package_name [ more_package_names ]

exclude cho phép chúng ta loại trừ các gói theo từ khóa trong quá trình cài đặt hoặc cập nhật. Khi chúng ta muốn liệt kê nhiều gói loại trừ chúng ta sẽ trích dẫn danh sách các gói được phân tách bằng dấu cách.

1️2️. multilib_policy= Giá trị

  • best: Lựa chọn gói cài đặt tốt nhất cho hệ thống.

    Ví dụ: multilib_policy=best trên hệ thống 64 bit thì yum cài đặt tất cả các gói là phiên bản 64 bit.

  • all: Cài đặt mọi gói cho hệ thống.

    Ví dụ: multilib_policy=all trên hệ thống 64 bit thì yum có thể cài đặt cả hai phiên bản 32 bit và 64 bit của gói.

1️3️. retries= Giá trị

Giá trị là một số nguyên bằng 0 hoặc lớn hơn. Giá trị này là số lần yum sẽ truy xuất tệp trước khi trả về lỗi. Nếu giá trị này bằng 0 thì yum thử lại mãi mãi. Thường thì giá trị mặc định là 10.

4. Tệp /etc/yum.repos.d

Bạn có thể xác định kho lưu trữ yum bằng cách thêm phần [repository] (repository là ID kho lưu trữ duy nhất) vào /etc/yum.conf hoặc vào .repo trong các tệp /etc/yum.repos.d/file. Tất cả .repo trong /etc/yum.repos.d/ được đọc bởi yum. Bạn có thể tạo các .repo mới.

Một ví dụ về tệp .repo:

[repository_ID]
name=A Repository Name
baseurl=http://path/to/repo or ftp://path/to/repo or file://path/to/local/repo

Mỗi phần [repository] phải chứa các phần sau:

  • [repository_ID]: Là một chuỗi ký tự (không có dấu cách; dấu gạch dưới được sử dụng) đóng vai trò là định danh kho lưu trữ.
  • Name = My Repository Name: Là một chuỗi con người có thể đọc được mô tả kho lưu trữ.
  • baseurl=http://path/to/repo or ftp://path/to/repo or file://path/to/local/repo: Đây là một URL đến thư mục chứa thư mục repodata của kho lưu trữ.

Thông thường URL này là một liên kết HTTP, ví dụ như:

    baseurl = http: http://mirror.cisp.com/CentOS/7/os/x86_64/releases/$releasever/server/$basearch/os/

yum mở rộng các biến $releasever, $arch và $basearch trong URL.

Một tùy chọn khác trong [repository]:

enabled= Giá trị

  • 0: Tắt repo.
  • 1: Mở repo.

👉 Ví dụ: Một tệp /etc/yum.repos.d/CentOS-Debuginfo.repo

[root@test1 ~]# cat /etc/yum.repos.d/CentOS-Debuginfo.repo
# CentOS-Debug.repo
#
# The mirror system uses the connecting IP address of the client and the
# update status of each mirror to pick mirrors that are updated to and
# geographically close to the client.  You should use this for CentOS updates
# unless you are manually picking other mirrors.
#

# All debug packages from all the various CentOS-7 releases
# are merged into a single repo, split by BaseArch
#
# Note: packages in the debuginfo repo are currently not signed
#

[base-debuginfo]
name=CentOS-7 - Debuginfo
baseurl=http://debuginfo.centos.org/7/$basearch/
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-Debug-7
enabled=0
#

5. Lời kết

Qua bài trên, giúp cho chúng ta biết cách cài đặt, cập nhật, gỡ bỏ hoặc tìm kiếm các gói phần mềm bằng lệnh yum trên hệ điều hành Linux thuộc họ RHEL như RHEL, CentOS, Fedora, CloudLinux, Oracle Linux, Amazon Linux. Đồng thời cũng giúp chúng ta dễ dàng hơn khi làm quen DNF (Dandified YUM) - phiên bản kế tiếp của yum được sử dụng mặc định trên phiên bản RHEL/CentOS 8 trở đi.