Linux中國

如何在 CentOS 中添加、啟用和禁用一個倉庫

在基於 RPM 的系統上,例如 RHEL、CentOS 等,我們中的許多人使用 yum 包管理器來管理軟體的安裝、刪除、更新、搜索等。

Linux 發行版的大部分軟體都來自發行版官方倉庫。官方倉庫包含大量免費和開源的應用和軟體。它很容易安裝和使用。

由於一些限制和專有問題,基於 RPM 的發行版在其官方倉庫中沒有提供某些包。另外,出於穩定性考慮,它不會提供最新版本的核心包。

為了克服這種情況,我們需要安裝或啟用需要的第三方倉庫。對於基於 RPM 的系統,有許多第三方倉庫可用,但所建議使用的倉庫很少,因為這些不會替換大量的基礎包。

建議閱讀:

這可以在基於 RPM 的系統上完成,比如 RHEL, CentOS, OEL, Fedora 等。

  • Fedora 系統使用 dnf config-manager [options] [section …]
  • 其它基於 RPM 的系統使用 yum-config-manager [options] [section …]

如何列出啟用的倉庫

只需運行以下命令即可檢查系統上啟用的倉庫列表。

對於 CentOS/RHEL/OLE 系統:

# yum repolist
Loaded plugins: fastestmirror, security
Loading mirror speeds from cached hostfile
repo id                                                       repo name                                                                 status
base                                                          CentOS-6 - Base                                                           6,706
extras                                                        CentOS-6 - Extras                                                            53
updates                                                       CentOS-6 - Updates                                                        1,255
repolist: 8,014

對於 Fedora 系統:

# dnf repolist

如何在系統中添加一個新倉庫

每個倉庫通常都提供自己的 .repo 文件。要將此類倉庫添加到系統中,使用 root 用戶運行以下命令。在我們的例子中將添加 EPEL 倉庫 和 IUS 社區倉庫,見下文。

但是沒有 .repo 文件可用於這些倉庫。因此,我們使用以下方法進行安裝。

對於 EPEL 倉庫,因為它可以從 CentOS 額外倉庫獲得,所以運行以下命令來安裝它。

# yum install epel-release -y

對於 IUS 社區倉庫,運行以下 bash 腳本來安裝。

# curl 'https://setup.ius.io/' -o setup-ius.sh
# sh setup-ius.sh

如果你有 .repo 文件,在 RHEL/CentOS/OEL 中,只需運行以下命令來添加一個倉庫。

# yum-config-manager --add-repo http://www.example.com/example.repo

Loaded plugins: product-id, refresh-packagekit, subscription-manager
adding repo from: http://www.example.com/example.repo
grabbing file http://www.example.com/example.repo to /etc/yum.repos.d/example.repo
example.repo                                             |  413 B     00:00
repo saved to /etc/yum.repos.d/example.repo

對於 Fedora 系統,運行以下命令來添加一個倉庫:

# dnf config-manager --add-repo http://www.example.com/example.repo
adding repo from: http://www.example.com/example.repo

如果在添加這些倉庫之後運行 yum repolist 命令,你就可以看到新添加的倉庫了。Yes,我看到了。

注意:每當運行 yum repolist 命令時,該命令會自動從相應的倉庫獲取更新,並將緩存保存在本地系統中。

# yum repolist

Loaded plugins: fastestmirror, security
Loading mirror speeds from cached hostfile
epel/metalink                                                                                                          | 6.1 kB     00:00
* epel: epel.mirror.constant.com
* ius: ius.mirror.constant.com
ius                                                                                                                    | 2.3 kB     00:00
repo id                                     repo name                                                                                   status
base                                        CentOS-6 - Base                                                                              6,706
epel                                        Extra Packages for Enterprise Linux 6 - x86_64                                              12,505
extras                                      CentOS-6 - Extras                                                                               53
ius                                         IUS Community Packages for Enterprise Linux 6 - x86_64                                         390
updates                                     CentOS-6 - Updates                                                                           1,255
repolist: 20,909

每個倉庫都有多個渠道,比如測試(Testing)、開發(Dev)和存檔(Archive)等。通過導航到倉庫文件位置,你可以更好地理解這一點。

