Command line tip - add a user from the command line

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 - http://peter.upfold.org.uk/

Peter Upfold is a technology enthusiast from the UK. Peter’s interest in Linux stems back to 2003, when curiosity got the better of him and he began using SUSE 9.0. Now he runs Linux Mint 9 on the desktop, runs a CentOS-based web server from home for his personal website and dabbles in all sorts of technology things across the Windows, Mac and open source worlds.

Home » Articles »

Discussion: Command line tip - add a user from the command line

  1. Albert (guest)

    # Posted on 31 August 2007 at 11:17 PM

    You can change this:

    mkdir /home/newuser

    useradd -d /home/newuser -s /bin/bash newuser

    chown -R newuser /home/newuser

    for:

    useradd -m -d /home/newuser -s /bin/bash newuser

    and finished!!



  2. Randall (guest)

    # Posted on 02 September 2007 at 08:21 AM

    Debian (and probably Debian-based distros like Ubuntu) have an even better command for creating users -- the 'adduser' command -- which is meant to be a much friendlier frontend to the useradd command.

    It automatically does most of the work for you, like setting home directory permissions, prompting for the password, and copying over default dot-files to the user's home directory. To create a new user with the normal Debian user configuration, all you need to do is run:

    adduser USERNAME

    (There is also a corresponding 'addgroup' frontend to the usual 'groupadd' command.)



Home » Articles » Command line tip - add a user from the command line