Language and Page Formatting Options
3.4. Managing Users via Command-Line Tools
passwd. The files affected include
/etc/passwdwhich stores user accounts information and
/etc/shadow, which stores secure user account information.
3.4.1. Creating Users
useraddutility creates new users and adds them to the system. Following the short procedure below, you will create a default user account with its UID, automatically create a home directory where default user settings will be stored,
/home/username/, and set the default shell to
- Run the following command at a shell prompt as
rootsubstituting username with the name of your choice:
- By setting a password unlock the account to make it accessible. Type the password twice when the program prompts you to.
Example 3.1. Creating a User with Default Settings
~]# useradd robert ~]# passwd robert Changing password for user robert New password: Re-type new password: passwd: all authentication tokens updated successfully.
useradd robertcommand creates an account named
robert. If you run
cat /etc/passwdto view the content of the
/etc/passwdfile, you can learn more about the new user from the line displayed to you:
roberthas been assigned a UID of 502, which reflects the rule that the default UID values from 0 to 499 are typically reserved for system accounts. GID, group ID of
User Private Group, equals to UID. The home directory is set to
/home/robertand login shell to
/bin/bash. The letter
xsignals that shadow passwords are used and that the hashed password is stored in
useradd(8) man page for the whole list of options). As you can see from the basic syntax of the command, you can add one or more options:
useradd [option(s)] username
-coption to specify, for example, the full name of the user when creating them. Use
-cfollowed by a string, which adds a comment to the user:
useradd -c "string" username
Example 3.2. Specifying a User's Full Name when Creating a User
~]# useradd -c "Robert Smith" robert ~]# cat /etc/passwd robert:x:502:502:Robert Smith:/home/robert:/bin/bash
robert, sometimes called the login name, and full name Robert Smith.
/home/username/directory for the user account, set a different one instead of it. Execute the command below:
useradd -d home_directory
Example 3.3. Adding a User with non-default Home Directory
~]# useradd -d /home/dir_1 robert
robert's home directory is now not the default
-Moption. However, when such a user logs into a system that has just booted and their home directory does not exist, their login directory will be the root directory. If such a user logs into a system using the
sucommand, their login directory will be the current directory of the previous user.
useradd -M username
/homedirectory while creating a new user, make use of the
-koptions together followed by the path.
Example 3.4. Creating a User while Copying Contents to the Home Directory
/home/jane, which is the default home directory of a new user
~]# useradd -m -k /dir_1 jane
useraddcommand, this means creating an account for a certain amount of time only and disabling it at a certain date. This is a particularly useful setting as there is no security risk resulting from forgetting to delete a certain account. For this, the
-eoption is used with the specified expire_date in the YYYY-MM-DD format.
useradd -e YYYY-MM-DD username
Example 3.5. Setting the Account Expiration Date
~]# useradd -e 2015-11-05 emily
emilywill be created now and automatically disabled on 5 November, 2015.
/bin/bash, but can be changed by the
-soption to any other shell different from bash, ksh, csh, tsh, for example.
useradd -s login_shell username
Example 3.6. Adding a User with Non-default Shell
~]# useradd -s /bin/ksh robert
robertwhich has the
-roption creates a system account, which is an account for administrative use that has some, but not all, root privileges. Such accounts have a UID lower than the value of UID_MIN defined in
/etc/login.defs, typically 500 and above for ordinary users.
useradd -r username