制作deb软件包

4115 字
21 分钟
制作deb软件包

一、制作deb包#

1.1 deb包构建工具文件#

制作deb包使用的依赖工具

Terminal window
sudo apt update
sudo apt install build-essential devscripts debhelper dh-make

各工具作用:

工具作用
build-essential提供基本编译环境
devscripts包含大量有用脚本
debhelper制作 deb 包的核心工具
dh-make快速生成包的模板

1.1.1 基本结构#

假设打包一个名为 mymonitor 的监控工具:

Terminal window
mymonitor-1.0/ # 主工作目录
├── src/ # 源码目录
└── mymonitor # 可执行文件
├── config/ # 配置文件目录
└── mymonitor.conf # 配置文件
├── systemd/ # systemd 服务文件目录
└── mymonitor.service # 服务文件
└── debian/ # deb 包制作目录
├── control # 包信息文件
├── rules # 构建规则文件
├── changelog # 变更日志
├── copyright # 版权信息
├── compat # 兼容性版本
├── postinst # 安装后脚本
├── prerm # 卸载前脚本
└── postrm # 卸载后脚本

1.1.2 核心构建文件#

1.1.2.1 control 文件#

这个文件定义了包的基本信息:

Source: mymonitor
Section: utils
Priority: optional
Maintainer: Your Name <your.email@example.com>
Build-Depends: debhelper (>= 10)
Standards-Version: 4.1.2
Package: mymonitor
Architecture: amd64
Depends: python3, systemd
Description: A simple monitoring tool
This is a monitoring tool for system resources.
It provides real-time monitoring capabilities and can
send alerts when thresholds are exceeded.

注意:Description字段的格式比较特殊。第一行是简短描述,后面的详细描述每行都要以空格开头,否则会构建失败

1.1.2.2 rules 文件#

这个文件定义了如何构建和安装包:

#!/usr/bin/make -f
%:
dh $@
override_dh_auto_install:
mkdir -p debian/mymonitor/usr/bin
mkdir -p debian/mymonitor/etc/mymonitor
mkdir -p debian/mymonitor/var/log/mymonitor
mkdir -p debian/mymonitor/lib/systemd/system
cp src/mymonitor debian/mymonitor/usr/bin/
cp config/mymonitor.conf debian/mymonitor/etc/mymonitor/
cp systemd/mymonitor.service debian/mymonitor/lib/systemd/system/
chmod +x debian/mymonitor/usr/bin/mymonitor

注意:需要给rule文件执行权限

Terminal window
chmod +x debian/rules

rules 文件结构与语法#

debian/rules 使用 GNU Make 语法,它的第一行必须是 Shebang,指定解释器:

#!/usr/bin/make -f

指明了使用 make 程序来解析和执行这个文件。同时,这个文件本身必须具备可执行权限

  • %: 这是一个模式规则,它会匹配所有未明确指定的目标。
  • dh $@: dh 是 debhelper 的核心命令,$@ 是 Makefile 的自动变量,代表当前被调用的目标名。

当执行 debian/rules build 时,% 规则会捕获 build,并执行 dh builddh 命令则会按顺序调用一系列以 dh_ 开头的工具(如 dh_auto_configure, dh_auto_build 等)来完成工作。

**例如:**自定义命令

#!/usr/bin/make -f
# 覆盖 dh_auto_build 默认行为,在构建前执行自定义脚本
override_dh_auto_build:
./my-custom-build-script.sh
dh_auto_build
# 在安装阶段,将额外文件复制到临时目录
override_dh_auto_install:
dh_auto_install
cp my-extra-file debian/my-package/usr/share/my-app/
%:
dh $@

1.1.2.3 changelog 文件#

这个文件记录版本变更历史,格式很严格:

Terminal window
mymonitor (1.0-1) unstable; urgency=medium
* Initial release
* Added basic monitoring functionality
* Added systemd service support
-- Your Name <your.email@example.com> Mon, 15 Jan 2024 10:00:00 +0800

