redhat替换yum源

redhat更换yum源

由于redhat的yum源需要注册,如果没有注册则无法使用

[born@localhost bin]$ yum install redhat-lsb
Loaded plugins: langpacks, product-id, subscription-manager
You need to be root to perform this command.

查看相关信息

首先查看一下版本号和安装的yum的包

[born@localhost bin]$ cat /etc/redhat-release
Red Hat Enterprise Linux Server release 7.0 (Maipo)
[born@localhost bin]$ rpm -qa | grep yum
yum-utils-1.1.31-24.el7.noarch
yum-langpacks-0.4.2-3.el7.noarch
yum-metadata-parser-1.1.4-10.el7.x86_64
yum-rhn-plugin-2.0.1-4.el7.noarch
PackageKit-yum-0.8.9-11.el7.x86_64
yum-3.4.3-118.el7.noarch

卸载原始的包

首先强制卸载上述这些包,但是发现权限不够

[born@localhost bin]$ rpm -qa | grep yum | xargs rpm -e --nodeps
error: can't create transaction lock on /var/lib/rpm/.rpm.lock (Permission denied)
xargs: rpm: exited with status 255; aborting

加上sudo试试,发现自己不属于可以使用sudo的成员

[born@localhost bin]$ sudo rpm -qa | grep yum | xargs rpm -e --nodeps
[sudo] password for born: 
born is not in the sudoers file.  This incident will be reported.
rpm: no packages given for erase

所以需要在sudoer中加入自己的账户,首先进入超级管理员su模式,为配置文件增加权限,然后对该文件进行修改

[born@localhost bin]$ su
Password: 
[root@localhost bin]# chmod u+w /etc/sudoers
[root@localhost bin]# vim /etc/sudoers

在vim中输入/进入查找模式,搜索ALL,在root ALL = (ALL) ALL下面加上一行kkkkkk ALL = (ALL) ALL,其中kkkkkk是自己的用户名(不是主机名),比如我的就是born。

可以按i进入vim的输入模式,输入完成后,按esc键,然后输入:wq保存并退出。

在修改完上述文件后收回该文件的可写权力

[root@localhost bin]# chmod u-w /etc/sudoers

重新执行卸载步骤,并看一下是否卸载完成,发现没有了

[root@localhost bin]# sudo rpm -qa | grep yum | xargs rpm -e --nodeps
[root@localhost bin]# rpm -qa | grep yum

下载安装新的包

现在需要下载下列的一些包:

[born@localhost yumpack]$ ls
python-urlgrabber-3.10-10.el7.noarch.rpm
rpm-4.11.3-45.el7.x86_64.rpm
yum-3.4.3-168.el7.centos.noarch.rpm
yum-metadata-parser-1.1.4-10.el7.x86_64.rpm
yum-plugin-fastestmirror-1.1.31-54.el7_8.noarch.rpm
yum-utils-1.1.31-54.el7_8.noarch.rpm

手动下载这些包的方法:在https://mirrors.aliyun.com/centos/7/os/x86_64/Packages/这个网站上搜索上述包的关键词,可以找到,但是可能版本不同,右键复制链接后,在指定的目录使用wget命令进行下载

在包所在的目录进行安装

[root@localhost yumpack]# rpm -ivh --force *.rpm
warning: python-iniparse-0.4-9.el7.noarch.rpm: Header V3 RSA/SHA256 Signature, key ID f4a80eb5: NOKEY
Preparing...                          ################################# [100%]
Updating / installing...
   1:yum-metadata-parser-1.1.4-10.el7 ################################# [ 14%]
   2:rpm-4.11.3-45.el7                ################################# [ 29%]
   3:python-urlgrabber-3.10-10.el7    ################################# [ 43%]
   4:python-iniparse-0.4-9.el7        ################################# [ 57%]
   5:yum-plugin-fastestmirror-1.1.31-5################################# [ 71%]
   6:yum-3.4.3-168.el7.centos         ################################# [ 86%]
   7:yum-utils-1.1.31-54.el7_8        ################################# [100%]

添加yum源

这里使用清华的yum源,进行一定修改修改过后的/etc/yum.repos.d/CentOS-Base.repo文件见下,可以直接粘贴:

