The script of this project currently mainly serves Simplified Chinese users, but from the very beginning it was defined as a universal mirror switching script, providing many command options for customization, which is also one of the reasons why this project is now available to users of other languages. The script output is currently fixed in Simplified Chinese. With feedback and usage from users of other languages, the script may support Traditional Chinese and English in the future.
The documentation is translated with tools, and some professional terms below may be inaccurate. Corrections are welcome.
> Not supported in mainland China network environments.
!!! tip "Usage Help"
The tabs represent the built-in mirror types and script sources. Please check whether your target mirror supports your operating system before use. You can check in the [Mirror List](../mirrors/index.md).
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.
The script supports use in abnormal environments such as when the system's original software source is **damaged**, **missing**, **invalid**, etc., without the need to install any additional dependencies.
!!! quote "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.
<divclass="grid cards"markdown>
- :material-numeric-1:{style="color: #3CA7E5" .lg} __Run the script as `ROOT` user__
---
Switch command: `sudo -i` or `su root`. Different systems use different commands because some systems do not set a password for the ROOT account during initial installation (such as Ubuntu) or the system disables ROOT login by default.
- :material-numeric-2:{style="color: #3CA7E5" .lg} __It is recommended to use a modern `SSH` client__
---
If your terminal displays garbled Chinese, you will not be able to see the interactive content. Some GUI terminal apps may have unpredictable display issues. Some systems automatically enable SSH service; otherwise, see [how to enable SSH](#how-to-enable-ssh-remote-login).
- :material-numeric-3:{style="color: #3CA7E5" .lg} __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_](#about-command-not-found-error) below for installation. The script itself is unrelated to `curl` or `wget` and does not download any content.
- :material-numeric-4:{style="color: #3CA7E5" .lg} __Interactive configuration is required during script execution__
---
Use arrow keys ++arrow-up++++arrow-down++++arrow-left++++arrow-right++ or ++w++++a++++s++++d++ to control options and press ++enter++ to confirm. If interaction is abnormal, try resizing the terminal window and avoid full screen.
</div>
### FAQ
- #### About Command not found error
!!! quote ""
This error is caused by the system not having the `curl` package installed. Install it as follows:
Usually, systems without `curl` also lack `wget`, so this may not work.
``` bash
wget https://linuxmirrors.cn/main.sh
```
=== "Download with browser"
If you have GUI access, open the link in a browser to download the script. Many systems preinstall Firefox.
Then run the script:
=== ":material-home-city: Mainland China"
``` bash
bash main.sh
```
=== ":material-earth: International Regions"
``` bash
bash main.sh --abroad
```
=== ":material-library: China Education Network"
``` bash
bash main.sh --edu
```
If none of these work, copy the [source code](https://gitee.com/SuperManito/LinuxMirrors/raw/main/ChangeMirrors.sh) to a new `.sh` file and run it with `bash`.
- #### Backup of original mirrors
!!! quote ""
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.
> Service names may differ; if not `sshd`, try `ssh`.
- #### No arrow key interactive UI
!!! quote ""
If you see a text input instead of the new arrow key UI, install the `ncurses` or `nano` package. The new interaction relies on the `tput` command.
- #### About the script's online location
!!! quote ""
The project uses [GitHub Action](https://github.com/SuperManito/LinuxMirrors/blob/main/.github/workflows/build-docs.yml#L29) to automatically copy the source code to the docs directory after each commit for website publishing. The site is hosted on :netlify: [Netlify](https://www.netlify.com), 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
!!! quote ""
First, see the recommendations in the [Mirror List](../mirrors/index.md) 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 disabled repositories
!!! quote ""
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:
=== "Debian-based / openKylin"
By default, `deb-src` and `proposed` are disabled. Uncomment the relevant lines in `/etc/apt/sources.list` to enable.
The repository here refers to the path after the mirror address, i.e., the branch on the mirror site. Although all are called repositories, it is easy to confuse.
Main scenario: The target mirror site has a corresponding system repo but does not match the default rule in this script.
??? note "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 repo name for Rocky Linux on Aliyun is [`rockylinux`](https://mirrors.aliyun.com/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`
- ### Switch EPEL repo only
!!! info "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:
For EPEL 7 (EOL), use the [`archive`](https://dl.fedoraproject.org/pub/archive/epel) repo. For overseas networks, use `--use-official-source-epel true` to use the official repo.
- ### Restore official mirrors
If you accidentally deleted the backup of the official mirror, use this command to restore. This skips mirror selection.
After switching, run `apt-get dist-upgrade` and reboot, then rerun the script. Only changing the codename may cause compatibility issues later.
``` { .bash title="If the script can't set the codename, you can manually replace it after running" }
sed -i "s/$(lsb_release -cs)/your-codename/g" /etc/apt/sources.list
```
- #### Switch Ubuntu EOL version mirrors
!!! info "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](https://wiki.ubuntu.com/Releases) for version support and check each mirror for `Ubuntu Old Releases` support.
This feature is introduced to facilitate developers. After it is enabled, the script content output will be simplified. It is recommended to use it with other command options without interaction.
!!! tip "Scrolling command logs may have unpredictable display issues, but no problems have been found so far."
---
## Customizing the Script
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.
1. 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.
2. 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=()`
3. To customize script output, modify the `run_start`, `run_end`, and `print_title` functions.
4. To add custom shell content, see the execution order in `main`. Insert content directly or encapsulate in a function.
5. To reduce script size, delete unnecessary content. Besides the mirror lists, you can remove some modules:
1. Keep the original structure; empty out the function bodies if not needed.
2. Remove unused OS repo content `gen_repo_files_xxx`, which takes up over 60% of the script.
6. 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.