我在我的(docker)centos 环境中设置了一个环境变量:
[arman@7b33ffd8619e ~]$ echo $YUM0
yumrepo.myhost.com
echo
请注意,当我在命令前面加上 . 时,这也有效sudo
。
按照Centos 文档;
$美味0-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
?
答案1
默认情况下,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 值。
答案2
环境sudo
,除非在 中另外配置(例如通过设置env_keep
)/etc/sudoers
,不会继承你的YUM0
变量。尝试:
$ sudo "export YUM0=$YUM0; yum install less"
$YUM0
在将命令链发送到 之前,您的 shell 将展开sudo
,将命令转换为 例如export YUM0=repo.example.com; yum install less
。
答案3
只是为了详细说明这里的其他建议,这些建议解释了这一点须藤缺少用户的环境,须藤命令确实允许您使用您的权限运行它--保留环境转变:
sudo --preserve-env yum install less