12

Almost every page I've found is about to automatically start Xorg after login wihout explianation, take ~/.bash_profile for example:

if [[ ! $DISPLAY && $XDG_VTNR -eq 1 ]]; then exec xinit fi 

I suppose $XDG_VTNR could be a variable for obtaining the current TTY number, however, there is already a command called tty, which can meet the same purpose.

My questions:

  1. What is $XDG_VTNR? Where and when is it being set?
  2. Where can I find the official documentation about this variable?
  3. tty is a built-in command while $XDG_VTNR is provided by Xorg, why people choose to use $XDG_VTNR instead of built-in tty?

2 Answers 2

11

What is $XDG_VTNR? Where and when is it being set?

It's set by the pam_systemd PAM module, and is only set on machines which are using systemd, which means that you should not rely on it in your scripts, unless you want to make them depend on systemd.

On systems which are using systemd, $XDG_VTNR will be set both in graphical (by lightdm, gdm, etc) and in text-mode sessions (by /bin/login).

Where can I find the official documentation about this variable?

In the pam_systemd(8) manpage.

tty is a built-in command while $XDG_VTNR is provided by Xorg, why people choose to use $XDG_VTNR instead of built-in tty?

1) tty is a standalone program, not a built-in, and $XDG_VTNR is not provided by Xorg.

2) Because they're completely different things. As clearly stated in its manpage, tty(1) will tell you the name of the terminal connected to its standard input, not the name of the virtual terminal your GUI session or such may be running on[1]. Consider this:

$ script -q /dev/null $ tty /dev/pts/5 $ script -q /dev/null $ tty /dev/pts/6 $ tty </dev/zero not a tty 

[1] for which XDG_VTNR isn't a reliable indicator either.

2
  • This is misleading, XDG_VTRN is available in at least GhostBSD, which obviously does not have systemd. I believe the quote you gave is correct however, this is an Xorg item, not a systemd related item. I don't know what sets it, it may vary. Commented Mar 17, 2021 at 2:06
  • I don't know what GhostBSD is, but I know that some BSDs have a systemd shim, in order to fool the GNOME stuff which has come to depend on systemd. On linux, XDG_VTNR is set by pam_systemd, as described in the linked manpage. Commented Jul 11, 2021 at 5:51
-1

XDG_VTNR is a somewhat poorly documented environmental variable. Systemd uses/sets it. See systemd.execand pam_systemd man pages. Some display managers also use/set it. Some X11 startup scripts use/set it.

From systemd/pam_systemd.c:

if (vtnr > 0) { char buf[DECIMAL_STR_MAX(vtnr)]; sprintf(buf, "%u", vtnr); r = update_environment(handle, "XDG_VTNR", buf); if (r != PAM_SUCCESS) return r; } 

You can start an X11 session within a virtual terminal (VT), not just in your console. The XDG_VTNR variable specifies the VT number.

The additional DISPLAY test is necessary since the code snippet may be executed on both graphical and non-graphical logins. Since XDG_VTNR will be set to the same value in the shells started within the X11 session (e.g. terminals), it is necessary to prevent them from trying to start X11.

The exec command replaces the login shell with the X11 session. This means that when the X11 session terminates, you will be logged out.

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.