# ls -lh /etc/yum.repos.d
total 64K
-rw-r--r-- 1 root root 2.0K Apr 12 02:44 CentOS-Base.repo
-rw-r--r-- 1 root root 647 Apr 12 02:44 CentOS-Debuginfo.repo
-rw-r--r-- 1 root root 289 Apr 12 02:44 CentOS-fasttrack.repo
-rw-r--r-- 1 root root 630 Apr 12 02:44 CentOS-Media.repo
-rw-r--r-- 1 root root 916 May 18 11:07 CentOS-SCLo-scl.repo
-rw-r--r-- 1 root root 892 May 18 10:36 CentOS-SCLo-scl-rh.repo
-rw-r--r-- 1 root root 6.2K Apr 12 02:44 CentOS-Vault.repo
-rw-r--r-- 1 root root 7.9K Apr 12 02:44 CentOS-Vault.repo.rpmnew
-rw-r--r-- 1 root root 957 May 18 10:41 epel.repo
-rw-r--r-- 1 root root 1.1K Nov 4 2012 epel-testing.repo
-rw-r--r-- 1 root root 1.2K Feb 23 2017 ius-archive.repo
-rw-r--r-- 1 root root 1.2K Feb 23 2017 ius-dev.repo
-rw-r--r-- 1 root root 1.1K May 18 10:41 ius.repo
-rw-r--r-- 1 root root 1.2K Feb 23 2017 ius-testing.repo

如何在系統中啟用一個倉庫

當你在默認情況下添加一個新倉庫時,它將啟用它們的穩定倉庫,這就是為什麼我們在運行 yum repolist 命令時獲取了倉庫信息。在某些情況下,如果你希望啟用它們的測試、開發或存檔倉庫,使用以下命令。另外,我們還可以使用此命令啟用任何禁用的倉庫。

為了驗證這一點,我們將啟用 epel-testing.repo,運行下面的命令:

# yum-config-manager --enable epel-testing

Loaded plugins: fastestmirror
==================================================================================== repo: epel-testing =====================================================================================
[epel-testing]
bandwidth = 0
base_persistdir = /var/lib/yum/repos/x86_64/6
baseurl =
cache = 0
cachedir = /var/cache/yum/x86_64/6/epel-testing
cost = 1000
enabled = 1
enablegroups = True
exclude =
failovermethod = priority
ftp_disable_epsv = False
gpgcadir = /var/lib/yum/repos/x86_64/6/epel-testing/gpgcadir
gpgcakey =
gpgcheck = True
gpgdir = /var/lib/yum/repos/x86_64/6/epel-testing/gpgdir
gpgkey = file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-6
hdrdir = /var/cache/yum/x86_64/6/epel-testing/headers
http_caching = all
includepkgs =
keepalive = True
mdpolicy = group:primary
mediaid =
metadata_expire = 21600
metalink =
mirrorlist = https://mirrors.fedoraproject.org/metalink?repo=testing-epel6&arch=x86_64
mirrorlist_expire = 86400
name = Extra Packages for Enterprise Linux 6 - Testing - x86_64
old_base_cache_dir =
password =
persistdir = /var/lib/yum/repos/x86_64/6/epel-testing
pkgdir = /var/cache/yum/x86_64/6/epel-testing/packages
proxy = False
proxy_dict =
proxy_password =
proxy_username =
repo_gpgcheck = False
retries = 10
skip_if_unavailable = False
ssl_check_cert_permissions = True
sslcacert =
sslclientcert =
sslclientkey =
sslverify = True
throttle = 0
timeout = 30.0
username =

運行 yum repolist 命令來檢查是否啟用了 「epel-testing」。它被啟用了,我可以從列表中看到它。

# yum repolist
Loaded plugins: fastestmirror, security
Determining fastest mirrors
epel/metalink                                                                                                                                                         |  18 kB     00:00
epel-testing/metalink                                                                                                                                                 |  17 kB     00:00
 * epel: mirror.us.leaseweb.net
 * epel-testing: mirror.us.leaseweb.net
 * ius: mirror.team-cymru.com
