Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed
#!/bin/bash
#
################################################################
#
# Enable or disable auto login to the system
#
# Urs Beyerle
#
################################################################
function usage() {
## Usage
# ----------------------------------------------------------
cat <<EOF
Enable or disables auto login
Usage:
livecd-autologin [-h] [-u <user>] [-s <session> ] on | off
on: enable autologin to system
off: disable autologin to system
Optional Options:
-u <user>: define user for auto login, default is sluser
-s <session>: define session startup command, default is icewm-session
-h: print this screen
Note: Works only for users with bash shell.
EOF
}
###############################################################
### definitions
###############################################################
AUTOLOGIN=""
ROOTLOGIN=""
LOCALUSER_EXISTS=""
LOCALUSER="sluser"
STARTSESSION="icewm-session"
###############################################################
### Read options from command-line
###############################################################
if [ $# -eq 0 ]; then
usage; exit
fi
while [ $# -gt 0 ]; do
case "$1" in
-h)
usage; exit;;
-u)
shift; LOCALUSER=$1; shift; continue;;
-s)
shift; STARTSESSION=$1; shift; continue;;
on)
AUTOLOGIN=on; shift; continue;;
off)
AUTOLOGIN=off; shift; continue;;
*)
usage; exit;;
esac
done
###############################################################
### User
###############################################################
### test if $LOCALUSER exists
id $LOCALUSER >/dev/null 2>&1
[ "$?" = "0" ] && LOCALUSER_EXISTS="true"
### home directory of $LOCALUSER
[ $LOCALUSER_EXISTS ] && LOCALUSER_HOME=$(grep "^${LOCALUSER}:" /etc/passwd | cut -d":" -f6)
### warn message
[ ! $LOCALUSER_EXISTS ] && echo "$LOCALUSER does not exisit. Create user first and run script again."
###############################################################
# Enable autologin
###############################################################
if [ $AUTOLOGIN = on ]; then
echo "Enable auto login for $LOCALUSER ..."
### backup /etc/inittab
echo " - Backup /etc/inittab"
if [ ! -f /etc/inittab.no_autologin ]; then
cp -a /etc/inittab /etc/inittab.no_autologin
fi
### change init runlevel to 3
echo " - Change init runlevel to 3"
sed -i "s/id\:.\:initdefault/id\:3\:initdefault/" /etc/inittab
### autologin at tty6
echo " - Modify /etc/inittab"
line="6:2345:respawn:\/sbin\/mingetty --autologin $LOCALUSER tty6"
sed -i "s/.*respawn.*tty6.*/$line/" /etc/inittab
if [ $LOCALUSER_EXISTS ]; then
echo " - Create user files ~/.xinitrc, ~/.xsession"
### create .xinitrc for LOCALUSER
XINITRC=$LOCALUSER_HOME/.xinitrc
echo "[ -x ~/autostart ] && ~/autostart" > $XINITRC
echo "exec $STARTSESSION" >> $XINITRC
chmod 755 $XINITRC
### link .xsession to .xinitrc
ln -sf .xinitrc $LOCALUSER_HOME/.xsession
echo " - Modify user file ~/.bashrc"
### run startx (over .bashrc) at autotlogin of LOCALUSER and root
grep -q "/dev/tty6" $LOCALUSER_HOME/.bashrc >/dev/null 2>&1
if [ "$?" = "1" ]; then
echo 'if [ "$( tty )" = "/dev/tty6" ]; then startx; fi' >> $LOCALUSER_HOME/.bashrc
fi
fi
echo "done."
fi
###############################################################
# Disable autologin
###############################################################
if [ $AUTOLOGIN = off ]; then
echo "Disable auto login for $LOCALUSER ..."
### restore /etc/inittab
echo " - Restore original /etc/inittab"
if [ -f /etc/inittab.no_autologin ]; then
mv /etc/inittab.no_autologin /etc/inittab
else
# fall back if no original /etc/inittab found
sed -i "s|.*respawn.*tty6.*|6:2345:respawn:/sbin/mingetty tty6|" /etc/inittab
fi
### disable auto login for $LOCALUSER and root
if [ $LOCALUSER_EXISTS ]; then
echo " - Delete user files ~/.xinitrc, ~/.xsession"
# remove .xinitrc
[ $LOCALUSER_EXISTS ] && rm -f $LOCALUSER_HOME/.xinitrc
# remove link .xsession
[ $LOCALUSER_EXISTS ] && rm -f $LOCALUSER_HOME/.xsession
echo " - Modify user file ~/.bashrc"
# remove startx line from .bashrc
[ $LOCALUSER_EXISTS ] && sed -i "/.*\/dev\/tty6.*then startx.*/d" $LOCALUSER_HOME/.bashrc
fi
echo "done."
fi
###############################################################
# Final note
###############################################################
echo
echo "Please note, you may have to reboot to make the changes active."
echo