LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   getent does not work for usernames with numbers (https://www.linuxquestions.org/questions/linux-newbie-8/getent-does-not-work-for-usernames-with-numbers-830784/)

vinaytp 09-07-2010 08:26 AM

getent does not work for usernames with numbers
 
Hi All,

Here is the output of getent, which I am finding strange.

Code:

[3001@CDCTGIMCLSA ~]$ sudo getent passwd 3001
Also the exit status is 2

But the user 3001 exists in the system.

Code:

[3001@CDCTGIMCLSA ~]$ cat /etc/passwd | grep 3001
3001:x:513:513::/home/3001:/bin/bash

Also getent works well for usernames having alpabets.

Code:

[3001@CDCTGIMCLSA ~]$ sudo getent passwd test
test:x:511:511::/home/test:/bin/bash

I am finding it pretty amazing. Does anyone know why getent behaves like this ?

fpmurphy 09-07-2010 09:27 AM

what is the content of your /etc/nsswitch.conf file?

vinaytp 09-07-2010 10:30 AM

Quote:

Originally Posted by fpmurphy (Post 4090406)
what is the content of your /etc/nsswitch.conf file?

Here is the contents of /etc/nsswitch.conf

Code:

passwd:    files
shadow:    files
group:      files
hosts:      files dns
bootparams: nisplus [NOTFOUND=return] files

ethers:    files
netmasks:  files
networks:  files
protocols:  files
rpc:        files
services:  files

netgroup:  files nis

publickey:  nisplus

automount:  files nis
aliases:    files nisplus


vinaytp 09-08-2010 06:29 AM

Any idea on this behavior of getent ?

gdejonge 09-10-2010 09:45 PM

You probably should contact the programmer of the tool.
But I think that it will try to find a userid if the parameter is a pure numerical value in stead of trying to find a user with that
name.

Try using something like u3021
_______________
gerrard

ghostdog74 09-10-2010 09:50 PM

try adding another user with numbers, eg useradd 3002. useradd will complain. Did you add 3001 using useradd? or did someone just create an entry in passwd ?

mgsmith7475 09-10-2010 10:24 PM

Worked fine for me when adding a user by the name of 4000. just typed - getent passwd | grep 4000
and it found this - getent passwd | grep 4000
4000:x:525:527::/home/4000:/bin/bash

Definitely no bug.

Using Ubuntu 10.04

It also worked just fine on a RHEL 5.5 Server that I have available to me.

sem007 09-11-2010 12:05 AM

Quote:

Originally Posted by vinaytp (Post 4091286)
Any idea on this behavior of getent ?

When you query numeric username it will search for uid not username

i.e when you run

sudo getent passwd 3001

it will detect 3001 as UID not username. if uid not present then your exit status return 2

quote from getent man page

Quote:

Each key must be in a format appropriate for searching on
the respective database. For example, it can be a username
or numeric-uid for passwd

So if you want to query numeric user names use their UIDs.

i.e

Code:

$ sudo getent passwd UID
HTH

sem007 09-11-2010 12:09 AM

Quote:

Originally Posted by mgsmith7475 (Post 4094066)
Worked fine for me when adding a user by the name of 4000. just typed - getent passwd | grep 4000
and it found this - getent passwd | grep 4000
4000:x:525:527::/home/4000:/bin/bash

Definitely no bug.

Using Ubuntu 10.04

It also worked just fine on a RHEL 5.5 Server that I have available to me.

Filtering from whole databse is ok but when you query for specific numeric user it will show exit status 2.

i.e try to run below command

Code:

getent passwd 4000

vinaytp 09-13-2010 12:17 AM

Thanks for all your comments.

We cannot add a user with username 3001 in fedora. But we can add in RHEL and CentOS.

So getnent is not working as expected when it comes to numerical usernames.

Can we consider this as a bug in getent?

sem007 09-13-2010 01:29 AM

Quote:

Originally Posted by vinaytp (Post 4095714)
Thanks for all your comments.

We cannot add a user with username 3001 in fedora. But we can add in RHEL and CentOS.

So getnent is not working as expected when it comes to numerical usernames.

Can we consider this as a bug in getent?

when you query numeric username it consider as UID. read post #8.

It is not bug.

fpmurphy 09-14-2010 12:02 AM

Interestingly, I can add a user called "3001" in Fedora 13 without a problem.

But if I then use
Code:

getent passwd 3001
nothing is returned and the errno is 2.

System is standalone.

Disillusionist 09-14-2010 02:07 AM

I didn't think the system allowed you to create accounts (or groups) beginning with a numeric or special character.

I assume changing the usernames is out of the question?

It might be worth reviewing naming conventions to ensure that the issue doesn't get any worse (just a thought)

druuna 09-14-2010 02:34 AM

Hi,

Officially this is the legal template for a username: [a-z_][a-z0-9_-]*

Some distro's do allow other characters and might let you start a username with a number, but this could get you into trouble.

Hope this helps.

fpmurphy 09-15-2010 05:49 AM

Quote:

Originally Posted by druuna (Post 4096823)
Hi,
Officially this is the legal template for a username: [a-z_][a-z0-9_-]*

What do you mean by officially? Where is that specified in any formal specification?


All times are GMT -5. The time now is 09:03 PM.