Ubuntu 22.04/24.04 LTS 用 sed 快速换国内源

Ubuntu 配置国内源

  • 首先了解下/etc/apt/sources.list文件
    • /etc/apt/sources.list是一个普通可编辑的文本文件,保存了ubuntu软件更新的源服务器的地址。
    • /etc/apt/sources.list.d/*.list 该文件夹下的文件是第三方软件的源,可以分别存放不同的第三源地址,只需“扩展名”为list即可。

配置步骤如下:

时间:2024-10
请注意:选择和系统对应的版本,使用 lsb_release -a 查看系统版本

  • 1.Ubuntu配置文件位置:/etc/apt/sources.list

  • 2.需用root权限进入:sudo vim /etc/apt/sources.list

有2种修改方式

  • 1.sed 把原文件中:archive.ubuntu.com,替换为:mirrors.aliyun.com(或其他服务器列表源网址)
  • 2.直接复制下方对应版本的内容,覆盖原文件中的内容即可。

修改完成后,更新本地安装包数据库

方式一

以 Ubuntu 22.04.5 LTS 为例

服务器列表
可将 http://cn.archive.ubuntu.com/ubuntu/ 替换为下列任意服务器 :

# Ubuntu 官方(欧洲,国内较慢,无同步延迟)
http://archive.ubuntu.com/ubuntu/
# Ubuntu 官方中国(目前是阿里云)
http://cn.archive.ubuntu.com/ubuntu/
# 网易(广东广州电信/联通千兆双线接入)
http://mirrors.163.com/ubuntu/
# 搜狐(山东联通千兆接入)
http://mirrors.sohu.com/ubuntu/
# 阿里云(北京万网/浙江杭州阿里云服务器双线接入)
http://mirrors.aliyun.com/ubuntu/
# 腾讯
http://mirrors.cloud.tencent.com/ubuntu/
# 华为
http://mirrors.huaweicloud.com/ubuntu/
# 清华
https://mirrors.tuna.tsinghua.edu.cn/ubuntu/


root@ubuntu2204:~# lsb_release -a
No LSB modules are available.
Distributor ID:	Ubuntu
Description:	Ubuntu 22.04.5 LTS
Release:	22.04
Codename:	jammy


root@ubuntu2204:~# sed -i_bak 's/cn.archive.ubuntu.com/mirrors.aliyun.com/g' /etc/apt/sources.list  # 更换阿里源
或
root@ubuntu2204:~# sed -i_bak 's/cn.archive.ubuntu.com/mirrors.tuna.tsinghua.edu.cn/g' /etc/apt/sources.list  # 更换清华源

root@ubuntu2204:~# apt update
root@ubuntu2204:~# apt upgrade -y

方式二

以下内容的具体含义如下:
1)每一行的开头是deb或者deb-src,分别表示直接通过.deb文件进行安装和通过源文件的方式进行安装。
2)deb或者deb-src字段之后,是一段URL,之后是五个用空格隔开的字符串,分别对应相应的目录结构。

  • ubuntu 22.04 LTS (jammy)
# 阿里源
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



# 清华源
# 默认注释了源码镜像以提高 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
  • ubuntu 24.04 (noble)
# 阿里源
deb https://mirrors.aliyun.com/ubuntu/ noble main restricted universe multiverse
deb-src https://mirrors.aliyun.com/ubuntu/ noble main restricted universe multiverse

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

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

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

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



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

# 以下安全更新软件源包含了官方源与镜像站配置,如有需要可自行修改注释切换
deb http://security.ubuntu.com/ubuntu/ noble-security main restricted universe multiverse
# deb-src http://security.ubuntu.com/ubuntu/ noble-security main restricted universe multiverse

# 预发布软件源,不建议启用
# deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ noble-proposed main restricted universe multiverse
# # deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ noble-proposed main restricted universe multiverse
posted @ 2024-10-18 09:27  讲文张字  阅读(483)  评论(0编辑  收藏  举报
返回顶部