跳到主要内容

Mac M1 Centos8

2024年07月03日
柏拉文
越努力,越幸运

一、认识


二、安装


2.1 运行 Vmware Function

2.2 选择 创建自定义虚拟机

Preview

2.3 选择 Linux Debian 11 ARM 操作系统

Preview

2.4 选择虚拟磁盘

默认选择新建虚拟磁盘,一般容量是20G(如果不够,后续可以扩容)

Preview

2.5 点击完成

到此,虚拟机初步配置完成,点击完成,选择存储 vmwarevm 的位置,可以自动更改文件名称,点击完成,回弹出如图2所示,表示镜像没有配置完成(别急!接下来完成)

Preview

2.6 处理器和内存配置

Preview

2.7 配置网络适配

Preview
Preview

2.8 选择磁盘

Preview

2.9 导入镜像(通过CD/DVD)

Preview
Preview

2.10 重新启动磁盘

Preview
Preview

2.11 选择安装系统

Preview
  1. 选择install centos linux8,并点击

  2. 剩下的系统自动安装,安装完成之后弹出界面如下,选择语言

2.12 选择语言

Preview

2.13 调整时间

Preview

选的是亚洲-上海

2.14 选择安装目的地

Preview
Preview

2.15 配置网络

Preview
Preview

2.16 设置根密码

Preview
Preview

2.17 开始安装

Preview
Preview

三、配置


3.1 yum 源配置

1. 本地创建名为 switch_yum_repo.sh 脚本

vim switch_yum_repo.sh
#!/bin/bash

set -e # 遇到错误时停止脚本执行

# 定义变量
NEW_REPO_URL="https://mirrors.aliyun.com/repo"
REPO_DIR="/etc/yum.repos.d"
BACKUP_REPO_DIR="/etc/yum.repos.d.bak"

# 错误处理函数
handle_error() {
echo "Error: $1"
exit 1
}

# 备份现有 YUM 配置文件
backup_yum_config() {
echo "Backing up current YUM configuration files..."
if [ -d "$REPO_DIR" ]; then
mv "$REPO_DIR" "$BACKUP_REPO_DIR" || handle_error "Failed to backup YUM configuration files"
echo "YUM configuration files backed up to $BACKUP_REPO_DIR"
else
echo "No YUM configuration files found to back up"
fi
mkdir -p "$REPO_DIR"
}

# 下载新的镜像源配置文件
download_new_repos() {
echo "Downloading new YUM repository configuration from $NEW_REPO_URL..."
curl -s ${NEW_REPO_URL}/Centos-vault-8.5.2111.repo -o ${REPO_DIR}/Centos-vault-8.5.2111.repo || handle_error "Failed to download Centos-vault-8.5.2111.repo"
curl -s ${NEW_REPO_URL}/epel-archive-8.repo -o ${REPO_DIR}/epel-archive-8.repo || handle_error "Failed to download epel-archive-8.repo"
echo "New YUM repository configuration files downloaded."
}

# 清除 YUM 缓存
clear_yum_cache() {
echo "Clearing YUM cache..."
yum clean all || handle_error "Failed to clean YUM cache"
yum makecache || handle_error "Failed to make YUM cache"
}

# 更新系统
update_system() {
echo "Updating system..."
yum update -y || handle_error "Failed to update system"
}

# 解决 appStream 元数据下载失败问题(可选)
fix_appstream_metadata() {
echo "Fixing appStream metadata download issues..."
for repo_file in $REPO_DIR/CentOS-*; do
if [ -f "$repo_file" ]; then
sudo sed -i -e "s|mirrorlist=|#mirrorlist=|g" "$repo_file"
sudo sed -i -e "s|#baseurl=http://mirror.centos.org|baseurl=http://vault.centos.org|g" "$repo_file"
fi
done
echo "AppStream metadata issues fixed."
}

# 主函数
main() {
backup_yum_config
download_new_repos
clear_yum_cache
update_system
fix_appstream_metadata
}

# 执行主函数
main

2. 获取虚拟机 IP 地址

ip addr show

3. 本地向虚拟机传输 switch_yum_repo.sh 脚本

scp switch_yum_repo.sh root@192.168.105.132:/root

4. 虚拟机赋予脚本可执行权限

chmod +x switch_yum_repo.sh

5. 虚拟机执行脚本,切换镜像源

