Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

How can I find out, in a shell script, whether a given user name exists on the current system?


/etc/passwd and /etc/shadow are incomplete. Consider OS X's Directory Services, or Linux with Likewise Active Directory integration.

share|improve this question

4 Answers 4

up vote 46 down vote accepted

One of the most basic tools to be used for that is probably id.

#!/bin/bash
if id -u "$1" >/dev/null 2>&1; then
        echo "user exists"
else
        echo "user does not exist"
fi

Which produces

$ ./userexists root
user exists
$ ./userexists alice
user does not exist
$ ./userexists
user does not exist
share|improve this answer
2  
You don't need the backquotes here -- just use if id -u "$1" >/dev/null 2>&1; then ... –  Gordon Davisson Sep 17 '11 at 3:03
1  
@Gordon absolutely right of course. Thanks :) –  barbaz Sep 17 '11 at 11:17

finger

Parse the output of finger -m <username>. No error code if no user was found, unfortunately, but if not found, error output will be written. No drawbacks so far.

finger -ms <username> 2>&1 1>/dev/null | wc -l

Will print 0 if user is found (because there's no error output), larger numbers otherwise.

chown

Run (as any user, surprisingly):

T=$( mktemp -t foo.XXX ) ; chown <username> $T

If it fails as root, the account name is invalid.

If it fails as non-root user, parse the possibly localized output for Operation not permitted or invalid user (or equivalents). Set LANG beforehand to do this reliably.

share|improve this answer

getent

This command is designed to gather entries for the databases that can be backed by /etc files and various remote services like LDAP, AD, NIS/Yellow Pages, DNS and the likes.

To figure out if a username is known by one of the password naming services, simply run:

getent passwd username

This works also with group, hosts and others, depending on the OS and implementation.

share|improve this answer
1  
While Solaris and Linux, and more recently also most BSDs have getent, there is no getent on Mac OS X –  barbaz Sep 17 '11 at 15:37
    
Indeed, I missed Mac OS/X is missing getent. –  jlliagre Sep 17 '11 at 20:57
    
Nevertheless it's quite useful on the systems it supports. –  Daniel Beck Sep 19 '11 at 7:52
    
getent is nice because you can query multiple usernames where id only supports a single username. –  nall Jun 4 at 21:45

I would say that you would want to rely on /etc/passwd and similar (e.g. /etc/shadow for Shadow-based systems; on an off-topic side-note, some similar systems might use /etc/master.passwd or other such files).

The /etc/passwd is typically treated as the absolute authoritative decision on whether a user exists or not. If you use any of the other methods described on this page, and if those other methods point to an existing user but /etc/passwd does not, then I would say that the user does not properly exist on the system, by definition of the most common standard that software would likely rely on.

That said, I'll throw in another way to add to the mix of some other options that could be used.

ls -l /home | grep ^customUserName$<BR> echo $?

Clearly, replace "customuserName" with the name of the user you want to check for. Replace /home with /users if that is what your system uses. This might not find all users in /etc/passwd if no home directory was made for the particular user, which could occur if you simply imported users (that is, lines of text into /etc/passwd) and if home directories don't get made unless/until a person logs in.

share|improve this answer
    
I don't get why you state a user who has no entry in the /etc/password file but is reported to be valid by either id or getent does not "properly" exist on the system, especially when the OP is clearly stating naming services are to be considered. –  jlliagre Jun 4 at 23:03

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.