User Information

Every user who can log in on the system is identified by a unique number called the user ID. Each process has an effective user ID which says which user's access permissions it has.

Users are classified into groups for access control purposes. Each process has one or more group ID values which say which groups the process can use for access to files.

The system keeps a database of all the registered users, and another database of all the defined groups. There are library functions you can use to examine these databases.

User ID

The user identifier (UID) has its own type — uid_t — defined in sys/types.h.


#include <sys/types.h>
#include <unistd.h>

uid_t getuid(void);
char *getlogin(void);

The getuid function returns the UID with which the program is associated. This is usually the UID of the user who started the program.

The getlogin function returns the login name associated with the given user.

User Database

The user database contains information about the registered users. The database itself is kept in the file /etc/passwd on most systems, but on some systems a special network server gives access to it.


#include <sys/types.h>
#include <pwd.h>

struct passwd *getpwuid(uid_t uid);
struct passwd *getpwnam(const char *name);

The password database structure, passwd, defined in pw.h includes the following members:

passwd MemberDescription
char *pw_nameThe user's login name.
uid_t pw_uidThe user ID number.
gid_t pw_gidThe user's group ID number
char *pw_gecosThe user's real name
char *pw_dirThe user's home directory
char *pw_shellThe user's default shell.

Group Database

You can search the group database for information about a specific group using getgrgid or getgrnam. These functions are declared in grp.h.


#include <sys/types.h>
#include <grp.h>

struct group *getgrgid(gid_t gid);
struct group *getgrnam(const char *name);

The function getgrgid returns a pointer to a statically-allocated structure containing information about the group whose group ID is gid.

A null pointer indicates there is no group with ID gid.

References

Neil Matthew and Richard Stone, Beginning Linux Programming, Third Edition,
Wrox, 2004. ISBN 0-7645-4497-7. p 155-158.

Users and Groups (from the GNU C Library Reference Manual).


Maintained by John Loomis, last updated 4 September 2006