Managing user and groups, UID and GID in Linux


What are users in the operating system?

A user is a person who accesses or utilizes computer resources or network services. In order to gain access to a computer system, user are required to log-in to an operating system. Each and every file or folder is owned by a particular user.

There are three types of users in the Linux operating system root or superuser, regular user, and system user.

·     Root user (superuser): When the Linux is installed on computer the root account is created by default you just need to set a password to enable the root account which is also known as superuser. The root user is the most privileged user who can access all commands, files, and all services.

  

·      Regular user: A regular user has only the necessary privileges to perform standard tasks such as creating folders, etc. They can store files in their own home directory and have to work only in their home directory. Since the regular user does not have root user privileges they cannot delete or modify system configuration files or other critical files.

 

·      System user: System users are the software or application user created automatically by the system to run services such as mail, apache, ftp, etc have their own individual system account. It is also known as a service user. These accounts exist to allow each of these services to interact with the computer 

 

 Important files

·     /etc/passwd: This file contains information about users. It is a text-based file. The passwd file is stored under “etc” directory.

·   /etc/shadow: This file contain the actual password in an encrypted format. This shadow file is also stored under “etc” directory.

The above two files can be view by regular file viewer, cat, less, more or tail command, etc.

Ø  To know more about the above two files click here.

 

Managing local user

1.    useradd command: This command is used to create a new user account. This command does not set any valid password and the user cannot log-in to the account until the password is set.

From the above example, you can see the user Ronak is successfully created but we did not get any notification that the user Ronak is created. So if you want to check whether the user Ronak is created or not just check “/etc/passwd” file and the example is given below.

2.    Passwd command: The newly created user account has no password. This command is used to add a password to the new user account and this same command is used to change the password of an existing user account.

The password is successfully given to Ronak. But you can see “New password and Retype new password” is seems empty this feature is by default in the Linux operating system. You just have to type your password as per your requirement it will not appear but it will take your new password.   

3.     usermod command: To modify an existing user account usermod command is used.

From the above example you can see –u option is used for changing UID.  By using the “usermod” command the UID is successfully changed from 1003 to 1005 for Ronak.

4.     userdel command: The userdel command is used to delete an existing user account and it related files

From the above example, you can see userdel command is used with option –r stands for recursively and –f is for forcefully and this both options can be used together like –rf. 

Note: Regular user or normal user cannot execute the above commands and if you try to execute with the regular user it will give you permission denied error. All the above commands can execute by the root user or sudo user only. 

 

What is the groups?

The main purpose of the group is to set a privilege like read, write, or execute permission to the files or resources that can be shared among the users within the group. In a single group, there can be multiple users. Basically groups are a collection of users. In Linux every user has a UID (user identifier) and a GID (Group identifier).

There are two types of groups:

Primary groups

Normally, when a new user is created a new group is also created automatically while creating a new user. A new group has the name and GID (group ID) same as the user name and UID (user ID). A new user becomes a member of its own group which becomes a primary group for the user. The newly created user is the only member of this (UPG) user private group.

·         Every user has one primary group.

·         For the local user, the primary group is defined by the GID number of the group listed in the fourth field of “/etc/passwd”.

Supplementary groups

A supplementary group is an additional group the user can become a member. For example, a newly created user john automatically becomes a part of the group name john but if john wants to access files owned by the group name rocky in that case john has to be a member of the group rocky to access those files. So john becomes a member of the group name rocky which is a supplementary group.

·         The User may be a member of zero or more supplementary groups.

·         The user’s that are members of the supplementary groups are listed in the last field of the group’s entry in “/etc/group”.  The user who is members of the groups is determined by a comma-separated list of users found in the group’s entry in “/etc/group” file.

Important file

·      /etc/group: This file is a text file that contains the group information for each user account.

The above file can be view by regular file viewer, cat, less, more or tail command, etc.

Ø  To know more about the above file click here.

 

Managing local group

1.    groupadd command: This command is used to create groups and by writing it without option the next available GID (group identifier) from the range specified in the “/etc/login.defs” file.

By using “groupadd” command new group is created with the name hockey and the new group hockey is created with GID 1003. The fourth field is empty because no user is added to hockey group.

2.    groupmod command: This command is used to change an existing group name and GID. The –n option is used to specify a new name to an existing group. The option –g is used to specify your own GID number to the existing group.

By using “groupmod” command you can see that the GID of the group hockey is been changed from 1003 to 8000.

3.    groupdel command: The groupdel command is used to remove the existing group.

From the above example by using “groupdel” command the group hockey is successfully deleted.

Note: usermod alters group membership. You can change the user’s primary group with “usermod –g groupname” command. To add an existing user to a supplementary group “usermod –aG groupname username” command is used.

 

UID and GID:

