TTY Autologin: Difference between revisions

From Alpine Linux
mNo edit summary
m (added category)
 
(12 intermediate revisions by 4 users not shown)
Line 1: Line 1:
What follows is one from many different ways to get autologin.
=By using agetty=


== How ==
== How ==
# Writing a wrapper, called autologin, around /bin/login and moving it in /usr/sbin/
Install {{pkg|agetty}}: {{cmd|# apk add agetty}}
# Editing /etc/inittab specifying the use of /usr/sbin/autologin instead of /bin/login
Edit {{path|/etc/inittab}} to use agetty<br>
Example for the virtual terminal tty1:<br>
<code>tty1::respawn:/sbin/agetty --autologin root tty1 linux</code>


== Prerequisites ==
Example inittab entry for a serial terminal on ttys01:<br>
* A C compiler
<code>ttyS0::respawn:/sbin/agetty --autologin root ttyS0 vt100</code>
* The '''''musl-dev''''' package which contains the C standard library
{{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.}}


==== Example on how to assolve the prerequisites: ====
=By making your own autologin wrapper=
{{Cmd|&#35; apk add --repository https://dl-cdn.alpinelinux.org/alpine/edge/testing tcc
&#35; apk add musl-dev }}


== Writing the autologin.c program ==
== How ==
# Writing a wrapper script, called autologin, around {{path|/bin/login}} and moving it in {{path|/usr/sbin/}}
# Editing {{path|/etc/inittab}} specifying the use of {{path|/usr/sbin/autologin}} instead of {{path|/bin/login}}


Create a file; in this example called autologin.c
== Writing the autologin script ==
{{Cmd|&#35; vi autologin.c }}


Write into it the following C program.
Edit and create the script in this example in the location {{path|/usr/sbin/autologin}}:


{{ cmd|&#35;include <unistd.h>
{{cat|/usr/sbin/autologin|#!/bin/sh
exec login -f root}}


int main()
Remember to make the scripts executable:
{
{{cmd|# chmod +x /usr/sbin/autologin}}
    execlp( "login", "login", "-f", "root", 0);
}
}}


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


As parameters are passed:
As parameters are passed:
* '''-f'''  flag which stands for "Do not authenticate (user already authenticated)"
* '''-f'''  flag which stands for "Do not authenticate (user already authenticated)"
* ''username'' in this example is ''root'' but if you created a new user, the user username can be used instead.
* ''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 tcc:
{{Cmd|&#35; tcc -o autologin autologin.c }}
 
Move the binary autologin to /usr/sbin
{{Cmd|&#35; mv autologin /usr/sbin/ }}


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


Open /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 each "'':respawn:/sbin/getty''" with "'':respawn:/sbin/getty -n -l /usr/sbin/autologin''"
* The getty's '''-n''' flag do not prompt the user for a login name
* 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
* 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}}


== Cleaning up ==
==== Note ====
It is possible to remove the autologin.c file, the C compiler and the '''musl-dev''' package
To perform such a replacement on all TTYs, the following command can be used:
{{Cmd|&#35; rm autologin.c
{{Cmd|# sed -i 's@:respawn:/sbin/getty@:respawn:/sbin/getty -n -l /usr/sbin/autologin@g' /etc/inittab }}
&#35; apk del tcc
* "'''@'''" is used as a delimiter
&#35; apk del musl-dev}}
* The '''-i''' flag edits the file in-place


== References ==
== References ==
# http://littlesvr.ca/linux-stuff/articles/autologinconsole/autologinconsole.php
* [https://git.busybox.net/busybox/tree/init/init.c Busybox init source, substantial comments documenting /etc/inittab are at the bottom]
# https://wiki.gumstix.com/index.php/AutoLogin
* [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]
 
[[Category:Display Managers]]
[[Category:Desktop]]

Latest revision as of 04:05, 24 September 2024

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 making your own autologin wrapper

How

  1. Writing a wrapper script, 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

Writing the autologin script

Edit and create the script in this example in the location /usr/sbin/autologin:

Contents of /usr/sbin/autologin

#!/bin/sh exec login -f root

Remember to make the scripts executable:

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

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

References