Find the answer to your Linux question:
Results 1 to 9 of 9
I'm trying to convert the unix uid of users to the actual username in a perl script im working on but cant for the life of me figure out how ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Nov 2005
    Posts
    17

    convert uid to username?


    I'm trying to convert the unix uid of users to the actual username in a perl script im working on but cant for the life of me figure out how I can do this....there seems to be plenty of ways of getting the uid from the username but not the other way round.

    Any ideas would be great.

    Thanks,

    Lewis

  2. #2
    Linux User DThor's Avatar
    Join Date
    Jan 2006
    Location
    Ca..na...daaa....
    Posts
    319
    I'm confused. First off, how do you get a userid(which is an integer and has recommend restrictions on range) from a username(which is text)? Of course, there could be some method to somehow get from text to integer, but I'm unsure how this could have any relevance to anything.

    Going the way you wish to go, I would think would be as simple as replacing the username field in /etc/passwd with the userid field, line by line, but you have a whole mess of authentication stuff to deal with. Also, I'm not sure what this is buying you.

    DT

  3. #3
    Just Joined!
    Join Date
    Nov 2005
    Posts
    17
    Sorry, i'll explain a little further.

    I ran a script that went over my file system getting all the information about every file (perl stat function) . It got information such as the name, the modified date, the size etc etc.

    At the minute the uid field contains the user ID of the owner of that file, unfortunately when im printing this information on my script to give me a run down of particular files I have to output the uid because thats all i have stored in my database. I was hoping there was a lookup table somewhere relating uid's to usernames so that i could print the username instead of the integer uid....is this going to be possible?

    Hope that explains it a little better, thanks for the reply.

  4. #4
    Linux User DThor's Avatar
    Join Date
    Jan 2006
    Location
    Ca..na...daaa....
    Posts
    319
    Aha, got it. Yah, a bit different from where I was going with it - it was the "convert" that threw me.

    Well, what about using the /etc/passwd file itself? You don't need root permissions to read it, and each line contains the userid in a specific location(field separated with ":"), match that to the user name on the same line(again, a specific location), and you're done. In a way, that's sort of what the passwd file is - a lookup table.

    Offhand, not sure of a util that returns that info, although there might be. Odds are if there is, there's a Perl mod that lets you do it too, though...

    DT

  5. #5
    Just Joined!
    Join Date
    Nov 2005
    Posts
    17
    Looking at the /etc/passwd file im wondering if my system is setup in a slightly odd way.

    If i log in as root and view the passed file i dont see any user called lfoster, this is my normal login. In fact, i'd expect there to be an awful lot more users than im seeing. Theres users such as apache, and root, and even my group but not a great deal more.

    I've got a feeling this file isn't going to contain all our users. I seem to remember speaking to my boss about logins and there's some other way of authenticating other than file, nys/nis maybe?

  6. #6
    Linux User DThor's Avatar
    Join Date
    Jan 2006
    Location
    Ca..na...daaa....
    Posts
    319
    Yup, that's very possible. You'd need to query the server. Now I'm afraid you're into territory I'm not certain about, sorry. Maybe someone else can jump in...

    DT

  7. #7
    Just Joined!
    Join Date
    Nov 2005
    Posts
    17
    Thanks for your help up to this point then DThor. Time for some googling on nys/nis maybe.

  8. #8
    Just Joined! tb10alj's Avatar
    Join Date
    Feb 2006
    Location
    Germany
    Posts
    31
    If you want to look at your network-wide password database
    Code:
    ypcat passwd
    is your friend.

  9. #9
    Just Joined!
    Join Date
    Apr 2008
    Posts
    1

    convert uid to username? - use getpwuid

    You're looking for the "getpwuid" command - see "man perlfunc".

    For example:
    perl -e '($login,$pass,$uid,$gid) = getpwuid($<);print "$login\n";'

    Note that "$<" is your uid. It could also be the uid of a file determined via the stat command:
    $uid = (stat($filename))[4];
    print "$filename belongs to me!\n" if ($uid -eq $<);

    To go the other way, use "getpwnam".

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •