shell脚本快速配置yum源

我们在使用Red Hat系列与CentOS系列的Linux系统时经常要配置yum源,本文档提出一个快速配置yum源的方法,就是用shell脚本来实现。

首先确保系统已挂载好镜像文件,然后执行vim rhel7.repo.sh命令,这个命令是创建一个脚本

在打开的文件中添加以下内
用一个神奇的方法配置yum源用一个神奇的方法配置yum源

[root@linuxprobe ~]# vim rhel7.repo.sh 
1 #!/bin/bash 2 3 cd /etc/yum.repos.d 4 5 cat >>rhel7.repo<<OK 6 [rhel7] 7 name=rhel7 8 baseurl=file:///media/cdrom 9 enabled=1 10 gpgcheck=0 11 OK 12 13 mkdir -p /media/cdrom 14 mount /dev/cdrom /media/cdrom 15 echo "/dev/cdrom /media/cdrom iso9660 defaults 0 0" >> /etc/fstab 16 17 yum clean all 18 yum makecache

写完脚本后记得赋予脚本执行权限:

[root@linuxprobe ~]# chmod 755 rhel7.repo.sh 
或
[root@linuxprobe ~]# chmod +x rhel7.repo.sh 

赋予脚本执行权限后再运行脚本:

[root@linuxprobe ~]# base rhel7.repo.sh 
或
[root@linuxprobe ~]# ./rhel7.repo.sh

测试:

[root@linuxprobe ~]# yum instll httpd
Loaded plugins: fastestmirror, langpacks
No such command: instll. Please use /usr/bin/yum --help
[root@linuxprobe ~]# yum install httpd
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
Resolving Dependencies
--> Running transaction check
---> Package httpd.x86_64 0:2.4.6-17.el7.centos.1 will be installed
--> Processing Dependency: httpd-tools = 2.4.6-17.el7.centos.1 for package: httpd-2.4.6-17.el7.centos.1.x86_64
--> Processing Dependency: /etc/mime.types for package: httpd-2.4.6-17.el7.centos.1.x86_64
--> Processing Dependency: libaprutil-1.so.0()(64bit) for package: httpd-2.4.6-17.el7.centos.1.x86_64
--> Processing Dependency: libapr-1.so.0()(64bit) for package: httpd-2.4.6-17.el7.centos.1.x86_64
--> Running transaction check
---> Package apr.x86_64 0:1.4.8-3.el7 will be installed
---> Package apr-util.x86_64 0:1.5.2-6.el7 will be installed
---> Package httpd-tools.x86_64 0:2.4.6-17.el7.centos.1 will be installed
---> Package mailcap.noarch 0:2.1.41-2.el7 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

=========================================================================
 Package         Arch       Version                      Repository
                                                                    Size
=========================================================================
Installing:
 httpd           x86_64     2.4.6-17.el7.centos.1        rhel7     2.7 M
Installing for dependencies:
 apr             x86_64     1.4.8-3.el7                  rhel7     103 k
 apr-util        x86_64     1.5.2-6.el7                  rhel7      92 k
 httpd-tools     x86_64     2.4.6-17.el7.centos.1        rhel7      77 k
 mailcap         noarch     2.1.41-2.el7                 rhel7      31 k

Transaction Summary
=========================================================================
Install  1 Package (+4 Dependent packages)

Total download size: 3.0 M
Installed size: 10 M
Is this ok [y/d/N]: y
Downloading packages:
-------------------------------------------------------------------------
Total                                        23 MB/s | 3.0 MB  00:00     
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : apr-1.4.8-3.el7.x86_64                                1/5 
  Installing : apr-util-1.5.2-6.el7.x86_64                           2/5 
  Installing : httpd-tools-2.4.6-17.el7.centos.1.x86_64              3/5 
  Installing : mailcap-2.1.41-2.el7.noarch                           4/5 
  Installing : httpd-2.4.6-17.el7.centos.1.x86_64                    5/5 
  Verifying  : mailcap-2.1.41-2.el7.noarch                           1/5 
  Verifying  : httpd-2.4.6-17.el7.centos.1.x86_64                    2/5 
  Verifying  : apr-util-1.5.2-6.el7.x86_64                           3/5 
  Verifying  : apr-1.4.8-3.el7.x86_64                                4/5 
  Verifying  : httpd-tools-2.4.6-17.el7.centos.1.x86_64              5/5 

Installed:
  httpd.x86_64 0:2.4.6-17.el7.centos.1                                   

Dependency Installed:
  apr.x86_64 0:1.4.8-3.el7                                               
  apr-util.x86_64 0:1.5.2-6.el7                                          
  httpd-tools.x86_64 0:2.4.6-17.el7.centos.1                             
  mailcap.noarch 0:2.1.41-2.el7                                          

Complete!

[root@linuxprobe ~]# ll /etc/yum.repos.d
total 28
-rw-r--r--. 1 root root 1612 Jul  4  2014 CentOS-Base.repo.bak
-rw-r--r--. 1 root root  640 Jul  4  2014 CentOS-Debuginfo.repo
-rw-r--r--. 1 root root 1331 Jul  4  2014 CentOS-Sources.repo
-rw-r--r--. 1 root root  156 Jul  4  2014 CentOS-Vault.repo
-rw-r--r--. 1 root root   66 Jan 12 12:37 ding.repo.bak
-rw-r--r--. 1 root root   68 Jan 30 19:54 rhel7.repo
-rw-r--r--. 1 root root  176 Jan 30 15:28 scootersoftware.repo

最后出现Complete!说明httpd安装成功,同时也说明了yum仓库配置成功!
注:我用的系统是RHEL/CentOS 7,用其他Linux系统的话只需要改yum仓库名称即可。

posted @ 2020-01-30 20:08  丁海龙  阅读(2331)  评论(0编辑  收藏  举报