Home Code Other Software

Table of Contents


The pyutmp module provides a Python-oriented interface to the utmp file on Unix-like operating systems. To paraphrase the Linux Programmer’s Manual page utmp(5), the utmp file allows one to discover information about who is currently using (i.e., is logged into) the system. The utmp file is a series of entries whose structure is typically defined by the utmp.h C header file.

This module provides an read-only interface to the underlying operating system’s C utmp API.

Getting and installing pyutmp

pyutmp relies on the presence of the utmp library, which isn’t automatically present on all operating systems. In addition, pyutmp uses Cython-generated C files to provide Python access to the C utmp libraries.

It is impractical to provide binaries of pyutmp for every combination of Unix-like operating system and operating system release. So, currently, you must build pyutmp from source code, as described below.

First, obtain the source code. You can download the source (as a zip or tarball) from http://github.com/bmc/pyutmp/downloads, or you can make a local read-only clone of the GitHub repository using one of the following commands:

$ git clone git://github.com/bmc/pyutmp.git
$ git clone http://github.com/bmc/pyutmp.git

Once you have a local pyutmp source directory, change your working directory to the source directory, and type:

python setup.py install

To install it somewhere other than the default location (such as in your home directory) type:

python setup.py install --prefix=$HOME

Interface and Usage

The pyutmp module supplies two classes: UtmpFile and Utmp. A UtmpFile object represents the open utmp file; when you iterate over a UtmpFile object, it yields successive Utmp objects. For example:

from pyutmp import UtmpFile
import time

for utmp in UtmpFile():
    # utmp is a Utmp object
    if utmp.ut_user_process:
        print '%s logged in at %s on tty %s' % (utmp.ut_user, time.ctime(utmp.ut_time), utmp.ut_line)


In addition to the __iter__() generator method, allowing iteration over the contents of the utmp file, the UtmpFile class provides a rewind() method that permits you to reset the file pointer to the top of the file. See the class documentation for details.


The fields of the Utmp class are operating system-dependent. However, they will always include at least the following fields:

On some operating systems, other fields may be present. For instance, on Linux and Solaris systems (and other System V-derived systems), Utmp also contains the following fields:

If you’re writing portable code, you should not count on the presence of this secont set of attributes–or, at the very least, you should wrap access to them in a try/catch block that catches AttributeError.


This module has been tested on the following operating systems:

Adding support for other Unix variants should be straightforward.



Brian M. Clapper.

This module is copyright © 2008-2010 Brian M. Clapper and is released under a BSD license.


I gladly accept patches from their original authors. Feel free to email patches to me or to fork the GitHub repository and send me a pull request. Along with any patch you send: