第 07 章:桌面环境
第 07 章:桌面环境
在 Alpine Linux 上配置图形桌面环境、Xorg/Wayland 和日常应用。
7.1 桌面环境选择
| 桌面环境 | 资源占用 | 包名 | 推荐场景 |
|---|---|---|---|
| XFCE | ~300 MB | xfce4 | 日常使用,轻量推荐 |
| MATE | ~350 MB | mate | GNOME 2 风格 |
| LXQt | ~250 MB | lxqt | Qt 轻量桌面 |
| Sway | ~100 MB | sway | 平铺式,Wayland |
| i3wm | ~50 MB | i3wm | 极简平铺 |
| GNOME | ~800 MB | gnome | 全功能桌面 |
| KDE Plasma | ~600 MB | plasma-desktop | 功能丰富 |
7.2 Xorg 显示服务器
安装 Xorg
# 安装 Xorg 基础
apk add xorg-server xf86-video-vesa xf86-input-libinput
# 安装显卡驱动(按需选择)
# Intel
apk add xf86-video-intel
# AMD/ATI
apk add xf86-video-amdgpu xf86-video-ati
# NVIDIA(开源)
apk add xf86-video-nouveau
# NVIDIA(闭源,社区仓库)
# apk add nvidia-driver nvidia-xconfig
# 通用/虚拟机
apk add xf86-video-vesa xf86-video-fbdev
# 输入设备
apk add xf86-input-libinput xf86-input-evdev
# 字体
apk add font-dejavu font-noto-cjk font-noto-emoji
Xorg 配置
# 自动生成配置
Xorg -configure
mv /root/xorg.conf.new /etc/X11/xorg.conf
# 手动配置示例
mkdir -p /etc/X11
cat > /etc/X11/xorg.conf << 'EOF'
Section "ServerLayout"
Identifier "Layout0"
Screen 0 "Screen0"
InputDevice "Keyboard0" "CoreKeyboard"
InputDevice "Mouse0" "CorePointer"
EndSection
Section "Device"
Identifier "Device0"
Driver "intel"
Option "AccelMethod" "sna"
EndSection
Section "Screen"
Identifier "Screen0"
Device "Device0"
DefaultDepth 24
SubSection "Display"
Depth 24
Modes "1920x1080" "1366x768"
EndSubSection
EndSection
EOF
# 使用 xrandr 配置分辨率
xrandr --output eDP-1 --mode 1920x1080 --rate 60
xrandr --output HDMI-1 --right-of eDP-1 --auto # 双显示器
7.3 XFCE 桌面安装
完整安装
# 启用社区仓库
vi /etc/apk/repositories
# 取消注释 community 行
# 安装 XFCE
apk add xfce4 xfce4-terminal thunar lightdm
# 安装额外组件
apk add \
xfce4-notifyd \
xfce4-power-manager \
xfce4-screensaver \
xfce4-pulseaudio-plugin \
xfce4-whiskermenu-plugin \
networkmanager \
networkmanager-applet
# 配置显示管理器
rc-update add dbus
rc-update add lightdm
rc-service dbus start
# 创建普通用户(桌面环境需要)
adduser -s /bin/bash myuser
addgroup myuser video
addgroup myuser audio
addgroup myuser input
addgroup myuser plugdev
# 启动桌面
rc-service lightdm start
XFCE 优化
# XFCE 配置文件位于 ~/.config/xfce4/
# 面板配置
xfce4-panel --configure
# 快捷键设置
# 设置 -> 键盘 -> 应用程序快捷键
# 桌面壁纸
xfconf-query -c xfce4-desktop -p /backdrop/screen0/monitor0/workspace0/last-image \
-s /usr/share/backgrounds/xfce/xfce-teal.jpg
# 透明效果
# 窗口管理器 -> 合成器 -> 启用显示合成
7.4 Wayland(Sway)
# 安装 Sway(Wayland 平铺窗口管理器)
apk add sway swaybg swaylock swayidle \
foot wmenu waybar \
xdg-desktop-portal-wlr \
grim slurp wl-clipboard \
font-noto-cjk
# 创建配置文件
mkdir -p ~/.config/sway
cp /etc/sway/config ~/.config/sway/
# 编辑配置
cat >> ~/.config/sway/config << 'EOF'
# 输入配置
input type:keyboard {
xkb_layout "cn"
}
# 外观
output * bg /usr/share/backgrounds/sway/Sway_Wallpaper_Blue_1920x1080.png fill
# 快捷键
bindsym $mod+Return exec foot
bindsym $mod+d exec wmenu-run
bindsym $mod+Shift+q kill
bindsym $mod+Shift+e exec swaymsg exit
# 常用快捷键
bindsym $mod+h focus left
bindsym $mod+j focus down
bindsym $mod+k focus up
bindsym $mod+l focus right
bindsym $mod+Shift+h move left
bindsym $mod+Shift+j move down
bindsym $mod+Shift+k move up
bindsym $mod+Shift+l move right
# 工作区
bindsym $mod+1 workspace 1
bindsym $mod+2 workspace 2
bindsym $mod+Shift+1 move container to workspace 1
bindsym $mod+Shift+2 move container to workspace 2
EOF
# 从 TTY 启动 Sway
# 登录后执行:
sway
7.5 常用桌面应用
办公应用
# LibreOffice 办公套件
apk add libreoffice
# 或轻量替代
apk add abiword gnumeric # AbiWord (文字) + Gnumeric (表格)
# PDF 阅读器
apk add evince # GNOME 文档查看器
# 或
apk add zathura zathura-pdf-mupdf # 轻量 PDF 阅读器
# 文本编辑器
apk add vim
apk add mousepad # XFCE 文本编辑器
网络应用
# 浏览器
apk add firefox # Mozilla Firefox
# 或
apk add chromium # Chromium(较新版本可能需 edge 仓库)
# 邮件客户端
apk add thunderbird
# 或轻量替代
apk add neomutt # 终端邮件客户端
# 即时通讯
apk add pidgin # 多协议聊天
# 或
apk add telegram-desktop # Telegram(社区仓库)
多媒体
# 视频播放
apk add mpv # 轻量视频播放器
apk add vlc # VLC 播放器(社区仓库)
# 音频
apk add pulseaudio pulseaudio-utils pavucontrol
apk add audacity # 音频编辑
# 图片查看
apk add ristretto # XFCE 图片查看器
apk add gimp # 图像编辑
# 截图工具
apk add scrot # 命令行截图
# Sway 用户使用 grim + slurp
开发工具
# IDE
apk add code # VS Code(社区仓库,可能需 edge)
# Git 客户端
apk add git gitg # Git + Git GUI
# 终端
apk add xfce4-terminal
apk add alacritty # GPU 加速终端(Wayland 兼容)
7.6 显示管理器
# LightDM(推荐,轻量)
apk add lightdm lightdm-gtk-greeter
rc-update add lightdm
# SDDM(用于 KDE/Plasma)
apk add sddm
rc-update add sddm
# 无显示管理器(手动启动 X)
# 在 ~/.profile 中添加:
if [ -z "$DISPLAY" ] && [ "$(tty)" = "/dev/tty1" ]; then
exec startx
fi
# ~/.xinitrc
cat > ~/.xinitrc << 'EOF'
exec startxfce4
# 或 exec sway
EOF
7.7 音频配置
# 安装 PulseAudio
apk add pulseaudio pulseaudio-utils pavucontrol
# 音频组
addgroup myuser audio
# 配置 PulseAudio
cat > /etc/pulse/default.pa << 'EOF'
load-module module-udev-detect
load-module module-alsa-card device_id="0"
load-module module-native-protocol-unix
load-module module-default-device-restore
load-module module-rescue-streams
load-module module-always-sink
load-module module-suspend-on-idle
EOF
# 测试音频
speaker-test -c 2
aplay -l # 列出音频设备
# PipeWire(Wayland 推荐)
apk add pipewire pipewire-pulse wireplumber
7.8 打印服务
# 安装 CUPS
apk add cups cups-filters cups-pdf
# 配置
rc-update add cups
rc-service cups start
# 添加打印机(Web 界面)
# 访问 http://localhost:631
# 命令行添加
lpadmin -p MyPrinter -E -v usb://HP/LaserJet -m everywhere
7.9 字体配置
# 中文字体
apk add font-noto-cjk
apk add font-wqy-zenhei
# 英文字体
apk add font-dejavu font-liberation
# Emoji 字体
apk add font-noto-emoji
# 等宽字体(编程用)
apk add font-fira-code font-hack font-jetbrains-mono
# 字体渲染优化
cat > /etc/fonts/local.conf << 'EOF'
<?xml version="1.0"?>
<!DOCTYPE fontconfig SYSTEM "fonts.dtd">
<fontconfig>
<match target="font">
<edit name="antialias" mode="assign"><bool>true</bool></edit>
<edit name="hinting" mode="assign"><bool>true</bool></edit>
<edit name="hintstyle" mode="assign"><const>hintslight</const></edit>
<edit name="rgba" mode="assign"><const>rgb</const></edit>
<edit name="lcdfilter" mode="assign"><const>lcddefault</const></edit>
</match>
</fontconfig>
EOF
# 重建字体缓存
fc-cache -fv
fc-list :lang=zh # 查看中文字体
7.10 远程桌面
# VNC 服务器
apk add tigervnc
# 设置 VNC 密码
vncpasswd
# 启动 VNC 服务器
vncserver :1 -geometry 1920x1080 -depth 24
# 配置 XFCE
cat > ~/.vnc/xstartup << 'EOF'
#!/bin/sh
unset SESSION_MANAGER
unset DBUS_SESSION_BUS_ADDRESS
exec startxfce4
EOF
chmod +x ~/.vnc/xstartup
# RDP 服务器
apk add xrdp
rc-update add xrdp
rc-service xrdp start
7.11 注意事项
⚠️ 显卡驱动
- Alpine 使用开源驱动,NVIDIA 闭源驱动需从社区仓库安装
- 虚拟机环境使用
xf86-video-vesa或xf86-video-qxl- Wayland 环境下 NVIDIA 支持有限
💡 性能优化
- XFCE 是 Alpine 上最平衡的桌面环境选择
- Sway (Wayland) 比 Xorg 更轻量高效
- 禁用不需要的桌面特效节省资源
- 考虑使用
zram交换替代传统 swap
扩展阅读
上一章:第 06 章:服务管理 下一章:第 08 章:Docker 基础镜像