Linux get uid by name

Everything Important You Need to Know About UID in Linux

This Linux Basics guide teaches you everything important associated with UID in Linux.

What is UID in Linux?

UID stands for user identifier. A UID is a number assigned to each Linux user. It is the user’s representation in the Linux kernel. The UID is used for identifying the user within the system and for determining which system resources the user can access. This is why the user ID should be unique.

You can find UID stored in the /etc/passwd file. This is the same file that can be used to list all the users in a Linux system.

Use a Linux command to view text file and you’ll see various information about the users present on your system.

root:x:0:0:root:/root:/bin/bash daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin bin:x:2:2:bin:/bin:/usr/sbin/nologin sys:x:3:3:sys:/dev:/usr/sbin/nologin sync:x:4:65534:sync:/bin:/bin/sync games:x:5:60:games:/usr/games:/usr/sbin/nologin man:x:6:12:man:/var/cache/man:/usr/sbin/nologin lp:x:7:7:lp:/var/spool/lpd:/usr/sbin/nologin mail:x:8:8:mail:/var/mail:/usr/sbin/nologin news:x:9:9:news:/var/spool/news:/usr/sbin/nologin johndoe:x:1000:1000:John Doe. /home/helder:/bin/bash davmail:x:127:65534::/var/lib/davmail:/usr/sbin/nologin statd:x:128:65534::/var/lib/nfs:/usr/sbin/nologin

The third field here represents the user ID or UID.

Uid Gid Etc Passwd File Linux

Do note that in most Linux distributions, UID 1-500 are usually reserved for system users. In Ubuntu and Fedora, UID for new users start from 1000.

For example, if you use adduser or useradd command to create a new user, it will get the next available number after 1000 as its UID.

How to find the UID of a user in Linux?

You can always rely on the /etc/passwd file to get the UID of a user. That’s not the only way to get the UID information in Linux.

The id command in Linux will display the UID, GID and groups your current user belongs to:

id uid=1000(abhishek) gid=1000(abhishek) groups=1000(abhishek),4(adm),24(cdrom),27(sudo),30(dip),46(plugdev),116(lpadmin),126(sambashare),127(kvm)

You can also specify the user names with the id command to get the UID of any Linux user:

id standard uid=1001(standard) gid=1001(standard) groups=1001(standard)

How to change UID of a user in Linux?

Suppose you had several users on your Linux system. You had to delete a user because he/she left the organization. Now you want its UID to be taken by another user already on the system.

You can change the UID by modifying the user using usermod command like this:

You need to have superuser privilege to execute the above command.

Do you remember the file permission and ownership concept in Linux? The ownership of a file is determined by the UID of the owner user.

When you update the UID of a user, what happens to the files owned by this user?While all the files in the home directory of user_2 will have their associated UID changed, you’ll have to manually update the associated UID of other files outside the home directory.

Читайте также:  Windows linux subsystem windows server

What you can do is manually update the ownership of the files associated with the old UID of the user_2.

find / -user old_uid_of_user_2 -exec chown -h user_2 <> \;

How does UID associate with different system resources? [for advanced users]

UIDs are unique to one another and thus they can also be used to identify ownership of different system resources such as files and processes.

UID and files

I hope you are familiar with the file permission concept in Linux. When you’re creating a file, you’re the owner of this file. Now you can decide who gets to do what with this file. This is part of Linux’s DAC mechanism where each file is left at its owner’s discretion.

You can read a file’s ownership by using either ls or stat command. Let’s do it with the popular ls command and check the ownership of either the binary sleep or passwd .

As you can see, the file /usr/bin/sleep belongs to root:

ls -l $(which sleep) -rwxr-xr-x 1 root root 39048 Mar 6 2020 /usr/bin/sleep

Let’s force it to map the ownership with UID instead of username:

ls -lhn $(which sleep) -rwxr-xr-x 1 0 0 39K Mar 6 2020 /usr/bin/sleep

Here’s fun information. Your operating system doesn’t understand «usernames». Whenever a program needs to work with usernames or needs to print such, it refers to the /etc/passwd file to extract the information.

You don’t have to take my words for it. See it yourself with strace program which prints all the system calls made by a program.

strace ls -lhn $(which sleep) 2>&1 | grep passwd 

What you are trying to see is whether ls command is trying to read the /etc/passwd file or not.

strace ls -lh $(which sleep) 2>&1 | grep passwd openat(AT_FDCWD, "/etc/passwd", O_RDONLY|O_CLOEXEC) = 6

UID and processes

Processes have owners too, just like files. Only the owner (or the root user) of a process can send process signals to it. This is where the UID comes into play.

If a normal user tries to kill a process owned by another user, it will result in error:

kill 3708 bash: kill: (3708) - Operation not permitted

Only the owner of the process or the root can do this.

