Linux: Users & Groups

Linux Users and Groups

Linux users and groups are essential concepts to understand for anyone who wants to use Linux effectively. Users are individuals who can access and use Linux systems. Groups are collections of users who share common privileges.

Viewing users

You can view the list of users on your Linux system using the following command:

sudo cat /etc/passwd

This will print a list of all users on the system, along with their user IDs (UIDs) and group IDs (GIDs).

Adding users

To add a new user to your Linux system, you can use the following command:

sudo useradd -m username

This will create a new user account with the specified username and create a home directory for the user.

Adding groups

To add a new group to your Linux system, you can use the following command:

sudo addgroup groupname

This will create a new group with the specified name.

Checking groups

To view the list of groups on your Linux system, you can use the following command:

sudo cat /etc/group

This will print a list of all groups on the system, along with their group IDs (GIDs).

Adding users to groups

To add a user to a group, you can use the following command:

sudo gpasswd -a username groupname

This will add the specified user to the specified group.

Changing ownership of files and directories

To change the ownership of a file or directory to a group, you can use the following command:

sudo chgrp groupname filename

This will change the group ownership of the specified file or directory to the specified group.

Example

The following example shows how to add a new user named newuser to the Linux system and add them to the group developers:

sudo useradd -m newuser
sudo gpasswd -a newuser developers

The following example shows how to change the ownership of the file /etc/hosts to the group developers:

sudo chgrp developers /etc/hosts

Linux users and groups are essential concepts to understand for anyone who wants to use Linux effectively. By understanding how users and groups work, you can manage your system and its resources more effectively.