Creating a user:
The syntax for creating a user in Linux is
Let’s create a user with default attributes.
- When no option is used with useradd command the options like UID, GID, home dir and shell will be assigned default.
- #useradd <username>
- #useradd student
Observe that the uid, gid, home dir, and shell is assigned automatically.
- # useradd <option> <username>
- options are
- -u user id
- -G Secondary group id
- -g primary group id
- -d home directory
- -c comment
- -s shell
The syntax is as follows to rename by a UID (user ID):
- Create a user with following attributes
- Name = student
- uid=1331
Set password to user :
- As a root user we can assign any password to any user
- The syntax for assigning a password is
- #passwd to assign password to current user ( the one with which you have logged in, if it is root then root’s password will be changed
- #passwd <user name> to assign a password to a specific user, only root can assign password to other user.
Modifying the user’s attribute
- After creating a user if we need to modify the attributes of user like changing uid, changing secondary group id or adding a comment, locking or unlocking the user account, can be done by following command
- Syntax. # usermod <options> <username>
- all the options which are used with useradd command can be used and also the following,
- -l to change login name
- -L to LOCK account
- -U to UNLOCK account
- ex. # usermod -l newname oldname (changing the name of the user)
- ex. # usermod -L newname to lock the user account
- ex. # usermod -U newname to unlock the user account
- Note: - when an account is locked it will show! (Exclamation mark) in /etc/shadow file.
Locking and unlocking a user account:
- To lock a user a/c use the following
- #usermod –L < user name>
- #usermod –L student
- Verify it in /etc/shadow file, it shows exclamation mark before user a/c or try login as student
Unlocking a user account:
- Unlock the above a/c
- #usermod –U < user name >
- #usermod –U student
- Verify it in /etc/shadow file, it shows exclamation mark before user a/c or try login as student
- Observe in both pictures that once the account is unlocked the exclamation is gone.
Password parameters:
- For any user we can set the parameters for the password, like min and max password age, password expiration warnings and a/c expiration date etc.
- To view the advanced parameters of the user, use
- #chage -l < user name>
- #chage -l student
- Last password change: When the password was change last time.
- Password expires: Password expiry date
- Password inactive: After password expiry grace period before the account gets locked.
- Account expires: Date on which the account expires.
- Minimum number of days b/w password change: once the password is changed, it cannot be changed until a min period of specified date. [0] means never.
- Max number of days b/w password change: After changing the password how long it will be valid for.
- Number of days of warning before password expires: start of warnings to change the password, no. of days before the password expires.
Changing the password parameters:
- Changing of the password parameters can be done by two ways.
- #chage <user name >
- #chage <option> <value> <username>
- Let’s see the first method and then the other.
- To set the password parameters of a user “student” to
- Min password age : 25 days
- Max password age: 30 days
- Password expiration warnings: 2 days before password expires
- Password inactive [-1]: 0 same day account is locked after password expiry.
- A/C expiration date: 2022-12-31 (dec 31st 2022)
- The second method is for, if you want to change a particular field of password aging policy
- #chage <option> <value> <username>
- The options which can be used are as follows
- -m for Min password age
- -M for Max password age
- -d for last time the password is changed.
- -W Password expiration warnings
- -I Password inactive [-1 means inactive].
- -E A/C expiration date
Deleting a User:
- To delete a user the syntax used is
- #userdel <username> it will only delete the user but home directory will be there. To delete the user with its home directory use the following command.
- #userdel –r < user name >
- #userdel –r student
- tail command is used to showing bottom of the files .
- tail -2 is mentioned show only bottom two lines.