为树莓派一键更换国内apt镜像源

关于如何为树莓派启用root系统并开启ssh登录,可参考之前的文章

  • 为什么要使用国内镜像源?
    树莓派官方raspbian系统默认设置软件源为
    http://raspbian.raspberrypi.org/raspbian/http://archive.raspberrypi.org/debian/,这两个网站在国内通常连接缓慢,影响使用体验。
  • 如何更换国内源?
    raspbian系统的apt软件源由/etc/apt/sources.list和/etc/apt/sources.list.d/raspi.list两个文件设定,只需更改两个文件内的记录即可。
  • 国内源那么多,该选哪一个呢?

国内支持raspbian的软件源(不完全统计,欢迎补充):

https://mirrors.cloud.tencent.com/raspbian/raspbian/
https://mirrors.cloud.tencent.com/raspberrypi/

https://mirrors.tuna.tsinghua.edu.cn/raspbian/raspbian/
https://mirrors.tuna.tsinghua.edu.cn/raspberrypi/

https://mirrors.sjtug.sjtu.edu.cn/raspbian/raspbian/
https://mirrors.sjtug.sjtu.edu.cn/raspberrypi/debian/

https://mirrors.ustc.edu.cn/raspbian/raspbian/
https://mirrors.ustc.edu.cn/archive.raspberrypi.org/debian/

https://mirrors.aliyun.com/raspbian/raspbian/

http://mirrors.zju.edu.cn/raspbian/raspbian/

http://mirrors.neusoft.edu.cn/raspbian/raspbian/

https://mirrors.cqu.edu.cn/raspbian/raspbian/

  • 为避免误更换为无效的源,先对配置文件在同目录进行备份:
sudo cp /etc/apt/sources.list /etc/apt/sources.list.save
sudo cp /etc/apt/sources.list.d/raspi.list /etc/apt/sources.list.d/raspi.list.save
  • 如需还原应执行:
sudo cp /etc/apt/sources.list.save /etc/apt/sources.list
sudo cp /etc/apt/sources.list.d/raspi.list.save /etc/apt/sources.list.d/raspi.list
  • 下面即可通过sed命令修改配置文件

上海交通大学SJTUG软件源

 sudo sed -i "s?http://raspbian.raspberrypi.org/raspbian/?https://mirrors.sjtug.sjtu.edu.cn/raspbian/raspbian/?g" /etc/apt/sources.list
 sudo sed -i "s?http://archive.raspberrypi.org/debian/?https://mirrors.sjtug.sjtu.edu.cn/raspberrypi/debian/?g" /etc/apt/sources.list.d/raspi.list

腾讯软件源

 sudo sed -i "s?http://raspbian.raspberrypi.org/raspbian/?https://mirrors.cloud.tencent.com/raspbian/raspbian/?g" /etc/apt/sources.list
 sudo sed -i "s?http://archive.raspberrypi.org/debian/?https://mirrors.cloud.tencent.com/raspberrypi/?g" /etc/apt/sources.list.d/raspi.list

清华大学开源软件镜像站TUNA

 sudo sed -i "s?http://raspbian.raspberrypi.org/raspbian/?https://mirrors.tuna.tsinghua.edu.cn/raspbian/raspbian/?g" /etc/apt/sources.list
 sudo sed -i "s?http://archive.raspberrypi.org/debian/?https://mirrors.tuna.tsinghua.edu.cn/raspberrypi/?g" /etc/apt/sources.list.d/raspi.list

中国科学技术大学开源软件镜像

 sudo sed -i "s?http://raspbian.raspberrypi.org/raspbian/?https://mirrors.ustc.edu.cn/raspbian/raspbian/?g" /etc/apt/sources.list
 sudo sed -i "s?http://archive.raspberrypi.org/debian/?https://mirrors.ustc.edu.cn/archive.raspberrypi.org/debian/?g" /etc/apt/sources.list.d/raspi.list

其他为提供raspberrypi存储库的源未写成一键脚本。

  • 修改完成后即可使用apt进行软件包升级
 sudo apt update
 sudo apt upgrade
### 更改树莓派上的Debian默认镜像源国内镜像源 #### 准备工作 为了确保更改过程顺利,在开始之前建议更新当前系统的包列表并升级已安装的软件包。这可以通过执行以下命令来完成: ```bash sudo apt update && sudo apt upgrade -y ``` #### 修改`sources.list`文件 要将默认的Debian镜像源更改为国内镜像源,比如清华大学开源软件镜像站提供的镜像源,则需编辑`/etc/apt/sources.list`文件。 可以使用文本编辑器如nano打开此文件: ```bash sudo nano /etc/apt/sources.list ``` 删除原有内容,并替换为适合版本的清华源配置。对于基于Debian Bookworm的Raspberry Pi OS而言,应输入如下行[^3]: ``` deb https://mirrors.tuna.tsinghua.edu.cn/debian bookworm main contrib non-free deb-src https://mirrors.tuna.tsinghua.edu.cn/debian bookworm main contrib non-free deb https://mirrors.tuna.tsinghua.edu.cn/debian-security bookworm-security main contrib non-free deb https://mirrors.tuna.tsinghua.edu.cn/debian bookworm-updates main contrib non-free deb https://mirrors.tuna.tsinghua.edu.cn/debian bookworm-backports main contrib non-free ``` 保存修改后的文件并退出编辑器(如果使用的是nano编辑器,可通过按Ctrl+X键,然后确认保存)。 #### 更新APT缓存 完成上述操作之后,应当再次刷新apt-get的缓存以使新的设置生效: ```bash sudo apt update ``` 此时应该可以看到正在从新指定的地址下载索引文件的信息显示出来。 通过以上步骤即可成功切换到更快捷稳定的国内镜像源,从而加速后续软件包的安装与更新流程[^1]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值