**注意:**时间格式必须严格遵循 RFC 2822 标准,建议用 date -R 生成。

版权信息文件:

Terminal window
Format: https://www.debian.org/doc/packaging-manuals/copyright-format/1.0/
Upstream-Name: mymonitor
Source: https://github.com/yourname/mymonitor
Files: *
Copyright: 2024 Your Name <your.email@example.com>
License: MIT

1.1.2.5 compat 文件#

只需一个数字,表示 debhelper 的兼容级别:

Terminal window
10

1.1.2.6 postinst 文件#

安装后执行执行的脚本文件,例如:创建用户、设置权限、启用服务等操作

#!/bin/bash
set -e
# 创建用户
if ! id "mymonitor" &>/dev/null; then
useradd -r -s /bin/false mymonitor
fi
# 设置权限
chown mymonitor:mymonitor /var/log/mymonitor
chmod 755 /var/log/mymonitor
# 启用并启动服务
systemctl daemon-reload
systemctl enable mymonitor
systemctl start mymonitor
exit 0

1.1.2.7 prerm 文件#

卸载前执行的脚本文件,例如卸载钱停止服务等操作

#!/bin/bash
set -e
if systemctl is-active --quiet mymonitor; then
systemctl stop mymonitor
fi
if systemctl is-enabled --quiet mymonitor; then
systemctl disable mymonitor
fi
exit 0

1.1.2.8 postrm 文件#

卸载后执行的脚本文件,例如清理用户和数据等操作

#!/bin/bash
set -e
case "$1" in
purge)
if id "mymonitor" &>/dev/null; then
userdel mymonitor
fi
rm -rf /var/log/mymonitor
;;
remove)
# 只是删除包,保留配置
;;
esac
systemctl daemon-reload
exit 0

**注意:**所有脚本必须添加执行权限

Terminal window
chmod +x debian/postinst debian/prerm debian/postrm

1.2 准备源码文件#

1.2.1 源码文件#

准备源码文件src/mymonitor

#!/usr/bin/env python3
import time
import psutil
import json
def main():
while True:
cpu_percent = psutil.cpu_percent(interval=1)
memory = psutil.virtual_memory()
data = {
'cpu': cpu_percent,
'memory': memory.percent,
'timestamp': time.time()
}
print(json.dumps(data))
time.sleep(10)
if __name__ == '__main__':
main()

1.2.2 配置文件#

准备配置文件config/mymonitor.conf

[monitor]
interval = 10
cpu_threshold = 80
memory_threshold = 85
[logging]
level = INFO
file = /var/log/mymonitor/mymonitor.log

1.2.3 systemd 服务文件#

准备systemd 服务文件systemd/mymonitor.service

Terminal window
[Unit]
Description=MyMonitor Service
After=network.target
[Service]
Type=simple
User=mymonitor
ExecStart=/usr/bin/mymonitor
Restart=always
RestartSec=10
[Install]
WantedBy=multi-user.target

1.3 构建与测试#

1.3.1 构建deb包#

Terminal window
cd mymonitor-1.0
dpkg-buildpackage -us -uc -b

参数说明:

  • -us:不签名源码包
  • -uc:不签名变更文件
  • -b:只构建二进制包

构建成功后在上级目录生成 mymonitor_1.0-1_amd64.deb

常见构建错误#

  1. 文件权限问题:确保所有脚本都有执行权限
  2. 格式错误:特别是 changelog 和 control 文件的格式
  3. 依赖问题:检查 Build-Depends 是否正确

1.3.2 测试安装#

Terminal window
# 安装
sudo dpkg -i ../mymonitor_1.0-1_amd64.deb
# 检查文件
ls -la /usr/bin/mymonitor
ls -la /etc/mymonitor/
ls -la /var/log/mymonitor/
# 检查服务
systemctl status mymonitor
# 检查用户
id mymonitor
# 卸载(保留配置)
sudo dpkg -r mymonitor
# 完全清理(包括配置文件)
sudo dpkg -P mymonitor
# 查看包内容
dpkg -c mymonitor_1.0-1_amd64.deb
# 查看包信息
dpkg -I mymonitor_1.0-1_amd64.deb
# 模拟安装
dpkg --simulate -i mymonitor_1.0-1_amd64.deb
# 检查包质量
lintian mymonitor_1.0-1_amd64.deb

