Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I've been running the useradd {user} command to add users to my system, though I plan on running this in an automated environment, and it might end up being run again, even though the user already exists.

Is there a way that I can run this, only if it doesn't already exist? The user doesn't have a home folder.

Thanks! Tom.

share|improve this question
    
adduser is generally preferred on Debian/Ubuntu systems. –  Faheem Mitha Jan 7 '12 at 14:18

5 Answers 5

up vote 28 down vote accepted

id -u somename returns a non-zero exit code when the user does not exist.

You can test it quite simply... (&>/dev/null just supresses the normal output/warning)

id -u somename &>/dev/null || useradd somename 
share|improve this answer
3  
Surely ||, not &&? –  Ulrich Schwarz Jan 7 '12 at 14:12
3  
@UlrichSchwarz The boolean operators || (or) and && (and) takes two arguments. When evaluating the or: if the first argument is true then the second argument is not evaluated. When evaluating the and: if the first argument is true then the next argument is evaluated. The second case here is not desired. –  Herman Torjussen Jan 7 '12 at 14:56
2  
@hesse: Yes, id && useradd as perer originally wrote is the wrong way around, that's what I'm saying. –  Ulrich Schwarz Jan 7 '12 at 16:12

try this:

useradd {user} || echo "User already exists."

or even this:

useradd {user} || true
share|improve this answer
    
This saves the check since it fails safely if the user already exists. –  CoverosGene Oct 1 at 19:09

The initial OP explained he wanted an action ONLY if user exists.. Aaron -- not your assumption that he wants to run again even if the user exists. Example:

I want to disable the password for a user, IF exists.. but NOT add a user, and a disabled password to systems where the user never existed.

I have not found this answer elsewhere, and it should be a simple check within puppet, not requiring external exec cmds.

I hoped for a simpler method - researched at puppetlabs - that I could leave the ensure out, but for user 'bob' the password should always be :!!: Same result if you set shell to /bin/false or /bin/nologin.

The answer - if you leave out ensure present - Puppet will default to ensure present before it can change all other settings. It can't just act on it without

Without doing some external shell check and setting a value or fact - puppet can't make changes only if the user isn't there. It has to either ensure its there, make the change, or ensure its GONE.

Most sysadmin don't like to remove a user - leaving a hole where the UID used to be defined. Instead they disable. That way existing files and procs with the UID are labeled, etc.

I guess I'd rather have a user thats gone added, then to miss a host where accounts or access is still present. But the above is a concept that maybe puppet devs should consider.

share|improve this answer

Unless you only have a small handful of systems, you are asking the wrong question. The answer is not to run useradd at all, but instead leave this work to a configuration management solution like puppet or chef. This will allow your user definitions to be centralized and prevent you from running for loops and using ssh with root users in order to configure your systems. You will always have systems in a known configuration state.

Documentation for puppet is available at http://docs.puppetlabs.com

As an example in puppet:

user { "bob" : 
  password   => "$1$yv3n066X$Vpb05Ac/fHTicNdT9T5vz1", # generated with `openssl passwd -1`
  ensure     => present,                              # ensure => absent to remove
  managehome => true,
}
share|improve this answer
    
Puppet is a great project, but you cannot make assumptions about what the OP is really asking :) –  rahmu Mar 24 '12 at 15:20
1  
The OP explained the goal very clearly - automated and able to be run again even though the user exists (idempotency). These are the exact use cases for a configuration management tool. When someone isn't aware that tools already exist to solve these problems, they tend to ask questions that involve solving a very specific, narrowly focused problem when there is a larger concept to grasp. –  Aaron Brown Mar 24 '12 at 16:29
    
I'm already using Chef. –  tarnfeld Jun 6 '13 at 9:50
1  
Chef will only add users if the user doesn't exist. That's what idempotency is, so I don't understand the question (1.5 years out now). –  Aaron Brown Jun 6 '13 at 13:06

useradd would not add the user again if it exists already, it intends to make sure the uid number and uid login are unique. If you are planning to run through a batch, make sure the uids being used are unique; useradd would complain for the problematic entries and you need to capture the errors/stderr to see which user accounts had problems getting into the account systems (/etc/passwd, group, shadow).

share|improve this answer

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.