Use the useradd command to add a new user (
man useradd):
Usage: useradd [options] USER
Options:
-b, --base-dir BASE_DIR base directory for the home directory of the new account (default "/home")
-d, --home-dir HOME_DIR home directory of the new account (default: "/{BASE_DIR}/{USER}")
-m, --create-home create the user's home directory
-G, --groups GROUPS list of supplementary groups of the new account
-s, --shell SHELL login shell of the new account (/bin/false to stop SSH logins)
-c, --comment COMMENT comment
-e, --expiredate EXPIRE_DATE expiration date of the new account
-f, --inactive INACTIVE password inactivity period of the new account
-D, --defaults print or change default useradd configuration
The command
useradd
affects two files "/etc/passwd" and "/etc/group".
Here's how data is structured in these files:
Examples:
-
Print default useradd configuration (defaults are configured in /etc/default/useradd):
$ useradd -D
GROUP=100
HOME=/home
INACTIVE=-1
EXPIRE=
SHELL=/bin/sh
SKEL=/etc/skel
CREATE_MAIL_SPOOL=no
-
Create a user named "mtitek1" with a default group named "mtitek1".
$ sudo useradd mtitek1
$ cat /etc/passwd | grep mtitek1
mtitek1:x:1003:1004::/home/mtitek1:/bin/sh
$ cat /etc/group | grep mtitek1
mtitek1:x:1004:
-
Create a user named "mtitek1" with a default group named "mtitek1".
+ create the home directory "/home/mtitek1".
+ set the "/bin/bash" as the default shell for the user.
$ sudo useradd -m -s /bin/bash mtitek1
$ cat /etc/passwd | grep mtitek1
mtitek1:x:1003:1004::/home/mtitek1:/bin/bash
$ cat /etc/group | grep mtitek1
mtitek1:x:1004:
-
Create a user named "mtitek1" with a default group named "mtitek1".
+ create the home directory "/home/mtitek1".
+ assign the user to group "group1" and "group2".
+ set the "/bin/bash" as the default shell for the user.
$ sudo useradd -m -G group1,group2 -s /bin/bash mtitek1
$ cat /etc/passwd | grep mtitek1
mtitek1:x:1003:1004::/home/mtitek1:/bin/bash
$ cat /etc/group | grep mtitek1
mtitek1:x:1004:
group1:x:1001:mtitek1
group2:x:1002:mtitek1
-
Create a user named "mtitek1" with a default group named "mtitek1".
+ set the base home directory to "/mtitek1_home_dir".
+ create the base home directory "/mtitek1_home_dir".
$ sudo useradd -m -d /mtitek1_home_dir mtitek1
$ cat /etc/passwd | grep mtitek1
mtitek1:x:1003:1004::/mtitek1_home_dir:/bin/sh
$ cat /etc/group | grep mtitek1
mtitek1:x:1004:
$ ls -al /mtitek1_home_dir
drwxr-xr-x 4 mtitek1 mtitek1 4096 Nov 27 12:51 .
drwxr-xr-x 26 root root 4096 Nov 27 12:51 ..
-rw-r--r-- 2 mtitek1 mtitek1 4096 Nov 27 12:51 .profile
-rw-r--r-- 2 mtitek1 mtitek1 4096 Nov 27 12:51 .bashrc
-rw-r--r-- 2 mtitek1 mtitek1 4096 Nov 27 12:51 .bash_logout
-
Create a user named "mtitek1" with a default group named "mtitek1".
+ set the base directory to "/user_base_dir".
+ create the base home directory "/user_base_dir/mtitek1".
$ sudo mkdir /user_base_dir
$ sudo useradd -m -b /user_base_dir mtitek1
$ cat /etc/passwd | grep mtitek1
mtitek1:x:1003:1004::/user_base_dir/mtitek1:/bin/sh
$ cat /etc/group | grep mtitek1
mtitek1:x:1004:
$ ls -al /user_base_dir
drwxr-xr-x 4 root root 4096 Nov 27 12:51 .
drwxr-xr-x 26 root root 4096 Nov 27 12:51 ..
drwxr-xr-x 2 mtitek1 mtitek1 4096 Nov 27 12:51 mtitek1
$ ls -al /user_base_dir/mtitek1
drwxr-xr-x 4 mtitek1 mtitek1 4096 Nov 27 12:51 .
drwxr-xr-x 26 root root 4096 Nov 27 12:51 ..
-rw-r--r-- 2 mtitek1 mtitek1 4096 Nov 27 12:51 .profile
-rw-r--r-- 2 mtitek1 mtitek1 4096 Nov 27 12:51 .bashrc
-rw-r--r-- 2 mtitek1 mtitek1 4096 Nov 27 12:51 .bash_logout