1.4 实用技巧#

1.4.1 使用 dh_make 快速生成模板#

Terminal window
dh_make -e your.email@example.com -f ../mymonitor-1.0.tar.gz

1.4.2 处理配置文件#

如果deb包包含配置文件,可以在debian目录下创建conffiles文件:

添加以下内容

Terminal window
/etc/mymonitor/mymonitor.conf

这样dpkg就知道这是配置文件,升级时会妥善处理

1.4.3 添加依赖检查#

postinst脚本中可以添加一些依赖检查:

Terminal window
# 检查Python3是否安装
if ! command -v python3 &> /dev/null; then
echo "Error: Python3 is required but not installed"
exit 1
fi
# 检查psutil模块
if ! python3 -c "import psutil" &> /dev/null; then
echo "Installing psutil..."
pip3 install psutil
fi

1.4.4 处理多架构#

如果软件需要支持多种架构,可以在control文件中设置:

Terminal window
Architecture: any
# 或者指定具体架构:
Architecture: amd64 arm64

1.4.5 添加预依赖和冲突#

有时候构建的包可能与其他包冲突,或者需要在某个包之前安装:

Terminal window
Pre-Depends: some-package
Conflicts: conflicting-package
Replaces: old-package

1.4.6 使用triggers#

如果构建的包需要在其他包安装后执行某些操作,可以使用triggers机制。创建debian/triggers文件,添加如下内容:

Terminal window
interest /usr/share/applications

然后在postinst文件中处理trigger

Terminal window
case "$1" in
triggered)
# 处理trigger事件
update-desktop-database
;;
esac

1.4.7 分包#

对于复杂的软件,可能需要分成多个包。比如主程序包、开发包、文档包等。在control文件中定义多个Package段即可。

1.5 定制nginx模块打包成deb包#

把自定义编译的nginx打包成deb包,添加一些第三方模块

Terminal window
# 下载源码包
wget http://nginx.org/download/nginx-1.20.2.tar.gz
tar -xzf nginx-1.20.2.tar.gz
cd nginx-1.20.2
# 创建debian目录结构
mkdir debian

1.5.1 创建control 文件#

Terminal window
cat > ./debian/control << 'EOF'
Source: nginx-custom
Section: httpd
Priority: optional
Maintainer: Your Name <your.email@example.com>
Build-Depends: debhelper (>= 10), libssl-dev, libpcre3-dev, zlib1g-dev, libgeoip-dev
Standards-Version: 4.1.2
Package: nginx-custom
Architecture: amd64
Depends: ${shlibs:Depends}, ${misc:Depends}, adduser, lsb-base
Provides: httpd, nginx
Conflicts: nginx, nginx-full, nginx-light
Description: High performance web server (custom build)
Nginx is a web server with a strong focus on high
concurrency, performance and low memory usage.
This is a custom build with additional modules.
EOF

${shlibs:Depends}会自动检测动态库依赖,Conflicts字段防止与系统自带的nginx冲突。

参数解释:

  • Source: nginx-custom
  • 含义:定义了这个源码包的名称。
  • 作用:当你使用 apt-get source 下载源码时,会显示这个名字。
  • Section: httpd
  • 含义:软件分类。
  • 作用:告诉包管理器这个软件属于哪一类(这里是 Web 服务器)。在 Debian/Ubuntu 仓库中,常见的还有 utils, net, admin 等。
  • Priority: optional
  • 含义:安装优先级。
  • 作用optional 表示这是大多数用户可能会用到的标准软件,但不是系统运行所必须的(区别于 requiredimportant)。
  • Maintainer: Your Name your.email@example.com
  • 含义:维护者信息。
  • 作用:当用户遇到 Bug 或需要反馈时,会联系这个人。建议修改为你自己的真实邮箱
  • Build-Depends: debhelper (>= 10), libssl-dev, …
  • 含义编译依赖。
  • 作用:这是在打包过程(dpkg-buildpackage)中需要的库,而不是安装后运行需要的。
    • debhelper: 辅助打包的工具集。
    • libssl-dev: 编译 SSL 模块(HTTPS)必须的头文件。
    • libpcre3-dev: 正则表达式库(Nginx 重写规则需要)。
    • zlib1g-dev: 压缩库(Gzip 功能需要)。
  • Standards-Version: 4.1.2
  • 含义:遵循的 Debian 策略版本。
  • 作用:声明你的包符合哪个版本的 Debian Policy Manual 规范。
  • Package: nginx-custom
  • 含义:最终安装时的软件包名称。
  • 作用:用户使用 apt install nginx-custom 时就是搜这个名字。
  • Architecture: amd64
  • 含义:适用架构。
  • 作用:限制该包只能在 64 位 x86 架构上安装。如果你希望它能在 arm64 或其他架构安装,应改为 any
  • Depends: shlibs,shlibs:Depend**s, {misc}, adduser, lsb-base
  • 含义运行依赖。
  • 作用:安装此包之前必须先安装的软件。
    • ${shlibs:Depends}: 非常重要。这是一个自动变量,打包工具会自动扫描你的二进制文件链接了哪些动态库(如 libc, libssl),并自动填入这里。
    • adduser: Nginx 启动时需要创建 nginx 用户,依赖此工具。
    • lsb-base: 提供标准的 init 脚本函数库(如果你的启动脚本用到了 /lib/lsb/init-functions)。
  • Provides: httpd, nginx
  • 含义:虚拟包提供。
  • 作用:宣称“我就是 nginx”。如果有其他软件依赖 nginx,安装你的 nginx-custom 也能满足它的依赖要求。
  • Conflicts: nginx, nginx-full, nginx-light
  • 含义:冲突列表。
  • 作用防止共存。因为你的包也会占用 80 端口或生成同名配置文件,所以不能与官方原版 Nginx 同时存在。如果用户电脑里装了官方版,dpkg 会报错阻止安装,或者提示卸载官方版。
  • Description: High performance web server…
  • 含义:软件描述。
  • 格式注意:第一行是短描述,第二行开始是长描述。长描述的每一行开头必须有一个空格,否则会被视为新段落或格式错误。

1.5.2 创建rules 文件#

建议不要修改程序的安装路径,遵循 Linux 文件系统层次结构标准,将二进制放在 /usr/sbin,配置放在 /etc/nginx,数据放在 /var/www

