Skip to content

Instantly share code, notes, and snippets.

@Sakura286
Created July 23, 2024 09:42
Show Gist options
  • Save Sakura286/954d42ac8c504ef25dd4bfe910869f3c to your computer and use it in GitHub Desktop.
Save Sakura286/954d42ac8c504ef25dd4bfe910869f3c to your computer and use it in GitHub Desktop.

搭建 apt 仓库

目标

使用本地包搭建私有 apt 仓库

思路

这里的 server 指的是提供 apt 仓库服务的机器,client 指的是要访问 apt 仓库的机器

  1. (server) 生成 GPG 私钥
  2. (server) 使用 aptly 添加包,并生成仓库的 snapshot
  3. (server) 使用 nginx 搭建私有的服务器,存放生成的 snapshot
  4. (client) 修改 apt 配置

步骤

1. (server) 生成签名用的 GPG KEY

运行命令:gpg --full-gen-key,按照提示操作。

最后将在~/.gnupg/openpgp-revocs.d/目录下生成.rev的 key 文件

2. (server) 使用 aptly 添加包,并生成仓库的 snapshot

安装 aptly 后,创建仓库,本步骤执行后将生成~/.aptly目录

# 请拓展尖括号的内容, 示例如下
# aptly repo create -architectures riscv64 -comment 'private riscv64 repo' -component main -distribution rockos-private my_repo
aptly repo create -architectures <arch> -comment <your_comment> -component <component> -distribution <distrubution> <repo_name>

向仓库内添加若干包,本步骤执行后将在~/.aptly/pool目录下存放所添加包的拷贝

aptly repo add <repo_name> <pkg_path>

创建仓库 snapshot 并发布,此过程会要求使用 GPG 私钥签名,所以会提示输入生成 GPG 私钥时的密码

snapshot 发布后的目录为~/.aptly/public

aptly snapshot create <snapshot_name> from repo <repo_name>
aptly publish -architectures <arch> snapshot <snapshot_name>

3. (server) 搭建服务器

使用 nginx 搭建文件服务器,使得外部网络可以访问仓库内容

sudo apt-get install nginx -y

编辑/etc/nginx/sites-enabled/default,将root /var/www.html;中的地址修改为 snapshot 发布后的目录。并在其下方添加autoindex on;一项

重启 nginx

sudo systemctl restart nginx.service

最后用浏览器访问一下服务器IP,验证是否搭建成功

4. (client) 访问 apt 仓库

/etc/apt/sources.list中写入如下一行

# 例如,我这边的是 deb [trusted=yes] http://10.9.127.200/public/ unstable main
deb [trusted=yes] <server_path> <distrubution> <component>
# 如果添加了源码包的话,还可以继续添加如下一行
# deb-src [trusted=yes] <server_path> <distrubution> <component>

其他问题

1. publish 后,向仓库添加新的包

需要先 drop 掉原先已经 publish 的 snapshot

aptly publish drop <distrubution> .

2. 如何添加源码包?

直接repo add目标包的.dsc文件即可,aptly 会自动添加源码包

脚本

最后附上自己在用的脚本,可以较为方便地添加新包并发布,使用前请适当修改

#!/bin/bash
set -x

# please modify this
REPO="rerererererepo"
DISTRIBUTION="dididididistribution"
SNAPSHOT=$(date "+%m%d_%M%S")

aptly publish drop $DISTRIBUTION .
aptly repo add $REPO $(ls | grep -E "\.deb$" | tr "\n" " ") $(ls | grep -E "\.dsc$" | tr "\n" " ")
aptly snapshot create $SNAPSHOT from repo $REPO
aptly publish -distribution=$DISTRIBUTION snapshot $SNAPSHOT

参考资料

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment