如何在 CentOS 中新增、啟用和禁用一個倉庫

2018-11-10 22:14:00

在基於 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 repolistLoaded plugins: fastestmirror, securityLoading mirror speeds from cached hostfilerepo id                                                       repo name                                                                 statusbase                                                          CentOS-6 - Base                                                           6,706extras                                                        CentOS-6 - Extras                                                            53updates                                                       CentOS-6 - Updates                                                        1,255repolist: 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.repoLoaded plugins: product-id, refresh-packagekit, subscription-manageradding repo from: http://www.example.com/example.repograbbing file http://www.example.com/example.repo to /etc/yum.repos.d/example.repoexample.repo                                             |  413 B     00:00repo saved to /etc/yum.repos.d/example.repo

對於 Fedora 系統,執行以下命令來新增一個倉庫:

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

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

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

# yum repolistLoaded plugins: fastestmirror, securityLoading mirror speeds from cached hostfileepel/metalink                                                                                                          | 6.1 kB     00:00* epel: epel.mirror.constant.com* ius: ius.mirror.constant.comius                                                                                                                    | 2.3 kB     00:00repo id                                     repo name                                                                                   statusbase                                        CentOS-6 - Base                                                                              6,706epel                                        Extra Packages for Enterprise Linux 6 - x86_64                                              12,505extras                                      CentOS-6 - Extras                                                                               53ius                                         IUS Community Packages for Enterprise Linux 6 - x86_64                                         390updates                                     CentOS-6 - Updates                                                                           1,255repolist: 20,909

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

# ls -lh /etc/yum.repos.dtotal 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-testingLoaded plugins: fastestmirror==================================================================================== repo: epel-testing =====================================================================================[epel-testing]bandwidth = 0base_persistdir = /var/lib/yum/repos/x86_64/6baseurl =cache = 0cachedir = /var/cache/yum/x86_64/6/epel-testingcost = 1000enabled = 1enablegroups = Trueexclude =failovermethod = priorityftp_disable_epsv = Falsegpgcadir = /var/lib/yum/repos/x86_64/6/epel-testing/gpgcadirgpgcakey =gpgcheck = Truegpgdir = /var/lib/yum/repos/x86_64/6/epel-testing/gpgdirgpgkey = file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-6hdrdir = /var/cache/yum/x86_64/6/epel-testing/headershttp_caching = allincludepkgs =keepalive = Truemdpolicy = group:primarymediaid =metadata_expire = 21600metalink =mirrorlist = https://mirrors.fedoraproject.org/metalink?repo=testing-epel6&arch=x86_64mirrorlist_expire = 86400name = Extra Packages for Enterprise Linux 6 - Testing - x86_64old_base_cache_dir =password =persistdir = /var/lib/yum/repos/x86_64/6/epel-testingpkgdir = /var/cache/yum/x86_64/6/epel-testing/packagesproxy = Falseproxy_dict =proxy_password =proxy_username =repo_gpgcheck = Falseretries = 10skip_if_unavailable = Falsessl_check_cert_permissions = Truesslcacert =sslclientcert =sslclientkey =sslverify = Truethrottle = 0timeout = 30.0username =

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

