TTY Autologin: Difference between revisions

From Alpine Linux
m (Indicate that the OP's method only works in edge (tcc is in edge/testing, everywhere else would require mixing 3.x and edge which is not supported).)
m (fixed the sentence)
 
(13 intermediate revisions by 6 users not shown)
Line 1: Line 1:
What follows is one of many different ways to get autologin on edge.
This page documents how to setup autologin to tty. Once configured, when computer is powered on, you will be logged in automatically without typing password. Note that, if someone else starts up your computer, they will be able to access your account and your personal data.  


== How ==
== By using agetty ==
# Writing a wrapper, called autologin, around /bin/login and moving it in /usr/sbin/
# Editing /etc/inittab specifying the use of /usr/sbin/autologin instead of /bin/login


== Prerequisites ==
Install {{pkg|agetty}}: {{cmd|# apk add agetty}}
* A C compiler
* The '''''{{pkg|musl-dev}}''''' package which contains the C standard library


==== Example on how to assolve the prerequisites: ====
Edit {{path|/etc/inittab}} to use agetty<br>
{{Cmd|&#35; apk add --repository https://dl-cdn.alpinelinux.org/alpine/edge/testing tcc
Example for the virtual terminal tty1:<br>
&#35; apk add musl-dev }}
<code>tty1::respawn:/sbin/agetty --autologin root tty1 linux</code>


== Writing the autologin.c program ==
Example inittab entry for a serial terminal on ttys01:<br>
<code>ttyS0::respawn:/sbin/agetty --autologin root ttyS0 vt100</code>
{{tip|You can change the `tty1` or `ttyS0` to a different serial port or virtual terminal as you please. `root` can be changed to a different user as well. Finally the terminal type (`linux` and `vt100` in our examples) can be changed to a wide variety of serial terminals.}}


Create a file; in this example called autologin.c
{{Note| If using [[Proxmox]], follow the [[Proxmox#Using agetty in proxmox|steps]] to enable autologin with agetty.}}
{{Cmd|&#35; vi autologin.c }}


Write into it the following C program.
== By making your own autologin wrapper ==


{{ cmd|&#35;include <unistd.h>
Create a script, called autologin, around {{path|/bin/login}} and moving it in {{path|/usr/sbin/}} and editing {{path|/etc/inittab}} to specify the use of {{path|/usr/sbin/autologin}} instead of {{path|/bin/login}}.


int main()
# Create and edit the autologin wrapper script {{path|/usr/sbin/autologin}} as follows:{{cat|/usr/sbin/autologin|#!/bin/sh
{
exec login -f root}}
    execlp( "login", "login", "-f", "root", 0);
# Remember to make the scripts executable: {{cmd|# chmod +x /usr/sbin/autologin}}
}
# The script executes the ''login'' binary (part of busybox) which will be searched in $PATH. As parameters are passed:
}}
#:  '''-f'''  flag which stands for "Do not authenticate (user already authenticated)"
#:  ''username'' in this example is ''root'' but if you created a new user, its username can be used instead.
# Open {{path|/etc/inittab}} and replace "'':respawn:/sbin/getty''" with "'':respawn:/sbin/getty -n -l /usr/sbin/autologin''" for each TTY you want to enable autologin.
#: The getty's '''-n''' flag do not prompt the user for a login name
#: The getty's '''-l''' flag invokes a custom login instead of {{path|/bin/login}}; in our case it is set to invoke {{path|/usr/sbin/autologin}}
# To perform such a replacement on all TTYs, the following command can be used:{{Cmd|# sed -i 's@:respawn:/sbin/getty@:respawn:/sbin/getty -n -l /usr/sbin/autologin@g' /etc/inittab }}
#:  "'''@'''" is used as a delimiter
#:  The '''-i''' flag edits the file in-place


The only thing it does is a system call to execute the ''login'' binary (part of busybox) which will be searched in PATH.
== Use greetd ==


As parameters are passed:
The [[Greetd]] display manager has a text based greeter that can be configured to autologin.
* '''-f'''  flag which stands for "Do not authenticate (user already authenticated)"
* ''username'' in this example is ''root'' but if you created a new user, its username can be used instead.


== Compiling the autologin.c program ==
== See also ==
If using tcc:
* [https://git.busybox.net/busybox/tree/init/init.c Busybox init source, substantial comments documenting /etc/inittab are at the bottom]
{{Cmd|&#35; tcc -o autologin autologin.c }}
* [http://littlesvr.ca/linux-stuff/articles/autologinconsole/autologinconsole.php Linux-Stuff: Log in automatically to a console when Linux boots]
* [https://wiki.gumstix.com/index.php/AutoLogin AutoLogin - Gumstix User Wiki]
* [https://busybox.net/downloads/BusyBox.html#getty Busybox getty arguments]
* [https://github.com/util-linux/util-linux/blob/master/term-utils/agetty.8.adoc agetty(8) Manual Page]


Move the binary autologin to /usr/sbin
[[Category:Display Managers]]
{{Cmd|&#35; mv autologin /usr/sbin/ }}
[[Category:Desktop]]
 
== Editing /etc/inittab ==
 
Open /etc/inittab
 
{{Cmd|&#35; vi /etc/inittab }}
 
replace "'':respawn:/sbin/getty''" with "'':respawn:/sbin/getty -n -l /usr/sbin/autologin''" for each TTY you want to enable autologin.
* The getty's '''-n''' flag do not prompt the user for a login name
* The getty's '''-l''' flag invokes a custom login instead of /bin/login; in our case it is set to invoke /usr/sbin/autologin
 
==== Note ====
To perform such a replacement on all TTYs, the following command can be used:
{{Cmd|&#35; sed -i 's@:respawn:/sbin/getty@:respawn:/sbin/getty -n -l /usr/sbin/autologin@g' /etc/inittab }}
* "'''@'''" is used as a delimiter
* The '''-i''' flag edits the file in-place
 
== Cleaning up ==
It is possible to remove the autologin.c file, the C compiler and the '''musl-dev''' package
{{Cmd|&#35; rm autologin.c
&#35; apk del tcc
&#35; apk del musl-dev}}
 
== References ==
# http://littlesvr.ca/linux-stuff/articles/autologinconsole/autologinconsole.php
# https://wiki.gumstix.com/index.php/AutoLogin

Latest revision as of 13:06, 13 March 2025

This page documents how to setup autologin to tty. Once configured, when computer is powered on, you will be logged in automatically without typing password. Note that, if someone else starts up your computer, they will be able to access your account and your personal data.

By using agetty

Install agetty:

# apk add agetty

Edit /etc/inittab to use agetty
Example for the virtual terminal tty1:
tty1::respawn:/sbin/agetty --autologin root tty1 linux

Example inittab entry for a serial terminal on ttys01:
ttyS0::respawn:/sbin/agetty --autologin root ttyS0 vt100

Tip: You can change the `tty1` or `ttyS0` to a different serial port or virtual terminal as you please. `root` can be changed to a different user as well. Finally the terminal type (`linux` and `vt100` in our examples) can be changed to a wide variety of serial terminals.
Note: If using Proxmox, follow the steps to enable autologin with agetty.

By making your own autologin wrapper

Create a script, called autologin, around /bin/login and moving it in /usr/sbin/ and editing /etc/inittab to specify the use of /usr/sbin/autologin instead of /bin/login.

  1. Create and edit the autologin wrapper script /usr/sbin/autologin as follows:

    Contents of /usr/sbin/autologin

    #!/bin/sh exec login -f root
  2. Remember to make the scripts executable:

    # chmod +x /usr/sbin/autologin

  3. The script executes the login binary (part of busybox) which will be searched in $PATH. As parameters are passed:
    -f flag which stands for "Do not authenticate (user already authenticated)"
    username in this example is root but if you created a new user, its username can be used instead.
  4. Open /etc/inittab and replace ":respawn:/sbin/getty" with ":respawn:/sbin/getty -n -l /usr/sbin/autologin" for each TTY you want to enable autologin.
    The getty's -n flag do not prompt the user for a login name
    The getty's -l flag invokes a custom login instead of /bin/login; in our case it is set to invoke /usr/sbin/autologin
  5. To perform such a replacement on all TTYs, the following command can be used:

    # sed -i 's@:respawn:/sbin/getty@:respawn:/sbin/getty -n -l /usr/sbin/autologin@g' /etc/inittab

    "@" is used as a delimiter
    The -i flag edits the file in-place

Use greetd

The Greetd display manager has a text based greeter that can be configured to autologin.

See also