博主头像
Rookie's Blog

ワクワク

头图

Alpine Linux 服务器简记

本文档整理了 Alpine Linux 在软件源配置、包管理以及服务管理方面的常用命令与配置。

一、软件源管理

Alpine Linux 的软件源信息存储在 /etc/apk/repositories 文件中。

配置文件示例

默认的 CDN 源地址为 http://dl-cdn.alpinelinux.org/

你可以指定具体的版本(如 v3.5),或者使用 latest-stable 来自动指向最新的稳定版仓库。

# 示例:v3.5 版本
http://dl-cdn.alpinelinux.org/v3.5/main
http://dl-cdn.alpinelinux.org/v3.5/community

# 推荐:使用 latest-stable
http://dl-cdn.alpinelinux.org/latest-stable/main
http://dl-cdn.alpinelinux.org/latest-stable/community

重要提示:修改完 /etc/apk/repositories 文件后,必须运行以下命令来更新本地的包索引,使更改生效:

apk update

二、包管理:apk 命令

Alpine Linux 使用 apk (Alpine Package Keeper) 工具进行包管理。以下是常用命令的总结。

参数 (parameter)命令示例 (example)说明 (description)
updateapk update更新最新的软件源列表索引。
searchapk search查找所有可用的软件包。
apk search -v查找所有可用包及其描述。
apk search -v 'acf*'通过包名(支持通配符)查找软件包。
apk search -v -d 'docker'通过软件包的描述信息来查找。
addapk add openssh安装单个软件包。
apk add openssh openntp vim同时安装多个软件包。
apk add --no-cache mysql-client不使用本地缓存安装(相当于先 updateadd)。
infoapk info列出所有已安装的软件包。
apk info -a zlib显示一个包的完整详细信息。
apk info --who-owns /sbin/lbu显示指定文件属于哪个软件包。
upgradeapk upgrade升级所有已安装的软件包。
apk upgrade openssh升级指定的单个软件包。
apk upgrade openssh openntp vim升级指定的多个软件包。
apk add --upgrade busybox使用 add 命令配合 --upgrade 参数来指定升级。
delapk del openssh删除一个软件包。

三、服务管理:OpenRC

Alpine Linux 使用 OpenRC 作为其初始化系统和服务管理器,而非 systemd。相关的命令以 rc- 开头。

1. rc-service:管理服务状态

用于启动、停止、重启单个服务。

示例:重启 sshd 服务。

rc-service sshd restart

帮助信息

# rc-service --help
Usage: rc-service [options] [-i] <service> <cmd>...
   or: rc-service [options] -e <service>
   or: rc-service [options] -l
   or: rc-service [options] -r <service>

Options: [ ce:ilr:INChqVv ]
  -e, --exists <arg>                tests if the service exists or not
  -c, --ifcrashed                   if the service is crashed then run the command
  -i, --ifexists                    if the service exists then run the command
  -I, --ifinactive                  if the service is inactive then run the command
  -N, --ifnotstarted                if the service is not started then run the command
  -l, --list                        list all available services
  -r, --resolve <arg>               resolve the service name to an init script

2. rc-update:管理开机自启

用于在不同的运行级别 (runlevel) 中添加或删除服务。

帮助信息

# rc-update --help
Usage: rc-update [options] add <service> [<runlevel>...]
   or: rc-update [options] del <service> [<runlevel>...]
   or: rc-update [options] [show [<runlevel>...]]

Options: [ asuChqVv ]
  -a, --all                         Process all runlevels
  -s, --stack                       Stack a runlevel instead of a service
  -u, --update                      Force an update of the dependency tree

3. rc-status:检查服务状态

主要用于查看运行级别和其中服务的状态。

帮助信息

# rc-status --help
Usage: rc-status [options] <runlevel>...
   or: rc-status [options] [-a | -c | -l | -m | -r | -s | -u]

Options: [ aclmrsuChqVv ]
  -a, --all                         Show services from all run levels
  -c, --crashed                     Show crashed services
  -l, --list                        Show list of run levels
  -m, --manual                      Show manually started services
  -r, --runlevel                    Show the name of the current runlevel
  -s, --servicelist                 Show service list
  -u, --unused                      Show services not assigned to any runlevel

4. openrc:管理运行级别

主要用于切换和管理不同的运行级别。

帮助信息

# openrc --help
Usage: openrc [options] [<runlevel>]

Options: [ a:no:s:SChqVv ]
  -n, --no-stop                     do not stop any services
  -o, --override <arg>              override the next runlevel to change into
                                    when leaving single user or boot runlevels
  -s, --service <arg>               runs the service specified with the rest
                                    of the arguments
  -S, --sys                         output the RC system type, if any

常用命令案例

  1. docker 服务添加到 boot 运行级别(即开机自启)

    rc-update add docker boot
  2. 重启网络服务

    rc-service networking restart
  3. 列出所有运行级别的所有服务及其状态

    rc-status -a
  4. 重启特定服务 (例如 sshd)

    rc-service sshd restart
小幽默一下:在 Alpine 的世界里,systemctl 休假去了,一切由高效简洁的 rc-* 家族接管。记得用 rc-service 来“伺候”你的服务,而不是去 systemctl 那儿吃闭门羹哦!
Alpine Linux 服务器简记
https://typecho.rookiez.link/note/123.html
本文作者 Rookie
发布时间 2026-01-30
许可协议 CC BY-NC-SA 4.0
发表新评论