Python pwd module Explained [Practical Examples]

 

Introduction to Python pwd module

The Python pwd module provides access to the Unix user account and password database. It is available on all Unix versions. However, most modern unices use a so-called shadow password system. In this tutorial, we will learn about the Python pwd module. We will cover different attributes that are most commonly used with the Python pwd module.

We will also cover the three important methods that are available in this module, including pwd.getpwuid(), pwd.getpwnam(), and pwd.pwall() method by taking different examples as well. In a nutshell, this tutorial will contain all the necessary details and methods that are available in the Python pwd module that you need to learn in order to start working on this module.

Advertisement

 

Getting started with the Python pwd module

As we already discussed that the Python pwd module can be used to read user information from the Unix password database. Unix is an operating system analogous to DOS and Windows, supporting multiple concurrent users. Password database entries are reported as a tuple-like object, whose attributes correspond to the members of the passwd structure. The following table shows different attributes and their meanings.

Attribute Meaning
pw_name Login name
pw_passwd Optional encrypted password
pw_uid Numerical user ID
pwd_gid Numerical group ID
pw_gecos User name or comment field
pw_dir User home directory
pw_shell User command interpreter

 

In the following sections, we will discuss the three most important methods that are available in the Python pwd module in detail.

 

Python pwd and getpwuid() method

The first method that we are going to discuss is pwd.getpwuid(). This method is used to get the password database entry for the specified user-id. The following is the simple syntax of the pwd.getpwuid() method.

pwd.getpwuid(uid)

This method takes the UID as an argument. A UID (unique identifier) is an identifier that marks that particular record as unique from every other record. It allows the record to be referenced in the Summon Index without confusion or unintentional overwriting from other records. You can read more about UIDs from the article Python UID. This method returns a tuple-like object of class pwd.struct_passwd which represents the password database entry for the specified user id.

 

Examples of pwd.getpwuid() method

Now let us take examples and see how the pwd.getpwuid() works. Before using this method, first we have to import the pwd module using the import keyword. See the Python program below which used the getpwuid() method.

# importing Python pwd module 
import pwd
# defining UID
UID = 100
# Geting the password database entry for the specified user id using pwd.getpwuid() method
X = pwd.getpwuid(UID)
# Print the retrieved entry
print("Password database entry for user id is :{}".format(UID))
# printing the returned value of getpwuid() method
print(X)

Output:

Advertisement
Password database entry for user id is :100
pwd.struct_passwd(pw_name='systemd-network', pw_passwd='x', pw_uid=100, pw_gid=102, pw_gecos='systemd Network Management,,,', pw_dir='/run/systemd', pw_shell='/usr/sbin/nologin')

Notice that the pwd.getpwuid() method has returned a tuple-like object that contains different information including pw_name, pw_passwd, pw_uid, etc. Now let us confirm the returned type of Python pwd.getpwuid() method. See the following Python program.

# importing Python pwd module 
import pwd
# defining UID
UID = 100
# Geting the password database entry for the specified user id using pwd.getpwuid() method
X = pwd.getpwuid(UID)
# printing the type
print(type(X))

Output:

<class 'pwd.struct_passwd'>

Notice that the actual returned type is pwd.struct_passwd which is tuple-like object.

 

Python pwd and getpwnam() method

The pwd.getpwnam() method returns the password database entry for the given user name. The following is the simple syntax of the getpwnam() method.

pwd.getpwnam(name)

This method takes one parameter which is a string value representing the user name for which password database entry is required. It also returns a tuple-like object of class pwd.struct_passwd which represents the password database entry for the specified name.

 

Examples of getpwnam() method

We already had discussed the getpwnam() method, now let us take an example and see how it is working. See the Python program below:

Advertisement
# importing Python pwd module 
import pwd
# User name 
User_name = "root"
# Getting the password database entry for the specified username using pwd.getpwnam() method
X = pwd.getpwnam(User_name)
# Print the retrieved entry
print("Password database entry for {}".format(User_name))
print(X)

Output;

Password database entry for root
pwd.struct_passwd(pw_name='root', pw_passwd='x', pw_uid=0, pw_gid=0, pw_gecos='root', pw_dir='/root', pw_shell='/bin/bash')

Now let us conirm the type the returned value. See the following python program which prints the type of the returned value of the given method.

# importing Python pwd module 
import pwd
# User name 
User_name = "root"
# Getting the password database entry for the specified username using pwd.getpwnam() method
X = pwd.getpwnam(User_name)
# printing the type 
print(type(X))

