Complete Guide to Changing Ubuntu Sources

Linux | Red Hat Certified | IT Technology | Operations Engineer

👇 Join the 1000-person technical exchange QQ group, note 【public account】 for faster approval

Complete Guide to Changing Ubuntu Sources

Why Change Sources?

Due to network restrictions, accessing the official Ubuntu source from China is currently slow. Therefore, to improve download speed, it is recommended to change the Ubuntu source to a domestic mirror source.

Detailed Steps

Note: All steps below need to be executed in the terminal and can be run from any path.

Must Read

Before changing sources, we must understand what version of the operating system we are using.

cat /etc/os-release

As you can see, my version is Ubuntu 22.04.

Complete Guide to Changing Ubuntu Sources

Note: The configuration file paths and commands may differ for different operating systems, please operate according to your operating system version.

Backup Existing Sources (Optional)

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

To minimize modifications to the system configuration, we will first back up the system’s built-in sources.

This command will create a copy of sources.list in the /etc/apt directory and rename it to sources.list.bak.

Open Configuration File

sudo nano /etc/apt/sources.list

There are various text editors available, I chose `nano` here.

Find the Corresponding Mirror Source

Taking Tsinghua Source as an example, first enter the Tsinghua University Open Source Software Mirror Station (https://mirror.tuna.tsinghua.edu.cn/), as shown below:

Complete Guide to Changing Ubuntu Sources

Then search for ubuntu, as shown below:

Complete Guide to Changing Ubuntu SourcesClick on the first search result (i.e., ubuntu) ? (Note: Do not click on “ubuntu”), which will redirect to the following image:

Complete Guide to Changing Ubuntu Sources

The introduction section provides various version notes, including the paths for the software source configuration files (corresponding to the file paths in step 2) and solutions for old versions, as shown below:

Complete Guide to Changing Ubuntu Sources

After selecting the corresponding version, copy the mirror source as shown below:

Complete Guide to Changing Ubuntu Sources

For example, the mirror source for Ubuntu 22.04 is

# The source mirror for apt update speed is commented out by default. Uncomment if needed.
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
# The following security update source includes both official source and mirror station configuration. Modify comments to switch if needed.
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
# Pre-release software sources, not recommended to enable
# 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

Note: The mirror sources under each major version of the system are consistent (e.g., 22.04.1 and 22.04.2 are the same, but 22.01.1 and 20.01.1 are different)

If you are unsure about your system version, you can check it using the lsb_release -a command, where the code in Release is the major version code, as shown below:

Complete Guide to Changing Ubuntu Sources

Modify Configuration File to Mirror Source

Delete the existing content in the sources.list configuration file and replace it with the copied mirror source.

Update Package Index

sudo apt update

This command will check if there are available updates for the installed packages and update the local package index repository.

Note: This command will not update any installed packages in the system. (It is equivalent to telling you how many packages can be updated, but not executing the update)

Upgrade Installed Packages (Optional)

sudo apt upgrade

This command will update all installed packages to the latest version. (Equivalent to executing the update)

Note: If the environment in the system is complex, this command may affect the system’s stability and compatibility, use with caution! (New systems can use it without worry)

Common Issues and Solutions

1. After following the steps, an error occurs when executing apt update after changing sources

This case is from a fan, as shown in the image below:

Complete Guide to Changing Ubuntu Sources

If the above issue occurs, please follow these three steps for troubleshooting:

Check if the mirror source has been changed to a different version

Execute cat /etc/os-release to check the system version, and verify if the mirror source you found is for that version.

Check if your machine can connect to the network

Check if you can normally open domestic webpages through your browser. If you see something similar to the image below, then the machine cannot connect to the network:

Complete Guide to Changing Ubuntu Sources

Please check for network issues on your machine.

If both of the above points are fine, please send me a private message, and I will try to reproduce and solve your issue.

Supplement

1. How to use Alibaba Cloud Source

Enter the Alibaba Open Source Mirror Station (https://developer.aliyun.com/mirror/), as shown below:

Complete Guide to Changing Ubuntu Sources

Search for ubuntu, as shown below:

Complete Guide to Changing Ubuntu Sources

Click on the first one, which will redirect to the following image:

Complete Guide to Changing Ubuntu Sources

Similar to Tsinghua Source, the introduction section provides various version notes, including the paths for the software source configuration files (corresponding to the file paths in step 2) and solutions for old versions, which will not be repeated here.

Find the corresponding version number and copy the mirror source as shown below:

Complete Guide to Changing Ubuntu Sources

2. Summary of Common Mirror Station Links

Mirror Station                              Link
[Tsinghua Source] Tsinghua University Open Source Software Mirror Station  https://mirror.tuna.tsinghua.edu.cn/
[Alibaba Cloud Source] Alibaba Open Source Mirror Station     https://developer.aliyun.com/mirror/
[Netease Source] Netease Open Source Mirror Station           http://mirrors.163.com/
[USTC Source] University of Science and Technology of China Open Source Software Mirror Station     https://mirrors.ustc.edu.cn/

Quick Version for Changing Tsinghua Source for Ubuntu 22.04

Backup Existing Sources (Optional)

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

Open Configuration File

sudo nano /etc/apt/sources.list

Change Source

# The source mirror for apt update speed is commented out by default. Uncomment if needed.
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
# The following security update source includes both official source and mirror station configuration. Modify comments to switch if needed.
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
# Pre-release software sources, not recommended to enable
# 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

Update

sudo apt update

Upgrade (Optional)

sudo apt upgrade

For course inquiries, add: HCIE666CCIE

↓ Or scan the QR code below ↓

Complete Guide to Changing Ubuntu Sources

What technical points and content would you like to see?

Feel free to leave a message below!

Leave a Comment