343

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

Braiam
  • 66,947
  • 30
  • 177
  • 264
a06e
  • 12,613
  • 25
  • 66
  • 102

5 Answers5

445

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
    
guntbert
  • 12,914
  • 37
  • 45
  • 86
jobin
  • 27,142
  • 16
  • 100
  • 116
  • 9
    How about GID ? – kangear Oct 29 '15 at 01:11
  • 26
    @kangear id -g – itsazzad Dec 18 '15 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 Dec 19 '18 at 20:33
  • The `username` is optional, defaulting to yourself. Maybe square brackets would be better for indicating this, instead of angle brackets. – mwfearnley May 13 '19 at 15:44
  • 2
    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*). – 0xC0000022L Dec 16 '20 at 15:39
105

Simply try

id

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

TAq
  • 1,201
  • 1
  • 7
  • 4
17

Try also :

getent passwd username

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

Or:

grep username /etc/passwd
nux
  • 37,371
  • 34
  • 117
  • 131
10

You can use id command.

Manpage

Diego Lopez
  • 199
  • 4
2

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:

$ id -u
1000
$ id -g
1000

and:

$ 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
  • 8,025
  • 7
  • 66
  • 105