Linux

Tech Tip: Manage user groups in Linux

Learn about handling user groups in Linux, and find out how to determine which groups you belong to.

On a Linux system, each user has one unique user id (uid). This number uniquely identifies the user on the system.

The same is not true, however, for user groups. Each user has one primary group id (gid), identified in the /etc/groups file. More often than not, a user's primary group is either a group with the same name and number as his or her uid. But on some systems, it could be a global group called "users."

To determine which groups you belong to, use the id command, as shown below:

$ id
uid=501(joe) gid=501(joe) groups=501(joe), 100(users)

In this example, user joe has a uid of 501, and he belongs to both the joe group (501) and the users group (100). His primary gid is the joe group.

When you create new files, Linux uses your uid and gid to assign ownership to the file. In this example, if joe creates a new file, both user joe and group joe would own the file; it would most likely be writable by both and readable by all other users.

But if joe wanted to create a file that was writable by other members of the users group, he would have to change the ownership of the file. You can accomplish this prior to creating the file by changing joe's primary group from joe to users. To do so, use the newgrp command:

$ newgrp users
$ id
uid=501(joe) gid=100(users) groups=501(joe), 100(users)

This sets the users group as joe's primary gid. From this point forward, both user joe and the users group will own everything he creates.

To change joe back to his original group, just enter exit at the command line, and use the id command to verify that you've restored the primary group.

0 comments

Editor's Picks