#!/usr/bin/make -f
export DEB_BUILD_MAINT_OPTIONS = hardening=+all
export DEB_CFLAGS_MAINT_APPEND = -Wp,-D_FORTIFY_SOURCE=2 -fPIC
%:
dh $@
override_dh_auto_configure:
./configure \
--prefix=/etc/nginx \
--sbin-path=/usr/sbin/nginx \
--modules-path=/usr/lib/nginx/modules \
--conf-path=/etc/nginx/nginx.conf \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--pid-path=/var/run/nginx.pid \
--lock-path=/var/run/nginx.lock \
--http-client-body-temp-path=/var/cache/nginx/client_temp \
--http-proxy-temp-path=/var/cache/nginx/proxy_temp \
--http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp \
--http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp \
--http-scgi-temp-path=/var/cache/nginx/scgi_temp \
--user=nginx \
--group=nginx \
--with-http_ssl_module \
--with-http_realip_module \
--with-http_addition_module \
--with-http_sub_module \
--with-http_dav_module \
--with-http_flv_module \
--with-http_mp4_module \
--with-http_gunzip_module \
--with-http_gzip_static_module \
--with-http_random_index_module \
--with-http_secure_link_module \
--with-http_stub_status_module \
--with-http_auth_request_module \
--with-http_xslt_module=dynamic \
--with-http_image_filter_module=dynamic \
--with-http_geoip_module=dynamic \
--with-threads \
--with-stream \
--with-stream_ssl_module \
--with-stream_ssl_preread_module \
--with-stream_realip_module \
--with-stream_geoip_module=dynamic \
--with-http_slice_module \
--with-file-aio \
--with-http_v2_module
override_dh_auto_install:
$(MAKE) DESTDIR=$(CURDIR)/debian/nginx-custom install
mkdir -p debian/nginx-custom/var/cache/nginx
mkdir -p debian/nginx-custom/var/log/nginx
mkdir -p debian/nginx-custom/etc/nginx/conf.d
mkdir -p debian/nginx-custom/etc/nginx/sites-available
mkdir -p debian/nginx-custom/etc/nginx/sites-enabled
mkdir -p debian/nginx-custom/usr/share/nginx/html
mkdir -p debian/nginx-custom/lib/systemd/system
cp debian/nginx.conf debian/nginx-custom/etc/nginx/
cp debian/default.conf debian/nginx-custom/etc/nginx/conf.d/
cp debian/nginx.service debian/nginx-custom/lib/systemd/system/
cp debian/index.html debian/nginx-custom/usr/share/nginx/html/
override_dh_auto_clean:
dh_auto_clean
rm -f objs/Makefile

参数解释:

  • export DEB_BUILD_MAINT_OPTIONS = hardening=+all

  • 含义:开启所有安全加固选项。

  • 作用:这会让编译器启用如栈保护、位置无关代码等安全特性,防止软件被恶意利用漏洞攻击。这是现代 Linux 发行版的标准要求。

  • export DEB_CFLAGS_MAINT_APPEND = ...

  • 含义:追加额外的 C 语言编译标志。

  • 作用-fPIC 确保生成的二进制文件是位置无关的,这对于共享库和某些安全机制是必须的。

  • verride_dh_auto_configure:这一部分覆盖了默认的自动配置行为,手动执行 Nginx 的 ./configure 脚本。这里的路径决定了 Nginx 安装后的目录结构。

  • override_dh_auto_install:这一部分告诉构建系统如何将编译好的文件“搬运”到临时的打包目录中

  • $(MAKE) DESTDIR=$(CURDIR)/debian/nginx-custom install

  • 含义:执行 make install,但通过 DESTDIR 将文件安装到一个临时目录,而不是真正的系统目录。

  • 作用:这是打包的关键!它创建了一个“虚拟的系统根目录”,所有文件都会进入 debian/nginx-custom/ 文件夹下,最终这个文件夹的内容会被压缩成 .deb 包。

1.5.3 创建postinst 脚本#

cat > ./debian/postinst << 'EOF'
#!/bin/bash
set -e
case "$1" in
configure)
# 创建 nginx 用户
if ! getent group nginx >/dev/null; then
addgroup --system nginx
fi
if ! getent passwd nginx >/dev/null; then
adduser --system --disabled-login --ingroup nginx \
--no-create-home --home /nonexistent \
--gecos "nginx user" --shell /bin/false nginx
fi
# 设置目录权限
chown -R nginx:nginx /var/log/nginx
chmod 755 /var/log/nginx
chown -R nginx:nginx /var/cache/nginx
chmod 700 /var/cache/nginx
# 测试配置文件并启动服务
if nginx -t 2>/dev/null; then
systemctl daemon-reload
systemctl enable nginx.service
systemctl start nginx.service
fi
;;
esac
exit 0
EOF

1.5.4 创建changelog文件#

Terminal window
cat > ./debian/changelog << 'EOF'
nginx-custom (1.20.2-1) unstable; urgency=medium
* Custom nginx build with additional modules:
- --with-http_ssl_module
- --with-http_v2_module
- --with-stream
- --with-stream_ssl_module
* Optimized configuration path and default settings
* Added systemd service unit
-- Zhang San <zhangsan@example.com> Mon, 13 Jul 2026 14:30:00 +0800
EOF

