23.1 什么是 X Window System

在 Unix-Like 上的图形接口 (GUI) 称为 X Window System,简称 XX11。它不是操作系统,而是跨网络、跨平台的软件

关键认知:X11 是应用程序,不是操作系统

23.1.1 X Window 的发展简史

年份事件
1984MIT 开发出 X Window,设计为不依赖硬件的图形接口
1987发布 X11 版——后续所有改进都建立在此基础上
1994发布 X11R6——沿用最久的架构基础
1992XFree86 计划启动,长期维护 X11R6(X + Free + x86)
约 2004XFree86 授权变更 → Xorg 基金会接手,发布 X11R6.8 / X11R7
现在CentOS 7 使用 Xorg 提供的 X11,MIT 授权(类似 GPL)

Mac OS X v10.3 也曾用过 X11 架构设计窗口!

23.1.2 主要元件:X Server / X Client / Window Manager / Display Manager

X Window 是 C/S 架构(主从架构),四大元件:

X Client (应用程序) → 告知要画什么
        ↕ X Protocol (网络传输)
X Server (硬件管理) → 实际绘图到屏幕

X Server:硬件管理、屏幕绘制与字体

管理内容说明
显卡 + 驱动程序核心绘图硬件
屏幕分辨率 + 色彩深度显示参数
键盘、鼠标、手写板输入设备
字体文字的显示

每部用户端主机都要安装 X Server,而服务器端提供 X Client——因为绘图用的是用户端的硬件!

交互流程:X Server 检测到鼠标移动 → 将动作告知 X Client → X Client 计算位移 → 回传绘图数据 → X Server 绘制到屏幕。

X Client:处理来自 X Server 的事件

X Client 就是 X 应用程序 (X Application)。它只负责数据运算,不直接绘制

各个 X Client 互不知道对方的存在——因此两个 Client 的窗口可能重叠。

好处:X Client 不需要知道 X Server 的硬件和操作系统是什么——真正跨平台。

Window Manager (WM):特殊的 X Client

因为 X Client 互不知道对方,需要 Window Manager 来管理:

功能说明
控制元素工作列、背景桌面
虚拟桌面多个工作区切换
窗口控制大小、重叠排序、移动、最小化

常见的 Window Manager:

名称说明
GNOMECentOS 默认,GNU Network Object Model Environment
KDEK Desktop Environment
XFCE轻量级桌面
twm最阳春的窗口管理员(Tab Window Manager)

每个 Window Manager 使用不同的显示发动机——不单是外观不同,底层实现也不同。

Display Manager:提供登录界面

graphical.target 下,tty1 那个让你输入帐号密码的图形登录界面就是 Display Manager。

CentOS 默认使用 GNOME Display Manager (gdm)。在 gdm 登录时可以选择不同的 Window Manager(不仅限于 GNOME)。

23.1.3 X Window 的启动流程

通过 startx 启动

startx [X client 参数] -- [X server 参数]
startx -- -depth 16      # X Server 以 16 bit 色彩深度启动

startx 是一个 shell script,任务只是找出参数后调用 xinit 真正启动 X。

参数搜寻顺序

优先级X Server 参数X Client 参数
1startx 后面接的 -- 参数startx 后面接的参数(在 -- 前)
2~/.xserverrc~/.xinitrc
3/etc/X11/xinit/xserverrc/etc/X11/xinit/xinitrc
4直接执行 /usr/bin/X直接执行 xterm

xinit 的实际执行

xinit [client option] -- [server or display option]

默认 xinit 的实际命令等效于:

xinit /etc/X11/xinit/xinitrc -- /usr/bin/X

/usr/bin/X 其实就是 Xorg 的链接文件(X Server 主程序)。

xinitrc — X Client 启动脚本

/etc/X11/xinit/xinitrc 最终会载入 KDE 或 GNOME。默认 WM 由 /etc/sysconfig/desktop 中的 DESKTOP=GNOMEDESKTOP=KDE 决定。

自订 ~/.xinitrc 示例(最后一个必须在前台执行):