./switch_yum_repo.sh

6. 虚拟机测试是否切换成功

yum update 

// 或者

dnf update

四、问题


4.1 appStream

问题描述:

Errors during downloading metadata for repository "appstream":
- Curl error(6): Couldn't resolve host name for ……

错误: 为仓库 "appstream" 下载元数据失败

解决问题

1. 创建名为 appstream.sh 脚本

sudo sed -i -e "s|mirrorlist=|#mirrorlist=|g" /etc/yum.repos.d/CentOS-*
sudo sed -i -e "s|#baseurl=http://mirror.centos.org|baseurl=http://vault.centos.org|g" /etc/yum.repos.d/CentOS-*

2. 发送 appstream.sh 脚本(可选)

scp appstream.sh root@192.168.105.132:/root

3. 赋予脚本可执行权限

chmod +x appstream.sh

4. 执行脚本,切换镜像源

./appstream.sh

4.2 epel-archive

问题描述: CentOS 8.2更换yum源报错Errors during downloading metadata for repository 'epel': - Status code

问题原因: 第三方的镜像站中均已移除CentOS 8的源,Centos 8版本已停止更新相应依赖导致的,下载新的yum源即可搞定

问题解决:

1. 创建名为 switch_yum_repo.sh 脚本

#!/bin/bash

set -e # 遇到错误时停止脚本执行

# 定义变量
NEW_REPO_URL="https://mirrors.aliyun.com/repo"
REPO_DIR="/etc/yum.repos.d"
BACKUP_REPO_DIR="/etc/yum.repos.d.bak"

# 错误处理函数
handle_error() {
echo "Error: $1"
exit 1
}

# 备份现有 YUM 配置文件
backup_yum_config() {
echo "Backing up current YUM configuration files..."
if [ -d "$REPO_DIR" ]; then
mv "$REPO_DIR" "$BACKUP_REPO_DIR" || handle_error "Failed to backup YUM configuration files"
echo "YUM configuration files backed up to $BACKUP_REPO_DIR"
else
echo "No YUM configuration files found to back up"
fi
mkdir -p "$REPO_DIR"
}

# 下载新的镜像源配置文件
download_new_repos() {
echo "Downloading new YUM repository configuration from $NEW_REPO_URL..."
curl -s ${NEW_REPO_URL}/Centos-vault-8.5.2111.repo -o ${REPO_DIR}/Centos-vault-8.5.2111.repo || handle_error "Failed to download Centos-vault-8.5.2111.repo"
curl -s ${NEW_REPO_URL}/epel-archive-8.repo -o ${REPO_DIR}/epel-archive-8.repo || handle_error "Failed to download epel-archive-8.repo"
echo "New YUM repository configuration files downloaded."
}

# 清除 YUM 缓存
clear_yum_cache() {
echo "Clearing YUM cache..."
yum clean all || handle_error "Failed to clean YUM cache"
yum makecache || handle_error "Failed to make YUM cache"
}

# 更新系统
update_system() {
echo "Updating system..."
yum update -y || handle_error "Failed to update system"
}

# 解决 appStream 元数据下载失败问题(可选)
fix_appstream_metadata() {
echo "Fixing appStream metadata download issues..."
for repo_file in $REPO_DIR/CentOS-*; do
if [ -f "$repo_file" ]; then
sudo sed -i -e "s|mirrorlist=|#mirrorlist=|g" "$repo_file"
sudo sed -i -e "s|#baseurl=http://mirror.centos.org|baseurl=http://vault.centos.org|g" "$repo_file"
fi
done
echo "AppStream metadata issues fixed."
}

# 主函数
main() {
backup_yum_config
download_new_repos
clear_yum_cache
update_system
fix_appstream_metadata
}

# 执行主函数
main

2. 发送 switch_yum_repo.sh 脚本(可选)

scp switch_yum_repo.sh root@192.168.105.132:/root

3. 赋予脚本可执行权限

chmod +x switch_yum_repo.sh

4. 执行脚本,切换镜像源

./switch_yum_repo.sh

5. 测试是否切换成功

yum update 

// 或者

dnf update

参考资料


Mac 2020版M1 安装虚拟机各种踩坑详解(关注文末小惊喜哟)

亲测CentOS 8.2更换yum源报错Errors during downloading metadata for repository 'epel': - Status code解决办法