Linux useradd: Creating User Accounts
The useradd command in Linux creates user accounts in the system. Its only required parameter is the user login.
If no options are provided, the useradd command will use the variables configured in the file /etc/default/useradd. The contents of this file may vary depending on each distribution:
$ cat /etc/default/useradd GROUP=100HOME=/homeINACTIVE=-1EXPIRE=SHELL=/bin/bashSKEL=/etc/skelCREATE_MAIL_SPOOL=yes
If any option is used when creating a user account with useradd, it will be used instead of the corresponding variable in /etc/default/useradd. The information in this file will only be completely ignored if all corresponding options are used when creating the user.
The most common options are:
- -c "username": This option writes to the passwd file the name of the account owner or any other important note and comment. It is an alphanumeric field and must be enclosed in double quotes;
- -d home_directory: This option provides the full path of the user's home directory;
- -m: Creates the home directory provided in the "-d" option with the files and structure defined in /etc/skel;
- -g group_number: This option provides the default group for the user account;
- -s shell: This option must provide the full path of the shell used by the account. For example /bin/bash, /bin/tcsh, /bin/false etc.
The account password should be configured after the account is created using the passwd command.
Examples:
# useradd –d /home/uira –m –c “Uirá Ribeiro” –s /bin/bash –g 100 uira
In the example, useradd creates the user uira with the home directory /home/uira with the structure defined in /etc/skel, with the name "Uirá Ribeiro" in the comments, with the shell /bin/bash and the group GID 100.
A user can be created simply with the command below, providing only the login. In this case the options from /etc/default/useradd were used to configure the user account
# useradd arthur
# cat /etc/passwd|grep arthurarthur:x:1005:100::/home/arthur:/bin/bash
Once the user account is created, the data can be manually altered by editing the /etc/passwd file, or using the usermod command.
Learn much more about Linux in our online course. You can enroll here. If you already have an account, or want to create one, just log in or create your user here.
Liked it? Share
That was just one piece. The course teaches the whole path — from your first command to exam day.