# xinitrc (简体中文)

相关文章

- Display manager (简体中文)
- Start X at Login (简体中文)
- Xorg_(简体中文)
- Xprofile_(简体中文)
- Xresources

翻译状态： 本文是英文页面 Xinitrc 的翻译，最后翻译时间：2015-07-27，点击这里可以查看翻译后英文页面的改动。

`~/.xinitrc` 文件是 `xinit` 和它的前端 `startx` 第一次启动时会读取的脚本。通常用在启动 X 时执行窗口管理器 和其他程序，例如启动守护进程和设置环境变量。`xinit`程序用来启动X窗口系统，是不使用显示管理器时的第一个客户端。

`~/.xinitrc` 一个主要功能是根据单个用户的设置决定 `/usr/bin/startx` 或 `/usr/bin/xinit` 程序启动的窗口系统。`~/.xinitrc` 中还可以加入许多系统定制选项。大部分显示管理器也会在 xinit 前读取相似的 xprofile 文件。

## 安装

安装 软件包 xorg-xinit.

## 配置

如果用户主目录中存在 `.xinitrc`，startx 和 xinit 会执行此文件。如果不存在，startx 会执行 `/etc/X11/xinit/xinitrc`。这个文件默认启动 Twm 和 Xterm (xinit 的默认行为不一样，请参阅 `man 1 xinit`). 所以要设置窗口管理器或桌面环境，先复制文件：

    $ cp /etc/X11/xinit/xinitrc ~/.xinitrc

根据示例文件修改可以保留一些默认行为，例如会执行 `/etc/X11/xinit/xinitrc.d` 中的脚本。

然后编辑 `~/.xinitrc` ，例如要使用 Openbox，修改为：

    #!/bin/sh
    #
    # ~/.xinitrc
    #
    # Executed by startx (run your window manager from here)
    ...
    # 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

    xscreensaver &
    xsetroot -cursor_name left_ptr &
    exec openbox-session

`~/.xinitrc` 中应该只有 一个 未注释掉的 `exec` 行，而且 exec 行必须位于配置文件的末尾。exec 后面的所有命令只有窗口退出后才会被执行。在窗口管理器前启动的命令应该用 & 在后台启动, 否则启动程序会等待它们退出。使用 `exec` 作为前缀会替换当前的进程，这样进程进入后台时 X 不会退出。

现在以普通用户启动 X：

     $ startx

或者

    $ xinit -- :1 -nolisten tcp vt$XDG_VTNR

- 上面命令在用户登录的虚拟终端执行 Xorg，[1] 这样 `logind` 就可以保持认证会话，而且切换虚拟终端也无法跳过屏保。
- 在 xinit 命令中必须使用 `vt$XDG_VTNR` 才能 保持会话权限.
- xinit 在登录了不同的虚拟终端是不会处理多个会话。所以必须通过`-- :session_no` 指定会话。如果 X 已经在运行，需要指定 :1 或更高。

## 在启动的时候自动启用x

Note: These solutions run X on the same tty used to login, which is required in order to maintain the login session.

For Bash, add the following to the bottom of `~/.bash_profile`. If the file does not exist, copy a skeleton version from `/etc/skel/.bash_profile`. For Zsh, add it to `~/.zlogin` (or `~/.zprofile`) instead.

    [[ -z $DISPLAY && $XDG_VTNR -eq 1 ]] && exec startx

Note:

- You can replace the `-eq 1` comparison with one like `-le 3` (for vt1 to vt3) if you want to use graphical logins on more than one VT.
- X must always be run on the same tty where the login occurred, to preserve the logind session. This is handled by the default `/etc/X11/xinit/xserverrc`.
- `xinit` may be faster than `startx`, but needs additional parameter such as `-nolisten tcp`.
- If you would like to remain logged in when the X session ends, remove `exec`.

See also Fish#Start X at login and Systemd/User#Automatic login into Xorg without display manager.

### 自动登录到虚拟终端

This method can be combined with automatic login to virtual console. When doing this you have to set correct dependencies for the autologin systemd service to ensure that dbus is started before `~/.xinitrc` is read and hence pulseaudio started (see: BBS#155416)

## 提示和技巧

### 从命令行覆盖 xinitrc

如果你有一个可用的 `~/.xinitrc`, 件，只想尝试下其他的窗口管理器/桌面环境，你可从命令行给 `startx` 完整路径

    $ startx /full/path/to/window-manager

Note that the full path is required. Optionally, you can also override `/etc/X11/xinit/xserverrc` file (which stores the default X server options) with custom options by appending them after `--`, e.g.:

    $ startx /usr/bin/enlightenment -- -nolisten tcp -br +bs -dpi 96 vt$XDG_VTNR

or

    $ xinit /usr/bin/enlightenment -- -nolisten tcp -br +bs -dpi 96 vt$XDG_VTNR

参阅 `man startx`.

### 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

Then copy the `/etc/X11/xinit/xserverrc` file to your home directory:

    $ cp /etc/X11/xinit/xserverrc ~/.xserverrc

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.

Tip: This method can be useful to launch graphical games, especially on systems where excluding the memory or CPU usage of a window manager or desktop environment, and possible accessory applications, can help improve the game's execution performance.

See also Display manager#Starting applications without a window manager.

Categories:

- Desktop environments (简体中文)
- X Server (简体中文)