Sometimes, a Linux system doesn’t have a desktop available to use the User Manager window. Other times, you might find it more convenient to add lots of users at once with a shell script or change user account features that are not available from the Users window. For those cases, commands are available to enable you to add and modify user accounts from the command line. The most straightforward method for creating a new user from the shell is the useradd command. After opening a Terminal window with root permission, you simply invoke useradd at the command prompt, with details of the new account as parameters.The only required parameter is the login name of the user, but you probably want to include some additional information ahead of it. Each item of account information is preceded by a single-letter option code with a dash in front of it. The options available with useradd include the following:
■ -c “comment here”—Provide a description of the new user account. Typically, this is the person’s full name. Replace comment with the name of the user account (-c Jake). Use quotes to enter multiple words (for example, –c “Jake Jackson”).
■ -d home_dir—Set the home directory to use for the account. The default is to name it the same as the login name and to place it in /home. Replace home_dir with the directory name to use (for example, -d /mnt/homes/jake).
■ -D—Rather than create a new account, save the supplied information as the new default settings for any new accounts that are created.
■ -e expire_date—Assign the expiration date for the account in YYYY-MM-DD format. Replace expire_date with a date you want to use. (For example, to expire an account on May 5, 2017, use -e 2017-05-05.)
■ -f -1—Set the number of days after a password expires until the account is permanently disabled. The default, -1, disables the option. Setting this to 0 disables the account immediately after the password has expired. Replace -1 (that’s minus one) with the number to use.
■ -g group—Set the primary group (it must already exist in the /etc/group file) the new user will be in. Replace group with the group name (for example, -g wheel). Without this option, a new group is created that is the same as the username and is used as that user’s primary group.
■ -G grouplist—Add the new user to the supplied comma-separated list of supplementary groups (for example, -G wheel,sales,tech,lunch). (If you use -G later with usermod, be sure to use -aG and not just -G. If you don’t, existing supplementary groups are removed and the groups you provide here are the only ones assigned.)
■ -k skel_dir—Set the skeleton directory containing initial configuration files and login scripts that should be copied to a new user’s home directory. This parameter can be used only in conjunction with the -m option. Replace skel_dir with the directory name to use. (Without this option, the /etc/skel directory is used.)
■ -m—Automatically create the user’s home directory and copy the files in the skeleton directory (/etc/skel) to it. (This is the default action for Fedora and RHEL, so it’s not required. It is not the default for Ubuntu.)
■ -M—Do not create the new user’s home directory, even if the default behaviour is set to create it.
■ -n—Turn off the default behaviour of creating a new group that matches the name and user ID of the new user. This option is available with Fedora and RHEL systems. Other Linux systems often assign a new user to the group named users instead.
■ -o—Use with -u uid to create a user account that has the same UID as another username. (This effectively lets you have two different usernames with authority over the same set of files and directories.)
■ -p passwd—Enter a password for the account you are adding. This must be an encrypted password. Instead of adding an encrypted password here, you can simply use the passwd user command later to add a password for user. (To generate an encrypted MD5 password, type openssl passwd.)
■ -s shell—Specify the command shell to use for this account. Replace shell with the command shell (for example, -s /bin/csh).
■ -u user_id—Specify the user ID number for the account (for example, -u 793).Without the -u option, the default behavior is to automatically assign the next available number. Replace user_id with the ID number.