LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Check if users exist in the system (https://www.linuxquestions.org/questions/programming-9/check-if-users-exist-in-the-system-228904/)

hindll01 09-10-2004 06:52 AM

Check if users exist in the system
 
Hello

I am writing a script to add users to the system, I need to know how to check if a user I am trying to add already exists in the system.

Can anyone help??

druuna 09-10-2004 07:09 AM

If you are not using NIS/Samba you could check one of these:

grep <username> /etc/passwd
or
id <username>

Depending on the exitcode you can take some action.

There might be more ways of doing this, these 2 came to mind first.

Hope this helps.

scorbett 09-10-2004 10:41 AM

check if users exist
 
You could also check to see if a directory named after the user exists in the /home directory.

slizadel 09-10-2004 03:32 PM

Re: check if users exist
 
Quote:

Originally posted by scorbett
You could also check to see if a directory named after the user exists in the /home directory.
This probably would not be the best way to determine if a user exists. Take for example the user root. It exists on all systems, yet most of the time the home directory for the root user in /root instead of /home/root.

Another example would be the rpm user (I'm using this since hindll01 is an RH user). rpm does not have an entry in the /home tree, but does exist.

hindll01 09-11-2004 07:53 PM

What is an rpm user and RH user?

druuna 09-12-2004 06:34 AM

slizadel is correct in pointing out that checking /home for the excistens of a user is not a good idea, although the second example could be a bit confusing.

In general there are 2 kind of users: system and regular.

Examples of system users are: root, bin, daemon, sshd, nobody, rpm. Most of the time they cannot (physically) log in to your box (root being the exception).

Regular users are those that can physically log in to a system. Most of the time these will be people like you and me.

The RH user mentioned in slizadel's thread is you. RedHat (RH) is nowadays called Fedora Core 1, which you might/might not know.

Most linux distro's use /home for the homedir location of regular users, but other locations can be used too and are legal.

Checking /home to see if a user excist is not failsafe.

Hope this clears things up a bit.

b0ng 09-12-2004 03:24 PM

The best way, I can think of looking to see if a use already exists is checking /etc/passwd for anyone that has a home directory.

(Yes, I know more than just people with, home directories will show up.)


#!/usr/bin/perl

open (FILE, "/etc/passwd") || die "$!";

chomp (@new = <FILE>);

chomp (@users = grep /home/, @new);


foreach (@users){

@_ = split /:.*/, $_;

print "@_\n";
}


That is a little script I wrote up, it should do just what your looking for. Only one problem with it, it won't show root.

Jan-Willem Arnold 09-08-2009 04:47 AM

another simple way
 
finger smith | grep -c 'Login'

would be my suggestion. The result would be > 0 when a user named smith exists, and 0 if there is no smith around.

Tested on Suse 11.

unSpawn 09-08-2009 06:02 AM

The easiest way in my opinion would be to use 'getent someusername >/dev/null 2>&1' and use the return value to determine what to do.

Stragonian 09-28-2012 04:33 AM

This will return the user your looking for or nothing
Code:

USER="games"
EXISTS=$( cat /etc/passwd | grep $USER | sed -e 's/:.*//g' )
echo $EXISTS

returns
Code:

games
if a user is not found it will return nothing.
it can also return a list of users that have the same letters in their name
Code:

USER="op"
EXISTS=$( cat /etc/passwd | grep $USER | sed -e 's/:.*//g' )
echo $EXISTS

returns
Code:

operator
oprofile
pop

because all those users have op in their name.

druuna 09-28-2012 04:54 AM

@Stragonian: Don't necropost!

You just opened a 8 year old thread to give an answer that was already given.

There's no reason to add to this long dead thread:
- The OP hasn't been seen since sept 2004,
- Multiple (better/simpler) answers have already been given.

sundialsvcs 09-28-2012 07:47 AM

In the most general case, you don't know how any particular system maintains its list of "recognized users." It could well be using LDAP or Kerberos. The best approach to problems like this is to attempt the operation and to trust the host system to reject a duplicate ... only he knows for sure.

Stragonian 09-28-2012 02:24 PM

Sorry! it wasn't marked as solved, and I noticed unSpawn, and Jan-Willem Arnold, had posted replies only 2 days shy of 5 years after the original post ( 09-10-04 -> 09-08-09 ). http://www.linuxquestions.org/linux/rules.html never mentions anything about "Necropost" so now that I'm aware of such an unwritten rule as a "Necropost" I'll just start new threads in the future, and make reference to the original post. Cool?

http://www.linuxquestions.org/questi...es-4175429482/


All times are GMT -5. The time now is 12:52 AM.