Linux user management (How to add users?)
The useradd command is the main tool for adding new users to a Linux system. This command allows you to quickly create a new user account and configure the user's $HOME directory structure.
The useradd command creates a user account by combining system default settings and command-line parameters. To see the system default values on your Linux distribution, use the useradd command with the -D option:
traw@sysxplore:~$ useradd -D
GROUP=100
HOME=/home
INACTIVE=-1
EXPIRE=
SHELL=/bin/sh
SKEL=/etc/skel
CREATE_MAIL_SPOOL=no
Important The /etc/default/useradd file is used to set the default settings for the useradd command. In addition, additional security settings are defined in the /etc/login.defs file. You can change the default security behavior on your Linux system by editing these files.
Here are the default values that the useradd command uses if you don't specify them on the command line when creating a new user on the system:
The new user will be assigned to a group with the ID 100.
The $HOME directory /home/username will be created for the new user.
When the password expires, the account will not be terminated.
The new account will not have a set expiration date.
The bash shell will be the new account's default shell.
The contents of the /etc/skel directory will be copied to the user's $HOME directory.
The system will create a file in the mail directory for the user account to receive mail.
An administrator can create a default $HOME directory configuration using the useradd command, and the new user's $HOME directory is then created using that configuration as a template. This enables you to automatically add system default files to each new user's $HOME directory.
The files can be found in the /etc/skel directory on many Linux systems:
traw@sysxplore:~$ ls -lah /etc/skel/
drwxr-xr-x 3 root root 4.0K Dec 8 2022 .
drwxr-xr-x 141 root root 12K Dec 5 09:00 ..
-rw-r--r-- 1 root root 220 Feb 25 2020 .bash_logout
-rw-r--r-- 1 root root 3.7K Feb 25 2020 .bashrc
drwxr-xr-x 4 root root 4.0K Dec 8 2022 .config
-rw-r--r-- 1 root root 5 Mar 16 2020 .hidden
-rw-r--r-- 1 root root 87 Feb 18 2021 .inputrc
-rw-r--r-- 1 root root 807 Feb 25 2020 .profile
These are the default startup files for your login shell environment (such as zsh, bash, fsh, and so on). When a user is created, the system automatically copies these default files into their $HOME directory.
You can check this by making a new user account with the system's default settings and then checking the new user's $HOME directory.
The useradd program does not by default create a $HOME directory in many Linux distributions, but the -m command-line option instructs it to do so. That behavior may be modified in the /etc/login.defs file.
You can use the useradd command-line options to alter a default value or behavior when creating a new user. Here are some of the options you should be aware of:
-d dir_name - Specifies a name other than the login name for the HOME directory.
-m - Creates the user’s HOME directory.
-M - Disables the creation of a user's HOME directory (used if the default setting is to create one).
-N - do not create a group with the same name as the user
-p pass - encrypted password of the new account.
-U- create a group with the same name as the user.
-s SHELL - login shell of the new account.
-r - Creates a system account.
-u uid - Specifies a unique UID for the account
Feel free to check the useradd command help (useradd --help) or manual (man useradd)for more options you can use.
That's it for today's article! Thank you for taking the time to read my brief Linux thread! If you enjoyed this thread, subscribe to our newsletter for future Linux posts, which we will post weekly.