base                                                                                                                                                                  | 3.7 kB     00:00
centos-sclo-sclo                                                                                                                                                      | 2.9 kB     00:00
epel                                                                                                                                                                  | 4.7 kB     00:00
epel/primary_db                                                                                                                                                       | 6.0 MB     00:00
epel-testing                                                                                                                                                          | 4.7 kB     00:00
epel-testing/primary_db                                                                                                                                               | 368 kB     00:00
extras                                                                                                                                                                | 3.4 kB     00:00
ius                                                                                                                                                                   | 2.3 kB     00:00
ius/primary_db                                                                                                                                                        | 216 kB     00:00
updates                                                                                                                                                               | 3.4 kB     00:00
updates/primary_db                                                                                                                                                    | 8.1 MB     00:00 ...
repo id                                                                repo name                                                                                                       status
base                                                                   CentOS-6 - Base                                                                                                  6,706
centos-sclo-sclo                                                       CentOS-6 - SCLo sclo                                                                                               495
epel                                                                   Extra Packages for Enterprise Linux 6 - x86_64                                                                  12,509
epel-testing                                                           Extra Packages for Enterprise Linux 6 - Testing - x86_64                                                           809
extras                                                                 CentOS-6 - Extras                                                                                                   53
ius                                                                    IUS Community Packages for Enterprise Linux 6 - x86_64                                                             390
updates                                                                CentOS-6 - Updates                                                                                               1,288
repolist: 22,250

如果你想同時啟用多個倉庫,使用以下格式。這個命令將啟用 epel、epel-testing 和 ius 倉庫:

# yum-config-manager --enable epel epel-testing ius

對於 Fedora 系統,運行下面的命令來啟用倉庫:

# dnf config-manager --set-enabled epel-testing

如何在系統中禁用一個倉庫

無論何時你在默認情況下添加一個新的倉庫,它都會啟用它們的穩定倉庫,這就是為什麼我們在運行 yum repolist 命令時獲取了倉庫信息。如果你不想使用倉庫,那麼可以通過下面的命令來禁用它。

為了驗證這點,我們將要禁用 epel-testing.repoius.repo,運行以下命令:

# yum-config-manager --disable epel-testing ius

Loaded plugins: fastestmirror
==================================================================================== repo: epel-testing =====================================================================================
[epel-testing]
bandwidth = 0
base_persistdir = /var/lib/yum/repos/x86_64/6
baseurl =
cache = 0
cachedir = /var/cache/yum/x86_64/6/epel-testing
cost = 1000
enabled = 0
enablegroups = True
exclude =
failovermethod = priority
ftp_disable_epsv = False
gpgcadir = /var/lib/yum/repos/x86_64/6/epel-testing/gpgcadir
gpgcakey =
gpgcheck = True
gpgdir = /var/lib/yum/repos/x86_64/6/epel-testing/gpgdir
gpgkey = file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-6
hdrdir = /var/cache/yum/x86_64/6/epel-testing/headers
http_caching = all
includepkgs =
keepalive = True
mdpolicy = group:primary
mediaid =
metadata_expire = 21600
metalink =
mirrorlist = https://mirrors.fedoraproject.org/metalink?repo=testing-epel6&arch=x86_64
mirrorlist_expire = 86400
name = Extra Packages for Enterprise Linux 6 - Testing - x86_64
old_base_cache_dir =
password =
persistdir = /var/lib/yum/repos/x86_64/6/epel-testing
pkgdir = /var/cache/yum/x86_64/6/epel-testing/packages
proxy = False
proxy_dict =
proxy_password =
proxy_username =
repo_gpgcheck = False
retries = 10
skip_if_unavailable = False
ssl_check_cert_permissions = True
sslcacert =
sslclientcert =
sslclientkey =
sslverify = True
throttle = 0
timeout = 30.0
username =

========================================================================================= repo: ius =========================================================================================
[ius]
bandwidth = 0
base_persistdir = /var/lib/yum/repos/x86_64/6
baseurl =
cache = 0
cachedir = /var/cache/yum/x86_64/6/ius
cost = 1000
enabled = 0
enablegroups = True
exclude =
failovermethod = priority
ftp_disable_epsv = False
gpgcadir = /var/lib/yum/repos/x86_64/6/ius/gpgcadir
gpgcakey =
gpgcheck = True
gpgdir = /var/lib/yum/repos/x86_64/6/ius/gpgdir
gpgkey = file:///etc/pki/rpm-gpg/IUS-COMMUNITY-GPG-KEY
hdrdir = /var/cache/yum/x86_64/6/ius/headers
http_caching = all
includepkgs =
keepalive = True
mdpolicy = group:primary
mediaid =
metadata_expire = 21600
metalink =
mirrorlist = https://mirrors.iuscommunity.org/mirrorlist?repo=ius-centos6&arch=x86_64&protocol=http
mirrorlist_expire = 86400
name = IUS Community Packages for Enterprise Linux 6 - x86_64
old_base_cache_dir =
password =
persistdir = /var/lib/yum/repos/x86_64/6/ius
pkgdir = /var/cache/yum/x86_64/6/ius/packages
proxy = False
proxy_dict =
proxy_password =
proxy_username =
repo_gpgcheck = False
retries = 10
skip_if_unavailable = False
ssl_check_cert_permissions = True
sslcacert =
sslclientcert =
sslclientkey =
sslverify = True
throttle = 0
timeout = 30.0
username =

運行 yum repolist 命令檢查 「epel-testing」 和 「ius」 倉庫是否被禁用。它被禁用了,我不能看到那些倉庫,除了 「epel」。

# yum repolist
Loaded plugins: fastestmirror, security
Loading mirror speeds from cached hostfile
 * epel: mirror.us.leaseweb.net
repo id                                                                     repo name                                                                                                  status
base                                                                        CentOS-6 - Base                                                                                             6,706
centos-sclo-sclo                                                            CentOS-6 - SCLo sclo                                                                                          495
epel                                        Extra Packages for Enterprise Linux 6 - x86_64                                              12,505
extras                                                                      CentOS-6 - Extras                                                                                              53
updates                                                                     CentOS-6 - Updates                                                                                          1,288
repolist: 21,051

或者,我們可以運行以下命令查看詳細信息:

# yum repolist all | grep "epel*|ius*"
 * epel: mirror.steadfast.net
epel                       Extra Packages for Enterprise Linux 6 enabled: 12,509
epel-debuginfo             Extra Packages for Enterprise Linux 6 disabled
epel-source                Extra Packages for Enterprise Linux 6 disabled
epel-testing               Extra Packages for Enterprise Linux 6 disabled
epel-testing-debuginfo     Extra Packages for Enterprise Linux 6 disabled
epel-testing-source        Extra Packages for Enterprise Linux 6 disabled
ius                        IUS Community Packages for Enterprise disabled
ius-archive                IUS Community Packages for Enterprise disabled
ius-archive-debuginfo      IUS Community Packages for Enterprise disabled
ius-archive-source         IUS Community Packages for Enterprise disabled
ius-debuginfo              IUS Community Packages for Enterprise disabled
ius-dev                    IUS Community Packages for Enterprise disabled
ius-dev-debuginfo          IUS Community Packages for Enterprise disabled
ius-dev-source             IUS Community Packages for Enterprise disabled
ius-source                 IUS Community Packages for Enterprise disabled
ius-testing                IUS Community Packages for Enterprise disabled
ius-testing-debuginfo      IUS Community Packages for Enterprise disabled
ius-testing-source         IUS Community Packages for Enterprise disabled

對於 Fedora 系統,運行以下命令來啟用一個倉庫:

# dnf config-manager --set-disabled epel-testing

或者,可以通過手動編輯適當的 repo 文件來完成。為此,打開相應的 repo 文件並將值從 enabled=0 改為 enabled=1(啟用倉庫)或從 enabled=1 變為 enabled=0(禁用倉庫)。

即從:

[epel]
name=Extra Packages for Enterprise Linux 6 - $basearch
#baseurl=http://download.fedoraproject.org/pub/epel/6/$basearch
mirrorlist=https://mirrors.fedoraproject.org/metalink?repo=epel-6&arch=$basearch
failovermethod=priority
enabled=0
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-6

改為:

[epel]
name=Extra Packages for Enterprise Linux 6 - $basearch
#baseurl=http://download.fedoraproject.org/pub/epel/6/$basearch
mirrorlist=https://mirrors.fedoraproject.org/metalink?repo=epel-6&arch=$basearch
failovermethod=priority
enabled=1
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-6

via: https://www.2daygeek.com/how-to-add-enable-disable-a-repository-dnf-yum-config-manager-on-linux/

作者:Prakash Subramanian 選題:lujun9972 譯者:MjSeven 校對:wxy

本文由 LCTT 原創編譯,Linux中國 榮譽推出


本文轉載來自 Linux 中國: https://github.com/Linux-CN/archive

對這篇文章感覺如何?

太棒了
0
不錯
0
愛死了
0
不太好
0
感覺很糟
0
雨落清風。心向陽

    You may also like

    Leave a reply

    您的電子郵箱地址不會被公開。 必填項已用 * 標註

    此站點使用Akismet來減少垃圾評論。了解我們如何處理您的評論數據

    More in:Linux中國