Automatic login to virtual console

From ArchWiki
Revision as of 17:31, 18 September 2012 by Stqn (Talk | contribs) (With systemd: Added multi-user.target.)

Jump to: navigation, search
Summary help replacing me
Describes how to automatically log in to a virtual console.
Related
Display Manager
Start X at Login

This article describes how to automatically log in to a virtual console at the end of the boot process. This article only covers console log-ins; methods for starting an X server are described in Start X at Boot.

With default init

Using agetty

This is the preferred (i.e. clean) method. Edit /etc/inittab like this:

/etc/inittab
c1:2345:respawn:/sbin/agetty -a USERNAME -8 -s 38400 tty1 linux
c2:2345:respawn:/sbin/agetty -8 -s 38400 tty2 linux
c3:2345:respawn:/sbin/agetty -8 -s 38400 tty3 linux
c4:2345:respawn:/sbin/agetty -8 -s 38400 tty4 linux
c5:2345:respawn:/sbin/agetty -8 -s 38400 tty5 linux
c6:2345:respawn:/sbin/agetty -8 -s 38400 tty6 linux

to automatically log in USERNAME to the first console (tty1).

If you want your username to be used automatically without skipping the password prompt:

c1:2345:respawn:/sbin/agetty -a USERNAME -o "-- \u" -8 -s 38400 tty1 linux

Using mingetty

Note: Although a mingetty derivative, fgettyAUR does not support the --autologin parameter.

Alternatively, you can install the mingetty package from the official repositories. Mingetty is designed to be a minimal getty and allows automatic log-ins:

/etc/inittab
c1:2345:respawn:/sbin/mingetty --autologin USERNAME tty1 linux
c2:2345:respawn:/sbin/agetty -8 -s 38400 tty2 linux
c3:2345:respawn:/sbin/agetty -8 -s 38400 tty3 linux
c4:2345:respawn:/sbin/agetty -8 -s 38400 tty4 linux
c5:2345:respawn:/sbin/agetty -8 -s 38400 tty5 linux
c6:2345:respawn:/sbin/agetty -8 -s 38400 tty6 linux

Using the above methods, you will get a re-login loop when trying to log out. If you wish to only login at boot, use the following:

/etc/inittab
c2:2345:respawn:/sbin/agetty -8 -s 38400 tty2 linux
c3:2345:respawn:/sbin/agetty -8 -s 38400 tty3 linux
c4:2345:respawn:/sbin/agetty -8 -s 38400 tty4 linux
c5:2345:respawn:/sbin/agetty -8 -s 38400 tty5 linux
c6:2345:respawn:/sbin/agetty -8 -s 38400 tty6 linux
a1:2345:wait:/sbin/mingetty --autologin USERNAME tty1 linux
c1:2345:respawn:/sbin/agetty -8 -s 38400 tty1 linux

Using a C login program

As an alternative, a C login program can be written:

autologin.c
#include <unistd.h>

int main(void) {
   execlp("login", "login", "-f", "USERNAME", NULL);
}

Here, the C function execlp executes the command login -f USERNAME.

The program must be compiled and copied to an appropriate location:

$ gcc -o autologin autologin.c
# cp autologin /usr/local/sbin/

Finally, tell /etc/inittab to use it:

/etc/inittab
c1:2345:respawn:/sbin/agetty -n -l /usr/local/sbin/autologin -s 38400 tty1 linux
c2:2345:respawn:/sbin/agetty -8 -s 38400 tty2 linux
c3:2345:respawn:/sbin/agetty -8 -s 38400 tty3 linux
c4:2345:respawn:/sbin/agetty -8 -s 38400 tty4 linux
c5:2345:respawn:/sbin/agetty -8 -s 38400 tty5 linux
c6:2345:respawn:/sbin/agetty -8 -s 38400 tty6 linux

With systemd

Create a new service file similar to getty@.service by copying it to /etc/systemd/system/:

# cp /usr/lib/systemd/system/getty@.service /etc/systemd/system/autologin@.service
Note: /etc/systemd/system/ takes precedence over /usr/lib/systemd/system/.

Once created you can link the new autologin@.service to your chosen tty, e.g. tty1, tty2, [...] tty8, etc, by specifying it as an alias in the [Install] section of the unit file. Also, change the value of ExecStart in autologin@.service, to end up with something like this:

/etc/systemd/system/autologin@.service
[Service]
[...]
ExecStart=-/sbin/agetty --noclear -a USERNAME %I 38400
[...]
[Install]
Alias=getty.target.wants/getty@tty1.service
Tip: It is possible to change Type=idle to Type=simple and avoid delaying the execution of agetty until all jobs (state change requests to units) are completed. This option is more useful when starting X automatically. See man systemd.service for more info.
Note: Type=simple can cause systemd bootup messages to pollute the login prompt.

For security reasons you may also want to change Restart=always to Restart=no.

Finally you should reload the daemons and enable and start the service:

# systemctl daemon-reload
# systemctl enable autologin@tty1.service
# systemctl start autologin@tty1.service
Warning: If you are currently in an X session on the same TTY configured in the service file, starting autologin@ttyX.service will kill your X server.

To avoid errors related to display-manager.service in dmesg, you should set the default target to multi-user instead of graphical:

# systemctl enable multi-user.target

(See also: Change default runlevel/target to boot into.)