Ubuntu 换源
换源
ubuntu的源存放在/etc/apt/source.list,在换源之前我们要记得对之前的源进行备份,这样的好处是,万一你不行,你还可以复原回去。
脚本
ubuntu有很多个版本,12.04之前的我们就不管它了,太老了。每一个版本都有一个代号,比如trusty(14.04)、xenial(16.04)、bionic(18.04)、groovy(20.10)、hirsute(21.04)。不管代号怎么变,相关的源的网址它是固定的,比如五道口的源是https://mirrors.tuna.tsinghua.edu.cn/ubuntu, 阿里的源是http://mirrors.aliyun.com/ubuntu。所以,我们其实可以写个脚本去做这件事,输入是ubuntu的版本号,输出是相关的源(这里选五道口和阿里的吧)。
脚本如下:
#!/bin/bash
function change_sources() {
echo "#======= change sources.list start =======#"
echo "Input Your Ubuntu Version:"
read UBUNTU_VERSION
echo "Your Ubuntu Version is ${UBUNTU_VERSION}"
if [ ${UBUNTU_VERSION} = "12.04" ] || [ ${UBUNTU_VERSION} = "14.04" ] || [ ${UBUNTU_VERSION} = "16.04" ] || [ ${UBUNTU_VERSION} = "18.04" ] || [ ${UBUNTU_VERSION} = "20.10" ] || [ ${UBUNTU_VERSION} = "21.04" ]; then
cp /etc/apt/sources.list /etc/apt/sources.list.bak
cp ./apt/${UBUNTU_VERSION}/sources.list /etc/apt/sources.list
apt-get update && apt-get upgrade
fi
echo '#======= change sources.list end =======#'
}
change_sources
操作如下:
git clone https://gitee.com/taoge2021/env-config.git
cd env-config/linux/ubuntu/sources
chmod u+x change-sources.sh
./change-sources.sh
20.10 版本的源
20.04的ubuntu经测试也可以用,将/etc/apt/source.list
的内容替换即可。
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ groovy main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ groovy-security main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ groovy-updates main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ groovy-backports main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ groovy main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ groovy-security main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ groovy-updates main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ groovy-backports main restricted universe multiverse
# deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ groovy-proposed main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ groovy-proposed main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ groovy main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ groovy-security main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ groovy-updates main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ groovy-backports main restricted universe multiverse
# deb-src http://mirrors.aliyun.com/ubuntu/ groovy main restricted universe multiverse
# deb-src http://mirrors.aliyun.com/ubuntu/ groovy-security main restricted universe multiverse
# deb-src http://mirrors.aliyun.com/ubuntu/ groovy-updates main restricted universe multiverse
# deb-src http://mirrors.aliyun.com/ubuntu/ groovy-backports main restricted universe multiverse
# deb http://mirrors.aliyun.com/ubuntu/ groovy-proposed main restricted universe multiverse
# deb-src http://mirrors.aliyun.com/ubuntu/ groovy-proposed main restricted universe multiverse
参考链接
- Ubuntu 换源看这一篇就够了:
https://cloud.tencent.com/developer/article/1880981