具体镜像网址为https://mirrors.tuna.tsinghua.edu.cn/help/centos/,这个网站上有说明怎么修改配置文件,可以先看一下。但是有一点需要注意,这个网站适用于centos系统,如果想在redhat上使用还需要将其中的参数修改一下,即\(releasever的参数,这个参数在centos中能够正常运行,但是在redhat下,需要把它变成对应的系统版本。可以在vim中直接输入`:1,\)s/$releasever/7/g`

# CentOS-Base.repo
#
# The mirror system uses the connecting IP address of the client and the
# update status of each mirror to pick mirrors that are updated to and
# geographically close to the client.  You should use this for CentOS updates
# unless you are manually picking other mirrors.
#
# If the mirrorlist= does not work for you, as a fall back you can try the
# remarked out baseurl= line instead.
#
#

[base]
name=CentOS-7 - Base
baseurl=https://mirrors.tuna.tsinghua.edu.cn/centos/7/os/$basearch/
#mirrorlist=http://mirrorlist.centos.org/?release=7&arch=$basearch&repo=os
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7

#released updates
[updates]
name=CentOS-7 - Updates
baseurl=https://mirrors.tuna.tsinghua.edu.cn/centos/7/updates/$basearch/
#mirrorlist=http://mirrorlist.centos.org/?release=7&arch=$basearch&repo=updates
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7

#additional packages that may be useful
[extras]
name=CentOS-7 - Extras
baseurl=https://mirrors.tuna.tsinghua.edu.cn/centos/7/extras/$basearch/
#mirrorlist=http://mirrorlist.centos.org/?release=7&arch=$basearch&repo=extras
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7

#additional packages that extend functionality of existing packages
[centosplus]
name=CentOS-7 - Plus
baseurl=https://mirrors.tuna.tsinghua.edu.cn/centos/7/centosplus/$basearch/
#mirrorlist=http://mirrorlist.centos.org/?release=7&arch=$basearch&repo=centosplus
gpgcheck=1
enabled=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7 

接着生成新的缓存,即输入下两个命令

yum clean all
yum makecache

运行效果见下

[born@localhost yum.repos.d]$ yum clean all
Loaded plugins: fastestmirror, product-id, subscription-manager
Cleaning repos: base centosplus extras updates
[born@localhost yum.repos.d]$ yum makecache
Loaded plugins: fastestmirror, product-id, subscription-manager
Determining fastest mirrors
base                                                     | 3.6 kB     00:00     
centosplus                                               | 2.9 kB     00:00     
extras                                                   | 2.9 kB     00:00     
updates                                                  | 2.9 kB     00:00     
(1/13): base/x86_64/group_gz                               | 153 kB   00:00     
(2/13): base/x86_64/primary_db                             | 6.1 MB   00:01     
(3/13): centosplus/x86_64/filelists_db                     | 900 kB   00:00     
(4/13): centosplus/x86_64/other_db                         |  28 kB   00:00     
(5/13): extras/x86_64/primary_db                           | 222 kB   00:00     
(6/13): extras/x86_64/other_db                             | 134 kB   00:00     
(7/13): base/x86_64/other_db                               | 2.6 MB   00:01     
(8/13): extras/x86_64/filelists_db                         | 224 kB   00:00     
(9/13): updates/x86_64/filelists_db                        | 2.7 MB   00:02     
(10/13): centosplus/x86_64/primary_db                      | 2.0 MB   00:03     
(11/13): updates/x86_64/other_db                           | 334 kB   00:00     
(12/13): updates/x86_64/primary_db                         | 4.7 MB   00:03     
(13/13): base/x86_64/filelists_db                          | 7.2 MB   00:06

可能出现的问题

在以上的步骤执行完之后,使用yum安装任意一款应用可能会有Header V3 RSA/SHA1 Signature, key ID c105b9de: NOKEY的提示,进而导致应用无法安装,解决办法是下载一个文件

注意不用镜像站的这个key文件不同,这里使用的是从清华源下载所用的命令,其他镜像站比如163,可以根据清华的地址略作修改,并且不同的系统版本用的也不同

cd /etc/pki/rpm-gpg/
curl -o /etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7 https://mirrors.tuna.tsinghua.edu.cn/centos/RPM-GPG-KEY-CentOS-7
rpm --import RPM-GPG-KEY-CentOS-7

这里结束之后我就没遇到问题了,随便安装一个应用也很顺利。

posted @ 2020-12-24 22:24  好好学习ttxs  阅读(879)  评论(0)    收藏  举报