Not recommended in Chinese mainland network environments.
Usage Help
The tabs represent the built-in mirror site types and script sources. Please check whether your target mirror site supports your operating system before use. You can check in the Mirror List.
By default, the original mirror configuration will be automatically backed up. For more details, see the documentation below. If you are confused by some options during use, just press Enter all the way after selecting the mirror, which is the best practice.
It supports use in abnormal environments such as when the original system software source is damaged, missing, or invalid. There is no need to install any additional dependencies.
About Mirror Selection (Must Read for New Users)
If you are not familiar with these mirrors, it is recommended to use Aliyun (high compatibility, strong availability) or USTC (fast speed). Don't worry about speed tests, the built-in mirrors generally do not differ much.
This project script is an automated operation and maintenance tool. It does not subjectively rank or customize mirrors. User choice should be free, which is also to better continue the spirit of open source software.
ROOT privileges required
Please run this script using the root account, switching between the sudo -i and su root commands, as the commands vary depending on the system. Also, avoid directly running one-click commands such as sudo bash <(xxx) using sudo.
It is recommended to use a modern SSH client
The system's GUI graphical interface terminal application may experience some unpredictable display issues. Some systems will automatically enable the SSH service; otherwise, please refer to how to enable SSH.
If running the script for the first time on a new system
The script relies on curl to fetch content, but some systems do not preinstall this package, resulting in Command not found errors. See About Command not found below for installation. The script itself is unrelated to curl or wget and does not download any content.
Interactive configuration is required during script execution
Use arrow keys UpDownLeftRight or WASD to control options and press Enter to confirm. If interaction is abnormal, try resizing the terminal window and avoid full screen.
The script automatically backs up the original mirror configuration. The backup path is the original file or directory with .bak appended, e.g., /etc/apt/sources.list => /etc/apt/sources.list.bak. If a backup exists, you will be prompted whether to overwrite it.
The commands and configuration steps are for reference only; please be flexible and adapt them accordingly.
Check if SSH service is installed
ls/etc|grepssh
If this folder does not exist, SSH is not installed. Install the openssh-server package with your package manager. The installation command is as follows:
apt-getinstall-yopenssh-server
DebianUbuntuKaliLinux MintDeepinZorin OSArmbianProxmox VERaspberry Pi OSopenKylin
Many systems have multiple repositories. The script follows the system default. Disabled repositories will not be enabled after running the script, but their URLs will be updated. To enable them:
By default, deb-src and proposed are disabled. Uncomment the relevant lines in /etc/apt/sources.list to enable.
DebianUbuntuKaliLinux MintDeepinZorin OSArmbianProxmox VERaspberry Pi OSopenKylin
Some repos are disabled by default. Set enabled=1 in the relevant repo files under /etc/yum.repos.d.
Red Hat Enterprise LinuxCentOSRocky LinuxAlmaLinuxFedoraopenEulerOpenCloudOSAnolis OS
Some repos are disabled by default. Set enabled=1 in the relevant repo files under /etc/zypp/repos.d.
The project uses GitHub Action to automatically copy the source code to the docs directory after each commit for website publishing. The site is hosted on Netlify, with almost no risk of hijacking.
You can also use the raw address from the code repository. This is just to explain why there are several different addresses; the default official site address is easier to remember and access.
About mirror download speed
First, see the recommendations in the Mirror List usage help. These are based on experience, but some users still worry about speed.
The network latency (Ping) of a mirror is not directly related to download speed. Distance does not equal experience. Some mirrors have high bandwidth but poor speed due to load strategies.
There are many open source speed test scripts written in C or Python. This project is written in Bash Shell and does not depend on third-party libraries. Bash is the most common scripting language for Linux operations and is preinstalled on most distributions, so users can run it directly without installing anything. This convenience cannot be replaced by other languages. However, Bash scripts may not easily achieve accurate speed testing; using other languages for this is reinventing the wheel.
About software source selection
Many people may wonder, "Since scripts are so convenient and highly automated, why can't we automatically select the optimal software source?" It's not that it can't be done, but that it can't be done.
"Software source selection" here refers to the script automatically selecting a software source with the best experience based on the client's network environment and location. This question needs to be addressed from multiple perspectives:
Speed
As explained above, in summary, the pursuit of speed isn't a universal requirement. For most people, it's simply not noticeable; it's enough to get it working. Moreover, many Linux distributions now offer good speeds without switching sources.
Regarding mirror sites themselves
To give a simple example, Alibaba Cloud mirror sites were once considered the top choice due to their high speed and availability. However, as the number of users increased, their speeds became increasingly slower, sometimes even below 10 Mbps.
Over time, mirror sites with large user bases may not be able to handle the high traffic, resulting in a poor user experience. Mirror sites with fewer users may eventually close, which is detrimental to the development of the entire ecosystem.
From Developer's Perspective
This project aims to develop a universal repository switching script from the perspective of an operations and maintenance tool, rather than a tool that specifically addresses software repository requirements. The script's default behavior should be suitable for the majority of users. As an operations and maintenance tool, it should have a clear functional positioning. As a script, its pros and cons should be carefully considered from multiple perspectives, including scalability,and practicality. The choice of software repository should be fully vested in the user.
The repository here refers to the directory after the mirror site address, i.e., the repository directory on the mirror site. Although all are called repositories, it is easy to confuse.
Main scenario: The target mirror site has a corresponding system repository but does not match the default rule in this script.
Default repo names for each OS (click to expand)
The script does not adapt to unique repo names of a single mirror site for compatibility.
The repository directory name for Rocky Linux on Aliyun is rockylinux, not the default rocky, but you can override the default rule with the command option.
Some systems configure multiple repos at once. See command options for details.
Since the repo path is appended to the mirror address, you can use multi-level paths, e.g., --branch "linux/debian" -> https://mirrors.example.com/linux/debian
EPEL (Extra Packages for Enterprise Linux) is an additional repo maintained by Fedora, mainly for Red Hat-based distributions except Fedora itself. Configuring EPEL is a common need and is recommended by default.
Sometimes your desired mirror does not have EPEL. You can skip EPEL during the first run and then run:
The script adapts to EOL CentOS 7/8 and CentOS Stream 8.
Chinese mainland
Use the script as usual, no additional steps are required. As most mirror sites in Chinese mainland have already synchronized the CentOS Vault repository.
International
Use Tencent Cloud or Aliyun mirrors, or use --use-official-source true for the official repo.
EPEL 7 is also EOL and is supported by the script.
After the upgrade is complete and the system is rebooted, perform the following steps
# Clear the original software sources (If there is any non-system software source content, please back it up manually first)sed-i'1,$d'/etc/apt/sources.list
# Rerun the mirror switching scriptbash<(curl-sSLhttps://linuxmirrors.cn/main.sh)--en
EOL means End Of Life. Non-LTS Ubuntu versions are supported for only 9 months. Old versions are removed from the main repo and may require using the Ubuntu Old Releases repo.
See the official Wiki for version support and check each mirror for Ubuntu Old Releases support.
This feature is introduced to facilitate the use of developers and operation and maintenance personnel. After it is enabled, the script content output will be simplified. It is recommended to use it with other command options without interaction.
If you are a developer of another project and want to create a custom script based on this project, refer to the following. Some maintainers of China education network mirrors have already done so.
It is not recommended to modify the core logic. Keep in sync with the upstream source code. The script execution order is controlled by the main function and is clear.
You can simply remove irrelevant content, e.g., clear the mirror list arrays mirror_list_(default|edu|abroad|extranet|intranet), e.g., mirror_list_default=()
To customize script output, modify the run_start, run_end, and print_title functions.
To add custom shell content, see the execution order in main. Insert content directly or encapsulate in a function.
To reduce script size, delete unnecessary content. Besides the mirror lists, you can remove some modules:
Keep the original structure; empty out the function bodies if not needed.
Remove unused OS repo content gen_repo_files_xxx, which takes up over 60% of the script.
Remove unnecessary language packs, for example: function msg_pack_en() {}
Main configuration is controlled by unified variables, as are command options. These global variables are all uppercase and use underscores. See the table below. Declare them at the top of the script (reserved comment area) for quick customization.
Variable list (click to expand)
Variable
Meaning
Value Type
SOURCE
Specify mirror address (domain or IP)
address
SOURCE_EPEL
Specify EPEL repo address (domain or IP)
address
SOURCE_SECURITY
Specify Debian/Ubuntu security repo address (domain or IP)
address
SOURCE_VAULT
Specify CentOS/AlmaLinux vault repo address (domain or IP)
address
SOURCE_PORTAGE
Specify Gentoo portage repo address (domain or IP)
address
SOURCE_BASE_SYSTEM
Specify Linux Mint/Raspberry Pi OS base system repo address (domain or IP)
address
SOURCE_BRANCH
Specify mirror repo (path)
repo name
SOURCE_EPEL_BRANCH
Specify EPEL repo (path)
repo name
SOURCE_SECURITY_BRANCH
Specify Debian security repo (path)
repo name
SOURCE_VAULT_BRANCH
Specify CentOS/AlmaLinux vault repo (path)
repo name
SOURCE_PORTAGE_BRANCH
Specify Gentoo portage repo (path)
repo name
SOURCE_BASE_SYSTEM_BRANCH
Specify Linux Mint/Raspberry Pi OS base system repo (path)
repo name
DEBIAN_CODENAME
Specify Debian/openKylin codename
codename
USE_INTRANET_SOURCE
Prefer intranet mirror address
true or false
USE_OFFICIAL_SOURCE
Use official mirror of target OS
true or false
USE_OFFICIAL_SOURCE_EPEL
Use official EPEL repo
true or false
WEB_PROTOCOL
Specify Web protocol
http or https
INSTALL_EPEL
Install EPEL repository
true or false
ONLY_EPEL
Only switch EPEL repo
true or false
BACKUP
Backup original mirrors
true or false
IGNORE_BACKUP_TIPS
Ignore backup overwrite prompt
true or false
UPGRADE_SOFTWARE
Upgrade packages
true or false
CLEAN_CACHE
Clean cache after upgrade
true or false
CLEAN_SCREEN
Clear screen before running
true or false
PRINT_DIFF
Print diff before and after modification
true or false
PURE_MODE
Pure mode, minimal output
true or false
Some variables have defaults. Unused variables do not need to be declared as empty strings. If a configuration is missing, interaction may occur.