Creating and managing user accounts is an essential part of system administration. But you have to have good understanding of what a user account is and how we can effectively create a new one. User account is method used by operating system to identify individual and to know which user has access to which file and services.
In this post I will cover only user creation part of user account handling. In next two post, we will learn user account modification and deletion. I am using Red Hat Enterprise Linux 6.3 (RHEL 6) though all command works with all other Linux flavors.
There are two type of users in Linux:
1. Normal users: Normal users have full access to their individual home folders and can perform tasks designated for them. They are restricted from viewing or altering other users’ files and folders.
2. Admin users: Admin users are just like any other normal user but can use or borrow (using ‘sudo’ command) rights of root user. They can perform any task by possessing powers of the root user.
Creating User Account
useradd command is used to create a new user account. To create an account for username ‘tony’ with default settings, use useradd command as follows:
# useradd tony
This will create the tony user account in the system with default user account settings. However useradd with no options/switch is not much useful.
Default ‘user account’ settings can be found in /etc/default/useradd file.
To create user account for username ‘tony’ with a display name ‘Tony Stark’:
# useradd -c 'Tony Stark' tony
Sometime you may need a user account but you don’t want that user to have shell access (Example: when creating user account for FTP/SMTP services, though these are created automatically) then you can use the command as follows:
# useradd -s /sbin/nologin -c 'Tony Stark' tony
Some important switches you should remember:
To specify primary group name/id for the user. When specifying groupID, don’t use 0-500 as groupID because these are reserved to be used by the system. Whenever a new user account is created system also create a group with same name as username and makes the user its member. This group is called primary group of the user. In following example we are giving user tony’s primary group’s groupid=1303
# useradd -g 1303 -c 'Tony Stark' tony
To make user member of supplementary groups. All other groups other than primary group are called supplementary groups of the user. In following example, we are making user tony member of three supplementary groups: personal, work and avengers
# useradd -G personal,research,avengers -c 'Tony Stark' tony
To manully specify userid for the user (not recommended, let the system choose appropriate userid). Don’t use 0-500 as userid. Following command will create user tony with userid 675.
# useradd -u 675 -c 'Tony Stark' tony
To tell the system, not to create user’s home directory. below example command will create user account for tony but won’t create home directory for the user.
# useradd -M -c 'Tony Stark' tony
To specify home directory path for the user. In general, new user’s home directory is /home/username. By using below command you can specify different home directory (/newhome/tony) for the user.
# useradd -b /newhome -c 'Tony Stark' tony
To set expiry date for the user account. By default a user account never expires. With following command, tony’s account will expire on 23-Jan-2015.
# useradd -e 23/01/2015 -c 'Tony Stark' tony
You can use ‘help’ switch to view all available option which can be used with useradd command.