- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Accessing The Unix/Linux password database (pwd)
The pwd module in standard library of Python provides access to the password database of user accounts in a Unix/Linux operating system. Entries in this Password database are atored as a tuple-like object. The structure of tuple is according to following passwd structure pwd.h file in CPython API
Index | Attribute | Meaning |
---|---|---|
0 | pw_name | Login name |
1 | pw_passwd | Optional encrypted password |
2 | pw_uid | Numerical user ID |
3 | pw_gid | Numerical group ID |
4 | pw_gecos | User name or comment field |
5 | pw_dir | User home directory |
6 | pw_shell | User command interpreter |
The pwd module defines the following functions −
>>> import pwd >>> dir(pwd) ['__doc__', '__loader__', '__name__', '__package__', '__spec__', 'getpwall', 'getpwnam', 'getpwuid', 'struct_passwd']
getpwnam() − This function returns record in the password database corresponding to the specified user name
>>> pwd.getpwnam('root') pwd.struct_passwd(pw_name s= 'root', pw_passwd = 'x', pw_uid = 0, pw_gid = 0, pw_gecos = 'root', pw_dir = '/root', pw_shell = '/bin/bash')
getpwuid() − This function returns record in the password database corresponding to given UID
>>> pwd.getpwuid(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')
getpwall() − This function returns a list of tuples. Each tuple contains passwd structure information of each user. The uid and gid items in the structure are integers. If an entry corresponding to the passed parameter cannot be found, KeyError exception is raised.
>>> pwd.getpwnam('hello') Traceback (most recent call last): File "<stdin>", line 1, in <module> KeyError: 'getpwnam(): name not found: hello'
- Related Articles
- Difference between Linux and Unix
- Reset Kali Linux Password
- Access to the Password Database in Python
- Init process on UNIX and Linux systems
- Perl Installation on Unix and Linux Platform
- Access to the Shadow Password Database in Python
- What are the differences between Unix and Linux Operating System?
- Installing MySQL on Unix/Linux Using Generic Binaries
- How MySQL prevents unauthorized clients from accessing the database system?
- Password blacklist table in SAP HANA database
- Downgrading Binary and Package-based Installations on Unix/Linux
- Using TCP/IP ports for accessing SAP HANA database
- How to Create a File in the Linux/Unix system using terminal?
- What are Named Pipes or FIFO in Linux/Unix systems?
- Upgrading MySQL Binary or Package-based Installations on Unix/Linux
