TTY Autologin: Difference between revisions

From Alpine Linux
(Changing tcc to gcc cause tcc is on edge while gcc on main)
(use single "apk add" command to install packages)
 
(One intermediate revision by one other user not shown)
Line 18: Line 18:


== Prerequisites ==
== Prerequisites ==
* A C compiler
* A C compiler (e.g. '''''{{pkg|gcc}}''''' or '''''{{pkg|tcc}}''''')
* The '''''{{pkg|musl-dev}}''''' package which contains the C standard library
* The '''''{{pkg|musl-dev}}''''' package which contains the C standard library


==== Example on how to assolve the prerequisites: ====
==== Example on how to assolve the prerequisites: ====
{{Cmd|# apk add gcc
{{Cmd|# apk add gcc musl-dev}}
# apk add musl-dev}}


== Writing the autologin.c program ==
== Writing the autologin.c program ==


Create a file; in this example called autologin.c
Create a file in this example called autologin.c
{{Cmd|# vi autologin.c }}
 
Write into it the following C program.


{{cat|autologin.c|&#35;include <unistd.h>
{{cat|autologin.c|&#35;include <unistd.h>
Line 40: Line 36:
}}
}}


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


As parameters are passed:
As parameters are passed:
Line 48: Line 44:
== Compiling the autologin.c program ==
== Compiling the autologin.c program ==
If using gcc:
If using gcc:
{{Cmd|&#35; gcc -o autologin autologin.c }}
{{Cmd|# gcc -o autologin autologin.c }}


Move the binary autologin to {{path|/usr/sbin}}
Move the binary autologin to {{path|/usr/sbin}}
{{Cmd|&#35; mv autologin /usr/sbin/}}
{{Cmd|# mv autologin /usr/sbin/}}


== Editing /etc/inittab ==
== Editing /etc/inittab ==


Open {{path|/etc/inittab}}
Open {{path|/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.
replace "'':respawn:/sbin/getty''" with "'':respawn:/sbin/getty -n -l /usr/sbin/autologin''" for each TTY you want to enable autologin.
Line 65: Line 59:
==== Note ====
==== Note ====
To perform such a replacement on all TTYs, the following command can be used:
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 }}
{{Cmd|# sed -i 's@:respawn:/sbin/getty@:respawn:/sbin/getty -n -l /usr/sbin/autologin@g' /etc/inittab }}
* "'''@'''" is used as a delimiter
* "'''@'''" is used as a delimiter
* The '''-i''' flag edits the file in-place
* The '''-i''' flag edits the file in-place
Line 71: Line 65:
== Cleaning up ==
== Cleaning up ==
It is possible to remove the autologin.c file, the C compiler and the {{pkg|musl-dev}} package
It is possible to remove the autologin.c file, the C compiler and the {{pkg|musl-dev}} package
{{Cmd|&#35; rm autologin.c
 
&#35; apk del gcc musl-dev}}


== References ==
== References ==

Latest revision as of 21:25, 16 September 2023

By using agetty

How

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.

By compiling your own autologin wrapper

How

  1. Writing a wrapper, called autologin, around /bin/login and moving it in /usr/sbin/
  2. Editing /etc/inittab specifying the use of /usr/sbin/autologin instead of /bin/login

Prerequisites

  • A C compiler (e.g. gcc or tcc)
  • The musl-dev package which contains the C standard library

Example on how to assolve the prerequisites:

# apk add gcc musl-dev

Writing the autologin.c program

Create a file in this example called autologin.c

Contents of autologin.c

#include <unistd.h> int main() { execlp( "login", "login", "-f", "root", 0); }

The program makes a system call to execute 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.

Compiling the autologin.c program

If using gcc:

# gcc -o autologin autologin.c

Move the binary autologin to /usr/sbin

# mv autologin /usr/sbin/

Editing /etc/inittab

Open /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:

# 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


References