xclock -geometry 100x100-5+5 &
xterm -geometry 80x50-50+150 &
exec /usr/bin/twm     # 最后一个不加 &,保持前台!

X 的显示接口

第 N 个 X显示接口默认终端机网络端口
第一个 X:0hostname:0tty2port 6000
第二个 X:1tty3port 6001

CentOS 7 的 tty 按需启动(与 CentOS 6 默认 6 个不同),X 默认丢到 tty2。

23.1.4 X 启动流程测试

手动体验 X Server / X Client / WM 的层次关系:

# 1. 在 tty1 启动纯 X Server(:1,即 tty3)
X :1 &
# 切换到 tty3:看到黑色画面 + 鼠标光标
 
# 2. 添加 X Client(在 tty1 下达,去 tty3 观察)
xterm -display :1 &
xclock -display :1 &
xeyes -display :1 &
# 可以看到终端机、时钟、大眼睛,但无法移动!
 
# 3. 添加 Window Manager
twm -display :1 &
# 现在可以移动窗口、调整大小了!
 
# 4. 清理
kill %6 %5 %4 %3 %2 %1

这个实验让你清楚看到:X Server → X Client → WM 各层的作用。

23.1.5 我是否需要启用 X Window System

用途建议
服务器不需要 X!所有配置文件都是纯文本
桌面电脑需要 X,办公室软件、图形分析等
数值分析/科研可能需要 X 来做数据可视化

X Window 只是 Linux 里的一个软件——服务器场景没必要装。

23.2 X Server 配置文件解析与设置

重要文件位置

路径说明
/etc/X11/xorg.conf主配置文件(CentOS 6+ 开始默认不存在,自动侦测)
/etc/X11/xorg.conf.d/*.conf额外设置片段(推荐方式)
/usr/lib64/xorg/modules/drivers/显卡驱动程序
/usr/share/X11/fonts/屏幕字体
/var/log/Xorg.0.logX Server 日志(查看实际使用的配置文件)
X -version     # 查看 X Server 版本
Xorg -configure # 自动生成 xorg.conf(输出到 /root/xorg.conf.new)

23.2.1 解析 xorg.conf 设置

配置文件以 Section "名称"EndSection 为段落:

Section用途
ServerLayout决定 X 最终使用哪组设置(屏幕+鼠标+键盘的组合)
Files字体路径(FontPath)、模块路径
Module额外载入的模块(如 glx 提供 3D 加速)
InputDevice键盘、鼠标等输入设备(Driver, Protocol, Device 路径)
Monitor显示器的水平和垂直更新频率(HorizSync, VertRefresh
Device显卡芯片组驱动(Driver "qxl" 等)
Screen分辨率 (Modes) + 色彩深度 (Depth) 的组合(引用 Device + Monitor)

Monitor 示例

Section "Monitor"
    Identifier   "Monitor0"
    HorizSync    30.0 - 80.0
    VertRefresh  50.0 - 100.0
EndSection

⚠️ CRT 屏幕的更新频率不能设太高——据说会烧毁显示器!

Device 示例

Section "Device"
    Identifier  "Card0"
    Driver      "intel"       # 或 nvidia, qxl, vesa...
    BusID       "PCI:0:2:0"
EndSection

如果显卡驱动有问题,可以暂时使用通用驱动:Driver "vesa"

Screen 示例

Section "Screen"
    Identifier "Screen0"
    Device     "Card0"
    Monitor    "Monitor0"
    SubSection "Display"
        Depth     24
        Modes     "1024x768" "800x600" "640x480"
    EndSubSection
EndSection

23.2.2 字体管理

字体目录:

  • /usr/share/X11/fonts/
  • /usr/share/fonts/

Xorg 实际载入的字体由 /etc/X11/fontpath.d/ 下的链接文件决定。

# 安装楷书字体
yum install cjkuni-ukai-fonts
 
# 重建字体缓存
fc-cache -v
fc-list | grep ukai          # 验证
 
# 手动添加字体(如 Windows 字体)
mkdir /usr/share/fonts/windows
cp /root/font/*.ttf /usr/share/fonts/windows/
fc-cache -f -v               # 强制重建缓存
fc-list : file | grep window # 验证

23.2.3 显示器参数微调

xrandr                       # 查看当前分辨率与可选分辨率
xrandr -s 1280x800           # 切换分辨率

用 gtf 计算显示器参数,写入 xorg.conf:

gtf 1024 768 75 -x
# Modeline "1024x768_75.00"  81.80  1024 1080 1192 1360  768 769 772 802  -HSync +Vsync

将 Modeline 那行添加到 Monitor Section 即可。

重启 X 的三种方法

  1. systemctl isolate multi-user.target; systemctl isolate graphical.target
  2. 在 X 画面中按 Alt+Ctrl+Backspace
  3. 注销后重新登录

23.3 显卡驱动程序安装范例

安装显卡驱动前的准备工作:

yum update
yum groupinstall "Development Tools"
yum install kernel-devel kernel-headers

23.3.1 NVidia

lspci | grep -Ei '(vga|display)'    # 确认显卡型号
# 去 http://www.nvidia.com.tw 下载对应驱动 (.run 文件)

关键步骤——禁用 nouveau

# 1. 创建 blacklist
echo "blacklist nouveau" > /etc/modprobe.d/blacklist.conf
echo "options nouveau modeset=0" >> /etc/modprobe.d/blacklist.conf
 
# 2. 修改 grub 核心参数
vim /etc/default/grub
# GRUB_CMDLINE_LINUX="... rd.driver.blacklist=nouveau nouveau.modeset=0"
 
grub2-mkconfig -o /boot/grub2/grub.cfg
reboot
lsmod | grep nouveau     # 确认清空
systemctl isolate multi-user.target    # 切换到纯文本模式
sh NVIDIA-Linux-x86_64-352.41.run     # 执行安装程序
# 接受授权 → 安装 32 位兼容库 → 自动修改 xorg.conf
nvidia-installer --update              # 后续升级用

23.3.2 AMD (ATI)

推荐使用 ELrepo 第三方软件库(专为 CentOS 7 提供 AMD 驱动):

rpm --import https://www.elrepo.org/RPM-GPG-KEY-elrepo.org
rpm -Uvh http://www.elrepo.org/elrepo-release-7.0-2.el7.elrepo.noarch.rpm
yum clean all
yum --enablerepo elrepo-testing search fglrx
yum --enablerepo elrepo-testing install fglrx-x11-drv
 
aticonfig -v    # 配置 xorg.conf

23.3.3 Intel

Intel 显卡驱动已开放源码,CentOS 7 默认已内置:

locate intel | grep xorg
# /usr/lib64/xorg/modules/drivers/intel_drv.so   ← 已存在!

如需手动配置 xorg.conf:

Section "Device"
    Identifier  "Videocard0"
    Driver      "intel"      # 改为 intel
EndSection
Section "Module"
    Load  "glx"
    Load  "dri"              # Direct Rendering Infrastructure
EndSection
Section "DRI"
    Mode 0666                # 允许所有用户使用 DRI
EndSection

CentOS 7 的 Xorg 自动侦测做得很好,大部分情况下不需要手动配置 xorg.conf

命令速查表

命令用途
startx从命令行启动 X Window
startx -- -depth 16指定色彩深度启动 X
X -version查看 X Server 版本
Xorg -configure自动生成 xorg.conf 配置文件
X :1 &手动在第 :1 显示接口启动 X Server
xterm -display :1 &在指定 display 启动终端机
xclock -display :1 &在指定 display 启动时钟
xrandr查看/设置分辨率和更新频率
xrandr -s 1280x800切换分辨率
gtf 1024 768 75 -x计算指定分辨率+更新频率的 Modeline
fc-cache -f -v重建字体缓存
fc-list列出所有可用字体
systemctl isolate graphical.target切换到图形模式
lspci | grep -i vga查看显卡型号
lsmod | grep nouveau检查 nouveau 模块是否被禁用
nvidia-installer --updateNVidia 驱动升级
aticonfig -vAMD 显卡配置工具