Получить идентификатор пользователя linux

How to Use the id Command in Linux: 5 Useful Examples

Every user in Linux has a unique, numeric user ID and a default group with a unique numeric group ID. The id command prints this information.

The id command in Linux is used for displaying the real and effective user ID and group ID of a user.

In this tutorial, I’ll show you how to use id command in Linux with some of the most common and useful examples.

id command in Linux

This is the syntax for the id command:

If you don’t provide a username, the command displays the details about the currently logged-in user.

Here’s the output it displayed for me in Ubuntu Linux.

[email protected]:~$ id uid=1000(abhishek) gid=1000(abhishek) groups=1000(abhishek),4(adm),24(cdrom),27(sudo),30(dip),46(plugdev),119(lpadmin),130(lxd),131(sambashare)

In the above output, user abhishek has uid 1000 and gid 1000. That’s the primary group the user abhishek belongs to by default.

Apart from that, the user abhishek is also member of certain other groups and those groups have also been displayed in the output.

Examples of id command

Here are the most common options for the id command:

Option Description
-u Print the effective user id
-g Print the effective group id
-G Print the IDs of all groups user belongs to
-n Print names instead of IDs (must be combined with -u, -g or -G)
-r Print real ID instead of effective IDs (must be combined with -u, -g or -G)

Real vs Effective user and group ID?

This could be confusing for you. When a user is created, it is given a username, a user ID (uid), a default group and the id of that default group is the gid for that user. This is the ‘real’ user and group ID.

Since in Linux, processes can be run as other user and group, there is also a second set of IDs called effective IDs.

Most of the time the real and effective UIDs and GIDs are the same. But there are situations when a regular user has to modify a privileged file. This is where the effective ID concept is used. Most common example is the using passwd command to change the password which modifies the /etc/passwd file owned by root.

Читайте также:  Linux create new partition

I cannot go in detail here but I recommend reading about it here. You should also read about SUID, GUID and sticky bit permissions in Linux.

1. Print the UID and GID of a certain user

To print the uid and gid and all the other group IDs of a user, you just have to specify the username:

You can list all the users in your Linux system to get the desired user name.

2. Print only the UID of a user

You can use the -u option to print the UID in Linux. As mentioned previously, if you omit the username, it displays the information about the logged-in user.

Keep in mind that it displays only the UID, the numeric value, not the name.

3. Print only the GID of a user

Similarly, you can use the option -g to print the GID of a user. When I say GID, it’s the numeric ID of the default group the user belongs to.

Again, it will only display the numeric ID of the group.

4. Print the IDs of all other groups the user belongs to

A user can belong to several groups. This is basic feature of the Linux filesystem. You can use the usermod command and add it to sudo group to give it root privileges.

You can use the -G option to display the IDs of all the groups the user belongs to.

This too displays the ID only.

[email protected]:~$ id -G abhishek 1000 4 24 27 30 46 119 130 131

5. Print names instead of the numeric IDs

The numeric IDs only can be confusing. If you want the names, you can combine the -n option with -u , -g or -G to display the names.

For example, if you want to display all the groups a user belongs to, you can use it like this:

If I use the previous example with option -n here’s what it shows:

[email protected]:~$ id -nG abhishek abhishek adm cdrom sudo dip plugdev lpadmin lxd sambashare

Bonus Tip: Print the real IDs

All the above examples display the effective IDs. Almost all the time it will be the real ID.

However, if you want to make sure to get the real ID, you can combine option -r with -u , -g or -G .

Alright! I think you know enough about the id command now. If you have questions or suggestions, feel free to leave a comment.

Источник

How Do I Find My User ID in Linux

Linux operating system has made everything very simple. It doesn’t stick the user with one approach as a single function can be performed in multiple ways. Whether you want to go with the built-in commands or prefer to install any tool, the choice is yours!

Similarly, in this guide, we will see many ways to find the user id through different commands. But the question is, why would you need to get an identity as in most of the cases, the admin is the only user.

It is necessary to find your ID when you give access to other people. In this case, a single pc has multiple accounts from which the users logged in. Another case might occur if the user is connected remotely on a server and needs a quick response to get details of the logged-in username.

Читайте также:  Unable to locate package linux image extra

How to Find User ID in Linux

In Linux distributions, we have multiple commands that help to find the user ID very quickly:

id Command

