[Linux] 修改 CentOS 上 yum 使用的變數值

[Linux] 修改 CentOS 上 yum 使用的變數值

今天遇到一個問題,我們有一個基於 CentOS 7 的專案,

在使用的時候,yum 一直無法正常執行,例如:

root@localhost ~ # yum search vim
One of the configured repositories failed (Unknown),
and yum doesn't have enough cached data to continue. At this point the only
 safe thing yum can do is fail. There are a few ways to work "fix" this:
     1. Contact the upstream for the repository and get them to fix the problem.
     2. Reconfigure the baseurl/etc. for the repository, to point to a working
        upstream. This is most often useful if you are using a newer
        distribution release than is supported by the repository (and the
        packages for the previous distribution release still work).
     3. Disable the repository, so yum won't use it by default. Yum will then
just ignore the repository until you permanently enable it again or use
--enablerepo for temporary usage:
yum-config-manager --disable <repoid>
4. Configure the failing repository to be skipped, if it is unavailable.
Note that yum will try to contact the repo. when it runs most commands,
so will have to try and fail each time (and thus. yum will be be much
slower). If it is a very temporary problem though, this is often a nice
yum-config-manager --save --setopt=<repoid>.skip_if_unavailable=true
Cannot find a valid baseurl for repo: base/7-1.1503.el7.centos.2.8/x86_64


說是找不到一個正確的 baseurl

給 base/7-1.1503.el7.centos.2.8/x86_64 這個 repository…

下了 yum repolist 看一下,repo id 也是同樣的值:

root@localhost ~ # yum repolist
repo id                                 repo name                                 status
base/7-1.1503.el7.centos.2.8/x86_64     CentOS-7-1.1503.el7.centos.2.8 - Base     0
extras/7-1.1503.el7.centos.2.8/x86_64   CentOS-7-1.1503.el7.centos.2.8 - Extras   0
updates/7-1.1503.el7.centos.2.8/x86_64  CentOS-7-1.1503.el7.centos.2.8 - Updates  0
repolist: 0


先來看看 /etc/yum.repos.d/CentOS-Base.repo 裡的設定…

name=CentOS-$releasever - Base


可以看到上面的 URL 裡面,有用到 $releasever 這個變數~ 

跟一般正常的 CentOS 7 比較起來,$releasever 會是像 7 這樣,

而不會是 7-1.1503.el7.centos.2.8 這麼長一串…

推測如果 $releasever 可以改成 7 的話,有可能 yum 就能找到 repository 了~ 


所以也沒辦法去改變 releasever 的預設值…

要怎麼讓它的 yum 能正常的運作呢?


查了一下,Redhat: Using Yum Variables 這裡提到了 yum 的變數是可以自訂的,

作法就是在 /etc/yum/vars 目錄下,建立一個以變數名稱為檔案名稱的檔案,



像我想把 $releasever 設定成 7 的話,就用下面的指令:

echo 7 > /etc/yum/vars/releasever


此時再去執行 yum repolist,可以看到 repo id 變成比較正常的格式了:

root@localhost ~ # yum repolist
repo id            repo name             status
base/7/x86_64      CentOS-7 - Base       8652
extras/7/x86_64    CentOS-7 - Extras     180
updates/7/x86_64   CentOS-7 - Updates    1275
repolist: 10107


yum 本身也恢復正常,可以搜尋與下載套件囉~

root@localhost ~ # yum search vim
golang-vim.noarch : Vim plugins for Go
vim-X11.x86_64 : The VIM version of the vi editor for the X Window System
vim-common.x86_64 : The common files needed by any version of the VIM editor
vim-enhanced.x86_64 : A version of the VIM editor which includes recent enhancements
vim-filesystem.x86_64 : VIM filesystem layout
vim-minimal.x86_64 : A minimal version of the VIM editor


(本頁面已被瀏覽過 3,106 次)

One thought on “[Linux] 修改 CentOS 上 yum 使用的變數值

  1. 我是設定IP時忘了設定DNS
    也出現Cannot find a valid baseurl for repo: base/7/x86_64的錯誤訊息…
    版主回覆:(09/02/2016 08:50:06 AM)


發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *

這個網站採用 Akismet 服務減少垃圾留言。進一步了解 Akismet 如何處理網站訪客的留言資料