367

The title says it all. What command I need to run from a terminal to find my user ID (UID)?

1

5 Answers 5

478

There are a couple of ways:

  1. 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.

  2. Using the shell variable. (It is not an environment variable, and thus is not available in env).

     echo $UID
    
6
  • 9
    How about GID ?
    – kangear
    Commented Oct 29, 2015 at 1:11
  • 27
    @kangear id -g <username>
    – itsazzad
    Commented Dec 18, 2015 at 15:23
  • It'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 out 1000 (or whatever your sudoer user's UID is), whereas sudo id -u prints out 0.
    – adentinger
    Commented Dec 19, 2018 at 20:33
  • 5
    The 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 $UID versus env|grep ^UID in 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). Commented Dec 16, 2020 at 15:39
  • 1
    @0xC0000022L That seems to be the case indeed. I get 1000 from echo $UID and an empty line from $ printenv UID.
    – Daniel
    Commented Dec 5, 2023 at 11:22
112

Simply try

id

This will return your user ID, group ID, and all your groups.

3
  • 16
    or id -u to see just the UID Commented May 17, 2014 at 13:21
  • Yepp. Prefer Jobin's answer, if you need to avoid parsing the output.
    – TAq
    Commented May 17, 2014 at 13:25
  • 6
    so much easier with id -u and id -g. Thanks. :)
    – thoroc
    Commented Aug 3, 2017 at 17:00
21

Try also :

getent passwd username

This will display user id , group id and home directory .

Or:

grep username /etc/passwd
4
  • why to try long or alternative command while echo $UID and id -u is simple and exact according to question?
    – Pandya
    Commented May 17, 2014 at 13:37
  • 8
    thats right , but its good to know all options
    – nux
    Commented May 17, 2014 at 13:38
  • 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
    Commented May 29 at 6:46
  • @DrP3pp3r Ok. I just casted my upvote to this answer :)
    – Pandya
    Commented 19 hours ago
10

You can use id command.

Manpage

6

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.

You must log in to answer this question.

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