LinuxLinux users and groups management

Users and Groups in Linux/Unix: Managing Access and Privileges for Multiple Users

Linux/Unix operating systems have the ability to multitask in a manner similar to other operating systems. However, Linux’s major difference from other operating systems is its ability to have multiple users. Linux was designed to allow more than one user to have access to the system at the same time.

Users and Groups

Linux/Unix operating systems have the ability to multitask in a manner similar to other operating systems. However, Linux’s major difference from other operating systems is its ability to have multiple users. Linux was designed to allow more than one user to have access to the system at the same time.

A user can be either a human being or an application account, identified by a unique numerical User ID (UID).

A group is an organizational unit tying users together for a common purpose, such as shared file permissions. Each group is associated with a Group ID (GID).

Permission Model

System Configuration Files

Linux stores user and group information in several key configuration files:

/etc/passwd - User Account Information

Stores user account information required during login. Readable by all utilities but writable only by root.

Format: username:password:UID:GID:GECOS:home_directory:shell

Example: ganapathi:x:501:501:Ganapathi Chidambaram:/home/ganapathi:/bin/bash

path
usernamestring
Required

Login username (1-32 characters). Used for authentication and identification.

path
passwordstring
Required

x indicates encrypted password stored in /etc/shadow. * means locked account, !! means no password set.

path
uidinteger
Required

User ID number. UID 0 = root, 1-99 = system accounts, 100-999 = admin accounts, 1000+ = regular users.

path
gidinteger
Required

Primary Group ID number for the user.

path
gecosstring

Comment field for user info (full name, phone, etc.). Used by finger command.

path
home_directorypath
Required

Absolute path to user's home directory. Defaults to / if non-existent.

path
shellpath
Required

Login shell executed on user login. Default: /bin/bash. Empty = /bin/sh. Non-existent = login disabled.

/etc/shadow - Encrypted Passwords

Stores encrypted password hashes and password aging information. Readable only by root.

Format: username:password:last_changed:min:max:warn:inactive:expire

This file contains sensitive password data. Protect it carefully and never share its contents.

path
usernamestring
Required

Login username matching /etc/passwd entry.

path
passwordstring
Required

13-24 character encrypted password hash. ! = locked, * = disabled, !! = never set.

path
last_changedinteger
Required

Days since epoch (Jan 1, 1970) when password was last changed.

path
mininteger

Minimum days before password can be changed. 0 = anytime.

path
maxinteger

Maximum days password is valid. -1 = no expiration.

path
warninteger

Days before expiration to warn user.

path
inactiveinteger

Days after expiration before account is disabled.

path
expireinteger

Absolute expiration date (days since epoch). Account disabled after this date.

/etc/group - Group Definitions

Defines groups to which users belong. Groups allow delegated access to resources like disks, printers, and peripherals.

Format: group_name:password:GID:member_list

Example: ganapathi:x:24:ganapathi,raja

path
group_namestring
Required

Group name displayed by commands like ls -l.

path
passwordstring

Usually empty (x). Encrypted group password for privileged groups.

path
gidinteger
Required

Group ID number used by OS for access control.

path
memberslist

Comma-separated list of group member usernames.

/etc/gshadow - Group Shadow Passwords

Stores encrypted group passwords and group administrator information. Readable only by root.

Format: group_name:password:admins:members

path
group_namestring
Required

Group name identifier.

path
passwordstring

Encrypted group password. ! = no access via newgrp, !! = never set, null = members only.

path
adminslist

Comma-separated list of group administrators who can add/remove members via gpasswd.

path
memberslist

Comma-separated list of regular group members.

/etc/login.defs

Under Linux password related utilities and config file(s) comes from shadow password suite.It defines the site-specific configuration for this suite.The lines consist of a configuration name and value, separated by whitespace.Absence of this file will not prevent system operation, but will probably result in undesirable operation.

Blank lines and comment lines are ignored. Comments are introduced with a “#” pound sign and the pound sign must be the first non-white character of the line.

Parameter values may be of four types: strings, booleans, numbers, and long numbers. A string is comprised of any printable characters. A boolean should be either the value “yes” or “no”. An undefined boolean parameter or one with a value other than these will be given a “no” value. Numbers (both regular and long) may be either decimal values, octal values (precede the value with “0”) or hexadecimal values (precede the value with “0x”). The maximum value of the regular and long numeric parameters is machine-dependent.