1.5.5 创建prerm文件#

cat > ./debian/prerm << 'EOF'
#!/bin/bash
set -e
case "$1" in
remove|upgrade|deconfigure)
if [ -x /usr/sbin/nginx ]; then
if systemctl is-active nginx.service >/dev/null 2>&1; then
systemctl stop nginx.service
fi
fi
;;
esac
exit 0
EOF

1.5.6 创建compat文件#

Terminal window
cat > ./debian/compat << 'EOF'
10
EOF

1.5.7 创建ngin.conf文件#

Terminal window
cat <<eoof >nginx.conf
user nginx;
worker_processes auto;
pid /run/nginx.pid;
events {
worker_connections 1024;
}
http {
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
error_log /var/log/nginx/error.log;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
include /etc/nginx/mime.types;
default_type application/octet-stream;
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}
eoof

1.5.8 创建nginx.service文件#

Terminal window
cat <<eoof >nginx.service
[Unit]
Description=A high performance web server and a reverse proxy server
Documentation=man:nginx(8)
After=network.target nss-lookup.target
[Service]
Type=forking
PIDFile=/run/nginx.pid
ExecStartPre=/usr/sbin/nginx -t -q -g 'daemon on; master_process on;'
ExecStart=/usr/sbin/nginx -g 'daemon on; master_process on;'
ExecReload=/bin/kill -s HUP $MAINPID
KillMode=mixed
KillSignal=SIGTERM
PrivateDevices=yes
LimitNOFILE=65536
[Install]
WantedBy=multi-user.target
eoof

1.5.9 创建 default.conf 文件#

Terminal window
cat > ./debian/default.conf << 'EOF'
server {
listen 80;
server_name _;
root /usr/share/nginx/html;
index index.html;
location / {
try_files $uri $uri/ =404;
}
}
EOF

1.5.10 创建 index.html文件#

Terminal window
cat > ./debian/index.html << 'EOF'
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Nginx 自定义构建测试页</title>
<style>
body {
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
background-color: #f4f6f9;
color: #333;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
}
.container {
background: white;
padding: 40px;
border-radius: 8px;
box-shadow: 0 4px 15px rgba(0,0,0,0.1);
text-align: center;
max-width: 600px;
width: 90%;
border-top: 5px solid #27ae60; /* 绿色顶部边框代表成功 */
}
h1 {
color: #2c3e50;
margin-bottom: 10px;
}
.status-badge {
display: inline-block;
background-color: #d4edda;
color: #155724;
padding: 5px 15px;
border-radius: 50px;
font-weight: bold;
font-size: 0.9em;
margin-bottom: 20px;
border: 1px solid #c3e6cb;
}
p {
line-height: 1.6;
color: #666;
}
.info-box {
background-color: #f8f9fa;
border: 1px solid #e9ecef;
border-radius: 4px;
padding: 15px;
margin-top: 20px;
text-align: left;
font-size: 0.9em;
}
.info-box strong {
color: #333;
}
code {
background-color: #eee;
padding: 2px 5px;
border-radius: 3px;
font-family: monospace;
color: #d63384;
}
</style>
</head>
<body>
<div class="container">
<!-- 状态徽章 -->
<div class="status-badge">✅ 部署成功</div>
<h1>欢迎使用自定义 Nginx</h1>
<p>恭喜!您已成功通过自制的 DEB 包安装并启动了 Nginx 服务。</p>
<div class="info-box">
<p><strong>📦 构建信息:</strong></p>
<ul style="list-style-type: none; padding-left: 0;">
<li>• <strong>包来源:</strong> 本地编译 (Custom Build)</li>
<li>• <strong>维护者:</strong> Zhang San &lt;zhangsan@example.com&gt;</li>
<li>• <strong>版本标识:</strong> nginx-custom / 1.20.2</li>
<li>• <strong>根目录:</strong> <code>/etc/nginx/html</code></li>
</ul>
</div>
<p style="margin-top: 20px; font-size: 0.8em; color: #999;">
如果看到此页面,说明 Nginx 配置文件与文件系统权限均已正确设置。
</p>
</div>
</body>
</html>
EOF

