Today i just got a question on how actually a useradd command works and is it possible for us to create user without useradd command. And here is my answer after some googling around š
what is useradd ?
useradd COMMAND:
useradd – Adds new user to the linux system, with specified user-name. When a new user is added then a corresponding entry is made in files /etc/passwd, /etc/group and /etc/shadow
SYNTAX:
The Syntax is
useradd [options] [username]
OPTIONS:
# useradd –help
Usage: useradd [options] LOGIN
Options:
| -b, –base-dir BASE_DIR | base directory for the new user account home directory | 
| -c, –comment COMMENT | set the GECOS field for the new user account | 
| -d, –home-dir HOME_DIR | home directory for the new user account | 
| -D, –defaults | print or save modified default useradd configuration | 
| -e, –expiredate EXPIRE_DATE | set account expiration date to EXPIRE_DATE | 
| -f, –inactive INACTIVE | set password inactive after expiration to INACTIVE | 
| -g, –gid GROUP | force use GROUP for the new user account | 
| -G, –groups GROUPS | list of supplementary groups for the new user account | 
| -h, –help | display this help message and exit | 
| -k, –skel SKEL_DIR | specify an alternative skel directory | 
| -K, –key KEY=VALUE | overrides /etc/login.defs defaults | 
| -m, –create-home | create home directory for the new user account | 
| -o, –non-unique | allow create user with duplicate (non-unique) UID | 
| -p, –password PASSWORD | use encrypted password for the new user account | 
| -r, –system | create a system account | 
| -s, –shell SHELL | the login shell for the new user account | 
| -u, –uid UID | force use the UID for the new user account | 
EXAMPLE:
- To add new user:
# useradd testThis command will add a new user with name test.
 - To add user but not allow to login in the system:
useradd -s /bin/nologin testThis command will add user hiox but not allow to login. Or you can go to the /etc/passwd and set the test usernameĀ Shell access tobe /usr/sbin/nologin or /usr/sbinfalse
Understanding fields in /etc/passwd
The /etc/passwd contains one entry per line for each user (or user account) of the system. All fields are separated by a colon (:) symbol. Total seven fields as follows.
Generally, passwd file entry looks as follows (click to enlarge image):
- Username: It is used when user logs in. It should be between 1 and 32 characters in length.
 - Password: An x character indicates that encrypted password is stored in /etc/shadow file.
 - User ID (UID): Each user must be assigned a user ID (UID). UID 0 (zero) is reserved for root and UIDs 1-99 are reserved for other predefined accounts. Further UID 100-999 are reserved by system for administrative and system accounts/groups.
 - Group ID (GID): The primary group ID (stored in /etc/group file)
 - User ID Info: The comment field. It allow you to add extra information about the users such as user’s full name, phone number etc. This field use by finger command.
 - Home directory: The absolute path to the directory the user will be in when they log in. If this directory does not exists then users directory becomes /
 - Command/shell: The absolute path of a command or shell (/bin/bash). Typically, this is a shell. Please note that it does not have to be a shell.
 
In above command: test -Is the user-name /bin/nologin -Is Shell assigned to the user  - To set expire date of the user:
useradd -e 2008-06-30 testThis command will add user test and set the expire date to 2009-06-30.
In above command: test -Is the user-name 2009-06-30 -Is date on which the user-account will be expired  - To create user without creating home directory:
useradd -M testThe above command will create user test but home directory will not be created.
 
How to add user without useradd command ?
as you probably know useradd command actualy create a user on /etc/passwd to determine the user id home directory comman shell etc, /etc/group to determine te group id and /etc/shadow for password. I will create a newtest username but since the password was encrpty MD5 i will use the previous username password on the newtest username that we will create withiut useradd command
# useradd testing # passwd testing New password: password Re-enter new password: password Password changed. # # cat /etc/shadow | grep testing testing:$1$AdHBr9EJ$VwEqlF.GprlX28oWWYprZ0:14313:0:99999:7::: # # cat /etc/passwd | grep testing testing:x:1001:1001::/home/tesing:/bin/sh # cat /etc/group | grep testing testing:x:1001: Create newtest username without adduser command # vi /etc/shadow add newtest password and save newtest:$1$AdHBr9EJ$VwEqlF.GprlX28oWWYprZ0:14313:0:99999:7::: # vi /etc/group add newtest username group and id (makesure u have use the new id number newtest:x:1002: # vi /etc/passwd add newtest username,id,home directory ,shell command newtest:x:1002:1002::/home:/bin/sh
Of course, this is my version of creating a user without useradd command. Please correct me if i am wrong
