How to create users in Linux with the generical useradd command

The useradd command is generical for all the Linux distros:

Syntax: useradd [options] username

Creating users with the default configuration:

$ sudo useradd yoda
$ grep yoda /etc/passwd
yoda:x:1010:1012::/home/yoda:/bin/sh

To view the default options for creating users, use useradd -D . (the displayed output is for a debian based Linux)

$ useradd -D
GROUP=100
HOME=/home
INACTIVE=-1
EXPIRE=
SHELL=/bin/sh
SKEL=/etc/skel
CREATE_MAIL_SPOOL=no

This options differ from distro to distro. For example, the default login shell in Debian is /bin/sh and in Fedora is bash (/bin/bash).

To modify this options, you have to edit the /etc/default/useradd file.

Creating users with custom homedir and login shell with useradd -m -d /home/dir -s /bin/bash user:

$ sudo useradd -m -d /home/starwars -s /bin/bash anakin
$ grep anakin /etc/passwd
anakin:x:1006:1008::/home/starwars:/bin/bash

-m is for creating the homedir if it doesn’t exist and copy the data from the /etc/skel folder in the freshly new created dir. if the folder already exists, nothing will be copied from the /etc/skel dir. -d is for setting the home directory and -s is for choosing the login shell.

By default, the new user gets assigned the first UID available (not used) and added to a group.

Next the root needs to set up the new users password:
$ sudo passwd username
Enter new UNIX password:
Retype new UNIX password:
passwd: password updated successfully

Now you are able to create users in whatever Linux distro you want.

The Debian based distos have adduser, a user-friendly interactive tool for creating users and newusers, a command for creating a list of users simultaneously.
Read more about adding users in Debian based distros here.

Share and Enjoy

  • Twitter
  • Facebook
  • Google Plus
  • Email
  • RSS
Tagged with: , , , ,
Posted in The Linux and Unix Articles!
One comment on “How to create users in Linux with the generical useradd command
  1. Mika says:

    Thanks. With your help I was able to shrink my new user creation process into just one command! Just put everything ready in the /ets/skel folder.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

Subscribe

  

Subscribe to get the latest Linux news and how to guides directly on your e-mail!