Linux add a user using command line tools

Q. How do I add a new user using command line tools? What are command line option recommended.

A. You need to use useradd command, which is responsible for creating a new user or update default new user information

The useradd command creates a new user account using the values specified on the command line and the default values from the system. The new user account will be entered into the system files (/etc/passwd) as needed, the home directory (/home/username) will be created, and initial files copied, depending on the command line options.

Task: Add a user to the system

Syntax is as follows for useradd command:
useradd <username>

By default user account is locaked, you need to setup a new password:
passwd <username>

For example add a new user called tom and set password to jerry:
# adduser tom
# passwd tom

If you want to add a user to group read how to add a user user to group

List of common options:

  • -c comment: Add a comment for the user
  • -d home-directory: Create a new home-directory
  • -e yyyy-mm-dd: Date for the account to be disabled
  • -f days: Number of days after the password expires until the account is disabled. (If 0 is specified, the account is disabled immediately after the password expires. If -1 is specified, the account is not be disabled after the password expires.)
  • -g group-name: Primary Group name or group number for the user’s default group (group name must exist)
  • -G group-list: List of secondary additional (other than default) group names or group numbers, separated by commas, of which the user is a member (group name must exist)
  • -m: Create the home directory
  • -M: Do not create the home directory
  • -s: User’s login shell (default /bin/bash)

To read all command line option type following command:
$ man adduser

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s