old-www/LDP/LG/issue72/chung.html

291 lines
12 KiB
HTML

<!--startcut ==============================================-->
<!-- *** BEGIN HTML header *** -->
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<HTML><HEAD>
<title>Automated Logins Revisited LG #72</title>
</HEAD>
<BODY BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#0000FF" VLINK="#0000AF"
ALINK="#FF0000">
<!-- *** END HTML header *** -->
<CENTER>
<A HREF="http://www.linuxgazette.com/">
<IMG ALT="LINUX GAZETTE" SRC="../gx/lglogo.png"
WIDTH="600" HEIGHT="124" border="0"></A>
<BR>
<!-- *** BEGIN navbar *** -->
<IMG ALT="" SRC="../gx/navbar/left.jpg" WIDTH="14" HEIGHT="45" BORDER="0" ALIGN="bottom"><A HREF="brod.html"><IMG ALT="[ Prev ]" SRC="../gx/navbar/prev.jpg" WIDTH="16" HEIGHT="45" BORDER="0" ALIGN="bottom"></A><A HREF="index.html"><IMG ALT="[ Table of Contents ]" SRC="../gx/navbar/toc.jpg" WIDTH="220" HEIGHT="45" BORDER="0" ALIGN="bottom" ></A><A HREF="../index.html"><IMG ALT="[ Front Page ]" SRC="../gx/navbar/frontpage.jpg" WIDTH="137" HEIGHT="45" BORDER="0" ALIGN="bottom"></A><A HREF="http://www.linuxgazette.com/cgi-bin/talkback/all.py?site=LG&article=http://www.linuxgazette.com/issue72/chung.html"><IMG ALT="[ Talkback ]" SRC="../gx/navbar/talkback.jpg" WIDTH="121" HEIGHT="45" BORDER="0" ALIGN="bottom" ></A><A HREF="../faq/index.html"><IMG ALT="[ FAQ ]" SRC="./../gx/navbar/faq.jpg"WIDTH="62" HEIGHT="45" BORDER="0" ALIGN="bottom"></A><A HREF="field.html"><IMG ALT="[ Next ]" SRC="../gx/navbar/next.jpg" WIDTH="15" HEIGHT="45" BORDER="0" ALIGN="bottom" ></A><IMG ALT="" SRC="../gx/navbar/right.jpg" WIDTH="15" HEIGHT="45" ALIGN="bottom">
<!-- *** END navbar *** -->
<P>
</CENTER>
<!--endcut ============================================================-->
<H4 ALIGN="center">
"Linux Gazette...<I>making Linux just a little more fun!</I>"
</H4>
<P> <HR> <P>
<!--===================================================================-->
<center>
<H1><font color="maroon">Automated Logins Revisited</font></H1>
<H4>By <a href="mailto:ajchung@email.com">Adrian J. Chung</a></H4>
</center>
<P> <HR> <P>
<!-- END header -->
<p>
As more users adopt GNU/Linux for use on their desktop PCs, machines
with only one
user are becoming increasingly common. Many new users
have little use for the multi-user logins that Linux supports. A very
common request among new desktop users is to configure their Linux
systems to automatically boot up a graphical desktop environment
(i.e. KDE or GNOME), for a single unprivileged user, without
prompting for a login ID or password.
<p>
This question is asked so frequently I am surprised that a HOWTO has
not been written up for it. (Well, none that I can find.) This
article is in no way comprehensive enough to fulfill such a role, but
hopefully it will point users in the right direction.
<h3>Prepackaged solutions</h3>
<p>
Solutions to automated logins have been proposed before and one answer
appears in an earlier issue of Linux Gazette (<a
href="../issue27/kodis.html">http://www.linuxgazette.com/issue27/kodis.html</a>)
The particular solution requires patching the <tt>/sbin/mingetty</tt>
program that is launched by <tt>init</tt> on bootup. (See <a
href="http://users.jagunet.com/~kodis/autologin/autologin.html">http://users.jagunet.com/~kodis/autologin/autologin.html</a>
for the patch and how to apply it.) Although automatic logins on
virtual consoles are facilitated, this by itself will not initiate a
graphical desktop. Read below for tips on how to set this up.
<p>
Alternatively one can install the <tt>autologin</tt> package (<a
href="http://www.linux-easy.com/development/autologin/">http://www.linux-easy.com/development/autologin/</a>)
This can handle the launching of graphical desktops on bootup
also. Not many GNU/Linux distributions include this as standard.
<p>
Automatic login is a feature provided by recent versions of
<tt>kdm</tt> (a KDE-style replacement for <tt>xdm</tt> -- the X11
login manager). Edit the <tt>/etc/kde2/kdmrc</tt> so that the
following lines are uncommented:
<p><pre>
AutoLoginEnable=true
AutoLoginUser=fred
</pre>
<p>
This configures <tt>kdm</tt> to automatically login <em>fred</em> on startup,
initiating <em>fred</em>'s chosen graphical desktop environment without any
user interaction. Mandrake provides a GUI component to enable this
<tt>kdm</tt> feature, thus avoiding any messy text editing.
<p>
But maybe one does not want nor need to install <tt>kdm</tt>. (Perhaps
there is not enough disk space, or <tt>kdm</tt> is too heavy weight
for an older PC.) Fortunately there are ways to automatically login a
user on one of the virtual consoles immediately after booting up,
without resorting to patches or additional downloads. The process can
be somewhat more involved, but it will work on a pretty minimal
GNU/Linux box -- no need to have GNOME, KDE, or QT-heavy <tt>kdm</tt>.
Even without X an automated login to a command prompt (or any other
interactive console application) on bootup can be quite handy.
<h3>The nuts-n-bolts method</h3>
<p>
Using your favourite text editor create a file named
<tt>autologinfred.c</tt> and type in this short C program:
<p>
<pre>
int main() {
execlp( "login", "login", "-f", "fred", 0);
}
</pre>
<p>
The <tt>execlp</tt> system call invokes the command <tt>"login -f
fred"</tt> and replaces the current processing context with this
invocation. The man page for <tt>login</tt> describes the action of
the <tt>-f<tt> argument. Compile this tiny C program using the GNU
C-compiler:
<p>
<pre>
$ <b>gcc -o autologinfred autologinfred.c</b>
</pre>
<p>
Gain root privileges (using <tt>su</tt>) and copy the executable to a
public directory:
<p>
<pre>
# <b>cp autologinfred /usr/local/sbin/</b>
</pre>
<p>
Now take a look at <tt>/etc/inittab</tt>. This is the configuration
file is used by <tt>init<tt>, the very first process started when
Linux initialises. You should observe lines similar to the following:
<p><pre>
1:2345:respawn:/sbin/mingetty tty1
2:2345:respawn:/sbin/mingetty tty2
3:2345:respawn:/sbin/mingetty tty3
</pre>
<p>
The exact contents of <tt>/etc/inittab</tt> differ from distribution
to distribution. On Debian systems one sees:
<p><pre>
1:2345:respawn:/sbin/getty 38400 tty1
2:23:respawn:/sbin/getty 38400 tty2
3:23:respawn:/sbin/getty 38400 tty3
</pre>
<p>
Edit the line beginning with <tt>"1:2345"</tt> so that it reads as
follows:
<p><pre>
1:2345:respawn:/sbin/getty -n -l /usr/local/sbin/autologinfred 38400 tty1
</pre>
<p>
The above will cause the user <em>fred</em> to be logged in
automatically on the first virtual console. On some GNU/Linux
distributions (like RedHat) <tt>/sbin/agetty</tt> must be used
instead. The <b><tt>-l &lt;alternative login&gt;</tt></b> argument to
<tt>getty</tt> substitutes the default <tt>/sbin/login</tt> program
with the one we compiled earlier. The <b><tt>-n</tt></b> tells
<tt>getty</tt> to not prompt for a user ID.
<h3>Initiating the desktop on login</h3>
<p>
If we reboot, the <tt>init</tt> process will automatically login the
user <em>fred</em> on the first virtual console and a command shell
will by started. User <em>fred</em> must still type in the
<tt><b>startx</b></tt> command to initiate the graphical desktop. Can
we automate this too?
<p>
If <em>fred</em>'s login shell is <tt>/bin/bash</tt>, the first
commands to be executed will always be listed in the file,
<tt>~fred/.bash_profile</tt>. We can add the <tt>startx</tt> command
here but this causes problems, since the <tt>.bash_profile</tt> will be
used in other situations such as when one is logging into a second
virtual console or when opening an <tt>xterm</tt>. Instead we append the
following lines:
<p><pre><b>
if [ -z "$DISPLAY" ] && [ $(tty) == /dev/tty1 ]; then
startx
fi
</b></pre>
<p>
Any new login shell started on the first virtual console will
automatically initiate a graphical desktop. The surrounding if
statement ensures that login shells launched from the desktop, or
initiated in virtual consoles other than the first one, do not
immediately start up a new GUI desktop. Users of <tt>/bin/sh</tt>
should append the above to <tt>~fred/.profile</tt>, and <tt>tcsh</tt>
users need to convert the above to the equivalent <tt>csh</tt> script.
<p>
If there is already a GUI desktop running (via <tt>xdm</tt>,
<tt>gdm</tt> or <tt>kdm</tt>, etc) then invoke <tt><b>startx --
:1</b></tt> instead. This creates a second GUI desktop. If one need
only have one desktop active, it would be better to disable any
existing <tt>Xserver</tt> instance by reducing the run level (RedHat)
or unlinking the <tt>/etc/rc?.d/S99?dm</tt> start up configuration
files (Debian).
<h3>Variations</h3>
<p>
Now whenever the machine boots, user <em>fred</em> is automatically
logged into the first virtual console, a <tt>bash</tt> login shell is
initiated, his <tt>~/.bash_profile</tt> is sourced, and
<tt>startx</tt> is invoked -- all without any user interaction or
prompting for passwords. Neat, huh?
<p>
We can go further by making use of the <tt>~/.xinitrc</tt> file to
initiate particular desktop applications. (<tt><b>man startx</b></tt>
for details.) Place your favourite game here and a Linux box can be
used like one of those arcade machines, minus the decorative case.
Launch an <em>Ogg Vorbis</em> player with visualisations and you can
have a dedicated music machine.
<p>
Unprompted logins can also be useful in a non-graphical context. One
could arrange to login a special user who has <tt>/usr/bin/top</tt> as
her shell. Now one virtual console will be devoted to an interactive
listing of active processes. The possibilities are limitless.
<h3>Conclusion</h3>
GNU/Linux, the multi-user operating system, is steadily becoming more
popular in single user settings. In these situations one often can
dispense with the user login protocols. This article illustrates that
its roots in the UNIX world do not detract from using Linux in these
dedicated areas. With simple changes in configuration, and a small
touch of programming, one can automate the login process on most
GNU/Linux distributions and still preserve a significant measure of
flexibility.
<!-- *** BEGIN bio *** -->
<SPACER TYPE="vertical" SIZE="30">
<P>
<H4><IMG ALIGN=BOTTOM ALT="" SRC="../gx/note.gif">Adrian J Chung</H4>
<EM>When not teaching undergraduate computing at the University of the West
Indies, Trinidad, Adrian is writing system level scripts to manage a network
of Linux boxes, and conducts experiments with interfacing various scripting
environments with home-brew computer graphics renderers and data visualization
libraries.</EM>
<!-- *** END bio *** -->
<!-- *** BEGIN copyright *** -->
<P> <hr> <!-- P -->
<H5 ALIGN=center>
Copyright &copy; 2001, Adrian J. Chung.<BR>
Copying license <A HREF="../copying.html">http://www.linuxgazette.com/copying.html</A><BR>
Published in Issue 72 of <i>Linux Gazette</i>, November 2001</H5>
<!-- *** END copyright *** -->
<!--startcut ==========================================================-->
<HR><P>
<CENTER>
<!-- *** BEGIN navbar *** -->
<IMG ALT="" SRC="../gx/navbar/left.jpg" WIDTH="14" HEIGHT="45" BORDER="0" ALIGN="bottom"><A HREF="brod.html"><IMG ALT="[ Prev ]" SRC="../gx/navbar/prev.jpg" WIDTH="16" HEIGHT="45" BORDER="0" ALIGN="bottom"></A><A HREF="index.html"><IMG ALT="[ Table of Contents ]" SRC="../gx/navbar/toc.jpg" WIDTH="220" HEIGHT="45" BORDER="0" ALIGN="bottom" ></A><A HREF="../index.html"><IMG ALT="[ Front Page ]" SRC="../gx/navbar/frontpage.jpg" WIDTH="137" HEIGHT="45" BORDER="0" ALIGN="bottom"></A><A HREF="http://www.linuxgazette.com/cgi-bin/talkback/all.py?site=LG&article=http://www.linuxgazette.com/issue72/chung.html"><IMG ALT="[ Talkback ]" SRC="../gx/navbar/talkback.jpg" WIDTH="121" HEIGHT="45" BORDER="0" ALIGN="bottom" ></A><A HREF="../faq/index.html"><IMG ALT="[ FAQ ]" SRC="./../gx/navbar/faq.jpg"WIDTH="62" HEIGHT="45" BORDER="0" ALIGN="bottom"></A><A HREF="field.html"><IMG ALT="[ Next ]" SRC="../gx/navbar/next.jpg" WIDTH="15" HEIGHT="45" BORDER="0" ALIGN="bottom" ></A><IMG ALT="" SRC="../gx/navbar/right.jpg" WIDTH="15" HEIGHT="45" ALIGN="bottom">
<!-- *** END navbar *** -->
</CENTER>
</BODY></HTML>
<!--endcut ============================================================-->