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?? |
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. |
check if users exist
You could also check to see if a directory named after the user exists in the /home directory.
|
Re: check if users exist
Quote:
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. |
What is an rpm user and RH user?
|
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. |
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. |
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. |
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.
|
This will return the user your looking for or nothing
Code:
USER="games" Code:
games it can also return a list of users that have the same letters in their name Code:
USER="op" Code:
operator |
@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. |
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.
|
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. |