PS:在Linux运维工作中,我们可能会安装一些系统自带或第三方的软件来满足我们的工作需求,这样能节省不少的时间,避免工作过于复杂。有一次我朋友问我,如何把下载的程序(varnish)上传至Linux服务器,要是安装ftpsamba的话很不方便。我建议他下载secureCRT远程管理工具,然后在服务器上安装lrzsz软件;通过rzsz上传下载,前提是需要在本地安装YUM源或者让你的VMware虚拟机能够访问互联网,满足其一便可正常安装。有时候在QQ群中,也有朋友问起,如何在服务器上搭建本地YUM源或国内YUM源,下面我用shell写的自动安装本地或网络yum源脚本,希望对大家能够有些帮助。

1、提供安装脚本,大家可以参考一下

[root@localhost yum.repos.d]# cat /server/scripts/auto_install_yum_source.sh#!/bin/bash# create date of 2013-10-16# author of cfwl# blog http://cfwlxf.blog.51cto.com# Source function library. /etc/rc.d/init.d/functions# User custom variableYUM_BACK_DIR=/etc/yum.repos.d/backup/YUM_DIR=/etc/yum.repos.d/MOUNT_DIR=/mnt/cdrom/# User custom functionLOCAL_YUM_SOURCE(){        # Judge YUM_BACK_DIR directory if exist        [ ! -d ${YUM_BACK_DIR} ] && mkdir ${YUM_BACK_DIR}        cd ${YUM_DIR}        find . -type f -name "*.repo" | xargs mv -t ${YUM_BACK_DIR}        RETVAL=0        [ $? = ${RETVAL} ] && mv ${YUM_BACK_DIR}/CentOS-Media.repo ${YUM_DIR} && action  "File mv is successfully." /bin/true &&        cp -a CentOS-Media.repo CentOS-Media.repo.save        sed -i 's/baseurl=file:\/\/\/media\/CentOS\//baseurl=file:\/\/\/mnt\/cdrom\//g' CentOS-Media.repo        sed -i 's/file:\/\/\/media\/cdrom\///g' CentOS-Media.repo        sed -i 's/file:\/\/\/media\/cdrecorder\///g' CentOS-Media.repo        sed -i 's/enabled=0/enabled=1/g' CentOS-Media.repo        rpm --import /etc/pki/rpm-gpg/RPM-GPG-KEY-*        sleep 2        # Judge mount directory if exsit        [ ! -d ${MOUNT_DIR} ] && mkdir ${MOUNT_DIR} && printf "\033[33mdirectory ${MOUNT_DIR} yet by create.\n\033[0m"        [ $? = 0 ] && mount /dev/cdrom ${MOUNT_DIR} && printf "\033[33mmount mirrors is successfully.\n\033[0m"        sleep 2        #yum cache        yum makecache}NETWORK_YUM_SOURCE(){        # Judge YUM_BACK_DIR directory if exist        [ ! -d ${YUM_BACK_DIR} ] && mkdir ${YUM_BACK_DIR}        cd ${YUM_DIR}        find . -type f -name "*.repo" | xargs mv -t ${YUM_BACK_DIR}        RETVAL=0        [ $? = ${RETVAL} ] && printf "\033[32m                              1 Inland 163  yum source                              2 Inland sohu yum source                                please input install 163 or sohu  yum source:\033[0m" str        read str        case "$str" in           163)                wget http://mirrors.163.com/.help/CentOS6-Base-163.repo                RETVAL=0                [ $? = ${RETVAL} ] && printf "\033[35m yum source is download successfully of 163 mirrors.\n\033[0m"                sleep 2                rpm --import http://mirror.centos.org/centos/RPM-GPG-KEY-CentOS-6                yum makecache                ;;          sohu)                wget http://mirrors.sohu.com/help/CentOS-Base-sohu.repo                RETVAL=0                [ $? = ${RETVAL} ] && printf "\033[35m yum source is download successfully of sohu mirrors.\n\033[0m"                sleep 2                rpm --import /etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-*                yum makecache                ;;             *)                echo -e "\033[31m Error: plase you input 163 or sohu.\033[0m"        esac}# Select auto install list of printprintf "\033[32m        1 Local   Yum Source        2 Network Yum Sourceplease input auto install de mode(local or network):\033[0m"  strread str        case "$str" in            local)                     LOCAL_YUM_SOURCE                     ;;          network)                     NETWORK_YUM_SOURCE                     ;;                *)                     echo -e "\033[31m Error: please input local or network. \033[0m"        esac

2、安装网络YUM

提示:安装网络YUM源时,可以根据脚本的提示选择国内的YUM源,如163、搜狐。如果想增加另外的YUM源,可以自行修改脚本。

yum makecache :建立数据缓存,可以提升软件的安装速度。

3、本地YUM

提示:

当前测试环境是在VMware虚拟机下,CnetOS镜像挂载后,默认存放位置是:/dev/cdrom如下图所示:

提示:

实验环境我一般都会安装本地YUM源,如果线上环境想安装本地YUM源,我建议拷贝CentOS镜像或者网上下载一份到目录/mirrors目录下,然后使用mount挂载镜像;线上环境建议大家用网络YUM源,实现软件在线更新。

PSCentOS6.x版本,都可以使用此脚本安装,测试通过。