Skip to content

Python:pwd

The password database

About

이 모듈은 유닉스 사용자 계정과 암호 데이터베이스에 대한 액세스를 제공합니다. 모든 유닉스 버전에서 사용할 수 있습니다.

passwd

암호 데이터베이스 항목은 passwd 구조체(아래의 어트리뷰트 필드, <pwd.h>를 보세요)의 멤버에 해당하는 어트리뷰트를 가진 튜플류 객체로 보고됩니다:

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

uid 및 gid 항목은 정수이고, 다른 모든 항목은 문자열입니다. 요청된 항목을 찾을 수 없으면 KeyError가 발생합니다.

Example

프로세스 사용자 ID 변경시 os.setuid(uid)를 사용하는데 UID를 찾고싶을 때 다음과 같이 사용한다.

def drop_privileges():
    from certidude import config
    import pwd
    _, _, uid, gid, gecos, root, shell = pwd.getpwnam("certidude")
    restricted_groups = []
    restricted_groups.append(gid)

    # PAM needs access to /etc/shadow
    if config.AUTHENTICATION_BACKENDS == {"pam"}:
        import grp
        name, passwd, num, mem = grp.getgrnam("shadow")
        click.echo("Adding current user to shadow group due to PAM authentication backend")
        restricted_groups.append(num)

    os.setgroups(restricted_groups)
    os.setgid(gid)
    os.setuid(uid)
    click.echo("Switched %s (pid=%d) to user %s (uid=%d, gid=%d); member of groups %s" %
        (getproctitle(), os.getpid(), "certidude", os.getuid(), os.getgid(), ", ".join([str(j) for j in os.getgroups()])))
    os.umask(0o007)

See also

Favorite site