Previous Section  < Day Day Up >  Next Section

Recipe 8.4. Adding Users with useradd

8.4.1 Problem

You need to add new users to a Linux system.

8.4.2 Solution

Use useradd -m to create a login name, home directory, and other environment variables, and use the passwd -e command to set the new password. The account is not active until you create a password.

This is the simplest invocation. The -m flag creates a home directory and copies in the files from /etc/skel:

# useradd -m 


newusername

Under most circumstances, you should also specify the user's full name, using the -c (comments) flag. Put four commas after the user's name, to leave other parts of the comments field (office number, etc.) blank.

# useradd -m -c Grace Hopper,,,, ghopper

When adding a new user, newusername becomes the user's login name. This must be a unique name.

Next, run passwd -e. The -e flag expires the password at first login, forcing the user to change it:

# passwd -e ghopper

Enter new UNIX password:

Retype new UNIX password:

passwd: password updated successfully

The user's environment is created according to the defaults in /etc/default/useradd and /etc/skel/. You can display the useradd defaults:

# useradd -D

Any of the default values can be overridden at the command line—for example, the UID and shell:

# useradd -u 1500 -s tcsh ghopper

Or you can add to the default values—for example, adding additional group memberships:

# useradd -G users,cdrecord,dialout ghopper

8.4.3 Discussion

useradd, unlike its cousin adduser, performs splendidly in scripts (such as the mass_useradd script in Recipe 8.17).

The comments fields are also known as the GECOS data. GECOS has five comma-delimited subfields. If you're going to use the comments fields, it is best to include all four commas, even if you don't enter all the values. This pays off handsomely over the long run, especially for batch and search operations. The traditional values are full name, room number, work phone, home phone, and other (this field can be used for anything you like). Many external programs, such as mail servers, use the full name field. But you can use the other subfields any way you like. It provides a useful way to arbitrarily categorize and sort users (see Recipe Recipe 8.19).

GECOS is a dusty holdover from the very olden days; it stands for the "General Electric Comprehensive Operating System." Visit the Jargon File for the full story.

8.4.4 See Also

    Previous Section  < Day Day Up >  Next Section