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
Python3
# 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
Python3
# 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
Python3
# 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')
Similar Reads
Python Module Index
Python has a vast ecosystem of modules and packages. These modules enable developers to perform a wide range of tasks without taking the headache of creating a custom module for them to perform a particular task. Whether we have to perform data analysis, set up a web server, or automate tasks, there
4 min read
Pathlib module in Python
Pathlib module in Python offers classes and methods to quickly perform the most common tasks involving file system paths, incorporating features from several other standard modules like os.path, glob, shutil, and os.Path classes in Pathlib module are divided into pure paths and concrete paths. Pure
6 min read
OS Path module in Python
OS Path module contains some useful functions on pathnames. The path parameters are either strings or bytes. These functions here are used for different purposes such as for merging, normalizing, and retrieving path names in Python. All of these functions accept either only bytes or only string obje
2 min read
Pyscaffold module in Python
Starting off with a Python project is usually quite complex and complicated as it involves setting up and configuring some files. This is where Pyscaffold comes in. It is a tool to set up a new python project, is very easy to use and sets up your project in less than 10 seconds! To use Pyscaffold, G
4 min read
Platform Module in Python
Python defines an in-built module platform that provides system information. The Platform module is used to retrieve as much possible information about the platform on which the program is being currently executed. Now by platform info, it means information about the device, it's OS, node, OS versio
6 min read
Python Fire Module
Python Fire is a library to create CLI applications. It can automatically generate command line Interfaces from any object in python. It is not limited to this, it is a good tool for debugging and development purposes. With the help of Fire, you can turn existing code into CLI. In this article, we w
3 min read
PyDictionary module in Python
AyDictionary It's a Python module used to fetch definitions of words, while googletrans provides translation services.meaningstranslationsInstallation To install AyDictionary run the following pip code on the terminal / command prompt: pip install AyDictionary googletrans==4.0.0-rc1Getting Started N
1 min read
Python Modules
Python Module is a file that contains built-in functions, classes,its and variables. There are many Python modules, each with its specific work.In this article, we will cover all about Python modules, such as How to create our own simple module, Import Python modules, From statements in Python, we c
7 min read
Import module in Python
In Python, modules allow us to organize code into reusable files, making it easy to import and use functions, classes, and variables from other scripts. Importing a module in Python is similar to using #include in C/C++, providing access to pre-written code and built-in libraries. Pythonâs import st
3 min read
Python Math Module
Math Module consists of mathematical functions and constants. It is a built-in module made for mathematical tasks. The math module provides the math functions to deal with basic operations such as addition(+), subtraction(-), multiplication(*), division(/), and advanced operations like trigonometric
13 min read