Output:

<class 'pwd.struct_passwd'>

Notice that the returned value belongs to pwd.struct_passwd class. If we will provide any name that is not available or is not the root, then this method will return an error. See the example below:

# importing Python pwd module 
import pwd
# User name that is not available
User_name = "bashir"
# Getting the password database entry for the specified username using pwd.getpwnam() method
X = pwd.getpwnam(User_name)
# Print the retrieved entry
print("Password database entry for {}".format(User_name))
print(X)

Output:

Python pwd

Notice that we get an error because the name bashir was not found on my system.

Advertisement

 

Python pwd and getpwall() method

Now let us discuss the pwd.getpwall() method. It returns a list of all available password database entries, in arbitrary order. The following is the simple syntax of the getpwall() method.

pwd.getpwall()

Notice that this method does not require any parameter. The returned type is the same as the above mentioned two methods It also returns a list of tuple-like object of class pwd.struct_passwd whose elements represent all available entries stored in the password database.

 

Examples of getpwall() method

Now let us take an example and see how the getwall() method works. See the python program below.

# importing Python pwd module 
import pwd
# Getting the list  of all available password database entries using pwd.getpwall() method
X = pwd.getpwall()
# Printing
print("Password database entries are:")
# for loop to iterate
for row in X:
    # printings
    print(row)

Output:

