Post Thumbnail

id – print Unix user identity

id command is one of the basic unix commands, and it servers a very simple purpose of confirming the identity of a specified Unix user.

Simplest way to use the id command

All you do is just type id in your command line prompt, and it then gets back to you with confirmations of your own user id, group id, and a list of other groups you’re a member of:

{% highlight command %} ubuntu$ id uid=1000(greys) gid=113(admin) groups=33(www-data),113(admin) {% endhighlight %}

Identity of a Unix user

Just as easily, you can specify a username to find out similar information. In this example, we’re confirming the identity of the privileged uset, root:

{% highlight command %} ubuntu$ id root uid=0(root) gid=0(root) groups=0(root) {% endhighlight %}

Creative ways of using the id command

id command has a few command line options which are particularly useful in shell scripting. While the default use (see above) gives you all the information, you can use options to get only the part of user identity which interests you.

Unix user primary (effective) group

Using -g parameter, you can confirm the primary Unix group of any user. Used alone, this option returns you a Unix group id:

{% highlight command %} ubuntu$ id -g greys 113 {% endhighlight %}

But if you use -g with -n option, you can see not the group id, but its name:

{% highlight command %} ubuntu$ id -gn greys admin {% endhighlight %}

All the unix groups a user belongs to

In a very similar way, you can use -G parameter to show all the groups a given user belongs to. You can either get the group ids, or get id to report group names:

{% highlight command %} ubuntu$ id -G greys 113 33 ubuntu$ id -Gn greys admin www-data {% endhighlight %}

Unix user ID (effective userid)

If you’re just after the user id of a given user, you need to use the -u command line option:

{% highlight command %} ubuntu$ id -u greys 1000 {% endhighlight %}

See also