apt 使用

使用

apt 命令 功能
apt install 安装软件包
apt remove 移除软件包
apt purge 移除软件包及配置文件
apt update 刷新存储库索引
apt upgrade 升级所有可升级的软件包
apt autoremove 自动删除不需要的包
apt full-upgrade 在升级软件包时自动处理依赖关系
apt search 搜索应用程序
apt show 显示安装细节

whereis <package>: 查找已安装的二进制包的位置

换源

关于 APT 源配置文件的介绍:/etc/apt/sources.list 文件和 /etc/apt/sources.list.d 目录介绍 | 博客园

APT 的源配置文件位于 /etc/apt/sources.list

在换源之前,你可以先备份你的原始文件:

sudo cp /etc/apt/sources.list{,.bak}  # 文件备份到 sources.list.bak

使用命令行换源

x86

该镜像仅适用于配置 x86 架构下的 Ubuntu系统,如果你的系统为 ARM,PowerPC 等其他架构,请使用 ubuntu-ports 源进行配置。

你可以直接通过一行命令来完成 APT 换源:

sudo sed -i 's@//.*archive.ubuntu.com@//mirrors.ustc.edu.cn@g' /etc/apt/sources.list  # 使用中科大源

因镜像站同步有延迟,可能会导致生产环境系统不能及时检查、安装上最新的安全更新,不建议替换 security 源:

sudo sed -i -r 's/security.ubuntu.com/mirrors.ustc.edu.cn/g' /etc/apt/sources.list  # 替换 security 源(可选)

最后更新 APT 索引:

sudo apt update  # 更新索引

可用的镜像站:

Ubuntu Ports

如果你使用 ARM,PowerPC 等架构的 Ubuntu 系统,请使用 ubuntu-ports 源进行配置:

sudo sed -i -r 's/ports.ubuntu.com/mirrors.ustc.edu.cn/g' /etc/apt/sources.list  # 使用中科大的 ubuntu-ports 源

不要忘了更新索引:

sudo apt update  # 更新索引

可用的镜像站:

EOL 发行版换源

对于 EOL 发行版,需要使用 old-releases.ubuntu.com

EOL: End Of Life,是那些过于古早的发行版,已经不再维护。

使用官方 old-releases 源(非镜像):

sudo sed -i -r 's/([a-z]{2}\.)?archive.ubuntu.com/old-releases.ubuntu.com/g' /etc/apt/sources.list
sudo sed -i -r 's/security.ubuntu.com/old-releases.ubuntu.com/g' /etc/apt/sources.list

使用中科大镜像:

sudo sed -i 's@//.*archive.ubuntu.com/ubuntu@//mirrors.ustc.edu.cn/ubuntu-old-releases@g' /etc/apt/sources.list
sudo sed -i -r 's@security.ubuntu.com/ubuntu@mirrors.ustc.edu.cn/ubuntu-old-releases@g' /etc/apt/sources.list

最后更新 APT 索引:

sudo apt update  # 更新索引

可用的镜像站:

参考:

使用配置文件换源

你也可以直接使用现成文件覆盖原来的 sources.list 文件来换源。中科大镜像站给出了 Ubuntu 各个版本号的配置文件:

repository file generator

选择你的 Ubuntu 版本,复制配置文件,并粘贴到 /etc/apt/sources.list

最后不要忘了更新索引:

sudo apt update  # 更新索引

对于 ubuntu-ports 镜像,你只需将配置文件中的 https://mirrors.xxxx.xxx/ubuntu/ 改为 https://mirrors.xxxx.xxx/ubuntu-ports/ 即可。

Troubleshooting

apt-get 出现 Err 404 Not Found 的解决办法

posted @ 2024-05-22 13:35  Undefined443  阅读(2)  评论(0编辑  收藏  举报