Command line tip - add a user from the command line

  • August 31, 2007
  • Avatar for peter
    Peter
    Upfold

You're probably reasonably familiar with your Linux distribution's graphical method for creating new users, but if you're curious about how to create a user from the command line, this tip is for you.

It's actually possible (but just a little clunky) to manually create users by editing the /etc/passwd file. I wouldn't recommend it though, unless you really know what you are doing.

Most distributions include a slightly easier way to do it, which is a series of scripts - useradd, usermod, userdel and more. Here, I'm going to look at useradd. All of these scripts need to be run with root privileges, so either use su - first, or prefix the commands with sudo if you're on Ubuntu or a variant thereof.

At its simplest, simply give useradd the name of the desired user and they will be created with the default options for your system, or in some cases with some minimalist options.

# useradd newuser

For this reason, it's usually best to specify the parameters you want for your new user. In this example, I'll make a user for newuser. They'll have a home directory in /home/newuser, use the bash shell and we we'll leave group membership at the defaults by not specifying it.

# mkdir /home/newuser
# useradd -d /home/newuser -s /bin/bash newuser
# chown -R newuser /home/newuser

A couple things to note here. First of all, I made the home directory before adding the user, then changed the ownership of it after adding our new user. If you don't do this, the user might not be able to use their home directory for its intended purpose! Secondly note that I haven't done anything with the password yet. While you can specify the password to useradd with -p, that way it will appear in your command history which might not be desirable. It's usually better to use the passwd program after adding the user to set their password. As root:

# passwd newuser
Enter new UNIX password:
Retype new UNIX password:
passwd: password updated successfully

In this way, the password won't appear on-screen and won't show up in plaintext log files.

That should be it - your user is created and is ready for login! Of course, there are more advanced options available with useradd (if you need to mess with group membership for example), which you can get information about by running useradd without any arguments.

Calling all FOSSwire readers! We’re looking for your feedback - answer our reader survey and you could win an awesome FOSSwire t-shirt (we’ll ship anywhere on this planet).

Avatar for peter Peter Upfold

Home » Articles »