The following configuration items are provided:

  • CHFN_AUTH (boolean) : If yes, the chfn and chsh programs will require authentication before making any changes, unless run by the superuser.

  • CHFN_RESTRICT (string) : This parameter specifies which values in the gecos field of the /etc/passwd file may be changed by regular users using the chfn program. It can be any combination of letters f ,r, w, h, for Full name, Room number, Work phone, and Home phone, respectively. For backward compatibility, “yes” is equivalent to “rwh” and “no” is equivalent to “frwh”. If not specified, only the superuser can make any changes. The most restrictive setting is better achieved by not installing chfn SUID.

  • DEFAULT_HOME : Should login be allowed if we can't cd to the home directory?. Default in no.So change it as Yes for cd to the home directory.

  • ENCRYPT_METHOD : Encryption method for the password entered by the user.More complicated algorithm is used to difficult to brute forcing the password.

  • ENV_PATH & ENV_SUPATH - Default Environment path for normal user and super user login.This is must be defined to successful login of user into the system.

  • FAILLOG_ENABLE : Enable logging and display of /var/log/faillog login failure info.

  • LOGIN_RETRIES : Max number of login retries if password is bad. This will most likely be overriden by PAM, since the default pam_unix module has it's own built in of 3 retries. However, this is a safe fallback in case you are using an authentication module that does not enforce PAM_MAXTRIES.

  • LOGIN_TIMEOUT : Max time in seconds for login.

  • GID_MAX (number), GID_MIN (number) : Range of group IDs to choose from for the useradd and groupadd programs.

  • MAIL_DIR (string) : The mail spool directory. This is needed to manipulate the mailbox when its corresponding user account is modified or deleted. If not specified, a compile-time default is used.

  • PASS_MAX_DAYS (number) : The maximum number of days a password may be used. If the password is older than this, a password change will be forced. If not specified, -1 will be assumed (which disables the restriction).

  • PASS_MIN_DAYS (number) : The minimum number of days allowed between password changes. Any password changes attempted sooner than this will be rejected. If not specified, -1 will be assumed (which disables the restriction).

  • PASS_WARN_AGE (number) : The number of days warning given before a password expires. A zero means warning is given only upon the day of expiration, a negative value means no warning is given. If not specified, no warning will be provided.

  • UID_MAX (number), UID_MIN (number) : Range of user IDs to choose from for the useradd program.

  • UMASK (number) : The permission mask is initialized to this value. If not specified, the permission mask will be initialized to 022.

  • USERDEL_CMD (string) : If defined, this command is run when removing a user. It should remove any at/cron/print jobs etc. owned by the user to be removed (passed as the first argument).

The following cross reference shows which programs in the shadow password suite use which parameters.

  • chfn CHFN_AUTH CHFN_RESTRICT
  • chsh CHFN_AUTH
  • groupadd GID_MAX GID_MIN
  • newusers PASS_MAX_DAYS PASS_MIN_DAYS PASS_WARN_AGE UMASK
  • pwconv PASS_MAX_DAYS PASS_MIN_DAYS PASS_WARN_AGE
  • useradd GID_MAX GID_MIN PASS_MAX_DAYS PASS_MIN_DAYS PASS_WARN_AGE UID_MAX UID_MIN UMASK
  • userdel MAIL_DIR USERDEL_CMD
  • usermod MAIL_DIR

Adding a New User

User Creation Flow

Create user with useradd

Basic user creation with default settings.

useradd username

Creates user with default home directory, shell, and a group with the same name.

Create user with adduser (interactive)

Interactive user creation with prompts for additional information.

adduser username

This will prompt for:

  • Password
  • Full name
  • Room number
  • Work phone
  • Home phone

Verify user creation

Confirm the user was created successfully.

id username
grep username /etc/passwd
ls -ld /home/username

What Happens During User Creation

Common useradd Options

adduser Options

  • --conf FILE

    Use FILE instead of /etc/adduser.conf.

  • --disabled-login

    Do not run passwd to set the password. The user won't be able to use her account until the password is set.

  • --disabled-password

    Like --disabled-login, but logins are still possible (for example using SSH RSA keys) but not using password authentication.

  • --force-badname

    By default, user and group names are checked against the configurable regular expression NAME_REGEX (or NAME_REGEX_SYSTEM if --system is specified) specified in the configuration file. This option forces adduser and addgroup to apply only a weak check for validity of the name.

  • --gecos GECOS

    Set the gecos field for the new entry generated. adduser will not ask for finger information if this option is given.

  • --gid ID

    When creating a group, this option forces the new groupid to be the given number. When creating a user, this option will put the user in that group.

  • --group

    When combined with --system, a group with the same name and ID as the system user is created. If not combined with --system, a group with the given name is created. This is the default action if the program is invoked as addgroup.

  • --home DIR

    Use DIR as the user's home directory, rather than the default specified by the configuration file. If the directory does not exist, it is created and skeleton files are copied.

