Alpine Linux 完全指南 / 第 03 章:基础操作
第 03 章:基础操作
掌握 Alpine Linux 的包管理系统、常用命令和用户权限管理。
3.1 apk 包管理器
apk(Alpine Package Keeper)是 Alpine Linux 的包管理工具,类似 Debian 的 apt 或 Red Hat 的 dnf。
基础操作
# 更新包索引(类似 apt update)
apk update
# 升级所有已安装的包(类似 apt upgrade)
apk upgrade
# 同时更新索引并升级
apk update && apk upgrade
# 搜索包
apk search nginx
apk search -v nginx # 显示详细信息
apk search -d nginx # 包含描述
# 查看包信息
apk info nginx
apk info -a nginx # 显示全部信息
apk info -d nginx # 仅显示描述
apk info -R nginx # 显示依赖
apk info -r nginx # 显示被哪些包依赖
# 安装包
apk add nginx
apk add nginx=1.26.2-r0 # 安装指定版本
apk add nginx --no-cache # 不缓存索引(Docker 推荐)
# 删除包
apk del nginx
apk del nginx --purge # 同时删除配置文件
# 升级单个包
apk add -u nginx
# 清理缓存
apk cache clean
rm -rf /var/cache/apk/*
仓库管理
# 查看当前仓库配置
cat /etc/apk/repositories
# 默认内容:
# https://dl-cdn.alpinelinux.org/alpine/v3.20/main
# #https://dl-cdn.alpinelinux.org/alpine/v3.20/community
# 启用社区仓库(很多常用软件在 community 中)
vi /etc/apk/repositories
# 取消 community 行的注释
# 添加 edge 仓库(获取最新软件版本)
cat >> /etc/apk/repositories << 'EOF'
https://dl-cdn.alpinelinux.org/alpine/edge/main
https://dl-cdn.alpinelinux.org/alpine/edge/community
https://dl-cdn.alpinelinux.org/alpine/edge/testing
EOF
# 从 edge 安装特定包(不影响其他包)
apk add --repository=https://dl-cdn.alpinelinux.org/alpine/edge/community package-name
# 添加本地仓库
apk add --allow-untrusted /path/to/package.apk
# 使用国内镜像源(加速)
sed -i 's/dl-cdn.alpinelinux.org/mirrors.tuna.tsinghua.edu.cn/g' /etc/apk/repositories
apk update
常用仓库镜像源
| 镜像源 | 地址 |
|---|---|
| 官方 CDN | https://dl-cdn.alpinelinux.org/alpine/v3.20/main |
| 清华 TUNA | https://mirrors.tuna.tsinghua.edu.cn/alpine/v3.20/main |
| 阿里云 | https://mirrors.aliyun.com/alpine/v3.20/main |
| 中科大 | https://mirrors.ustc.edu.cn/alpine/v3.20/main |
| 华为云 | https://repo.huaweicloud.com/alpine/v3.20/main |
apk 虚拟包与世界文件
# 世界文件(/etc/apk/world)记录了用户显式安装的包
cat /etc/apk/world
# linux-lts
# nginx
# openssh
# 已安装的包数据库
ls /lib/apk/db/
# installed - 已安装包信息
# lock - 锁文件
# scripts.tar - 安装脚本
# triggers - 触发器
# 使用虚拟包组合安装
apk add --virtual .build-deps gcc musl-dev make
# 用完后删除
apk del .build-deps
常用包速查表
| 用途 | 包名 | 说明 |
|---|---|---|
| 基础工具 | bash bash-completion vim htop tmux | 日常使用 |
| 网络工具 | curl wget bind-tools nmap tcpdump | 网络调试 |
| 编译工具 | gcc g++ make cmake musl-dev | C/C++ 编译 |
| Python | python3 py3-pip py3-virtualenv | Python 开发 |
| Node.js | nodejs npm yarn | Node.js 开发 |
| 数据库 | mariadb postgresql16 sqlite | 数据库 |
| Web 服务器 | nginx apache2 lighttpd | Web 服务 |
| 容器 | docker podman lxc | 容器运行时 |
| 安全 | openssh openssl gnupg | 安全工具 |
| 文档 | mandoc man-pages linux-docs | 手册文档 |
3.2 基础命令
文件操作
# BusyBox 版文件操作
ls -lah /etc/ # 列出文件(长格式+隐藏+人类可读)
cp -r /src /dst # 复制目录
mv old.txt new.txt # 移动/重命名
rm -rf /tmp/data # 递归删除
mkdir -p /a/b/c # 创建多级目录
ln -s /real/path /link # 创建符号链接
# 文件查看
cat /etc/os-release # 查看文件内容
head -n 20 /var/log/messages # 查看前 20 行
tail -f /var/log/messages # 实时跟踪
less /var/log/messages # 分页查看
wc -l /etc/passwd # 统计行数
# 文件搜索
find / -name "*.conf" -type f 2>/dev/null
find /var -size +10M -type f # 查找大于 10M 的文件
find /tmp -mtime +7 -delete # 删除 7 天前的临时文件
# 文件权限
chmod 755 /usr/local/bin/myscript
chmod u+x /usr/local/bin/myscript
chown user:group /path/to/file
文本处理
# grep 搜索
grep -r "error" /var/log/ # 递归搜索
grep -i "warning" /var/log/syslog # 忽略大小写
grep -c "fail" /var/log/auth.log # 统计匹配行数
# sed 替换
sed -i 's/old/new/g' file.txt # 全局替换
sed -n '10,20p' file.txt # 打印 10-20 行
sed '/^#/d' config.conf # 删除注释行
# awk 处理
awk '{print $1, $3}' file.txt # 打印第 1、3 列
awk -F: '{print $1}' /etc/passwd # 指定分隔符
# 排序和去重
sort file.txt | uniq -c # 排序后去重并计数
sort -t: -k3 -n /etc/passwd # 按 UID 数字排序
# 管道组合
ps aux | grep nginx | awk '{print $2}' | xargs kill
cat /var/log/access.log | cut -d' ' -f1 | sort | uniq -c | sort -rn | head
进程管理
# 查看进程
ps aux # 所有进程
ps aux | grep nginx # 过滤特定进程
top # 实时进程监控
htop # 更好的进程监控(需安装)
# 进程控制
kill PID # 发送 SIGTERM
kill -9 PID # 强制终止
killall nginx # 按名称终止
pkill -f "python.*server" # 按模式匹配终止
# 后台运行
command & # 后台执行
nohup command & # 终端关闭后继续运行
jobs # 查看后台任务
fg %1 # 切换到前台
disown %1 # 从 shell 分离
# 系统信息
uname -a # 内核信息
cat /proc/cpuinfo # CPU 信息
free -h # 内存使用
df -h # 磁盘使用
du -sh /var/* # 目录大小
uptime # 系统运行时间和负载
网络命令
# 网络配置
ip addr show # 查看 IP 地址
ip route show # 查看路由表
ip link show # 查看网络接口
# 网络测试
ping -c 4 8.8.8.8 # 测试连通性
traceroute google.com # 路由追踪
nslookup example.com # DNS 查询
dig example.com # DNS 查询(更详细)
# 端口检查
ss -tlnp # 查看监听端口
ss -s # 连接统计
netstat -tlnp # 传统方式(需安装 net-tools)
# 数据传输
curl -I https://example.com # HTTP 头信息
wget https://example.com/file # 下载文件
scp file user@host:/path # 远程复制
rsync -avz /src/ user@host:/dst/ # 增量同步
3.3 用户管理
用户与组操作
# 创建用户
adduser myuser # 交互式创建
adduser -D -s /bin/bash myuser # 非交互式创建(-D 无密码)
adduser -D -s /bin/bash -G wheel myuser # 指定附加组
# 修改用户
passwd myuser # 设置密码
usermod -aG docker myuser # 添加到附加组
usermod -s /bin/bash myuser # 修改 shell
# 删除用户
deluser myuser # 删除用户
deluser --remove-home myuser # 删除用户及主目录
# 组管理
addgroup mygroup # 创建组
addgroup myuser mygroup # 将用户添加到组
delgroup mygroup # 删除组
# 查看用户信息
id myuser # 用户 UID、GID、所属组
whoami # 当前用户
who # 当前登录用户
last # 登录历史
用户配置文件
# /etc/passwd 格式
# 用户名:密码占位:UID:GID:描述:主目录:Shell
cat /etc/passwd
# root:x:0:0:root:/root:/bin/ash
# myuser:x:1000:1000:Linux User,,,:/home/myuser:/bin/bash
# /etc/group 格式
# 组名:密码占位:GID:成员列表
cat /etc/group
# wheel:x:10:myuser
# docker:x:998:myuser
# /etc/shadow(密码哈希存储)
cat /etc/shadow # 仅 root 可读
# Shell 配置
cat /etc/profile # 全局 profile
cat ~/.profile # 用户 profile
cat ~/.bashrc # bash 配置(需安装 bash)
sudo 配置
# 安装 sudo
apk add sudo
# 方法一:使用 wheel 组(推荐)
adduser myuser wheel
echo '%wheel ALL=(ALL:ALL) ALL' > /etc/sudoers.d/wheel
chmod 440 /etc/sudoers.d/wheel
# 方法二:直接编辑 sudoers(不推荐)
visudo
# 添加: myuser ALL=(ALL:ALL) ALL
# 测试 sudo
su - myuser
sudo whoami # 应输出 root
# sudo 常用选项
sudo -i # 切换到 root 的交互式 shell
sudo -u myuser cmd # 以指定用户执行命令
sudo -l # 列出当前用户的权限
PAM 认证(可选)
# 安装 PAM
apk add linux-pam pam-client
# 密码策略配置
cat > /etc/pam.d/common-password << 'EOF'
password requisite pam_pwquality.so minlen=12 dcredit=-1 ucredit=-1 lcredit=-1 ocredit=-1
password required pam_unix.so use_authtok sha512 shadow
EOF
# 登录限制
cat > /etc/pam.d/common-auth << 'EOF'
auth required pam_tally2.so deny=5 onerr=fail unlock_time=900
auth required pam_unix.so nullok
EOF
3.4 Shell 环境配置
ash 配置
# Alpine 默认使用 BusyBox ash
# 配置文件路径: /etc/profile, ~/.profile
# 全局环境变量
cat > /etc/profile.d/custom.sh << 'EOF'
export PATH="/usr/local/bin:$PATH"
export EDITOR=vim
export HISTSIZE=1000
export HISTFILESIZE=2000
alias ll='ls -lah'
alias grep='grep --color=auto'
EOF
# 用户环境变量
cat > ~/.profile << 'EOF'
# 加载全局配置
[ -f /etc/profile ] && . /etc/profile
# 自定义配置
export PS1='\u@\h:\w\$ '
alias ..='cd ..'
alias ...='cd ../..'
EOF
切换到 bash
# 安装 bash
apk add bash bash-completion
# 设置为默认 shell
sed -i 's|/root:/bin/ash|/root:/bin/bash|' /etc/passwd
sed -i "s|/home/myuser:/bin/ash|/home/myuser:/bin/bash|" /etc/passwd
# bash 配置
cat > ~/.bashrc << 'EOF'
# 历史记录
export HISTSIZE=10000
export HISTFILESIZE=20000
export HISTCONTROL=ignoredups:erasedups
shopt -s histappend
# 别名
alias ll='ls -lah --color=auto'
alias la='ls -A --color=auto'
alias l='ls -CF --color=auto'
alias grep='grep --color=auto'
alias df='df -h'
alias du='du -h'
# 提示符
PS1='\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '
# bash-completion
[ -f /etc/profile.d/bash_completion.sh ] && . /etc/profile.d/bash_completion.sh
EOF
tmux 终端复用
# 安装 tmux
apk add tmux
# 基本配置
cat > ~/.tmux.conf << 'EOF'
# 前缀键改为 Ctrl+a
unbind C-b
set -g prefix C-a
bind C-a send-prefix
# 鼠标支持
set -g mouse on
# 状态栏
set -g status-style bg=colour235,fg=colour136
set -g status-left '[#S] '
set -g status-right '%Y-%m-%d %H:%M'
# 窗口编号从 1 开始
set -g base-index 1
setw -g pane-base-index 1
# 快捷键
bind | split-window -h
bind - split-window -v
bind r source-file ~/.tmux.conf
EOF
# 常用命令
tmux new -s dev # 新建会话
tmux attach -t dev # 连接会话
tmux ls # 列出会话
# 前缀 + d # 分离会话
# 前缀 + c # 新建窗口
# 前缀 + | # 水平分割
# 前缀 + - # 垂直分割
3.5 常用系统信息查看
# 系统信息汇总脚本
cat > /usr/local/bin/sysinfo << 'SCRIPT'
#!/bin/sh
echo "=== 系统信息 ==="
echo "主机名: $(hostname)"
echo "系统版本: $(cat /etc/alpine-release)"
echo "内核版本: $(uname -r)"
echo "架构: $(uname -m)"
echo "运行时间: $(uptime -p 2>/dev/null || uptime)"
echo ""
echo "=== CPU ==="
echo "型号: $(grep 'model name' /proc/cpuinfo | head -1 | cut -d: -f2)"
echo "核心: $(nproc)"
echo ""
echo "=== 内存 ==="
free -h
echo ""
echo "=== 磁盘 ==="
df -h | grep -E '^/dev'
echo ""
echo "=== 网络 ==="
ip -4 addr show | grep inet | grep -v 127.0.0.1
echo ""
echo "=== 已安装包数 ==="
echo "包数量: $(apk info 2>/dev/null | wc -l)"
SCRIPT
chmod +x /usr/local/bin/sysinfo
3.6 本章小结
| 主题 | 关键命令 |
|---|---|
| 更新系统 | apk update && apk upgrade |
| 安装包 | apk add package-name |
| 搜索包 | apk search keyword |
| 删除包 | apk del package-name |
| 创建用户 | adduser -D -s /bin/bash username |
| 设置 sudo | adduser username wheel |
| 查看进程 | ps aux, top, htop |
| 网络信息 | ip addr show, ss -tlnp |
下一章:第 04 章:网络配置