UID: A UID (user identifier) is a unique number that Linux operating system assigned to each user. Which is used to identify the user on the system and to determine which system resources the user can access.

There are some specific ranges of UID are used for a specific purpose are given below.

·         The UID 0 is only assigned to the superuser account, root.

·         The UID 1 – 200 is a range of “system users” assigned statically to system processes.

·         The UID range 201 – 999 is a range of “system users” used by system processes that do not own files on the system.

·         The range of UID 1000+ is available to assign to the regular users and the maximum range is 60,000 for assign to regular users.

 

GID: A GID (group identifier) is a unique number to identify each group.

There are some specific ranges of GID are used for a specific purpose are given below.

·         The GID 0 is only assigned to the superuser account, root.

·         The GID 1 – 200 is a range of “system users” assigned statically to system processes just like UID.

·         The GID range 201 – 999 is a range same as UID used for “system user” used by system processes that do not own files on the system.

·         The range of GID 1000+ is available to assign to the regular users and the maximum range is 60,000 for assign to regular users.

Note: Default ranges used by useradd and groupadd can be changed in the “/etc/login.defs” file.


Understanding /etc/group



/etc/group

This file contains the group names with GID (group identifier) and defines which user belongs to which group.  It is a simple text file stored under “etc” directory.  The “/etc/group” file has a similar role for groups as the “/etc/passwd” file has for users. This file contains one entry per line and each entry has four fields that are separated by a colon (:).

The entry in this file looks like given below:

Groupname: password: GID:list of users 

Field

Description

Groupname

The name of the group.

Password

The encrypted password of the group represented by x.

GID

This field contains the group identification number.

List of users

It contains the names of users that are members.

 

To access the “/etc/group” file you can use commands like cat, tail, head, etc.


From the above image “hockey” is a group name, “x” is the encrypted password, 1004 is a GID, and user2, john, and user1 are the members of the group hockey.

 The command “cat” is short of “concatenate” it allows us to view file content, redirect output in terminal or file, create single or multiple files, and concatenate files.

The “pipe” is a vertical bar (|) on the command line between two commands. The pipe basically takes the output from one command and use it as input for another command.

The “grep” stands for (globally search for a regular expression and print out) is used to filter search through a pattern of characters, and displays all lines related to the pattern.

 

 


Understanding /etc/passwd and /etc/shadow files:


/etc/passwd:

This file contains information about users. It is a text-based file with seven fields separated by a colon (:). This file in stored under “etc” directory and to view this file we can use regular file viewer, cat, less, more or tail command, etc.  

From the above image, the tail command is used along with –n option followed by 1 which means only one line from the bottom of this file.

Basically tail command is used to view a specific number of lines as per need. Here –n option stands for a number.

Note: The “/etc/passwd” file can be accessed by a normal user there is no need to gain root access. 

username:password:UID:GID:GECOS:/home/user:default shell

Breaking down of the user entry from left to right:

 

1.  Username is a mapping of a UID which uniquely identifies the user on the computer system.

2.  Password is where passwords were kept in an encrypted format but display only “X”. The full encrypted password is stored in a separate file called “/etc/shadow”.

3.   UID is a user ID, a number that identifies the user at the most fundamental level.

4.   GID is a user’s primary group ID number.

5.  GECOS field usually includes the user’s full name, or address, or phone number, or any basic information, and this field is also known as the comment field.

6.   /home/user is the location of a user home directory, personal data, and configuration files.

7.  Shell is a program that runs as the user logs in. For a regular user, this is normally the program that provides the user’s command interpreter.

 

/etc/shadow:

This file stores the actual password in encrypted format using a hashing algorithm and also holds information about password aging. It is a text-based file with nine fields separated by a colon (:). 

Note: Root access is required to view “/etc/shadow” file. If you try to view this file as a normal user the shell will throw an error message given below.

Output after taking root access.


Breaking down the entry from left to right:

 

1. Login name: The login name of the user which is store on the system.

2. Encrypted password: A password field starts with an exclamation ($) mark which means that the password is locked.

3. Last password change: This field shows the date of the last password changes, represented as the number of days since 1970/01/01.

4. Minimum: The minimum number of days before a password may be changed, where 0 means “no minimum age requirement”.

5. Maximum: This field shows the maximum number of days before a password must be changed.

6. Warning period: The warning period that a password is about to expire. Represented in days, where 0 means “no warning given”.

7. Inactive period: The number of days a user account remains active after a password has expired. A user may still log into the system and change the password during this period. After the specified number of days, the account is locked and becoming inactive.

8. Expiration date: This field shows the expiration date of the user account represented as the number of days since 1970/01/01.

9. Blank: This field is blank. It is reserved for future use.   


Featured Post

Managing user and groups, UID and GID in Linux

What are users in the operating system? A user is a person who accesses or utilizes computer resources or network services. In order to ...

Popular Posts