The id command is the simplest way to display a list of real and effective users, and it also shows the group IDs.

You can either simply type the “id” in the terminal or use the username with the “id” command as mentioned below:

getent Command

The “getent” command in UNIX and Linux-like systems is used to fetch the entries from a system database supported by NSS (Name Service Switch) libraries. The user details have been stored in the passwd and group databases.

To get the user’s details through the getent command, follow the syntax:

So, if I want to show the details of user ID, the command would be:

lslogins Command

The “lslogins” command utility in Linux must grab all the available details from the system files about specific users only.

The “-u” flag in the “lslogins” command is used to show user accounts:

w Command

The “w” command helps to display the current logged in users and the actions they performed in a system:

who Command

The “who” command utility is used to print the username currently logged in and the terminal details with date and time. You can either simply type the “who” or use it with the flag “-u” in the terminal:

whoami Command

To display the only username of the current logged in account, type:

grep Command

The “grep” command is considered a powerful Linux tool to search for a particular pattern. It helps to search data (text or strings) from the given and display it. To display the user details from the/etc/passwd file, follow the command:

users and echo $USER Command

The “users” command is used to display a list of all users logged in to the system, whereas the “echo $USER” is an alternate of the command “whoami.” It displays a one-line answer.

last Command

Using the last command, the user can print the list of the last logged in users to the operating system:

finger Command

If you want to install the command-line tool to search for the user’s information, nothing is best from the “finger” command.

The “finger” command-line utility in Linux is used to print user information in the terminal. It is not a built-in tool in most Linux distributions. So, to get it, install it first:

Now, to show the necessary information about the user that includes the user’s real name, directory information, shell, etc., type:

Conclusion

In this write-up, we have learned several ways to find the user identity on the Linux system. The Linux system introduced many command-line utilities to display the user ID. Also, if you want to install the tool to get information, we have mentioned the “finger” command-line tool to print all necessary details of the user.

Читайте также:  Linux лучшие базы данных

About the author

Syeda Wardah Batool

I am a Software Engineer Graduate and Self Motivated Linux writer. I also love to read latest Linux books. Moreover, in my free time, i love to read books on Personal development.

Источник

5 Ways to find a Linux User ID (UID) in Ubuntu 20.04

Find UID

The User ID or UID in Linux is a unique entity through which a user is identified on a system. Every user on a Linux system has a dedicated UID. There are several ways of finding the UID of a Linux user and we are going to share with you all those ways for an Ubuntu or Linux Mint system.

5 Ways of Finding the UID in Ubuntu Linux

There are five main methods for finding the UID in Linux Mint 20 which are as follows:

Method # 1: Using the “id” Command

For using the “id” command to find the UID of the currently logged in user in Ubuntu, you have to execute it in the following manner:

Ubuntu ID command

The UID of our currently logged in user is highlighted in the image shown below:

Result of the id command in Linux

Method # 2: Using the “id” Command with Username

The “id” command can also be paired up with the username of your desired user to get that user’s UID in the following manner:

Replace username with the name of the user whose UID you want to find out.

Find other users UID

The UID of our specified user is shown in the image below:

UID of another Linux User

Method # 3: Using the “getent” Command

To use the “getent” command for finding the UID in Linux Mint 20, you will have to execute it in the following manner:

Replace username with the name of the user whose UID you want to find out.

Linux getent command

The UID of our specified user is shown in the image below:

Find UID using getent

Method # 4: Using the “lslogins” Utility

For using the “lslogins” utility to find the UID in Linux Mint 20, you will have to execute the following command:

Linux lslogins command

This utility will present you with a list of all of your system users along with their respective UIDs as shown in the image below:

Using lslogins

Method # 5: Using the “grep” Command

The “grep” command can also be used for finding the UID of the specified user in Ubuntu in the following manner:

Replace username with the name of the user whose UID you want to find out.

Find UID using grep command

The UID of our specified user is shown in the image below:

User details from passwd file

Conclusion

By picking out any method of your choice from this tutorial, you will be able to find the UID of any user you want while using Ubuntu 20.04. All the commands and utilities that we have used for this tutorial are built-in. Therefore, you will not have to waste your precious time in installing anything while following this tutorial.

About This Site

Vitux.com aims to become a Linux compendium with lots of unique and up to date tutorials.

Latest Tutorials

Источник

Оцените статью
Adblock
detector