Password database entries are:
pwd.struct_passwd(pw_name='root', pw_passwd='x', pw_uid=0, pw_gid=0, pw_gecos='root', pw_dir='/root', pw_shell='/bin/bash')
pwd.struct_passwd(pw_name='daemon', pw_passwd='x', pw_uid=1, pw_gid=1, pw_gecos='daemon', pw_dir='/usr/sbin', pw_shell='/usr/sbin/nologin')
pwd.struct_passwd(pw_name='bin', pw_passwd='x', pw_uid=2, pw_gid=2, pw_gecos='bin', pw_dir='/bin', pw_shell='/usr/sbin/nologin')
pwd.struct_passwd(pw_name='sys', pw_passwd='x', pw_uid=3, pw_gid=3, pw_gecos='sys', pw_dir='/dev', pw_shell='/usr/sbin/nologin')
pwd.struct_passwd(pw_name='sync', pw_passwd='x', pw_uid=4, pw_gid=65534, pw_gecos='sync', pw_dir='/bin', pw_shell='/bin/sync')
pwd.struct_passwd(pw_name='games', pw_passwd='x', pw_uid=5, pw_gid=60, pw_gecos='games', pw_dir='/usr/games', pw_shell='/usr/sbin/nologin')
pwd.struct_passwd(pw_name='man', pw_passwd='x', pw_uid=6, pw_gid=12, pw_gecos='man', pw_dir='/var/cache/man', pw_shell='/usr/sbin/nologin')
pwd.struct_passwd(pw_name='lp', pw_passwd='x', pw_uid=7, pw_gid=7, pw_gecos='lp', pw_dir='/var/spool/lpd', pw_shell='/usr/sbin/nologin')
pwd.struct_passwd(pw_name='mail', pw_passwd='x', pw_uid=8, pw_gid=8, pw_gecos='mail', pw_dir='/var/mail', pw_shell='/usr/sbin/nologin')
pwd.struct_passwd(pw_name='news', pw_passwd='x', pw_uid=9, pw_gid=9, pw_gecos='news', pw_dir='/var/spool/news', pw_shell='/usr/sbin/nologin')
pwd.struct_passwd(pw_name='uucp', pw_passwd='x', pw_uid=10, pw_gid=10, pw_gecos='uucp', pw_dir='/var/spool/uucp', pw_shell='/usr/sbin/nologin')
pwd.struct_passwd(pw_name='proxy', pw_passwd='x', pw_uid=13, pw_gid=13, pw_gecos='proxy', pw_dir='/bin', pw_shell='/usr/sbin/nologin')
pwd.struct_passwd(pw_name='www-data', pw_passwd='x', pw_uid=33, pw_gid=33, pw_gecos='www-data', pw_dir='/var/www', pw_shell='/usr/sbin/nologin')
pwd.struct_passwd(pw_name='backup', pw_passwd='x', pw_uid=34, pw_gid=34, pw_gecos='backup', pw_dir='/var/backups', pw_shell='/usr/sbin/nologin')
pwd.struct_passwd(pw_name='list', pw_passwd='x', pw_uid=38, pw_gid=38, pw_gecos='Mailing List Manager', pw_dir='/var/list', pw_shell='/usr/sbin/nologin')
pwd.struct_passwd(pw_name='irc', pw_passwd='x', pw_uid=39, pw_gid=39, pw_gecos='ircd', pw_dir='/var/run/ircd', pw_shell='/usr/sbin/nologin')
pwd.struct_passwd(pw_name='gnats', pw_passwd='x', pw_uid=41, pw_gid=41, pw_gecos='Gnats Bug-Reporting System (admin)', pw_dir='/var/lib/gnats', pw_shell='/usr/sbin/nologin')
pwd.struct_passwd(pw_name='nobody', pw_passwd='x', pw_uid=65534, pw_gid=65534, pw_gecos='nobody', pw_dir='/nonexistent', pw_shell='/usr/sbin/nologin')
pwd.struct_passwd(pw_name='systemd-network', pw_passwd='x', pw_uid=100, pw_gid=102, pw_gecos='systemd Network Management,,,', pw_dir='/run/systemd', pw_shell='/usr/sbin/nologin')
pwd.struct_passwd(pw_name='systemd-resolve', pw_passwd='x', pw_uid=101, pw_gid=103, pw_gecos='systemd Resolver,,,', pw_dir='/run/systemd', pw_shell='/usr/sbin/nologin')
pwd.struct_passwd(pw_name='systemd-timesync', pw_passwd='x', pw_uid=102, pw_gid=104, pw_gecos='systemd Time Synchronization,,,', pw_dir='/run/systemd', pw_shell='/usr/sbin/nologin')
pwd.struct_passwd(pw_name='messagebus', pw_passwd='x', pw_uid=103, pw_gid=106, pw_gecos='', pw_dir='/nonexistent', pw_shell='/usr/sbin/nologin')
pwd.struct_passwd(pw_name='syslog', pw_passwd='x', pw_uid=104, pw_gid=110, pw_gecos='', pw_dir='/home/syslog', pw_shell='/usr/sbin/nologin')
pwd.struct_passwd(pw_name='_apt', pw_passwd='x', pw_uid=105, pw_gid=65534, pw_gecos='', pw_dir='/nonexistent', pw_shell='/usr/sbin/nologin')
pwd.struct_passwd(pw_name='tss', pw_passwd='x', pw_uid=106, pw_gid=111, pw_gecos='TPM software stack,,,', pw_dir='/var/lib/tpm', pw_shell='/bin/false')
pwd.struct_passwd(pw_name='uuidd', pw_passwd='x', pw_uid=107, pw_gid=114, pw_gecos='', pw_dir='/run/uuidd', pw_shell='/usr/sbin/nologin')
pwd.struct_passwd(pw_name='tcpdump', pw_passwd='x', pw_uid=108, pw_gid=115, pw_gecos='', pw_dir='/nonexistent', pw_shell='/usr/sbin/nologin')
pwd.struct_passwd(pw_name='avahi-autoipd', pw_passwd='x', pw_uid=109, pw_gid=116, pw_gecos='Avahi autoip daemon,,,', pw_dir='/var/lib/avahi-autoipd', pw_shell='/usr/sbin/nologin')
pwd.struct_passwd(pw_name='usbmux', pw_passwd='x', pw_uid=110, pw_gid=46, pw_gecos='usbmux daemon,,,', pw_dir='/var/lib/usbmux', pw_shell='/usr/sbin/nologin')
pwd.struct_passwd(pw_name='rtkit', pw_passwd='x', pw_uid=111, pw_gid=117, pw_gecos='RealtimeKit,,,', pw_dir='/proc', pw_shell='/usr/sbin/nologin')
pwd.struct_passwd(pw_name='dnsmasq', pw_passwd='x', pw_uid=112, pw_gid=65534, pw_gecos='dnsmasq,,,', pw_dir='/var/lib/misc', pw_shell='/usr/sbin/nologin')
pwd.struct_passwd(pw_name='cups-pk-helper', pw_passwd='x', pw_uid=113, pw_gid=120, pw_gecos='user for cups-pk-helper service,,,', pw_dir='/home/cups-pk-helper', pw_shell='/usr/sbin/nologin')
pwd.struct_passwd(pw_name='speech-dispatcher', pw_passwd='x', pw_uid=114, pw_gid=29, pw_gecos='Speech Dispatcher,,,', pw_dir='/run/speech-dispatcher', pw_shell='/bin/false')
pwd.struct_passwd(pw_name='avahi', pw_passwd='x', pw_uid=115, pw_gid=121, pw_gecos='Avahi mDNS daemon,,,', pw_dir='/var/run/avahi-daemon', pw_shell='/usr/sbin/nologin')
pwd.struct_passwd(pw_name='kernoops', pw_passwd='x', pw_uid=116, pw_gid=65534, pw_gecos='Kernel Oops Tracking Daemon,,,', pw_dir='/', pw_shell='/usr/sbin/nologin')
pwd.struct_passwd(pw_name='saned', pw_passwd='x', pw_uid=117, pw_gid=123, pw_gecos='', pw_dir='/var/lib/saned', pw_shell='/usr/sbin/nologin')
pwd.struct_passwd(pw_name='nm-openvpn', pw_passwd='x', pw_uid=118, pw_gid=124, pw_gecos='NetworkManager OpenVPN,,,', pw_dir='/var/lib/openvpn/chroot', pw_shell='/usr/sbin/nologin')
pwd.struct_passwd(pw_name='hplip', pw_passwd='x', pw_uid=119, pw_gid=7, pw_gecos='HPLIP system user,,,', pw_dir='/run/hplip', pw_shell='/bin/false')
pwd.struct_passwd(pw_name='whoopsie', pw_passwd='x', pw_uid=120, pw_gid=125, pw_gecos='', pw_dir='/nonexistent', pw_shell='/bin/false')
pwd.struct_passwd(pw_name='colord', pw_passwd='x', pw_uid=121, pw_gid=126, pw_gecos='colord colour management daemon,,,', pw_dir='/var/lib/colord', pw_shell='/usr/sbin/nologin')
pwd.struct_passwd(pw_name='geoclue', pw_passwd='x', pw_uid=122, pw_gid=127, pw_gecos='', pw_dir='/var/lib/geoclue', pw_shell='/usr/sbin/nologin')
pwd.struct_passwd(pw_name='pulse', pw_passwd='x', pw_uid=123, pw_gid=128, pw_gecos='PulseAudio daemon,,,', pw_dir='/var/run/pulse', pw_shell='/usr/sbin/nologin')
pwd.struct_passwd(pw_name='gnome-initial-setup', pw_passwd='x', pw_uid=124, pw_gid=65534, pw_gecos='', pw_dir='/run/gnome-initial-setup/', pw_shell='/bin/false')
pwd.struct_passwd(pw_name='gdm', pw_passwd='x', pw_uid=125, pw_gid=130, pw_gecos='Gnome Display Manager', pw_dir='/var/lib/gdm3', pw_shell='/bin/false')
pwd.struct_passwd(pw_name='uca', pw_passwd='x', pw_uid=1000, pw_gid=1000, pw_gecos='UCA,,,', pw_dir='/home/uca', pw_shell='/bin/bash')
pwd.struct_passwd(pw_name='systemd-coredump', pw_passwd='x', pw_uid=999, pw_gid=999, pw_gecos='systemd Core Dumper', pw_dir='/', pw_shell='/usr/sbin/nologin')
pwd.struct_passwd(pw_name='mysql', pw_passwd='x', pw_uid=998, pw_gid=1001, pw_gecos='', pw_dir='/home/mysql', pw_shell='/bin/sh')

So this method returns an iterable object that contains all password database entries. Then we have used the for loop to iterate through the iterable and print all the passwords. Now, let us check the returned type of this method. See the Python code below:

# importing Python pwd module 
import pwd
# Getting the list  of all available password database entries using pwd.getpwall() method
X = pwd.getpwall()
# printing the type
print(type(X))

Output:

Advertisement
<class 'list'>

Notice that the return type is a list that is iterable containing all the available password database entries.

 

Summary

The Python pwd module is used to access the password database. Using this module, we can access the user's account and password database. The password database entries are like type objects.

In this tutorial, we learned about Python pwd module. We learned about different attributes that are available in this module. We also discussed the three different methods that are available in this module by taking various examples. To summarize, this tutorial contains, all the necessary details and information that you need to know in order to start working with Python pwd module.

 

Further Reading

Python pwd module documentation
Python pwd.getpwall() method
Python pwd.getpwnam() method

 

Didn't find what you were looking for? Perform a quick search across GoLinuxCloud

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can either use the comments section or contact me form.

Thank You for your support!!

Leave a Comment

X