How does Linux identify users?

If you force there to exist multiple users with the same username, then there will be multiple entries in /etc/{shadow,passwd} with the same name:

$ cat /etc/passwd
...
a:x:1001:1002::/home/a:/bin/bash
a:x:1002:1003::/home/b:/bin/bash

# cat /etc/shadow
a:...:17702:0:99999:7:::
a:...:17702:0:99999:7:::

If you try to log in as that user, you'll log in as the first match.

$ ssh a@<host>
Password:
$ id
uid=1001(a) gid=1002(a) groups=1002(a)
$ pwd
/home/a

There will be no way to log in as the second user with the same name.

Note that Linux tracks users by their uid, not by their username.

It would be possible, however, to have two different usernames be the same user ID. Consider a different version of /etc/passwd:

$ cat /etc/passwd
...
a:x:1001:1002::/home/a:/bin/bash
b:x:1001:1002::/home/b:/bin/bash

Note that for both usernames a and b, the third column is 1001 -- that's the uid / user ID. Now, if user a or user b logs in (even with different passwords), they'll both be "user 1001", and show as user a from the OS' perspective. Here too, the first matching entry is the one returned (in most cases):

$ ssh a@host
Password: <a's password>
$ id
uid=1001(a) gid=1002(a) groups=1002(a)

$ ssh b@host
Password: <b's password>
$ id
uid=1001(a) gid=1002(a) groups=1002(a)

Both a and b are uid 1001 and will have access to the resources available to uid 1001.


In Unix, users are identified by their ID (uid), which must be unique (in the scope of the local system). So even if it were possible to create 2 different users with the same name (adduser on my system refuses to do this, see this question for further information Can separate unix accounts share a username but have separate passwords?), they would need to get different uids. While you may be able to manipulate files containing the user information to match your criteria, every program is based on the assumption that uids are unique on the system, so such users would be identical.

EDIT: The other answer demonstrated a case where you have 2 different user names for the same uid - as far as the system is concerned though, this is like having two different names for the same user, so constructs like this should be avoided if possible, unless you specifically want to create an alias for a user on the system (see the unix user alias question on serverfault for more information on the technicalities).

The system uses these uids to enforce file permissions. The uid and gid (group id) of the user the file belongs to are written into the metadata of the file. If you carry the disk to another computer with a different user that randomly shares the same uid, the file will suddenly belong to this user on that system. Knowing that uids are usually not more than 16-bit integers on a unix system, this shows that the uids are not meant to be globally unique, only unique in scope of the local system.


I was considering to rename my home /home/old-arch before reinstalling the system. I wondered if the new system would give me the old permissions on my files or if it would recognize me as a different arch.

If you have a single-user system and do a reinstall with the same, or a similar distribution, it's very likely that your user account will have the same user id, and hence be the same user from the kernel's perspective. For example, the user created during installation has been UID 1000 on Debian systems as far as I can remember. Other systems might use some other number, but it's very likely to be some small-ish integer that's the same on every install.

The same applies to other users too (if you have any) since the UIDs are usually allocated sequentially. The third user created is likely to have the same UID as the third user created on another system. You'd need to take steps beforehand to make sure that the UIDs aren't being reused on both systems.

For similar reasons, anything using NFS will need to have a shared user database.

But in this case, since it's your personal system you can just login as root and run chown newuser. -R /home/olduser even if the UID were to be different.

(Windows systems are different, they generate that longish ID string that's more random. There, if you move a disk to another machine, the files will be seen owned by an unknown user, and you won't have access without using administrator powers.

Also, I said "likely" a lot in the above. There's no telling if some distribution behaves differently. Modern Linux also supports 32-bit UIDs, so while that's not nearly as long as Windows SIDs, there's still some space to use if one wants to have e.g. randomized UIDs. Usually, there's not much use for that, though. The system administrator is supposed to know what disks they attach to the system and to adjust the file ownerships accordingly, or make the mountpoint inaccessible to other users.)