背景
Ubuntu是新生代农民工比较常用的Linux系统。由于一些原因,当我们用官网的源的更新和下载包的时候,很慢、很卡、很费时间。所以,我们应该把它换成国内的源。
换源
ubuntu的源存放在/etc/apt/source.list
,在换源之前我们要记得对之前的源进行备份,这样的好处是,万一你不行,你还可以复原回去。然后我们可以用vim操作这个文件,可以把之前的删了注释都行,按住SHIFT G
跳到最后一行后,按住o
进入编辑模式,然后贴下源哇,具体的操作步骤如下(以21.04为例):
cp /etc/apt/sources.list /etc/apt/sources.list.bak
vim /etc/apt/sources.list
贴
代码语言:javascript复制# source && source code
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ hirsute main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ hirsute main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ hirsute-updates main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ hirsute-updates main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ hirsute-backports main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ hirsute-backports main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ hirsute-security main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ hirsute-security main restricted universe multiverse
# Advance distribution source, Not recommended
# deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ hirsute-proposed main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ hirsute-proposed main restricted universe multiverse
# source && source code
deb http://mirrors.aliyun.com/ubuntu hirsute main restricted
deb http://mirrors.aliyun.com/ubuntu hirsute-updates main restricted
deb http://mirrors.aliyun.com/ubuntu hirsute universe
deb http://mirrors.aliyun.com/ubuntu hirsute-updates universe
deb http://mirrors.aliyun.com/ubuntu hirsute multiverse
deb http://mirrors.aliyun.com/ubuntu hirsute-updates multiverse
deb http://mirrors.aliyun.com/ubuntu hirsute-backports main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu hirsute-security main restricted
deb http://cn.archive.ubuntu.com/ubuntu hirsute-security universe
deb http://cn.archive.ubuntu.com/ubuntu hirsute-security multiverse
按下esc
, 输入:wq
然后敲sudo apt-get update && apt-get upgrade
脚本为王
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, 阿里的源。所以,我们其实可以写个脚本去做这件事,输入是ubuntu的版本号,输出是相关的源(这里选五道口和阿里的吧)。
脚本如下:
代码语言:javascript复制#!/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
操作步骤如下:
代码语言:javascript复制git clone https://gitee.com/taoge2021/env-config.git
cd env-config/linux/ubuntu/sources
chmod u x change-sources.sh
./change-source.sh
这个是完整的日志:
代码语言:javascript复制root@ubuntu-ataola-2:/home/ataola# ./change-sources.sh
#======= change sources.list start =======#
Input Your Ubuntu Version:
21.04
Your Ubuntu Version is 21.04
Hit:1 http://mirrors.aliyun.com/ubuntu hirsute InRelease
Hit:2 http://mirrors.aliyun.com/ubuntu hirsute-updates InRelease
Hit:3 http://mirrors.aliyun.com/ubuntu hirsute-backports InRelease
Get:4 http://mirrors.aliyun.com/ubuntu hirsute-security InRelease [101 kB]
Get:5 https://mirrors.tuna.tsinghua.edu.cn/ubuntu hirsute InRelease [269 kB]
Get:6 http://cn.archive.ubuntu.com/ubuntu hirsute-security InRelease [101 kB]
Get:7 https://mirrors.tuna.tsinghua.edu.cn/ubuntu hirsute-updates InRelease [109 kB]
Get:8 https://mirrors.tuna.tsinghua.edu.cn/ubuntu hirsute-backports InRelease [101 kB]
Get:9 https://mirrors.tuna.tsinghua.edu.cn/ubuntu hirsute-security InRelease [101 kB]
Get:10 https://mirrors.tuna.tsinghua.edu.cn/ubuntu hirsute/main amd64 Packages [1,394 kB]
Get:11 https://mirrors.tuna.tsinghua.edu.cn/ubuntu hirsute/main Translation-en [511 kB]
Get:12 https://mirrors.tuna.tsinghua.edu.cn/ubuntu hirsute/main amd64 c-n-f Metadata [29.5 kB]
Get:13 https://mirrors.tuna.tsinghua.edu.cn/ubuntu hirsute/restricted amd64 Packages [78.0 kB]
Get:14 https://mirrors.tuna.tsinghua.edu.cn/ubuntu hirsute/restricted Translation-en [12.0 kB]
Get:15 https://mirrors.tuna.tsinghua.edu.cn/ubuntu hirsute/restricted amd64 c-n-f Metadata [436 B]
Get:16 https://mirrors.tuna.tsinghua.edu.cn/ubuntu hirsute/universe amd64 Packages [13.2 MB]
Get:17 http://cn.archive.ubuntu.com/ubuntu hirsute-security/universe amd64 Packages [211 kB]
Get:18 http://cn.archive.ubuntu.com/ubuntu hirsute-security/universe Translation-en [41.2 kB]
Get:19 http://cn.archive.ubuntu.com/ubuntu hirsute-security/universe amd64 c-n-f Metadata [4,968 B]
Get:20 http://cn.archive.ubuntu.com/ubuntu hirsute-security/multiverse amd64 Packages [3,364 B]
Get:21 http://cn.archive.ubuntu.com/ubuntu hirsute-security/multiverse Translation-en [828 B]
Get:22 http://cn.archive.ubuntu.com/ubuntu hirsute-security/multiverse amd64 c-n-f Metadata [220 B]
Get:23 https://mirrors.tuna.tsinghua.edu.cn/ubuntu hirsute/universe Translation-en [5,441 kB]
Get:24 https://mirrors.tuna.tsinghua.edu.cn/ubuntu hirsute/universe amd64 c-n-f Metadata [279 kB]
Get:25 https://mirrors.tuna.tsinghua.edu.cn/ubuntu hirsute/multiverse amd64 Packages [206 kB]
Get:26 https://mirrors.tuna.tsinghua.edu.cn/ubuntu hirsute/multiverse Translation-en [108 kB]
Get:27 https://mirrors.tuna.tsinghua.edu.cn/ubuntu hirsute/multiverse amd64 c-n-f Metadata [8,124 B]
Get:28 https://mirrors.tuna.tsinghua.edu.cn/ubuntu hirsute-updates/main amd64 Packages [325 kB]
Get:29 https://mirrors.tuna.tsinghua.edu.cn/ubuntu hirsute-updates/main Translation-en [88.3 kB]
Get:30 https://mirrors.tuna.tsinghua.edu.cn/ubuntu hirsute-updates/main amd64 c-n-f Metadata [6,420 B]
Get:31 https://mirrors.tuna.tsinghua.edu.cn/ubuntu hirsute-updates/restricted amd64 Packages [158 kB]
Get:32 https://mirrors.tuna.tsinghua.edu.cn/ubuntu hirsute-updates/restricted Translation-en [22.1 kB]
Get:33 https://mirrors.tuna.tsinghua.edu.cn/ubuntu hirsute-updates/restricted amd64 c-n-f Metadata [476 B]
Get:34 https://mirrors.tuna.tsinghua.edu.cn/ubuntu hirsute-updates/universe amd64 Packages [274 kB]
Get:35 https://mirrors.tuna.tsinghua.edu.cn/ubuntu hirsute-updates/universe Translation-en [64.3 kB]
Get:36 https://mirrors.tuna.tsinghua.edu.cn/ubuntu hirsute-updates/universe amd64 c-n-f Metadata [6,556 B]
Get:37 https://mirrors.tuna.tsinghua.edu.cn/ubuntu hirsute-updates/multiverse amd64 Packages [4,376 B]
Get:38 https://mirrors.tuna.tsinghua.edu.cn/ubuntu hirsute-updates/multiverse Translation-en [1,136 B]
Get:39 https://mirrors.tuna.tsinghua.edu.cn/ubuntu hirsute-updates/multiverse amd64 c-n-f Metadata [252 B]
Get:40 https://mirrors.tuna.tsinghua.edu.cn/ubuntu hirsute-backports/main amd64 c-n-f Metadata [112 B]
Get:41 https://mirrors.tuna.tsinghua.edu.cn/ubuntu hirsute-backports/restricted amd64 c-n-f Metadata [120 B]
Get:42 https://mirrors.tuna.tsinghua.edu.cn/ubuntu hirsute-backports/universe amd64 Packages [3,728 B]
Get:43 https://mirrors.tuna.tsinghua.edu.cn/ubuntu hirsute-backports/universe Translation-en [1,252 B]
Get:44 https://mirrors.tuna.tsinghua.edu.cn/ubuntu hirsute-backports/universe amd64 c-n-f Metadata [192 B]
Get:45 https://mirrors.tuna.tsinghua.edu.cn/ubuntu hirsute-backports/multiverse amd64 c-n-f Metadata [120 B]
Get:46 https://mirrors.tuna.tsinghua.edu.cn/ubuntu hirsute-security/main amd64 Packages [212 kB]
Get:47 https://mirrors.tuna.tsinghua.edu.cn/ubuntu hirsute-security/main Translation-en [55.8 kB]
Get:48 https://mirrors.tuna.tsinghua.edu.cn/ubuntu hirsute-security/main amd64 c-n-f Metadata [3,636 B]
Get:49 https://mirrors.tuna.tsinghua.edu.cn/ubuntu hirsute-security/restricted amd64 Packages [156 kB]
Get:50 https://mirrors.tuna.tsinghua.edu.cn/ubuntu hirsute-security/restricted Translation-en [21.8 kB]
Get:51 https://mirrors.tuna.tsinghua.edu.cn/ubuntu hirsute-security/restricted amd64 c-n-f Metadata [476 B]
Get:52 https://mirrors.tuna.tsinghua.edu.cn/ubuntu hirsute-security/universe amd64 Packages [211 kB]
Get:53 https://mirrors.tuna.tsinghua.edu.cn/ubuntu hirsute-security/universe Translation-en [41.2 kB]
Get:54 https://mirrors.tuna.tsinghua.edu.cn/ubuntu hirsute-security/universe amd64 c-n-f Metadata [4,968 B]
Get:55 https://mirrors.tuna.tsinghua.edu.cn/ubuntu hirsute-security/multiverse amd64 Packages [3,364 B]
Get:56 https://mirrors.tuna.tsinghua.edu.cn/ubuntu hirsute-security/multiverse Translation-en [828 B]
Get:57 https://mirrors.tuna.tsinghua.edu.cn/ubuntu hirsute-security/multiverse amd64 c-n-f Metadata [220 B]
Fetched 24.0 MB in 6s (3,771 kB/s)
Reading package lists... Done
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
Calculating upgrade... Done
The following packages have been kept back:
ubuntu-advantage-tools
0 upgraded, 0 newly installed, 0 to remove and 1 not upgraded.
#======= change sources.list end =======#
root@ubuntu-ataola-2:/home/ataola#
root@ubuntu-ataola-2:/home/ataola# ls /etc/apt
apt.conf.d auth.conf.d preferences.d sources.list sources.list.bak sources.list.curtin.old sources.list.d trusted.gpg.d
root@ubuntu-ataola-2:/home/ataola# cat /etc/apt/sources.list
# source && source code
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ hirsute main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ hirsute main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ hirsute-updates main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ hirsute-updates main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ hirsute-backports main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ hirsute-backports main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ hirsute-security main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ hirsute-security main restricted universe multiverse
# Advance distribution source, Not recommended
# deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ hirsute-proposed main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ hirsute-proposed main restricted universe multiverse
# source && source code
deb http://mirrors.aliyun.com/ubuntu hirsute main restricted
deb http://mirrors.aliyun.com/ubuntu hirsute-updates main restricted
deb http://mirrors.aliyun.com/ubuntu hirsute universe
deb http://mirrors.aliyun.com/ubuntu hirsute-updates universe
deb http://mirrors.aliyun.com/ubuntu hirsute multiverse
deb http://mirrors.aliyun.com/ubuntu hirsute-updates multiverse
deb http://mirrors.aliyun.com/ubuntu hirsute-backports main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu hirsute-security main restricted
deb http://cn.archive.ubuntu.com/ubuntu hirsute-security universe
deb http://cn.archive.ubuntu.com/ubuntu hirsute-security multiverseroot@ubuntu-ataola-2:/home/ataola#
参考文献
https://wiki.ubuntu.org.cn/源列表