User from uid linux

Linux Get User by ID

In Linux, everything is assigned a unique identification number which is used to store its information, like, each user has a separate user ID “UID”, a group has a unique Group ID “GID,” and a process has its unique ID “PID”. The user ID UID can be used to get any particular information about that user. In this article, we will use the UID to get the user information with different methods, which are as follows:

  • Pre-Requisite
  • How to Get User by ID in Linux?
  • Method 1: Get the User by ID Using the id Command
  • Method 2: Get the User by ID Using the cat Command
  • Method 3: Get the User’s Groups by ID Using getent Command
  • Method 4: Get User’s Groups by ID Using awk Command

Pre-Requisite

The UID will be used to get the user’s information. Let’s find the user’s ID for the “itslinuxfoss” using this command:

Note: The user has UID “1000”, which will be used through this guide to get the information about the user. Let’s get user details using UID.

How to Get User by ID in Linux?

User details can be obtained via the unique ID (UID) assigned to the user. Let’s use the UID to find the user details.

Method 1: Get the User by ID Using the id Command

The id command is utilized to find all the user’s information. To get the information for a specific UID “1000”, the below command is used:

The above output shows the details like user ID (UID), Group ID (GID), and groups for users with “1000” UID.

If you want to get all the group IDs (GID) for a specific UID (1000), use the following command:

The groups for UID “1000” are displayed in the output.

Method 2: Get the User by ID Using the cat Command

The “/etc/passwd” file contains all the information about the user. To get the information for a specific user with UID “1000”, using the “cat” command, use the below-mentioned command:

Читайте также:  Поставить пароль пользователю linux

The output shows the User Name, Encrypted Password, User ID (UID), Group ID (GID) and other details.

If you want to find the user name only for the UID (1000), using the /etc/passwd file and cat command, execute the following command:

$ cat /etc/passwd | grep 1000 | cut -d':' -f1

The user name “itslinuxfoss” is displayed on the output for UDI (1000).

Method 3: Get the User’s Groups by ID Using getent Command

The “getent” command stores the user information, which can be accessed using the UID. To get the details about the user with its UID (1000), use the following command:

The output shows all the details about the UID “1000”.

If you want to get the name of the user only using the “getent” command for a specific UID (1000), use this command:

$ getent passwd 1000 | cut -d':' -f1

The user is logged in as “itslinuxfoss” user name.

Method 4: Get User’s Groups by ID Using awk Command

The “awk” command is also used to find the user details about a particular UID. For instance, to get the user name for UID “1000”, use this command:

$ awk -F':' -v uid=1000 '$3 == uid < print $1 >' /etc/passwd

Similarly, we can use the “awk” command with the getent command to find the user name with the help of its UID (1000):

$ getent passwd 1000 | awk -F':' '< print $1 >'

The user name is displayed in the output.

That’s it from this article.

Conclusion

In Linux, the User ID is a unique identification number that stores information about the user. The User ID “UID” can be used with different commands to get specific information about the user. Moreover, this guide has explained different methods of using the UID to find all the information or specific information about the user.

Источник

How to get user’s name from uid

when I execute this the terminal only shows the prompt message then stops working. Am I missing something ?

I don’t know of a good shell command, but not all users will always be in /etc/passwd, to be sure you would have to use getpwent System library function instead (some script environments like Perl and Python offer a wrapper for it)

4 Answers 4

That will query the account database (whether it’s stored in /etc/passwd , LDAP, NIS+, a RDBMS. ) for the first user name with that uid.

Generally, there’s only one user name per uid, but that’s not guaranteed, the key in the user account database is the username, not user id.

Читайте также:  Арч линукс установка драйвера нвидиа

If you want to know all the user names for a given uid, you can do:

getent passwd | awk -F: '$3 == ENVIRON["ID"] ' 

But that may not work for some account databases that are not enumerable (as sometimes the case for large LDAP-based ones).

Not an LDAP expert, but according to serverfault.com/a/779769/363611 enumeration can be enabled via /etc/sssd/sssd.conf , so that should make getent approach working

$ printf 'User is %s\n' "$( getent passwd 1001 | cut -d : -f 1 )" User is myself 

(there is a myself user with UID 1001 on my system)

This would query the passwd database for the given UID, strip out the username from that response and use the result as an argument to printf . This would not work on macOS.

$ printf 'User is %s\n' "$( < getent passwd 1001 || echo 'UNKNOWN USER'; >| cut -d : -f 1 )" User is myself 
$ printf 'User is %s\n' "$( < getent passwd 1002 || echo 'UNKNOWN USER'; >| cut -d : -f 1 )" User is UNKNOWN USER 

As a script taking any number of UIDs on the command line (with slightly altered output):

#!/bin/sh for uid do printf 'User with UID %d is %s\n' "$uid" \ "$( < getent passwd "$uid" || echo 'NOT KNOWN'; >| cut -d : -f 1 )" done 

Testing (this would go through some service accounts’ UIDs on my OpenBSD system):

$ sh ./script.sh User with UID 110 is _sndiop User with UID 111 is NOT KNOWN User with UID 112 is _syspatch User with UID 113 is NOT KNOWN User with UID 114 is NOT KNOWN User with UID 115 is _slaacd 

Help with the original shellscript

The test in the if statement is causing the problem. I suggest the following shellscript,

#!/bin/bash read -p "donner l UID " cheruid if [ "$(grep -w "^$cheruid" /etc/passwd)" != "" ] then grep -w "$cheruid" /etc/passwd | cut -d ":" -f "1" | xargs echo "user is : " else echo "user not found" fi 

Edit1: I added a ^ in the test to only look for matches at the beginning of the line.

Edit2: since : is a separator, you can remove it and everything after, and use the result directly in the echo line, if accepted in the test, and simplify the shellscript to

#!/bin/bash read -p "donner l UID " cheruid cheruid=$ user=$(grep -wo "^$cheruid" /etc/passwd) if [ "$user" != "" ] then echo "user is : $user" else echo "user not found" fi 

This helps you make a good bash shellscript.

Efficient and more general method to find user IDs with id

If you want an efficient way to check if a particular user name exists, , you can use id , as indicated in another answer to your question by Stéphane Chazelas:

Читайте также:  Dwa 525 драйвер линукс

id will find not only user IDs stored in /etc/passwd but also those managed and stored in other ways, for example LDAP, which is common in professional server systems as commented by Matteo Italia.

If you want to scan all users

  • system users, in many linux systems with user numbers < 1000
  • ‘human’ users, in many linux systems with user numbers >= 1000
  • and assuming that all users have number < 2000 (modify if you need)

you can use the following bash one-liner, which uses a loop with id ,

for ((i=0;i<2000;i++));do name=$(id -un $i 2>/dev/null);if [ $? -eq 0 ];then echo "$i: $name" ;fi;done 

If you want to see only the ‘human’ users and assume that no user ID number is skipped (no deleted user), the following bash oneliner is very fast,

i=1000;while true;do name=$(id -un $i 2>/dev/null);if [ $? -eq 0 ];then echo "$name" ;else break;fi;i=$((i+1));done 

but it is more reliable to assume that some ID numbers may be skipped (deleted users),

for ((i=1000;i<2000;i++));do name=$(id -un $i 2>/dev/null);if [ $? -eq 0 ];then echo "$i: $name" ;fi;done 

There are also users and who which print the user names of users currently logged in to the current host.

Источник

How can I look up a username by id in linux?

Is there a command to lookup up a username from a uid ? I realize this can be done by looking at the /etc/passwd file but I’m asking if there is an existing command to to this, especially if the user executing it is not root. I’m not looking for the current user’s username, i.e. I am not looking for whoami or logname . This also made me wonder if on shared web hosting this is a security feature, or am I just not understanding something correctly? For examination, the /etc/passwd file from a shared web host:

root:x:0:0:root:/root:/bin/bash bin:x:1:1:bin:/bin:/sbin/nologin daemon:x:2:2:daemon:/sbin:/sbin/nologin adm:x:3:4:adm:/var/adm:/sbin/nologin lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin sync:x:5:0:sync:/sbin:/bin/sync shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown halt:x:7:0:halt:/sbin:/sbin/halt mail:x:8:12:mail:/var/spool/mail:/sbin/nologin news:x:9:13:news:/etc/news: uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin operator:x:11:0:operator:/root:/sbin/nologin games:x:12:100:games:/usr/games:/sbin/nologin gopher:x:13:30:gopher:/var/gopher:/sbin/nologin ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin nobody:x:99:99:Nobody:/:/sbin/nologin nscd:x:28:28:NSCD Daemon:/:/sbin/nologin vcsa:x:69:69:virtual console memory owner:/dev:/sbin/nologin pcap:x:77:77::/var/arpwatch:/sbin/nologin rpc:x:32:32:Portmapper RPC user:/:/sbin/nologin mailnull:x:47:47::/var/spool/mqueue:/sbin/nologin smmsp:x:51:51::/var/spool/mqueue:/sbin/nologin oprofile:x:16:16:Special user account to be used by OProfile:/home/oprofile:/sbin/nologin sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin dbus:x:81:81:System message bus:/:/sbin/nologin avahi:x:70:70:Avahi daemon:/:/sbin/nologin rpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologin haldaemon:x:68:68:HAL daemon:/:/sbin/nologin xfs:x:43:43:X Font Server:/etc/X11/fs:/sbin/nologin avahi-autoipd:x:100:104:avahi-autoipd:/var/lib/avahi-autoipd:/sbin/nologin named:x:25:25:Named:/var/named:/sbin/nologin mailman:x:32006:32006::/usr/local/cpanel/3rdparty/mailman/mailman:/usr/local/cpanel/bin/noshell dovecot:x:97:97:dovecot:/usr/libexec/dovecot:/sbin/nologin mysql:x:101:105:MySQL server:/var/lib/mysql:/bin/bash cpaneleximfilter:x:32007:32009::/var/cpanel/userhomes/cpaneleximfilter:/usr/local/cpanel/bin/noshell nagios:x:102:106:nagios:/var/log/nagios:/bin/sh ntp:x:38:38::/etc/ntp:/sbin/nologin myuser:x:1747:1744::/home/myuser:/usr/local/cpanel/bin/jailshell 
drwx------ 3 root root 1024 Apr 16 02:09 spamd-22217-init/ drwxr-xr-x 2 665 664 1024 Apr 4 00:05 update-cache-44068ab4/ drwxr-xr-x 4 665 664 1024 Apr 17 15:17 update-extraction-44068ab4/ -rw-rw-r-- 1 665 664 43801 Apr 17 15:17 variable.zip -rw-r--r-- 1 684 683 4396 Apr 17 07:01 wsdl-13fb96428c0685474db6b425a1d9baec 

We can see root is the owner of some files, and root is also showing up in /etc/passwd , however the other users/groups all show up as numbers.

Источник

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