Open In App

pwd module in Python

Last Updated : 02 Aug, 2019
Improve
Improve
Like Article
Like
Save
Share
Report

pwd module in Python provides access to the Unix user account and password database. Each entry stored in Unix user account and password database is reported as a tuple-like object whose attributes are similar as the members of passwd structure defined in <pwd.h> header file.

Following are the attributes of the tuple-like object which represents the entries stored in Unix user account and password database:

Index Attributes Meaning
0 pw_name Login name
1 pw_passwd Optional encrypted password
2 pw_uid Numerical user ID
3 pwd_gid Numerical group ID
4 pw_gecos User name or comment field
5 pw_dir User home directory
6 pw_shell User command interpreter

Note: pwd module is a UNIX specific service. So, all method of this module is available on UNIX versions only.

pwd module defines following three methods:

  • pwd.getpwuid() method
  • pwd.getpwnam() method
  • pwd.getpwall() method

pwd.getpwuid() method -

pwd.getpwnam() method in Python is used to get the password database entry for the specified user id.

Syntax: pwd.getpwuid(uid)

Parameter:
uid: A numeric value representing the user id for which password database entry is required.

Return type: This method returns a tuple-like object of class ‘pwd.struct_passwd’ which represents the password database entry for the specified user id.

Code: Use of pwd.getpwuid() method




# Python program to explain pwd.getpwuid() method
    
# importing pwd module 
import pwd
  
# User id
uid = 1000
  
# Get the password 
# database entry for the
# specified user id
# using pwd.getpwuid() method
entry = pwd.getpwuid(uid)
  
# Print the retrieved entry
print("Password database entry for user id : % d:" % uid)
print(entry)
  
# User id
uid = 0
  
# Get the password 
# database entry for the
# specified user id
# using pwd.getpwuid() method
entry = pwd.getpwuid(uid)
  
# Print the retrieved entry
print("Password database entry for user id : % d:" % uid)
print(entry)


Output:

Password database entry for user id : 1000
pwd.struct_passwd(pw_name='ihritik', pw_passwd='x', pw_uid=1000, pw_gid=1000,
pw_gecos='Hritik,,, ', pw_dir='/home/ihritik', pw_shell='/bin/bash')

Password database entry for user id : 0
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.getpwnam() method -

pwd.getpwnam() method in Python is used to get the password database entry for the specified user name

Syntax: pwd.getpwnam(name)

Parameter:
name: A string value representing the user name for which password database entry is required.

Return type: This method returns a tuple-like object of class ‘pwd.struct_passwd’ which represents the password database entry for the specified name.

Code: Use of pwd.getpwnam() method




# Python program to explain pwd.getpwnam() method
    
# importing pwd module 
import pwd
  
# User name
name = "ihritik"
  
# Get the password 
# database entry for the
# specified username
# using pwd.getpwnam() method
entry = pwd.getpwnam(name)
  
# Print the retrieved entry
print("Password database entry for '% s':" % name)
print(entry)
  
  
# User name
name = "root"
  
# Get the password 
# database entry for the
# specified username
# using pwd.getpwnam() method
entry = pwd.getpwnam(name)
  
# Print the retrieved entry
print("\nPassword database entry for '% s':" % name)
print(entry)


Output:

Password database entry for 'ihritik':
pwd.struct_passwd(pw_name='ihritik', pw_passwd='x', pw_uid=1000, pw_gid=1000,
pw_gecos='Hritik,,, ', pw_dir='/home/ihritik', pw_shell='/bin/bash')

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')

pwd.getpwall() method -

pwd.getpwall() method in Python is used to get the list of all available entries stored in password database.

Syntax: pwd.getpwall()

Parameter: No parameter is required.

Return type: This method returns a list of tuple-like object of class ‘pwd.struct_passwd’ whose elements represent all available entries stored in password database.

Code: Use of pwd.getpwall() method




# Python program to explain pwd.getpwall() method
    
# importing pwd module 
import pwd
  
# Get the list 
# of all available password
# database entries using
# pwd.getpwall() method
entries = pwd.getpwall()
  
  
  
# Print the list 
print("Password database entries:")
for row in entries:
    print(row)


Output:

