xinit (简体中文)
~/.xinitrc
文件是 xinit
和它的前端 startx
第一次启动时会读取的脚本。通常用在启动 X 时执行窗口管理器 和其他程序,例如启动守护进程和设置环境变量。xinit
程序用来启动X窗口系统,是不使用显示管理器时的第一个客户端。
~/.xinitrc
一个主要功能是根据单个用户的设置决定 /usr/bin/startx
或 /usr/bin/xinit
程序启动的窗口系统。~/.xinitrc
中还可以加入许多系统定制选项。大部分显示管理器也会在 xinit 前读取相似的 xprofile 文件。
Contents
安装
安装 软件包 xorg-xinit. 此软件包提供了 xinit、startx和默认的 xinitrc 文件。
配置
xserverrc
xserverrc
文件是一个启动 X server 的 shell 脚本。如果存在 ~/.xserverrc
,startx 和 xinit 都会执行这个文件。如果文件不存在,startx 会使用 /etc/X11/xinit/xserverrc
.
为了维护 logind
的 authenticated session 会话,避免切换终端时跳过屏幕锁, 必须找用户登录的虚拟终端启动 Xorg。[1] 所以建议在 ~/.xserverrc
中指定 vt$XDG_VTNR
:
~/.xserverrc
#!/bin/sh exec /usr/bin/Xorg -nolisten tcp "$@" vt$XDG_VTNR
如果要让 X 在其他的终端启动,可以使用 /usr/lib/systemd/systemd-multi-seat-x
提供的 X server 包裹程序。修改 ~/.xserverrc
,可以让 xinit 和startx 都使用这个包裹程序.
xinitrc
如果用户主目录中存在 .xinitrc
,startx 和 xinit 会执行此文件。如果不存在,startx 会执行默认的 /etc/X11/xinit/xinitrc
。这个文件默认启动 Twm 和 Xterm. xinit 的默认行为不一样,请参阅 xinit(1)。要设置窗口管理器或桌面环境,先通过复制创建默认文件:
$ cp /etc/X11/xinit/xinitrc ~/.xinitrc
根据示例文件修改可以保留一些默认行为,例如会引用 /etc/X11/xinit/xinitrc.d
中以 .sh
结尾的脚本。
然后编辑 ~/.xinitrc
,例如要使用 Openbox,修改为:
#!/bin/sh # # ~/.xinitrc # # Executed by startx (run your window manager from here) ... if [ -d /etc/X11/xinit/xinitrc.d ] ; then for f in /etc/X11/xinit/xinitrc.d/?*.sh ; do [ -x "$f" ] && . "$f" done unset f fi # exec gnome-session # exec startkde # exec startxfce4 # exec wmaker # exec icewm # exec blackbox # exec fluxbox # exec openbox-session # ...or the Window Manager of your choice ## some applications that should be run in the background xscreensaver & xsetroot -cursor_name left_ptr & exec openbox-session
~/.xinitrc
中应该只有 一个 未注释掉的 exec
行,而且 exec 行必须位于配置文件的末尾。exec 后面的所有命令只有窗口退出后才会被执行。在窗口管理器前启动的命令应该用 & 在后台启动, 否则启动程序会等待它们退出。使用 exec
作为前缀会替换当前的进程,这样进程进入后台时 X 不会退出。
使用
现在以普通用户启动 X:
$ startx
或者
$ xinit -- :1
-- :display_number
指定,display_number
是 1 或更高的数值。选择的窗口管理器或桌面环境就应该正常启动了.
要退出 X, 运行窗口管理器的退出功能,如果窗口管理器未提供此功能,可以运行:
$ pkill -15 Xorg
$ pkill -15 -t tty"$XDG_VTNR" Xorg
xprop
是软件包 xorg-xprop提供的。
在启动时自动启用 X
先确保 startx 已经配置好了。
如果使用Bash, 编辑 ~/.bash_profile
,加入如下内容. 如果文件不存在,从 /etc/skel/.bash_profile
复制一个框架版本。
如果使用 zsh,则编辑 ~/.zprofile
.
[ -z "$DISPLAY" -a "$(fgconsole)" -eq 1 ] && exec startx
- 此方法与automatic login to virtual console一起可以实现自动登陆。
- 如果 X 被关闭,用户将自动退出。要避免这个问题,删除
exec
。 - 要将 X 会话的输出重定向到一个文件,请创建一个别名alias:
-
alias startx='startx &> ~/.xlog'
参阅 Fish#Start X at login 和 Systemd/User#Automatic login into Xorg without display manager.
自动登录到虚拟终端
可以和 自动登录到虚拟终端一起使用.
提示和技巧
从命令行覆盖 xinitrc
如果你有一个可用的 ~/.xinitrc
, 件,只想尝试下其他的窗口管理器/桌面环境,你可从命令行给 startx
完整路径
$ startx /full/path/to/window-manager
Note that the full path is required. Optionally, you can also specify custom options for #xserverrc script by appending them after --
, e.g.:
$ startx /usr/bin/enlightenment -- -br +bs -dpi 96
参阅 startx(1).
Making a DE/WM choice
If you are frequently switching between different DEs/WMs, it is recommended to either use a Display manager or add code to .xinitrc
. The code described next consists of a simple few lines, which will take the argument and load the desired desktop environment or window manager.
The following example ~/.xinitrc
shows how to start a particular DE/WM with an argument:
~/.xinitrc
... # Here Xfce is kept as default session=${1:-xfce} case $session in awesome ) exec awesome;; bspwm ) exec bspwm;; catwm ) exec catwm;; cinnamon ) exec cinnamon-session;; dwm ) exec dwm;; enlightenment ) exec enlightenment_start;; ede ) exec startede;; fluxbox ) exec startfluxbox;; gnome ) exec gnome-session;; gnome-classic ) exec gnome-session --session=gnome-classic;; i3|i3wm ) exec i3;; icewm ) exec icewm-session;; jwm ) exec jwm;; kde ) exec startkde;; mate ) exec mate-session;; monster|monsterwm ) exec monsterwm;; notion ) exec notion;; openbox ) exec openbox-session;; unity ) exec unity;; xfce|xfce4 ) exec startxfce4;; xmonad ) exec xmonad;; # No known session, try to run it as command *) exec $1;; esac
After that, you can easily start a particular DE/WM by passing an argument, e.g.:
$ xinit $ xinit gnome $ xinit kde $ xinit wmaker
or
$ startx $ startx ~/.xinitrc gnome $ startx ~/.xinitrc kde $ startx ~/.xinitrc wmaker
不启动窗口管理器,直接启动程序
It is possible to start only specific applications without a window manager, although most likely this is only useful with a single application shown in full-screen mode. For example:
~/.xinitrc
... exec chromium
With this method you need to set each application window's geometry through its own configuration files, if possible at all.
See also Display manager#Starting applications without a window manager.