Log in automatically to a console when Linux boots

Posted by: Andrew Smith
Poster contact info: andrew-smith at mail ru
Author: Various
Originally published: http://www.linuxquestions.org/questions/t374338.html
Software: Linux All, agetty

Here's a scenario: someone wants a Linux machine (terminal-only, no GUI) to log in automatically to a console and execute a command.

In this guide I use Slackware (doesn't matter which version) but the same should work for any other distribution. you need to do a bit of work to set it up but it's all described here.

First edit the file /etc/login.defs and uncomment the following line (remove the '#' in front):
NO_PASSWORD_CONSOLE tty1:tty2:tty3:tty4:tty5:tty6

You have to do that so it doesn't ask for a password once you log in.

Second, edit /etc/inittab. This file is a bit compicated so it helps if you're familiar with it a little, but if you're not just work on the following line:
c1:12345:respawn:/sbin/agetty 38400 tty1 linux

You want to tell the agetty program to execute an auto login program (we will write this below). Replace the line above with the follwing:
c1:12345:respawn:/sbin/agetty -n -l /usr/sbin/autologin 38400 tty1 linux

The /usr/sbin/autologin program doesn't come with your system, you have to write it and compile it yourself, but it's pretty easy. First create an empty text file and add the following contents to it:
int main() 
{
  execlp( "login", "login", "-f", "andrew", 0);
}

Replace andrew with the name of the user you want to log in automatically and save the file as autologin.c.

Then compile the program like this (you have to be root for that to work):
cc autologin.c -o /usr/sbin/autologin

And that's it. When your machine finishes booting it will execute the autologin program which will log in as the user you specified in autologin.c and because you edited /etc/login.defs it won't ask for a password.

Now to execute a program after it logs in just edit .bash_profile in the user's home directory (create the file if it doesn't exist) and append the command you want to run at the end of that file, for example this is my .bash_profile, all it does is run pico:
pico

Enjoy.