LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-10-2004, 06:52 AM   #1
hindll01
LQ Newbie
 
Registered: Sep 2004
Location: New Zealand
Distribution: Fedora Core 1
Posts: 13

Rep: Reputation: 0
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??
 
Old 09-10-2004, 07:09 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
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.
 
Old 09-10-2004, 10:41 AM   #3
scorbett
Member
 
Registered: May 2002
Location: Canada
Distribution: Slackware, Mandriva, RedHat
Posts: 46

Rep: Reputation: 15
check if users exist

You could also check to see if a directory named after the user exists in the /home directory.
 
Old 09-10-2004, 03:32 PM   #4
slizadel
LQ Newbie
 
Registered: Jun 2003
Distribution: RH9, Gentoo, Slack
Posts: 23

Rep: Reputation: 15
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.
 
Old 09-11-2004, 07:53 PM   #5
hindll01
LQ Newbie
 
Registered: Sep 2004
Location: New Zealand
Distribution: Fedora Core 1
Posts: 13

Original Poster
Rep: Reputation: 0
What is an rpm user and RH user?
 
Old 09-12-2004, 06:34 AM   #6
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
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.
 
Old 09-12-2004, 03:24 PM   #7
b0ng
LQ Newbie
 
Registered: Aug 2004
Location: Location??? Where I am is top secret, if I tell you, I have to kill you.
Distribution: College, Slack
Posts: 24

Rep: Reputation: 15
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.

Last edited by b0ng; 09-12-2004 at 03:33 PM.
 
Old 09-08-2009, 04:47 AM   #8
Jan-Willem Arnold
LQ Newbie
 
Registered: Sep 2009
Posts: 1

Rep: Reputation: 0
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.
 
Old 09-08-2009, 06:02 AM   #9
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
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.
 
Old 09-28-2012, 04:33 AM   #10
Stragonian
Member
 
Registered: Dec 2003
Location: Indiana
Distribution: Slackware & PassionX
Posts: 103

Rep: Reputation: 34
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.
 
Old 09-28-2012, 04:54 AM   #11
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
@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.
 
Old 09-28-2012, 07:47 AM   #12
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,673
Blog Entries: 4

Rep: Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945
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.
 
Old 09-28-2012, 02:24 PM   #13
Stragonian
Member
 
Registered: Dec 2003
Location: Indiana
Distribution: Slackware & PassionX
Posts: 103

Rep: Reputation: 34
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/

Last edited by Stragonian; 09-28-2012 at 02:55 PM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
get_alias_user_groups: gid of user 00sarahl doesn't exist. Check your /etc/passwd and seanfitz Linux - Networking 0 09-30-2005 07:06 AM
Check if email exist or not proNick Linux - Software 2 08-27-2005 01:25 PM
How to check if a software exist in my computer Niceman2005 Linux - Newbie 4 12-28-2004 09:12 PM
How to check if array[0] doesn't exist? flamesrock Programming 3 11-05-2004 10:36 AM
script to check $PATH directories exist Frustin Linux - Software 3 09-21-2004 12:20 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:59 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration