Useradd

From LQWiki
Jump to: navigation, search

The command useradd is used to create new users to your Linux system. On Debian, administrators should usually use the adduser command instead.

Using options to useradd you can specify a different placement for the users home directory for example. The option -D shows/sets the defaults for new users.

Quickstart

To add a user and set the password in a simple scenario (i.e. without home folders shared over different computers), open a console and enter:

useradd -m username
passwd username

Command line options

Specify the home directory:

-d home_dir

Create the home directory if it doesnt exit:

-m

Users Initial group:

-g initial_group

Add use to other groups:

-G group1,group2,etc

Select which shell they get:

-s shell

Default Values

To find out the default values that useradd will use, run:

$ useradd -D

Specifying the Password

The Easy Way

Use the passwd command as root. By default passwd prompts for and changes the password of the user running it, but root can specify a different username as a command line argument and change that person's password.

Example of command to change the password for user Bob:

# passwd bob

The Not So Easy Way

You can specify the password for the user using the -p option. The password must be specified with the output of the crypt function.

Command line access to crypt is somewhat rare. The following C code generates an executable that will encrypt a password for you:

#include <stdio.h>
#include <unistd.h>
// compile with:  g++ crypt.c -o crypt -lcrypt

int main(int argc, char** argv)
{
  if( argc != 2 ) {
    printf("usage: %s key-to-encrypt", argv[0]);
    return 1;
  }

  printf("%s\n", crypt(argv[1], "01"));

  return 0;
}

See also