A process must be regulated. Regulated as in you need to have a way to limit or know how much a process is allowed to do. This is determined by its UID(s).

There are three types of UIDs associated with a process.

  1. Real UID: Real UID is the UID that a process adopts from its parent. In easier terms, whoever starts a process, the UID of that user is the real UID of the process. This is helpful in identifying who a process really belongs to. This is essential especially when the effective UID is not the same as the real UID which I’m going to talk about next.
  2. Effective UID: This is what mostly determines what permissions a certain process really has. While a user can start the process, it can run with a different user’s available permissions. The command passwd is one example of this. This program edits the file /etc/shadow , which is root owned. Therefore, a normal user shouldn’t be able to run this command or change his/her password. Luckily, the binary runs with an effective UID of 0 (i.e. root), which enables it to have enough privilege to edit the /etc/shadow file. Real and effective UIDs are mostly the same except in the case of SUID bit enabled binaries.
  3. Saved UID: UID that’s available at a process’s disposal. This one is not normally used, but is still there in case the process knows it’s not going to perform any privileged work, so it can change its effective UID to something that’s unprivileged. This reduces the surface of an unintentional misbehavior.
Читайте также:  Настройка 3proxy для linux

That’s it. I hope you have a better idea about UID in Linux now. Don’t hesitate to ask your questions, if any.

As a pro Linux user, if you think I missed some important concept about UID, please let me know in the comment section.

Источник

How to get UID and PID

So here’s what I want to do: User inputs a USERNAME. Based on this username, I need to get the list of processes started by this user. I am planning to do this by getting the UID of this user and listing all the processes with this UID. I only found UID in the /proc/$PID/status file. I am unclear about how do I proceed with this.

I want to get the UID of the user. Once UID is available, I need to fetch all the PIDs that are under this UID (Basically, get all the processes started by this user). pgrep lists all the PIDs of a particular user, but I need the UID too.

1 Answer 1

To get the UID from the username, use id -u :

$ id -u root 0 $ id -u lightdm 112 $ id -u nobody 65534 

But you are re-inventing the wheel. pgrep already handles this just fine:

$ pgrep -u www-data 1909 1910 1911 1912 $ id -u www-data 33 $ pgrep -u 33 1909 1910 1911 1912 

You can also use plain ps :

$ ps -U www-data -o uid,pid UID PID 33 1909 33 1910 33 1911 33 1912 

You must log in to answer this question.

Linked

Hot Network Questions

Subscribe to RSS

To subscribe to this RSS feed, copy and paste this URL into your RSS reader.

Site design / logo © 2023 Stack Exchange Inc; user contributions licensed under CC BY-SA . rev 2023.7.13.43531

Linux is a registered trademark of Linus Torvalds. UNIX is a registered trademark of The Open Group.
This site is not affiliated with Linus Torvalds or The Open Group in any way.

By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.

Источник

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:

Читайте также:  Route delete примеры linux

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

Источник

Get User ID in Ubuntu

Sagar Sharma

User ID in Ubuntu Command Line

User ID (UID), is nothing but a numeric value defined by your system to identify the users.

And if your system is running with multiple users, UID can be handy when you want to manage them.

The easiest way to get the UID use the id command:

use id command to get UID in ubuntu

But that’s not it. There are multiple ways of getting the UID of the user so let’s have a look at them one by one.

How to find user ID in Ubuntu

You can’t expect Linux to have one solution and that goes the same with this case.

So let’s get started with the most convenient one.

Use the lslogins utility to find the user ID in Ubuntu

As I mentioned earlier, I find the lslogins utility the best when it comes to finding UID as it does not clutter the terminal window and gets me the relevant information only.

The key advantage of using lslogins is you get the UID of every user just by using the -u option:

use the lslogins to get user ID in ubuntu

Use the getent utility to find the user ID in Ubuntu

You can get the list of the users by reading the contents of /etc/passwd and the getent utility does exactly that.

To find the UID using the getent command, all you’d need to do is follow the given command syntax:

For example, If I want to know the UID of the user named abhiman , I will be using the following command:

use getent utility to get the UID of user in ubuntu

Seems confusing right? Here’s the explanation:

meaning of /etc/passwd file

Use the id utility to find the user ID in Ubuntu

I’ve already mentioned this utility at the beginning of this guide where I explained how you can use the id command without any options and it will be the UID of the currently logged-in user:

find user ID in ubuntu using the id command

But what if you want to know the UID of the other user? It is quite simple actually.

All you need to do is append the username with the id command and that’s it:

For example, if I want to know the UID of the user name abhiman , I will be following the given command:

find UID of specific user using the id command in ubuntu

Wrapping Up

This was a quick tutorial on how you can find the UID in Ubuntu. I personally use the first method (using lslogins) as it beings the UID of every user without any hassle.

And if you have any issues including errors or suggestions, feel free to let me know in the comments.

Источник

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