CentOS 7 将YUM源更换为国内源以及基础配置完整教程
在初次使用CentOS 7的过程中,可能发现遇到没有安装yum和wget的情况,无法安装和下载东西,这个时候就需要用到换源,而且Centos7已经不再更新有些官方资源找不到,所以需要换源。
- 备份原有的YUM源配置
在更换YUM源之前,建议先备份原有的YUM源配置文件,以防万一需要恢复。
sudo是临时赋予管理员权限,如果是普通用户在命令前加上sudo如果为root用户则不用添加,下面默认为root用户
打开终端。
备份YUM源配置文件:(下面二者选其一)
cp -r /etc/yum.repos.d /etc/yum.repos.d.bak
mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup
- 移除原有的YUM源配置
为了避免源冲突,我们需要删除原有的YUM源配置文件。
删除原有的YUM源配置文件:
rm -f /etc/yum.repos.d/*.repo
- 添加国内YUM源配置(无wget,yum无法使用的情况)
阿里云镜像源
下载阿里云YUM源配置文件:
curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
网易镜像源
下载网易YUM源配置文件:
curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.163.com/.help/CentOS7-Base-163.repo
清华大学镜像源
下载清华大学YUM源配置文件:
curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.tuna.tsinghua.edu.cn/help/centos7/
如果可以下载wget可以使用下面的进行换源
阿里源(推荐):
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
网易源:
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.163.com/.help/CentOS7-Base-163.repo
清理YUM缓存:
yum clean all
生成新的缓存:
yum makecache
- 验证YUM源配置
确保新配置的YUM源能够正常工作,您可以通过以下命令进行验证:
更新YUM源信息:
yum update
检查YUM源的速度:
yum repolist
如果一切正常,您应该能够看到从新镜像源下载的软件包列表。
yum repolist
已加载插件:fastestmirror
Loading mirror speeds from cached hostfile
* base: mirrors.aliyun.com
* extras: mirrors.aliyun.com
* updates: mirrors.aliyun.com
源标识 源名称 状态
base/7/x86_64 CentOS-7 - Base - mirrors.aliyun.com 10,072
extras/7/x86_64 CentOS-7 - Extras - mirrors.aliyun.com 526
updates/7/x86_64 CentOS-7 - Updates - mirrors.aliyun.com 6,173
repolist: 16,771
- 恢复备份(如果需要)
如果更换YUM源后遇到问题,您可以随时恢复到备份的源配置:
删除现有的YUM源配置文件:
rm -f /etc/yum.repos.d/*.repo
恢复备份的YUM源配置文件:
cp -r /etc/yum.repos.d.bak/* /etc/yum.repos.d/
清理YUM缓存:
yum clean all
生成新的缓存:
yum makecache
6.下载wget
yum install -y wget
(用于从网络上下载资源,没有指定目录,下载资源会默认为当前目录)
7.ifconfig
yum install -y net-tools
(ifconfig是linux中用于显示或配置网络设备[网络接口卡]的命令,需要下载这个才能查看虚拟机的IP)
8.nslookup
yum install -y bind-utils
9.下载vim工具
yum install -y vim-enhanced
(可定制的文本编辑器)
注:以上如果你是安装的图形化界面可以省略;下面就算是你是图形化界面也要弄;
10.关闭和禁用防火墙
1、直接关闭防火墙
systemctl stop firewalld
2、禁止firewall开机启动
systemctl disable firewalld
3、查看状态
systemctl status firewalld #出现dead表示关闭成功
11.关闭selinux
vim /etc/selinux/config
(安全增强型Linux系统,它是一个linux内核模块,也是Linux的一个安全子系统。SELinux功能开启
后,会关闭系统中不安全的功能。关闭SELinux可以帮助解决许多应用程序的运行问题。在某些情况
下,关闭SELinux可能是更好的选择。)
将SELINUX=enforcing改为SELINUX=disabled >> 重启
进入(i)
```shell
insert #vim插入
Esc #退出编辑
shift + : #输入wq【保存设置】
编辑完成后重启
验证:
重启(# reboot 或者 # shutdown -r now)后验证(# getenforce 该命令可以帮助我们快速了解当前
系统的SELinux执行状态)
# vim /etc/selinux/config
# Esc 退出vim编辑,进入命令输入模式
# i 进入vim编辑
# :wq 保存设置
hell
shift + : #输入wq【保存设置】
编辑完成后重启
**验证:**
重启(# reboot 或者 # shutdown -r now)后验证(# getenforce 该命令可以帮助我们快速了解当前
系统的SELinux执行状态)
\# vim /etc/selinux/config
\# Esc 退出vim编辑,进入命令输入模式
\# i 进入vim编辑
\# :wq 保存设置
到这里基本的设置就已经完成了,就可以正常使用