Centos

yum 不選擇 YUM0 環境變數

  • August 5, 2021

我在我的(docker)centos 環境中設置了一個環境變數:

[arman@7b33ffd8619e ~]$ echo $YUM0
yumrepo.myhost.com

請注意,當我在echo命令前面加上sudo.

根據centos 文件

$YUM0-9這被替換為同名的 shell 環境變數的值。如果 shell 環境變數不存在,那麼配置文件變數將不會被替換。

但是,當我嘗試yum從我的容器中安裝任何東西時,我收到一條錯誤消息,清楚地表明環境變數沒有被拾取yum

[arman@7b33ffd8619e ~]$ sudo yum install less
Loaded plugins: fastestmirror
http://$YUM0/x86_64/centos/7.2.1511/base/repodata/repomd.xml: [Errno 14] curl#6 - "Could not resolve host: $YUM0; Name or service not known"
Trying other mirror.


One of the configured repositories failed (centos),
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 centos

    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
       compromise:

           yum-config-manager --save --setopt=centos.skip_if_unavailable=true

failure: repodata/repomd.xml from centos: [Errno 256] No more mirrors to try.
http://$YUM0/x86_64/centos/7.2.1511/base/repodata/repomd.xml: [Errno 14] curl#6 - "Could not resolve host: $YUM0; Name or service not known"

我正在執行以下版本:

[arman.schwarz@7b33ffd8619e ~]$ cat /etc/centos-release
CentOS Linux release 7.2.1511 (Core)
[arman.schwarz@7b33ffd8619e ~]$ yum --version
3.4.3

手動修改我的.repo文件/etc/yum.repos.d以使用儲存庫名稱而不是依賴環境變數導致yum安裝沒有問題,證明這不是儲存庫本身的問題。

跑步export YUM0=yumrepo.myhost.com沒有效果。

如何使YUM0環境變數可用於yum

除非在 中另外sudo配置(例如通過設置env_keep/etc/sudoers,否則環境不會繼承您的YUM0變數。嘗試:

$ sudo "export YUM0=$YUM0; yum install less"

$YUM0在將命令鏈發送到 之前,您的 shell 將展開sudo,將命令轉換為例如export YUM0=repo.example.com; yum install less.

預設情況下, sudo會為其執行的命令重置環境。這將包括您的 YUM0 變數,除非您也進行了env_keep配置。

如果要使用 sudo 執行命令,請按如下方式執行:

sudo YUM0=yumrepo.myhost.com yum ...rest of the command...

這將在 sudo 執行時設置變數的值。

或者,如果您的 sudo 規則允許,您可以在呼叫其餘命令之前啟動一個 shell 並設置變數:

sudo sh -c "YUM0=$YUM0; yum ... rest of the command ..."

因為雙引號將允許將內部 YUM0 變數設置為外部(目前)shell 的 YUM0 值。

引用自:https://unix.stackexchange.com/questions/389815