Ubuntu设置国内镜像源

文章目录

  • 环境
  • 背景
  • 方法1:使用清华大学镜像源
    • 使用HTTP方式
    • 使用HTTPS方式
  • 方法2:使用阿里云镜像源
  • 总结
  • 参考

环境

  • RHEL 9.3
  • Docker Community 24.0.7
  • ubuntu:latest Docker image (jammy 22.04)

背景

启动Ubuntu容器:

docker run -it ubuntu

在容器里:

vim
bash: vim: command not found

查看Ubuntu版本:

cat /etc/issue
Ubuntu 22.04.3 LTS \n \l

安装vim:

apt install vim
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
E: Unable to locate package vim

失败了。要先 apt update 更新package列表:

apt update

然后再安装vim:

apt install vim

这次安装成功了。但问题是,国内在连接Ubuntu网站的时候,特别慢,还经常连接失败。

查看 /etc/apt/sources.list 文件,如下:

cat /etc/apt/sources.list
# See http://help.ubuntu.com/community/UpgradeNotes for how to upgrade to
# newer versions of the distribution.
deb http://archive.ubuntu.com/ubuntu/ jammy main restricted
# deb-src http://archive.ubuntu.com/ubuntu/ jammy main restricted

## Major bug fix updates produced after the final release of the
## distribution.
deb http://archive.ubuntu.com/ubuntu/ jammy-updates main restricted
# deb-src http://archive.ubuntu.com/ubuntu/ jammy-updates main restricted

## N.B. software from this repository is ENTIRELY UNSUPPORTED by the Ubuntu
## team. Also, please note that software in universe WILL NOT receive any
## review or updates from the Ubuntu security team.
deb http://archive.ubuntu.com/ubuntu/ jammy universe
# deb-src http://archive.ubuntu.com/ubuntu/ jammy universe
deb http://archive.ubuntu.com/ubuntu/ jammy-updates universe
# deb-src http://archive.ubuntu.com/ubuntu/ jammy-updates universe

## N.B. software from this repository is ENTIRELY UNSUPPORTED by the Ubuntu
## team, and may not be under a free licence. Please satisfy yourself as to
## your rights to use the software. Also, please note that software in
## multiverse WILL NOT receive any review or updates from the Ubuntu
## security team.
deb http://archive.ubuntu.com/ubuntu/ jammy multiverse
# deb-src http://archive.ubuntu.com/ubuntu/ jammy multiverse
deb http://archive.ubuntu.com/ubuntu/ jammy-updates multiverse
# deb-src http://archive.ubuntu.com/ubuntu/ jammy-updates multiverse

## N.B. software from this repository may not have been tested as
## extensively as that contained in the main release, although it includes
## newer versions of some applications which may provide useful features.
## Also, please note that software in backports WILL NOT receive any review
## or updates from the Ubuntu security team.
deb http://archive.ubuntu.com/ubuntu/ jammy-backports main restricted universe multiverse
# deb-src http://archive.ubuntu.com/ubuntu/ jammy-backports main restricted universe multiverse

deb http://security.ubuntu.com/ubuntu/ jammy-security main restricted
# deb-src http://security.ubuntu.com/ubuntu/ jammy-security main restricted
deb http://security.ubuntu.com/ubuntu/ jammy-security universe
# deb-src http://security.ubuntu.com/ubuntu/ jammy-security universe
deb http://security.ubuntu.com/ubuntu/ jammy-security multiverse
# deb-src http://security.ubuntu.com/ubuntu/ jammy-security multiverse

为了提高效率,可以配置使用国内的Ubuntu镜像源。对于不同的镜像源,配置方法基本都一样。本文以清华大学镜像源和阿里云镜像源为例,介绍如何配置Ubuntu镜像源。

方法1:使用清华大学镜像源

打开浏览器,访问 https://mirror.tuna.tsinghua.edu.cn/help/ubuntu

Ubuntu设置国内镜像源_第1张图片

注意设置适当的选项。例如,我使用的是 Ubuntu 22.04,代号是 jammy

使用HTTP方式

不勾选“是否使用HTTPS”选项。

其生成的内容如下:

# 默认注释了源码镜像以提高 apt update 速度,如有需要可自行取消注释
deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy main restricted universe multiverse
# deb-src http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy main restricted universe multiverse
deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-updates main restricted universe multiverse
# deb-src http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-updates main restricted universe multiverse
deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-backports main restricted universe multiverse
# deb-src http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-backports main restricted universe multiverse

deb http://security.ubuntu.com/ubuntu/ jammy-security main restricted universe multiverse
# deb-src http://security.ubuntu.com/ubuntu/ jammy-security main restricted universe multiverse

# 预发布软件源,不建议启用
# deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-proposed main restricted universe multiverse
# # deb-src http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-proposed main restricted universe multiverse

要用这些内容替换容器里的 /etc/apt/sources.list 文件。

替换之前,先备份文件:

cp /etc/apt/sources.list /etc/apt/sources.list.bak

由于没有vi,无法直接编辑文件,可以先在宿主机上把文件编辑好,然后使用 docker cp 命令向容器里复制文件,例如:

docker cp /tmp/sources.list.tsinghua.http a65352ec73a9:/etc/apt/sources.list

在容器里查看文件:

ll -d /etc/apt/sources.list*
-rw-r--r--. 1 1000 1000 1122 Jan  8 01:31 /etc/apt/sources.list
-rw-r--r--. 1 root root 2403 Jan  8 07:14 /etc/apt/sources.list.bak
drwxr-xr-x. 2 root root    6 Apr  8  2022 /etc/apt/sources.list.d/

文件权限不同,不知道会不会有什么影响,最好还是和原来的文件保持一致吧:

chown root:root /etc/apt/sources.list

更新package列表:

apt update
Get:1 http://mirrors.tuna.tsinghua.edu.cn/ubuntu jammy InRelease [270 kB]
Get:2 http://mirrors.tuna.tsinghua.edu.cn/ubuntu jammy-updates InRelease [119 kB]
Get:3 http://mirrors.tuna.tsinghua.edu.cn/ubuntu jammy-backports InRelease [109 kB]
Get:4 http://mirrors.tuna.tsinghua.edu.cn/ubuntu jammy/multiverse amd64 Packages [266 kB]
Get:5 http://mirrors.tuna.tsinghua.edu.cn/ubuntu jammy/restricted amd64 Packages [164 kB]         
Get:6 http://mirrors.tuna.tsinghua.edu.cn/ubuntu jammy/main amd64 Packages [1792 kB]                       
Get:7 http://mirrors.tuna.tsinghua.edu.cn/ubuntu jammy/universe amd64 Packages [17.5 MB]                   
Get:8 http://mirrors.tuna.tsinghua.edu.cn/ubuntu jammy-updates/main amd64 Packages [1606 kB]
Get:9 http://mirrors.tuna.tsinghua.edu.cn/ubuntu jammy-updates/universe amd64 Packages [1307 kB]
Get:10 http://mirrors.tuna.tsinghua.edu.cn/ubuntu jammy-updates/multiverse amd64 Packages [49.8 kB]
Get:11 http://mirrors.tuna.tsinghua.edu.cn/ubuntu jammy-updates/restricted amd64 Packages [1611 kB]
Get:12 http://security.ubuntu.com/ubuntu jammy-security InRelease [110 kB]      
Get:13 http://mirrors.tuna.tsinghua.edu.cn/ubuntu jammy-backports/universe amd64 Packages [28.1 kB]
Get:14 http://mirrors.tuna.tsinghua.edu.cn/ubuntu jammy-backports/main amd64 Packages [50.4 kB]
Get:15 http://security.ubuntu.com/ubuntu jammy-security/universe amd64 Packages [1047 kB]
Get:16 http://security.ubuntu.com/ubuntu jammy-security/main amd64 Packages [1332 kB]
Get:17 http://security.ubuntu.com/ubuntu jammy-security/multiverse amd64 Packages [44.0 kB]
Get:18 http://security.ubuntu.com/ubuntu jammy-security/restricted amd64 Packages [1582 kB]
Fetched 29.0 MB in 4s (7161 kB/s)                           
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
All packages are up to date.

配置镜像源成功。

现在,就可以安装vim了:

apt install vim

使用HTTPS方式

如果要使用HTTPS,则需要安装证书,否则apt update会有警告,实际上并没有update成功,安装vim也会失败。

勾选“是否使用HTTPS”选项。

其生成的内容如下:

# 默认注释了源码镜像以提高 apt update 速度,如有需要可自行取消注释
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-updates main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-updates main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-backports main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-backports main restricted universe multiverse

deb http://security.ubuntu.com/ubuntu/ jammy-security main restricted universe multiverse
# deb-src http://security.ubuntu.com/ubuntu/ jammy-security main restricted universe multiverse

# 预发布软件源,不建议启用
# deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-proposed main restricted universe multiverse
# # deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-proposed main restricted universe multiverse

替换 /etc/apt/sources.list 文件,方法同上。

更新package列表:

