79

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.

0

4 Answers 4

118

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

#!/bin/bash
if id "$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
10
  • 2
    You don't need the backquotes here -- just use if id -u "$1" >/dev/null 2>&1; then ... Sep 17, 2011 at 3:03
  • 1
    @Gordon absolutely right of course. Thanks :)
    – barbaz
    Sep 17, 2011 at 11:17
  • 1
    "-u" also seems unnecessary
    – palacsint
    Oct 24, 2015 at 20:38
  • Wha's if I needed only if with negative result checker? Jan 4, 2018 at 21:49
  • id -u $1 1>/dev/null 2>&1; echo $? can be used for pipe
    – JasonWayne
    Feb 7, 2018 at 2:12
27

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.

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

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.

1

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.

1
  • 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, 2015 at 23:03

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .