Search notes:

Shell command useradd

useradd creates a new user or updates default new user information.
It is a low level utility, an alternative is adduser.
$ useradd -s /bin/bash -m rene
-s specifies the default shell for the new user. If not specified, it takes the value of SHELL variable in /etc/default/useradd.
-m creates the new user's home directory if it does not exist.

command line options

$ useradd ---help
useradd [options] LOGIN
useradd -D
useradd -D [options]
--badnames Do not check for bad names
-b --base-dir BASE_DIR Base directory for the home directory of the new account
--btrfs-subvolume-home Use BTRFS subvolume for home directory
-c --comment COMMENT GECOS field of the new account
-d --home-dir HOME_DIR Home directory of the new account
-D --defaults Print or change default useradd configuration
-e --expiredate EXPIRE_DATE Expiration date of the new account
-f --inactive INACTIVE Password inactivity period of the new account
-g --gid GROUP Name or ID of the primary group of the new account
-G --groups GROUPS List of supplementary groups of the new account
-h --help Display this help message and exit
-k --skel SKEL_DIR Use this alternative skeleton directory
-K --key KEY=VALUE Override /etc/login.defs defaults
-l --no-log-init Do not add the user to the lastlog and faillog databases
-m --create-home Create the user's home directory
-M --no-create-home Do not create the user's home directory
-N --no-user-group Do not create a group with the same name as the user
-o --non-unique Allow to create users with duplicate (non-unique) UID
-p --password PASSWORD Encrypted password of the new account
-r --system Create a system account
-R --root CHROOT_DIR Directory to chroot into
-P --prefix PREFIX_DIR Prefix directory where are located the /etc/* files
-s --shell SHELL Login shell of the new account
-u --uid UID User ID of the new account
-U --user-group Create a group with the same name as the user
-Z --selinux-user SEUSER Use a specific SEUSER for the SELinux user mapping

Adding a user to a group (for example sudo)

usermod can be used to add an already created user to an already created group:
$ usermod -a -G sudo rene
Make sure that /etc/sudoers is configured to allow users in the sudo group to execute sudo commands.

Populating the users home directory (-k)

By default, the home directory of a new user is populated with the files from the SKEL variable in /etc/default/useradd (default: /etc/skel/*).
This directory can be overwritten with -k /path/to/directory.
In order to not create any files in the home directory, use -k /dev/null.

Not adding the user to the faillog and lastlog databases (-l)

When -l is specified, the user is not added to the faillog and lastlog databases.

Do not create /var/spool/mail/_user_

The following command prevents the creation of the /var/spool/mail/_user_ directory:
useradd -K MAIL_DIR=/dev/null
This command prints the following warning which can be ignored:
Creating mailbox file: Not a directory

Changing the user's password

After creating a user, the password for the user might be set with passwd
useradd -m rene
passwd rene
Alternatively, the following command allows to set the password in a script
echo 'rene:theSecret' | chpasswd

Defaults

Some defaults (such as if a Home directory should be created) are configured in /etc/login.defs

See also

A user is removed/deleted with userdel and modified with usermod.
Shell commands such as groupadd, passwd and chpasswd.

Index

Fatal error: Uncaught PDOException: SQLSTATE[HY000]: General error: 8 attempt to write a readonly database in /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php:78 Stack trace: #0 /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php(78): PDOStatement->execute(Array) #1 /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php(30): insert_webrequest_('/notes/Linux/sh...', 1759465263, '216.73.216.42', 'Mozilla/5.0 App...', NULL) #2 /home/httpd/vhosts/renenyffenegger.ch/httpsdocs/notes/Linux/shell/commands/useradd(143): insert_webrequest() #3 {main} thrown in /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php on line 78