1.5.11 构建deb包#

Terminal window
# 设置执行权限
cd debian
chmod +x changelog control postinst prerm rules
#开始构建
cd ..
dpkg-buildpackage -us -uc -b

构建完成后会在工作目录的同级目录出现打包好的deb包

Terminal window
test/
├── nginx/ # nginx源码
└── debian # 创建的构建目录
├── nginx.deb # 构建成功后出现的deb包

1.5.12 安装测试#

Terminal window
# 安装
dpkg -i nginx-custom_1.20.2-1_amd64.deb
# 安装完成后,查看postinst是否执行创建nginx用户和用户组.同时也可查看对应目录权限
[root@localhost /test1]# id nginx
uid=105(nginx) gid=110(nginx) groups=110(nginx)
# 查看nginx默认语法和运行状态
nginx -t
systemctl status nginx
# 访问默认网页
http://ip
# 卸载包
dpkg -P nginx-custom

2.1 简单制作示例#

2.1.1 创建打包工作目录与文件结构#

创建一个工作目录,并模拟软件安装到系统后的目录结构。我们将脚本放在 usr/bin/ 目录下:

Terminal window
# 创建主工作目录
mkdir -p hello_1.0_all/usr/bin
# 进入工作目录
cd hello_1.0_all

2.1.2 准备软件文件#

usr/bin/目录下创建一个简单的可执行脚本文件hello

Terminal window
# 创建脚本文件并写入内容
echo -e '#!/bin/bash\necho "Hello, Debian Package!"' > usr/bin/hello
# 赋予脚本可执行权限
chmod +x usr/bin/hello

2.1.3 创建 DEBIAN 目录及 control 文件#

在工作目录下创建 DEBIAN 子目录,并编写 control 元数据文件:

Terminal window
# 创建 DEBIAN 目录
mkdir DEBIAN
# 创建并写入 control 文件
cat <<EOF > DEBIAN/control
Package: hello
Version: 1.0
Architecture: all
Maintainer: Your Name <your.email@example.com>
Description: A simple hello world script
This is a sample package to demonstrate how to create a deb package.
EOF

注意:Architecture: all 表示该包适用于所有硬件架构(因为是纯脚本)。control 文件的最后一行(Description 的长描述)前面必须有一个空格。

2.1.4 构建 deb 包#

退回到工作目录的上一级,使用 dpkg-deb 命令进行打包:

Terminal window
# 返回上级目录
cd ..
# 执行打包命令
dpkg-deb --build --root-owner-group hello_1.0_all

执行完毕后,当前目录下会生成一个名为 hello_1.0_all.deb 的软件包文件。

2.1.5 测试与安装#

使用 dpkg 命令来安装和测试这个软件包:

Terminal window
# 安装 deb 包
sudo dpkg -i hello_1.0_all.deb
# 运行测试
hello
# 预期输出: Hello, Debian Package!
# (可选)卸载软件包
sudo dpkg -r hello

文章分享

如果这篇文章对你有帮助,欢迎分享给更多人!

制作deb软件包
https://2233941.xyz/posts/制作deb包/
作者
lumifly
发布于
2026-08-04
许可协议
CC BY-NC-SA 4.0
Profile Image of the Author
lumifly
不逐世间万丈星河,自守一隅细碎明光
✨ 今日一言
" 加载中... "
——
公告
欢迎来到我的博客!这是一则示例公告。
音乐
封面

音乐

暂未播放

0:00 0:00
暂无歌词
分类
标签
本年还剩 -- --%
本月还剩 -- --%
本周还剩 -- --%
距离--
--
--
天气预报
站点统计
文章
24
分类
1
标签
15
总字数
136,852
运行时长
0
最后活动
0 天前
站点信息
构建平台
Local
博客版本
Firefly v6.12.1
文章许可
CC BY-NC-SA 4.0

文章目录