树莓派4B+ubuntu20.04设置国内源

步骤一:备份原始源列表

bash 复制代码
sudo cp /etc/apt/sources.list /etc/apt/sources.list.bak
sudo cp /etc/apt/sources.list.d/raspi.list /etc/apt/sources.list.d/raspi.list.bak

步骤二:更换主软件包源

请根据你的系统架构(通常是 aarch64/ARM64)进行修改。

  1. 编辑主源列表文件:

    bash 复制代码
    sudo nano /etc/apt/sources.list
  2. 注释或删除原有内容,替换为清华源:

    将文件内的所有内容替换为以下内容。确保使用的aarch64 架构:

    bash 复制代码
    # 默认注释了源码镜像以提高 apt update 速度,如有需要可自行取消注释
    deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ focal main restricted universe multiverse
    # deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ focal main restricted universe multiverse
    
    deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ focal-updates main restricted universe multiverse
    # deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ focal-updates main restricted universe multiverse
    
    deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ focal-backports main restricted universe multiverse
    # deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ focal-backports main restricted universe multiverse
    
    deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ focal-security main restricted universe multiverse
    # deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ focal-security main restricted universe multiverse
    
    # 预发布软件源,不建议启用
    # deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ focal-proposed main restricted universe multiverse
    # deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ focal-proposed main restricted universe multiverse

    关键点:

    • 树莓派是 ARM 架构,所以必须使用 ubuntu-ports 而不是普通的 ubuntu

    • URL 中的 focal 代表 Ubuntu 20.04 的代号。

步骤三:更换树莓派专用源

这个源包含一些树莓派的特定软件和内核更新。

  1. 编辑树莓派源列表文件:

    bash 复制代码
    sudo nano /etc/apt/sources.list.d/raspi.list
  2. 将其内容替换为清华镜像的树莓派源:

    bash 复制代码
    deb https://mirrors.tuna.tsinghua.edu.cn/raspberrypi/ focal main

    注意: 如果此文件原本是空的或者只有注释行,直接添加上面这一行即可。

步骤四:更新软件包列表

执行以下命令,让修改后的源生效。

bash 复制代码
# 更新软件包列表
sudo apt update

# 升级所有可升级的软件包(可选,但推荐)
sudo apt upgrade

如果 sudo apt update 命令执行过程中没有出现错误,并且下载链接显示来自 mirrors.tuna.tsinghua.edu.cn,就说明换源成功了。

bash 复制代码
命中:1 https://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports focal InRelease
命中:2 https://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports focal-updates InRelease
命中:3 https://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports focal-backports InRelease
命中:4 https://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports focal-security InRelease
获取:5 https://mirrors.tuna.tsinghua.edu.cn/raspberrypi focal InRelease [6,361 B]
...

常见问题与解决

  • 错误:Certificate verification failed: The certificate is NOT trusted.

    • 原因: 系统时间不正确或 CA 证书有问题。

    • 解决: 首先确保系统时间正确。如果时间正确,可以尝试安装正确的证书:

      bash 复制代码
      sudo apt install ca-certificates
  • 错误:404 Not Found [IP: ...]

    • 原因: 源地址拼写错误,或者该镜像站暂时没有某个特定的软件包版本。

    • 解决: 仔细检查你在 sources.list 中输入的 URL 是否正确。偶尔的 404 错误可以忽略,如果大量出现,可以暂时换回官方源或尝试其他国内源(如中科大源)。

  • 如何换回官方源或其他源?

    • 使用之前备份的文件恢复即可:

      bash 复制代码
      sudo cp /etc/apt/sources.list.bak /etc/apt/sources.list
      sudo cp /etc/apt/sources.list.d/raspi.list.bak /etc/apt/sources.list.d/raspi.list
      sudo apt update

如果你觉得清华源速度不理想,也可以尝试中国科学技术大学(USTC)源阿里云源。只需将上面步骤中的 URL 替换掉即可。

更新一些报错的解决方法

**报错:**the repository 'http://mirrors.tuna.tsinghua.edu.cn/ros/ubuntu focal InRelease' is no longer signed.

解决方法:

1. 删除旧的密钥

sudo apt-key del 421C365BD9FF1F717815A3895523BAEEB01FA116

2. 下载并添加新的密钥

sudo apt update

sudo apt install curl

curl -s https://raw.githubusercontent.com/ros/rosdistro/master/ros.asc | sudo apt-key add -

3. 或者直接从 ROS 官网下载密钥

sudo apt-key adv --keyserver 'hkp://keyserver.ubuntu.com:80' --recv-key C1CF6E31E6BADE8868B172B4F42ED6FBAB17C654

4. 更新软件包列表

sudo apt update

修复后验证是否成功:

bash 复制代码
# 检查更新是否成功
sudo apt update

# 检查 ROS 相关的包是否可安装
apt-cache search ros-foxy
相关推荐
raindayinrain10 分钟前
深入理解Linux内核--系统调用,性能优化
linux·性能优化·系统调用·返回值·入参·内核态用户态地址访问
码上有光16 分钟前
Linux:进程控制和进程替换
java·linux·服务器·进程控制·进程替换
爱和冰阔落25 分钟前
【Linux】多线程打印为什么会乱?pthread 创建、等待、退出、取消与分离全实战
linux·运维·c++·redis
DYWorker00125 分钟前
基于RK3568的Linux驱动开发:实战——WDT
linux·驱动开发
初願致夕霞27 分钟前
五种IO模型(详解非阻塞IO与多路转接)
linux·前端·网络·数据库·tcp/ip
技术猿禁33 分钟前
Linux运维开发
linux·运维·运维开发
脚踏实地,坚持不懈!39 分钟前
Android ANR 内核底层全解析:从一次触摸到“应用无响应”
android·linux
不会就选b43 分钟前
Linux之socket编程(三)
linux·运维·服务器
秋风&萧瑟1 小时前
【Linux系统编程】Linux IPC 的使用
linux·运维·网络
玖石书2 小时前
WSL2 手动安装 Ubuntu 24.04 到指定磁盘位置
linux·运维·ubuntu·wsl