Password database entries:
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-timesync’, pw_passwd=’x’, pw_uid=100, pw_gid=102, pw_gecos=’systemd Time Synchronization,,, ‘, pw_dir=’/run/systemd’, pw_shell=’/bin/false’)
pwd.struct_passwd(pw_name=’systemd-network’, pw_passwd=’x’, pw_uid=101, pw_gid=103, pw_gecos=’systemd Network Management,,, ‘, pw_dir=’/run/systemd/netif’, pw_shell=’/bin/false’)
pwd.struct_passwd(pw_name=’systemd-resolve’, pw_passwd=’x’, pw_uid=102, pw_gid=104, pw_gecos=’systemd Resolver,,, ‘, pw_dir=’/run/systemd/resolve’, pw_shell=’/bin/false’)
pwd.struct_passwd(pw_name=’systemd-bus-proxy’, pw_passwd=’x’, pw_uid=103, pw_gid=105, pw_gecos=’systemd Bus Proxy,,, ‘, pw_dir=’/run/systemd’, pw_shell=’/bin/false’)
pwd.struct_passwd(pw_name=’syslog’, pw_passwd=’x’, pw_uid=104, pw_gid=108, pw_gecos=”, pw_dir=’/home/syslog’, pw_shell=’/bin/false’)
pwd.struct_passwd(pw_name=’messagebus’, pw_passwd=’x’, pw_uid=105, pw_gid=109, pw_gecos=”, pw_dir=’/var/run/dbus’, pw_shell=’/bin/false’)
pwd.struct_passwd(pw_name=’_apt’, pw_passwd=’x’, pw_uid=106, pw_gid=65534, pw_gecos=”, pw_dir=’/nonexistent’, pw_shell=’/bin/false’)
pwd.struct_passwd(pw_name=’uuidd’, pw_passwd=’x’, pw_uid=107, pw_gid=113, pw_gecos=”, pw_dir=’/run/uuidd’, pw_shell=’/bin/false’)
pwd.struct_passwd(pw_name=’rtkit’, pw_passwd=’x’, pw_uid=108, pw_gid=114, pw_gecos=’RealtimeKit,,, ‘, pw_dir=’/proc’, pw_shell=’/bin/false’)
pwd.struct_passwd(pw_name=’avahi-autoipd’, pw_passwd=’x’, pw_uid=109, pw_gid=115, pw_gecos=’Avahi autoip daemon,,, ‘, pw_dir=’/var/lib/avahi-autoipd’, pw_shell=’/bin/false’)
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=’/bin/false’)
pwd.struct_passwd(pw_name=’dnsmasq’, pw_passwd=’x’, pw_uid=111, pw_gid=65534, pw_gecos=’dnsmasq,,, ‘, pw_dir=’/var/lib/misc’, pw_shell=’/bin/false’)
pwd.struct_passwd(pw_name=’whoopsie’, pw_passwd=’x’, pw_uid=112, pw_gid=119, pw_gecos=”, pw_dir=’/nonexistent’, pw_shell=’/bin/false’)
pwd.struct_passwd(pw_name=’kernoops’, pw_passwd=’x’, pw_uid=113, pw_gid=65534, pw_gecos=’Kernel Oops Tracking Daemon,,, ‘, pw_dir=’/’, pw_shell=’/bin/false’)
pwd.struct_passwd(pw_name=’speech-dispatcher’, pw_passwd=’x’, pw_uid=114, pw_gid=29, pw_gecos=’Speech Dispatcher,,, ‘, pw_dir=’/var/run/speech-dispatcher’, pw_shell=’/bin/false’)
pwd.struct_passwd(pw_name=’avahi’, pw_passwd=’x’, pw_uid=115, pw_gid=120, pw_gecos=’Avahi mDNS daemon,,, ‘, pw_dir=’/var/run/avahi-daemon’, pw_shell=’/bin/false’)
pwd.struct_passwd(pw_name=’saned’, pw_passwd=’x’, pw_uid=116, pw_gid=122, pw_gecos=”, pw_dir=’/var/lib/saned’, pw_shell=’/bin/false’)
pwd.struct_passwd(pw_name=’pulse’, pw_passwd=’x’, pw_uid=117, pw_gid=123, pw_gecos=’PulseAudio daemon,,, ‘, pw_dir=’/var/run/pulse’, pw_shell=’/bin/false’)
pwd.struct_passwd(pw_name=’colord’, pw_passwd=’x’, pw_uid=118, pw_gid=125, pw_gecos=’colord colour management daemon,,, ‘, pw_dir=’/var/lib/colord’, pw_shell=’/bin/false’)
pwd.struct_passwd(pw_name=’hplip’, pw_passwd=’x’, pw_uid=119, pw_gid=7, pw_gecos=’HPLIP system user,,, ‘, pw_dir=’/var/run/hplip’, pw_shell=’/bin/false’)
pwd.struct_passwd(pw_name=’geoclue’, pw_passwd=’x’, pw_uid=120, pw_gid=126, pw_gecos=”, pw_dir=’/var/lib/geoclue’, pw_shell=’/bin/false’)
pwd.struct_passwd(pw_name=’gdm’, pw_passwd=’x’, pw_uid=121, pw_gid=127, pw_gecos=’Gnome Display Manager’, pw_dir=’/var/lib/gdm3′, pw_shell=’/bin/false’)
pwd.struct_passwd(pw_name=’ihritik’, pw_passwd=’x’, pw_uid=1000, pw_gid=1000, pw_gecos=’Hritik,,, ‘, pw_dir=’/home/ihritik’, pw_shell=’/bin/bash’)
pwd.struct_passwd(pw_name=’sshd’, pw_passwd=’x’, pw_uid=122, pw_gid=65534, pw_gecos=”, pw_dir=’/run/sshd’, pw_shell=’/usr/sbin/nologin’)
pwd.struct_passwd(pw_name=’master’, pw_passwd=’x’, pw_uid=1001, pw_gid=1002, pw_gecos=’,,, ‘, pw_dir=’/home/master’, pw_shell=’/bin/bash’)



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads