Hi list, this is an odd problem, yet simple.. I could not find a command line tool that would return me the user name belonging to a UID (number) independent from the underlieing auth mechanism. It should be kind of a reverse "id" command.. the way an "ls" uses it. It seems so trivial to me, but I didn't find an answer googling for it. Any ideas? Markus
Markus Natter writes:
this is an odd problem, yet simple..
I could not find a command line tool that would return me the user name belonging to a UID (number) independent from the underlieing auth mechanism.
It should be kind of a reverse "id" command.. the way an "ls" uses it.
It seems so trivial to me, but I didn't find an answer googling for it.
Here is one way to do it. Save the following shell script as /usr/local/bin/rid and chmod r+x the file. Usage: rid # --------X snip X-------- #!/bin/sh awk -F: '{ if ($3 == id) print $1 }' id=$1 /etc/passwd --------X snip X-------- -Ti
Markus Natter <markus.natter@gmail.com> writes:
Hi list,
this is an odd problem, yet simple..
I could not find a command line tool that would return me the user name belonging to a UID (number) independent from the underlieing auth mechanism.
It should be kind of a reverse "id" command.. the way an "ls" uses it.
It seems so trivial to me, but I didn't find an answer googling for it.
Try: getent passwd uid Followed by some awk... Andreas -- Andreas Jaeger, aj@suse.de, http://www.suse.de/~aj SUSE LINUX Products GmbH, Maxfeldstr. 5, 90409 Nürnberg, Germany GPG fingerprint = 93A3 365E CE47 B889 DF7F FED1 389A 563C C272 A126
On 6/6/05, Andreas Jaeger <aj@suse.de> wrote:
Markus Natter <markus.natter@gmail.com> writes:
Hi list,
this is an odd problem, yet simple..
I could not find a command line tool that would return me the user name belonging to a UID (number) independent from the underlieing auth mechanism.
It should be kind of a reverse "id" command.. the way an "ls" uses it.
It seems so trivial to me, but I didn't find an answer googling for it.
Try: getent passwd uid
Followed by some awk...
Andreas -- Andreas Jaeger, aj@suse.de, http://www.suse.de/~aj SUSE LINUX Products GmbH, Maxfeldstr. 5, 90409 Nürnberg, Germany GPG fingerprint = 93A3 365E CE47 B889 DF7F FED1 389A 563C C272 A126
first of all I'd like to thank you for the suggestions, but is there also a passwd or other backend independent way? ( e.g. if you have users in passwd and LDAP, or NIS.. ) What does "ls" do to? I think it uses a kind of an abstraction layer, as it seems to ask any underlieing authentication mechanism and nscd.. Markus
Markus Natter writes:
... What does "ls" do to? I think it uses a kind of an abstraction layer, as it seems to ask any underlieing authentication mechanism and nscd..
The ls command does a stat(2) system call on each file or directory, gets the uid from the returned data, and then calls the getpwuid(3) to get the passwd data, which contains the login name. The getpwuid() function is NIS-aware (and I think LDAP as well). -Ti
On 6/6/05, Ti Kan <ti@amb.org> wrote:
Markus Natter writes:
... What does "ls" do to? I think it uses a kind of an abstraction layer, as it seems to ask any underlieing authentication mechanism and nscd..
The ls command does a stat(2) system call on each file or directory, gets the uid from the returned data, and then calls the getpwuid(3) to get the passwd data, which contains the login name. The getpwuid() function is NIS-aware (and I think LDAP as well).
-Ti
-- Check the headers for your unsubscription address For additional commands send e-mail to suse-linux-e-help@suse.com Also check the archives at http://lists.suse.com Please read the FAQs: suse-linux-e-faq@suse.com
Ah ok.. I see, I just expected a direct connection to PAM somehow.. Maybe I'd find some perl bindings to the getpwuid function. Looks like I'm the only one who's trying to get the username out of the UID.. Thanks a lot! Markus
On 6/6/05, Markus Natter <markus.natter@gmail.com> wrote:
On 6/6/05, Ti Kan <ti@amb.org> wrote:
Markus Natter writes:
... What does "ls" do to? I think it uses a kind of an abstraction layer, as it seems to ask any underlieing authentication mechanism and nscd..
The ls command does a stat(2) system call on each file or directory, gets the uid from the returned data, and then calls the getpwuid(3) to get the passwd data, which contains the login name. The getpwuid() function is NIS-aware (and I think LDAP as well).
-Ti
-- Check the headers for your unsubscription address For additional commands send e-mail to suse-linux-e-help@suse.com Also check the archives at http://lists.suse.com Please read the FAQs: suse-linux-e-faq@suse.com
Ah ok.. I see,
I just expected a direct connection to PAM somehow..
Maybe I'd find some perl bindings to the getpwuid function. Looks like I'm the only one who's trying to get the username out of the UID..
Thanks a lot!
Markus
Cool, there a built-in perl function of the same name. And it's obviously LDAP aware.. it works.. :-) once again, thanks to all of you. Markus
On Mon, Jun 06, Markus Natter wrote:
I just expected a direct connection to PAM somehow..
PAM is for authentication, not for connection a UID with a login name. NSS is what you are searching for. Thorsten -- Thorsten Kukuk http://www.suse.de/~kukuk/ kukuk@suse.de SUSE LINUX Products GmbH Maxfeldstr. 5 D-90409 Nuernberg -------------------------------------------------------------------- Key fingerprint = A368 676B 5E1B 3E46 CFCE 2D97 F8FD 4E23 56C6 FB4B
On 6/6/05, Thorsten Kukuk <kukuk@suse.de> wrote:
On Mon, Jun 06, Markus Natter wrote:
I just expected a direct connection to PAM somehow..
PAM is for authentication, not for connection a UID with a login name. NSS is what you are searching for.
Thorsten
-- Thorsten Kukuk http://www.suse.de/~kukuk/ kukuk@suse.de SUSE LINUX Products GmbH Maxfeldstr. 5 D-90409 Nuernberg -------------------------------------------------------------------- Key fingerprint = A368 676B 5E1B 3E46 CFCE 2D97 F8FD 4E23 56C6 FB4B
-- Check the headers for your unsubscription address For additional commands send e-mail to suse-linux-e-help@suse.com Also check the archives at http://lists.suse.com Please read the FAQs: suse-linux-e-faq@suse.com
yes, of course you're right.. NSS not PAM.. and getent /etc/passwd works, too.. I guess it was just the database name "/etc/passwd" which was missleading me.. I'm so sorry.. :-/ Markus
On Mon, Jun 06, Markus Natter wrote:
On 6/6/05, Thorsten Kukuk <kukuk@suse.de> wrote:
On Mon, Jun 06, Markus Natter wrote:
I just expected a direct connection to PAM somehow..
PAM is for authentication, not for connection a UID with a login name. NSS is what you are searching for.
Thorsten
-- Thorsten Kukuk http://www.suse.de/~kukuk/ kukuk@suse.de SUSE LINUX Products GmbH Maxfeldstr. 5 D-90409 Nuernberg -------------------------------------------------------------------- Key fingerprint = A368 676B 5E1B 3E46 CFCE 2D97 F8FD 4E23 56C6 FB4B
-- Check the headers for your unsubscription address For additional commands send e-mail to suse-linux-e-help@suse.com Also check the archives at http://lists.suse.com Please read the FAQs: suse-linux-e-faq@suse.com
yes, of course you're right.. NSS not PAM..
and getent /etc/passwd works, too..
No, getent passwd, getent /etc/passwd will not. -- Thorsten Kukuk http://www.suse.de/~kukuk/ kukuk@suse.de SUSE LINUX Products GmbH Maxfeldstr. 5 D-90409 Nuernberg -------------------------------------------------------------------- Key fingerprint = A368 676B 5E1B 3E46 CFCE 2D97 F8FD 4E23 56C6 FB4B
On 6/6/05, Thorsten Kukuk <kukuk@suse.de> wrote:
On Mon, Jun 06, Markus Natter wrote:
On 6/6/05, Thorsten Kukuk <kukuk@suse.de> wrote:
On Mon, Jun 06, Markus Natter wrote:
I just expected a direct connection to PAM somehow..
PAM is for authentication, not for connection a UID with a login name. NSS is what you are searching for.
Thorsten
-- Thorsten Kukuk http://www.suse.de/~kukuk/ kukuk@suse.de SUSE LINUX Products GmbH Maxfeldstr. 5 D-90409 Nuernberg -------------------------------------------------------------------- Key fingerprint = A368 676B 5E1B 3E46 CFCE 2D97 F8FD 4E23 56C6 FB4B
-- Check the headers for your unsubscription address For additional commands send e-mail to suse-linux-e-help@suse.com Also check the archives at http://lists.suse.com Please read the FAQs: suse-linux-e-faq@suse.com
yes, of course you're right.. NSS not PAM..
and getent /etc/passwd works, too..
No, getent passwd, getent /etc/passwd will not.
-- Thorsten Kukuk http://www.suse.de/~kukuk/ kukuk@suse.de SUSE LINUX Products GmbH Maxfeldstr. 5 D-90409 Nuernberg -------------------------------------------------------------------- Key fingerprint = A368 676B 5E1B 3E46 CFCE 2D97 F8FD 4E23 56C6 FB4B
I tried it with "getent passwd" intuitively.. think I should go home for today.. thanks a lot Markus
On Mon, Jun 06, Markus Natter wrote:
On 6/6/05, Andreas Jaeger <aj@suse.de> wrote:
Markus Natter <markus.natter@gmail.com> writes:
Hi list,
this is an odd problem, yet simple..
I could not find a command line tool that would return me the user name belonging to a UID (number) independent from the underlieing auth mechanism.
It should be kind of a reverse "id" command.. the way an "ls" uses it.
It seems so trivial to me, but I didn't find an answer googling for it.
Try: getent passwd uid
Followed by some awk...
first of all I'd like to thank you for the suggestions,
but is there also a passwd or other backend independent way? ( e.g. if you have users in passwd and LDAP, or NIS.. )
?? What is on getent /etc/passwd depending? getent is /etc/passwd independent and does of course work with NIS and LDAP. Thorsten -- Thorsten Kukuk http://www.suse.de/~kukuk/ kukuk@suse.de SUSE LINUX Products GmbH Maxfeldstr. 5 D-90409 Nuernberg -------------------------------------------------------------------- Key fingerprint = A368 676B 5E1B 3E46 CFCE 2D97 F8FD 4E23 56C6 FB4B
Markus Natter <markus.natter@gmail.com> writes:
but is there also a passwd or other backend independent way? ( e.g. if you have users in passwd and LDAP, or NIS.. )
passwd is the database - it uses LDAP, NIS or whatever exactly the same way that ls would do...
What does "ls" do to? I think it uses a kind of an abstraction layer, as it seems to ask any underlieing authentication mechanism and nscd..
ls calls the getent_* functions of glibc - the getent binary uses the same getent_* functions, Andreas -- Andreas Jaeger, aj@suse.de, http://www.suse.de/~aj SUSE LINUX Products GmbH, Maxfeldstr. 5, 90409 Nürnberg, Germany GPG fingerprint = 93A3 365E CE47 B889 DF7F FED1 389A 563C C272 A126
On 6/6/05, Andreas Jaeger <aj@suse.de> wrote:
Markus Natter <markus.natter@gmail.com> writes:
but is there also a passwd or other backend independent way? ( e.g. if you have users in passwd and LDAP, or NIS.. )
passwd is the database - it uses LDAP, NIS or whatever exactly the same way that ls would do...
What does "ls" do to? I think it uses a kind of an abstraction layer, as it seems to ask any underlieing authentication mechanism and nscd..
ls calls the getent_* functions of glibc - the getent binary uses the same getent_* functions,
Andreas -- Andreas Jaeger, aj@suse.de, http://www.suse.de/~aj SUSE LINUX Products GmbH, Maxfeldstr. 5, 90409 Nürnberg, Germany GPG fingerprint = 93A3 365E CE47 B889 DF7F FED1 389A 563C C272 A126
Thank you Andreas, I guess I was searching and googling too UID/Username specific in the beginning that I didn't find a hint on the getent_* functions and Thorsten's getent binary earlier. thanks a lot, to all of you Markus
Markus, Andreas, On Monday 06 June 2005 04:27, Andreas Jaeger wrote:
...
Try: getent passwd uid
Followed by some awk...
Awk?? Put this in a file, make it executable and put the file in a directory in your PATH. Call it "di", perhaps? -==--==--==--==--==--==--==--==--==--==--==--==--==--==--==- #!/bin/bash --norc getent passwd uid "$@" |sed -e 's/:.*//' -==--==--==--==--==--==--==--==--==--==--==--==--==--==--==- The down-side is that you get no output and no diagnostic for unknown ID. Fixing that can be an "exercise for the reader."
Andreas
Randall Schulz
On 6/6/05, Randall R Schulz <rschulz@sonic.net> wrote:
Markus, Andreas,
On Monday 06 June 2005 04:27, Andreas Jaeger wrote:
...
Try: getent passwd uid
Followed by some awk...
Awk??
Put this in a file, make it executable and put the file in a directory in your PATH. Call it "di", perhaps?
-==--==--==--==--==--==--==--==--==--==--==--==--==--==--==- #!/bin/bash --norc
getent passwd uid "$@" |sed -e 's/:.*//' -==--==--==--==--==--==--==--==--==--==--==--==--==--==--==-
The down-side is that you get no output and no diagnostic for unknown ID. Fixing that can be an "exercise for the reader."
Andreas
Randall Schulz
-- Check the headers for your unsubscription address For additional commands send e-mail to suse-linux-e-help@suse.com Also check the archives at http://lists.suse.com Please read the FAQs: suse-linux-e-faq@suse.com
thanks, something like this should do it then, if the ID is unknown.. -- snip -- #!/bin/bash res=$(getent passwd uid "$@" |sed -e 's/:.*//') if [ "$res" == "" ]; then echo "unknown user id" exit 2 fi -- snap -- markus
Markus, On Monday 06 June 2005 07:04, Markus Natter wrote:
On 6/6/05, Randall R Schulz <rschulz@sonic.net> wrote:
...
Put this in a file, make it executable and put the file in a directory in your PATH. Call it "di", perhaps?
-==--==--==--==--==--==--==--==--==--==--==--==--==--==--==- #!/bin/bash --norc
getent passwd uid "$@" |sed -e 's/:.*//' -==--==--==--==--==--==--==--==--==--==--==--==--==--==--==-
The down-side is that you get no output and no diagnostic for unknown ID. Fixing that can be an "exercise for the reader."
Andreas
Randall Schulz
thanks,
something like this should do it then, if the ID is unknown..
-- snip -- #!/bin/bash
res=$(getent passwd uid "$@" |sed -e 's/:.*//') if [ "$res" == "" ]; then echo "unknown user id" exit 2
fi -- snap --
Not really. There are two problems: 1) That will only give the diagnostic when none of the IDs passed are found by getent. 2) It produces only diagnostic output. Try this: -==--==--==--==--==--==--==--==--==--==--==--==--==--==--==- #!/bin/bash --norc for id; do pwEnt="$(getent passwd uid "$id")" if [ ! "$pwEnt" ]; then echo "di: User id $id unknown" >&2 else echo "$id: ${pwEnt%%:*}" fi done -==--==--==--==--==--==--==--==--==--==--==--==--==--==--==-
markus
Randall Schulz
On 6/6/05, Randall R Schulz <rschulz@sonic.net> wrote:
Markus,
On Monday 06 June 2005 07:04, Markus Natter wrote:
On 6/6/05, Randall R Schulz <rschulz@sonic.net> wrote:
...
Put this in a file, make it executable and put the file in a directory in your PATH. Call it "di", perhaps?
-==--==--==--==--==--==--==--==--==--==--==--==--==--==--==- #!/bin/bash --norc
getent passwd uid "$@" |sed -e 's/:.*//' -==--==--==--==--==--==--==--==--==--==--==--==--==--==--==-
The down-side is that you get no output and no diagnostic for unknown ID. Fixing that can be an "exercise for the reader."
Andreas
Randall Schulz
thanks,
something like this should do it then, if the ID is unknown..
-- snip -- #!/bin/bash
res=$(getent passwd uid "$@" |sed -e 's/:.*//') if [ "$res" == "" ]; then echo "unknown user id" exit 2
fi -- snap --
Not really. There are two problems:
1) That will only give the diagnostic when none of the IDs passed are found by getent. 2) It produces only diagnostic output.
Try this:
-==--==--==--==--==--==--==--==--==--==--==--==--==--==--==- #!/bin/bash --norc
for id; do pwEnt="$(getent passwd uid "$id")"
if [ ! "$pwEnt" ]; then echo "di: User id $id unknown" >&2 else echo "$id: ${pwEnt%%:*}" fi done -==--==--==--==--==--==--==--==--==--==--==--==--==--==--==-
markus
Randall Schulz
-- Check the headers for your unsubscription address For additional commands send e-mail to suse-linux-e-help@suse.com Also check the archives at http://lists.suse.com Please read the FAQs: suse-linux-e-faq@suse.com
Hi Randall, I've forgotten to put the else part in mail.. and to redirect the output to stderr.. It's not my day.. beeing able to give more than one ID at a time is much more than I thought of, but a million thanks, Markus
Markus, On Monday 06 June 2005 13:05, Markus Natter wrote:
...
Hi Randall,
I've forgotten to put the else part in mail.. and to redirect the output to stderr.. It's not my day..
beeing able to give more than one ID at a time is much more than I thought of, but a million thanks,
The specs are yours to dictate, of course, but if you want to allow only one ID per invocation, then two other things arise: 1) Diagnose improper invocation with other than 1 argument. (Not that my example diagnosed invocation with zero arguments...) 2) Don't use "$@", which means individually quote each argument. Or, at least, be aware of the difference between "$*" and "$@".
Markus
Randall Schulz
On 6/6/05, Randall R Schulz <rschulz@sonic.net> wrote:
Markus,
On Monday 06 June 2005 13:05, Markus Natter wrote:
...
Hi Randall,
I've forgotten to put the else part in mail.. and to redirect the output to stderr.. It's not my day..
beeing able to give more than one ID at a time is much more than I thought of, but a million thanks,
The specs are yours to dictate, of course, but if you want to allow only one ID per invocation, then two other things arise:
1) Diagnose improper invocation with other than 1 argument. (Not that my example diagnosed invocation with zero arguments...) 2) Don't use "$@", which means individually quote each argument. Or, at least, be aware of the difference between "$*" and "$@".
Markus
Randall Schulz
-- Check the headers for your unsubscription address For additional commands send e-mail to suse-linux-e-help@suse.com Also check the archives at http://lists.suse.com Please read the FAQs: suse-linux-e-faq@suse.com
Hello Randall, the "$@" was left from your first example by accident. I would use just a simple $1 and show a usage message if a $2 exists or the $1 doesn't. I'd like to keep things simple and readable for others. I never really got the difference of the discribed IFS states "unset" (unset IFS) and "null"(IFS=), which creates nearly the opposit results in the context of "$*".. sounds both like "undefined" to me, but I guess there is one I don't see at the moment.. but thanks for the advice, Markus
On 06.06.05,13:01, Markus Natter wrote:
Hi list,
this is an odd problem, yet simple..
I could not find a command line tool that would return me the user name belonging to a UID (number) independent from the underlieing auth mechanism.
It should be kind of a reverse "id" command.. the way an "ls" uses it.
It seems so trivial to me, but I didn't find an answer googling for it.
Any ideas?
Markus
You can try this too: awk -F ":" '/user/{ print "username: " $1,", uid: " $3 }' /etc/passwd Exchange user with the username to be searched for. - Jostein -- Jostein Berntsen <jbernts@broadpark.no>
On 6/6/05, Jostein Berntsen <jbernts@broadpark.no> wrote:
On 06.06.05,13:01, Markus Natter wrote:
Hi list,
this is an odd problem, yet simple..
I could not find a command line tool that would return me the user name belonging to a UID (number) independent from the underlieing auth mechanism.
It should be kind of a reverse "id" command.. the way an "ls" uses it.
It seems so trivial to me, but I didn't find an answer googling for it.
Any ideas?
Markus
You can try this too:
awk -F ":" '/user/{ print "username: " $1,", uid: " $3 }' /etc/passwd
Exchange user with the username to be searched for.
- Jostein
-- Jostein Berntsen <jbernts@broadpark.no>
-- Check the headers for your unsubscription address For additional commands send e-mail to suse-linux-e-help@suse.com Also check the archives at http://lists.suse.com Please read the FAQs: suse-linux-e-faq@suse.com
Thanks Jostein, but I didn't mean to parse the /etc/passwd as I wanted to write a script which works "indepently" from the auth backend. The perl implementation of the c-library function getpwuid() seems to work for me. I just assumed that such a simple and elementary tool must be hiding somewhere in the shoals of the linux cli toolbox.. ;) well.. it is somehow.. in the getpwuid function.. but there's no cli implementation of that function, like the id command.. such things are confusing me.. really shocking me.. but luckily they do not happen that often.. :-) Markus
Hi. El Lunes, 6 de Junio de 2005 15:01, Markus Natter escribió:
On 6/6/05, Jostein Berntsen <jbernts@broadpark.no> wrote:
On 06.06.05,13:01, Markus Natter wrote:
Hi list,
this is an odd problem, yet simple..
I could not find a command line tool that would return me the user name belonging to a UID (number) independent from the underlieing auth mechanism.
I just assumed that such a simple and elementary tool must be hiding somewhere in the shoals of the linux cli toolbox.. ;) well.. it is somehow.. in the getpwuid function.. but there's no cli implementation of that function, like the id command..
such things are confusing me.. really shocking me.. but luckily they do not happen that often.. :-)
try this. getent passwd | cut -f 3,1 -d : | grep uid
Markus
-- Un Saludo. Carlos Lorenzo Matés
On 6/7/05, Carlos Lorenzo Matés <clmates@mundo-r.com> wrote:
Hi.
El Lunes, 6 de Junio de 2005 15:01, Markus Natter escribió:
On 6/6/05, Jostein Berntsen <jbernts@broadpark.no> wrote:
On 06.06.05,13:01, Markus Natter wrote:
Hi list,
this is an odd problem, yet simple..
I could not find a command line tool that would return me the user name belonging to a UID (number) independent from the underlieing auth mechanism.
I just assumed that such a simple and elementary tool must be hiding somewhere in the shoals of the linux cli toolbox.. ;) well.. it is somehow.. in the getpwuid function.. but there's no cli implementation of that function, like the id command..
such things are confusing me.. really shocking me.. but luckily they do not happen that often.. :-)
try this.
getent passwd | cut -f 3,1 -d : | grep uid
Markus
--
Un Saludo.
Carlos Lorenzo Matés
many thanks Carlos, you and all the others have shown me a lot of possible solutions now. once again, thank you all! Markus
participants (7)
-
Andreas Jaeger
-
Carlos Lorenzo Matés
-
Jostein Berntsen
-
Markus Natter
-
Randall R Schulz
-
Thorsten Kukuk
-
ti@amb.org