12

I have to change a file/directory's ownership using chmod. I know only the owner's username but want to change the default group name simultaneously. At this stage the group name is not decided so I am putting same as the username:

chown -R username:username path_to_dir

Command may run for various systems/users. So my concern is, will Linux always have a pair username(user):username(group) OR at some point this default group_name may give error.

Although I have started to find GID from the user belongs

id username | tr '(=)' ':' | awk -F: '{print $3}

But for knowledge I want to know. Till now analysis of /etc/passwd /etc/groups except few users like "shutdown,halt,sync,operator" all users have listed in groups as well.

EDIT 1: id -gn username is also good option

3
  • 3
    In the second example, were you looking for id -g username? May 14, 2014 at 16:48
  • 1
    Or id -gn username which prints the name instead of the ID. May 15, 2014 at 0:14
  • In Linux, there are also lots of "device-groups" - eg. disk, mem, kmem, audio, video, etc. - which don't have corresponding user. May 15, 2014 at 12:58

4 Answers 4

20

There is no guarantee that the groupname = username will exist.

The most common scenario is that system administrators use on Linux is creating a new user locally on the system is without an explicit specification for the group, which means that the group will be created by default same as the user name and assign the user to have the default GID to be of the newly created group.

This doesn't mean that another system administrator will follow this procedure and if you throw other naming services into the mix like NIS or LDAP the group assigned to be default will likely not be same as the new GID with groupname same as username.

So you shouldn't rely on the fact that username=groupname

1
  • It may also depend not only on local admin policy, but on the tools included with various distributions. Many sites will just accept the defaults used by the tools provided. While most of the distributions I've used over the years ship with a useradd script that by default creates a group with the same name, not all have. Last time I used it (many, many years ago), slackware's user creation script defaulted to having a single group users which all users were made a member of instead. docs.slackware.com/slackbook:users suggests this may still be the case.
    – Jules
    May 15, 2014 at 5:12
12

Although the user private group idiom is often used in modern Linux systems, *nix does not actually require a unique primary group for each user, and it's quite possible for several user accounts to share a common primary group such as users or staff.

Even in cases where the UPG idiom is enforced, there is no particular reason that the numeric GID of a particular UPG should be equal to the owner's UID, although most account-creation tools will try to assign UIDs and GIDs in order, if they are available. The IDs can easily get out of step if the users and/or groups need to be made consistent across multiple systems (for example to accommodate pre-existing NFS shares).

6

As Ramesh has indicated by default the UID and the GID are equal for ordinary users. However this is not always the case so you must not make such an assumption as this may wreak havoc to your system. Assigning file/folder permissions to a wrong group is somewhat messy, isn't it?

So, the short answer: no, UID is not always equal to GID. Still, /etc/passwd contains both the UID and GID of the default group on the same line so it's rather easy to extract them.

2
  • "by default the UID and the GID are equal for ordinary users" -- actually, this statement is only true if you add "on some popular Linux distributions". Yes, Ubuntu et al. do it this way, but I would consider a common "users" group more of a default.
    – jstarek
    May 15, 2014 at 9:43
  • Yes, if we aim to be 100% precise then you are right. We would probably need to add a dozen more various 'ifs' to never miss a single exception. All of my Debian-based distros (LMDE, Crunchbang, pure Debian) behave this way and one CentOS 6.5 I have as well. Mind you that since there is no need for me to add huge amounts of users I tend to use adduser rather than useradd and the defaults in /etc/adduser.conf are set to USERGROUPS=yes which creates a group for each new user. These groups typically (not always, though) have the same GID as the UID of the user for whom they were created.
    – Erathiel
    May 15, 2014 at 10:49
2

From this link, I see the below information.

A user ID (UID) is a unique positive integer assigned by a Unix-like operating system to each user. Each user is identified to the system by its UID, and user names are generally used only as an interface for humans.

UIDs are stored, along with their corresponding user names and other user-specific information, in the /etc/passwd file, which can be read with the cat command as follows:

cat /etc/passwd

The third field contains the UID, and the fourth field contains the group ID (GID), which by default is equal to the UID for all ordinary users.

EDIT

However, as @Karlson points out it is not true that the UID will be equal to GID by default.

The statement is true only if no ordinary groups are added separately and no groups are explicitly specified on any user creation. By default gid for the next added user = max(gid) + 1. There is no correlation between UID and GID required by any *NIX system they just happen to coincide in some cases.

More references

http://www.thegeekstuff.com/2009/06/useradd-adduser-newuser-how-to-create-linux-users

10
  • The last statement is completely false. UID=GID only in the case when the new user is created on the local system and GID isn't explicitly assigned to an existing group.
    – Karlson
    May 14, 2014 at 14:54
  • No, it's not completely false, as it clearly says "by default". Some clarification would be much appreciated here.
    – Erathiel
    May 14, 2014 at 14:56
  • 1
    @Erathiel By default GID is equal to GID assigned at user creation. If user is created locally and without specifying GID in -g flag on useradd the program will create a group for you and unless a GID shift has happened it will = UID. However, if the creation method is not known you cannot rely on the fact that this will be the case: uid=123456(ME) gid=777(ABC) groups=777(ABC)
    – Karlson
    May 14, 2014 at 15:03
  • On some systems useradd also creates a group of the same name for the account. Depends on the flavour of Linux.
    – Pete
    May 14, 2014 at 15:06
  • @Karlson, I never mentioned it will always be equal. It is clearly specified that by default it is equal to the UID for all ordinary users.
    – Ramesh
    May 14, 2014 at 15:13

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .