The title says it all. What command I need to run from a terminal to find my user ID (UID)?
Asked
Active
Viewed 1.0M times
406
5 Answers
534
There are a couple of ways:
Using the id command you can get the real and effective user and group IDs.
id -u <username>If no username is supplied to
id, it will default to the current user.Using the shell variable. (It is not an environment variable, and thus is not available in
env).echo $UID
-
10
-
28
-
1It's worth noting that, due to the fact that the variables are resolved before being passed to a command, we have that
sudo echo ${UID}prints out1000(or whatever your sudoer user's UID is), whereassudo id -uprints out0. – adentinger Dec 19 '18 at 20:33 -
The
usernameis optional, defaulting to yourself. Maybe square brackets would be better for indicating this, instead of angle brackets. – mwfearnley May 13 '19 at 15:44 -
5The second part of this answer is wrong. The variable in question is explicitly not an environment variable. It's a shell variable. Big difference. You can see this with
echo $UIDversusenv|grep ^UIDin Bash, for example. This means in particular that the first method is more robust and the second will only work in shell scripts, not - say - in something like Python (python -c 'import os; print(os.environ)'to see the environment). – 0xC0000022L Dec 16 '20 at 15:39 -
1@0xC0000022L That seems to be the case indeed. I get 1000 from
echo $UIDand an empty line from$ printenv UID. – Daniel C Dec 05 '23 at 11:22
21
Try also:
getent passwd $USER
This will display user id, group id and home directory.
Or:
grep $USER /etc/passwd
-
why to try long or alternative command while
echo $UIDandid -uis simple and exact according to question? – Pandya May 17 '14 at 13:37 -
8
-
1@Pandya: It's a good answer because e.g. on a custom embedded linux you might not have the other options. This answer was very useful to me. – DrP3pp3r May 29 '24 at 06:46
-
Note that the last command does not always work (user is not always defined in
/etc/passwd), for example when using a network/domain login. – wovano Apr 16 '25 at 08:14
13
Get the User ID (UID) and Group ID (GID) for the running user
id -u # user ID (UID)
id -g # group ID (GID)
Example run and output for the active user (myself):
$ id -u
1000
$ id -g
1000
and for the root user (via sudo):
$ sudo id -u
[sudo] password for gabriel:
0
$ sudo id -g
0
Note that the first user is generally 1000 for both the UID and GID, and the root user is generally 0 for both the UID and GID.
Gabriel Staples
- 11,722
echo $UID? – Louis Matthijssen May 17 '14 at 13:17