apt update
Ign:1 https://mirrors.tuna.tsinghua.edu.cn/ubuntu jammy InRelease
Ign:2 https://mirrors.tuna.tsinghua.edu.cn/ubuntu jammy-updates InRelease
Ign:3 https://mirrors.tuna.tsinghua.edu.cn/ubuntu jammy-backports InRelease
Get:4 http://security.ubuntu.com/ubuntu jammy-security InRelease [110 kB]
Ign:1 https://mirrors.tuna.tsinghua.edu.cn/ubuntu jammy InRelease
Ign:2 https://mirrors.tuna.tsinghua.edu.cn/ubuntu jammy-updates InRelease
Ign:3 https://mirrors.tuna.tsinghua.edu.cn/ubuntu jammy-backports InRelease
Get:5 http://security.ubuntu.com/ubuntu jammy-security/main amd64 Packages [1332 kB]
Get:6 http://security.ubuntu.com/ubuntu jammy-security/universe amd64 Packages [1047 kB]
Ign:1 https://mirrors.tuna.tsinghua.edu.cn/ubuntu jammy InRelease
Ign:2 https://mirrors.tuna.tsinghua.edu.cn/ubuntu jammy-updates InRelease
Get:7 http://security.ubuntu.com/ubuntu jammy-security/multiverse amd64 Packages [44.0 kB]                 
Get:8 http://security.ubuntu.com/ubuntu jammy-security/restricted amd64 Packages [1582 kB]                       
Ign:3 https://mirrors.tuna.tsinghua.edu.cn/ubuntu jammy-backports InRelease                                          
Err:1 https://mirrors.tuna.tsinghua.edu.cn/ubuntu jammy InRelease                                                                                                                                                                           
  Certificate verification failed: The certificate is NOT trusted. The certificate issuer is unknown.  Could not handshake: Error in the certificate verification. [IP: 101.6.15.130 443]
Err:2 https://mirrors.tuna.tsinghua.edu.cn/ubuntu jammy-updates InRelease                                                                                                                                                                   
  Certificate verification failed: The certificate is NOT trusted. The certificate issuer is unknown.  Could not handshake: Error in the certificate verification. [IP: 101.6.15.130 443]
Err:3 https://mirrors.tuna.tsinghua.edu.cn/ubuntu jammy-backports InRelease                                                                                                                                                                 
  Certificate verification failed: The certificate is NOT trusted. The certificate issuer is unknown.  Could not handshake: Error in the certificate verification. [IP: 101.6.15.130 443]
Fetched 4116 kB in 7s (552 kB/s)                                                                                                                                                                                                            
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
All packages are up to date.
W: https://mirrors.tuna.tsinghua.edu.cn/ubuntu/dists/jammy/InRelease: No system certificates available. Try installing ca-certificates.
W: https://mirrors.tuna.tsinghua.edu.cn/ubuntu/dists/jammy-updates/InRelease: No system certificates available. Try installing ca-certificates.
W: https://mirrors.tuna.tsinghua.edu.cn/ubuntu/dists/jammy-backports/InRelease: No system certificates available. Try installing ca-certificates.
W: https://mirrors.tuna.tsinghua.edu.cn/ubuntu/dists/jammy/InRelease: No system certificates available. Try installing ca-certificates.
W: https://mirrors.tuna.tsinghua.edu.cn/ubuntu/dists/jammy-updates/InRelease: No system certificates available. Try installing ca-certificates.
W: https://mirrors.tuna.tsinghua.edu.cn/ubuntu/dists/jammy-backports/InRelease: No system certificates available. Try installing ca-certificates.
W: https://mirrors.tuna.tsinghua.edu.cn/ubuntu/dists/jammy/InRelease: No system certificates available. Try installing ca-certificates.
W: https://mirrors.tuna.tsinghua.edu.cn/ubuntu/dists/jammy-updates/InRelease: No system certificates available. Try installing ca-certificates.
W: https://mirrors.tuna.tsinghua.edu.cn/ubuntu/dists/jammy-backports/InRelease: No system certificates available. Try installing ca-certificates.
W: https://mirrors.tuna.tsinghua.edu.cn/ubuntu/dists/jammy/InRelease: No system certificates available. Try installing ca-certificates.
W: https://mirrors.tuna.tsinghua.edu.cn/ubuntu/dists/jammy-updates/InRelease: No system certificates available. Try installing ca-certificates.
W: https://mirrors.tuna.tsinghua.edu.cn/ubuntu/dists/jammy-backports/InRelease: No system certificates available. Try installing ca-certificates.
W: Failed to fetch https://mirrors.tuna.tsinghua.edu.cn/ubuntu/dists/jammy/InRelease  Certificate verification failed: The certificate is NOT trusted. The certificate issuer is unknown.  Could not handshake: Error in the certificate verification. [IP: 101.6.15.130 443]
W: Failed to fetch https://mirrors.tuna.tsinghua.edu.cn/ubuntu/dists/jammy-updates/InRelease  Certificate verification failed: The certificate is NOT trusted. The certificate issuer is unknown.  Could not handshake: Error in the certificate verification. [IP: 101.6.15.130 443]
W: Failed to fetch https://mirrors.tuna.tsinghua.edu.cn/ubuntu/dists/jammy-backports/InRelease  Certificate verification failed: The certificate is NOT trusted. The certificate issuer is unknown.  Could not handshake: Error in the certificate verification. [IP: 101.6.15.130 443]
W: Some index files failed to download. They have been ignored, or old ones used instead.

可见,更新是有问题的。

安装vim:

apt install vim
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
Some packages could not be installed. This may mean that you have
requested an impossible situation or if you are using the unstable
distribution that some required packages have not yet been created
or been moved out of Incoming.
The following information may help to resolve the situation:

The following packages have unmet dependencies:
 vim : Depends: libgpm2 (>= 1.20.7) but it is not installable
       Depends: libpython3.10 (>= 3.10.0) but it is not going to be installed
       Depends: libsodium23 (>= 1.0.14) but it is not installable
E: Unable to correct problems, you have held broken packages.

安装失败。

首先要解决证书的问题,安装证书:

apt install --reinstall ca-certificates

注意:在安装证书前,还是要先做一次 apt update ,否则找不到 ca-certificates 包。

注:能够安装成功,因为连接的是 http://security.ubuntu.com/ubuntu ,是HTTP协议。但是,这还是连接了国外网站。如果连接不上,则需要改为从国内镜像网站下载安装证书(注意清华镜像站上有一个“强制安全更新使用镜像”选项,勾选之后,则使用清华镜像源),或者手工下载安装证书(没试过)。

接下来,再次 apt updateapt install vim ,就能更新package列表和安装vim了。

方法2:使用阿里云镜像源

和使用清华镜像源的方法基本是一致的。

打开浏览器,访问 https://developer.aliyun.com/mirror/ubuntu

Ubuntu设置国内镜像源_第2张图片

找到对应的Ubuntu版本,其内容如下:

deb https://mirrors.aliyun.com/ubuntu/ jammy main restricted universe multiverse
deb-src https://mirrors.aliyun.com/ubuntu/ jammy main restricted universe multiverse

deb https://mirrors.aliyun.com/ubuntu/ jammy-security main restricted universe multiverse
deb-src https://mirrors.aliyun.com/ubuntu/ jammy-security main restricted universe multiverse

deb https://mirrors.aliyun.com/ubuntu/ jammy-updates main restricted universe multiverse
deb-src https://mirrors.aliyun.com/ubuntu/ jammy-updates main restricted universe multiverse

# deb https://mirrors.aliyun.com/ubuntu/ jammy-proposed main restricted universe multiverse
# deb-src https://mirrors.aliyun.com/ubuntu/ jammy-proposed main restricted universe multiverse

deb https://mirrors.aliyun.com/ubuntu/ jammy-backports main restricted universe multiverse
deb-src https://mirrors.aliyun.com/ubuntu/ jammy-backports main restricted universe multiverse

注意:这里有同样的HTTPS证书问题,同样也有两种解决方法:

  • 把所有的 https 改成 http
  • 在包含 security 的部分里,把 https 改为 http ,然后先安装证书,再更新package列表和安装软件

步骤和使用清华镜像源是一样的:在容器里备份 /etc/apt/sources.list 文件,在宿主机上创建好新文件,然后把文件复制到容器里,最后修改文件权限。

接下来,如果是使用HTTP就直接 apt update ,如果是使用HTTPS就需要先 apt update ,再 apt install --reinstall ca-certificates 安装证书,然后再 apt update

最后,运行 apt install vim ,安装vim。

总结

步骤如下:

  1. 找到Ubuntu镜像源网站
  2. 找到Ubuntu版本所对应的配置镜像源的内容
  3. 修改Ubuntu系统里的 /etc/apt/sources.list 文件
  4. apt update
    1. 如果是使用HTTP ,则直接更新
    2. 如果是使用HTTS,则要先更新一次(security用HTTP方式),再安装证书,然后再更新一次
  5. 至此,配置镜像源完成,就可以安装软件了。

参考

  • https://mirror.tuna.tsinghua.edu.cn/help/ubuntu
  • https://developer.aliyun.com/mirror/ubuntu

你可能感兴趣的:(Linux,ubuntu,linux)