# yum repolistLoaded plugins: fastestmirror, securityDetermining fastest mirrorsepel/metalink                                                                                                                                                         |  18 kB     00:00epel-testing/metalink                                                                                                                                                 |  17 kB     00:00 * epel: mirror.us.leaseweb.net * epel-testing: mirror.us.leaseweb.net * ius: mirror.team-cymru.combase                                                                                                                                                                  | 3.7 kB     00:00centos-sclo-sclo                                                                                                                                                      | 2.9 kB     00:00epel                                                                                                                                                                  | 4.7 kB     00:00epel/primary_db                                                                                                                                                       | 6.0 MB     00:00epel-testing                                                                                                                                                          | 4.7 kB     00:00epel-testing/primary_db                                                                                                                                               | 368 kB     00:00extras                                                                                                                                                                | 3.4 kB     00:00ius                                                                                                                                                                   | 2.3 kB     00:00ius/primary_db                                                                                                                                                        | 216 kB     00:00updates                                                                                                                                                               | 3.4 kB     00:00updates/primary_db                                                                                                                                                    | 8.1 MB     00:00 ...repo id                                                                repo name                                                                                                       statusbase                                                                   CentOS-6 - Base                                                                                                  6,706centos-sclo-sclo                                                       CentOS-6 - SCLo sclo                                                                                               495epel                                                                   Extra Packages for Enterprise Linux 6 - x86_64                                                                  12,509epel-testing                                                           Extra Packages for Enterprise Linux 6 - Testing - x86_64                                                           809extras                                                                 CentOS-6 - Extras                                                                                                   53ius                                                                    IUS Community Packages for Enterprise Linux 6 - x86_64                                                             390updates                                                                CentOS-6 - Updates                                                                                               1,288repolist: 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 iusLoaded plugins: fastestmirror==================================================================================== repo: epel-testing =====================================================================================[epel-testing]bandwidth = 0base_persistdir = /var/lib/yum/repos/x86_64/6baseurl =cache = 0cachedir = /var/cache/yum/x86_64/6/epel-testingcost = 1000enabled = 0enablegroups = Trueexclude =failovermethod = priorityftp_disable_epsv = Falsegpgcadir = /var/lib/yum/repos/x86_64/6/epel-testing/gpgcadirgpgcakey =gpgcheck = Truegpgdir = /var/lib/yum/repos/x86_64/6/epel-testing/gpgdirgpgkey = file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-6hdrdir = /var/cache/yum/x86_64/6/epel-testing/headershttp_caching = allincludepkgs =keepalive = Truemdpolicy = group:primarymediaid =metadata_expire = 21600metalink =mirrorlist = https://mirrors.fedoraproject.org/metalink?repo=testing-epel6&arch=x86_64mirrorlist_expire = 86400name = Extra Packages for Enterprise Linux 6 - Testing - x86_64old_base_cache_dir =password =persistdir = /var/lib/yum/repos/x86_64/6/epel-testingpkgdir = /var/cache/yum/x86_64/6/epel-testing/packagesproxy = Falseproxy_dict =proxy_password =proxy_username =repo_gpgcheck = Falseretries = 10skip_if_unavailable = Falsessl_check_cert_permissions = Truesslcacert =sslclientcert =sslclientkey =sslverify = Truethrottle = 0timeout = 30.0username ========================================================================================== repo: ius =========================================================================================[ius]bandwidth = 0base_persistdir = /var/lib/yum/repos/x86_64/6baseurl =cache = 0cachedir = /var/cache/yum/x86_64/6/iuscost = 1000enabled = 0enablegroups = Trueexclude =failovermethod = priorityftp_disable_epsv = Falsegpgcadir = /var/lib/yum/repos/x86_64/6/ius/gpgcadirgpgcakey =gpgcheck = Truegpgdir = /var/lib/yum/repos/x86_64/6/ius/gpgdirgpgkey = file:///etc/pki/rpm-gpg/IUS-COMMUNITY-GPG-KEYhdrdir = /var/cache/yum/x86_64/6/ius/headershttp_caching = allincludepkgs =keepalive = Truemdpolicy = group:primarymediaid =metadata_expire = 21600metalink =mirrorlist = https://mirrors.iuscommunity.org/mirrorlist?repo=ius-centos6&arch=x86_64&protocol=httpmirrorlist_expire = 86400name = IUS Community Packages for Enterprise Linux 6 - x86_64old_base_cache_dir =password =persistdir = /var/lib/yum/repos/x86_64/6/iuspkgdir = /var/cache/yum/x86_64/6/ius/packagesproxy = Falseproxy_dict =proxy_password =proxy_username =repo_gpgcheck = Falseretries = 10skip_if_unavailable = Falsessl_check_cert_permissions = Truesslcacert =sslclientcert =sslclientkey =sslverify = Truethrottle = 0timeout = 30.0username =

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

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

或者,我們可以執行以下命令檢視詳細資訊:

# yum repolist all | grep "epel*\|ius*" * epel: mirror.steadfast.netepel                       Extra Packages for Enterprise Linux 6 enabled: 12,509epel-debuginfo             Extra Packages for Enterprise Linux 6 disabledepel-source                Extra Packages for Enterprise Linux 6 disabledepel-testing               Extra Packages for Enterprise Linux 6 disabledepel-testing-debuginfo     Extra Packages for Enterprise Linux 6 disabledepel-testing-source        Extra Packages for Enterprise Linux 6 disabledius                        IUS Community Packages for Enterprise disabledius-archive                IUS Community Packages for Enterprise disabledius-archive-debuginfo      IUS Community Packages for Enterprise disabledius-archive-source         IUS Community Packages for Enterprise disabledius-debuginfo              IUS Community Packages for Enterprise disabledius-dev                    IUS Community Packages for Enterprise disabledius-dev-debuginfo          IUS Community Packages for Enterprise disabledius-dev-source             IUS Community Packages for Enterprise disabledius-source                 IUS Community Packages for Enterprise disabledius-testing                IUS Community Packages for Enterprise disabledius-testing-debuginfo      IUS Community Packages for Enterprise disabledius-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/$basearchmirrorlist=https://mirrors.fedoraproject.org/metalink?repo=epel-6&arch=$basearchfailovermethod=priorityenabled=0gpgcheck=1gpgkey=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/$basearchmirrorlist=https://mirrors.fedoraproject.org/metalink?repo=epel-6&arch=$basearchfailovermethod=priorityenabled=1gpgcheck=1gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-6