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 the user doesn't already exist? The user doesn't have a home folder.

  • adduser is generally preferred on Debian/Ubuntu systems. – Faheem Mitha Jan 7 '12 at 14:18
  • @FaheemMitha In automated scripts, it's probably better to use the more consistent useradd. – The Guy with The Hat Oct 15 at 20:12
up vote 59 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 
  • 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. – user13742 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
  • In a portable way : "id -u somename >/dev/null 2>&1 || useradd somename" – Tristan Jan 24 '17 at 14:34

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,
  • 3
    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

try this:

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

or even this:

useradd {user} || true
  • This saves the check since it fails safely if the user already exists. – CoverosGene Oct 1 '14 at 19:09

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).

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.