Adding a Group

To add a group to the system, use the command groupadd/addgroup and syntax as follows :

    groupadd <group-name>
    addgroup <group-name>

groupadd Options

  • -g <gid>

    Group ID for the group, which must be unique and greater than 499

  • -r

    Create a system group with a GID less than 500

  • -f

    When used with -g <gid> and <gid> already exists, groupadd will choose another unique <gid> for the group.

addgroup Options

  • -g <gid>

    Group ID for the group, which must be unique and greater than 499

User Modification

By usermod command we can modify the existing available users on the system with help of below configuration.

  • -a, --append

    Add the user to the supplementary group(s). Use only with the -G option.

  • -c, --comment COMMENT

    The new value of the user's password file comment field. It is normally modified using the chfn(1) utility.

  • -d, --home HOME_DIR

    The user's new login directory.

    If the -m option is given, the contents of the current home directory will be moved to the new home directory, which is created if it does not already exist.

  • -e, --expiredate EXPIRE_DATE

    The date on which the user account will be disabled. The date is specified in the format YYYY-MM-DD.

    An empty EXPIRE_DATE argument will disable the expiration of the account.

    This option requires a /etc/shadow file. A /etc/shadow entry will be created if there were none.

  • -f, --inactive INACTIVE

    The number of days after a password expires until the account is permanently disabled.

    A value of 0 disables the account as soon as the password has expired, and a value of -1 disables the feature.

    This option requires a /etc/shadow file. A /etc/shadow entry will be created if there were none.

  • -g, --gid GROUP

    The group name or number of the user's new initial login group. The group must exist.

    Any file from the user's home directory owned by the previous primary group of the user will be owned by this new group.

    The group ownership of files outside of the user's home directory must be fixed manually.

  • -G, --groups GROUP1[,GROUP2,...[,GROUPN]]]

    A list of supplementary groups which the user is also a member of. Each group is separated from the next by a comma, with no intervening whitespace. The groups are subject to the same restrictions as the group given with the -g option.

    If the user is currently a member of a group which is not listed, the user will be removed from the group. This behaviour can be changed via the -a option, which appends the user to the current supplementary group list.

  • -l, --login NEW_LOGIN

    The name of the user will be changed from LOGIN to NEW_LOGIN. Nothing else is changed. In particular, the user's home directory or mail spool should probably be renamed manually to reflect the new login name.

  • -L, --lock

    Lock a user's password. This puts a '!' in front of the encrypted password, effectively disabling the password. You can't use this option with -p or -U.

    !!! note if you wish to lock the account (not only access with a password), you should also set the EXPIRE_DATE to 1.

  • -m, --move-home

    Move the content of the user's home directory to the new location.

    This option is only valid in combination with the -d (or --home) option.

    usermod will try to adapt the ownership of the files and to copy the modes, ACL and extended attributes, but manual changes might be needed afterwards.

  • -o, --non-unique

    When used with the -u option, this option allows to change the user ID to a non-unique value.

  • -p, --password PASSWORD

    The encrypted password, as returned by crypt(3).

    !!! note This option is not recommended because the password (or encrypted password) will be visible by users listing the processes.

    The password will be written in the local /etc/passwd or /etc/shadow file. This might differ from the password database configured in your PAM configuration.

Password Aging

For security reasons, it is advisable to require users to change their passwords periodically.To configure password expiration for a user from a shell prompt, use the chage command, followed by an option which is mentioned below ,followed by the username of the user.

  • -m<days>

    Specifies the minimum number of days between which the user must change passwords. If the value is 0, the password does not expire.

  • -M<days>

    Specifies the maximum number of days for which the password is valid. When the number of days specified by this option plus the number of days specified with the -d option is less than the current day, the user must change passwords before using the account.

  • -d<days>

    Specifies the number of days since January 1, 1970 the password was changed

  • -I<days>

    Specifies the number of inactive days after the password expiration before locking the account. If the value is 0, the account is not locked after the password expires.

  • -E<date>

    Specifies the date on which the account is locked, in the format YYYY-MM-DD. Instead of the date, the number of days since January 1, 1970 can also be used.

  • -W<days>

    Specifies the number of days before the password expiration date to warn the user.