Linux User Management

Users

CommandDescription
adduseradd a user to the system
usermodmodify a user account
userdeldelete a user account and related files
chagechange user password expiry information
sudorun one or more commands as another user
idfind out user and group names and numeric ID’s (UID or group ID) of the current user or any other user in the server. ID 0 is preserved for the superuser root.user
ID’s ranged from 1 to 99 are preserved for system users.user
ID’s ranged from 100 to 999 are preserved for special use, but generally they are also used for system users.user
ID’s ranged from 1000 and up are preserved for ordinary users like eva, junior etc.

Add new user

$ sudo adduser eva
$ sudo useradd -c "Description" -m -g group -n adam
$ sudo passwd eva
SwitchAction
-c Adds comment to the user account
-mCreates user home directory in default location, if nonexistent
-gDefault group to assign the user
-nDoes not create a private group for the user, usually a group with username
-M Does not create a home directory
-sDefault shell other than /bin/bash
-uSpecifies UID (otherwise assigned by the system)
-GAdditional groups to assign the user to

Disable user

$ sudo passwd -l 'eva'

Delete user

$ sudo userdel -r 'eva'

Lock/unlock student

$ sudo usermod --lock eva
$ sudo usermod --unlock eva

Set the expiration date

$ sudo usermod --expire-date 2020-01-14 eva

Force user to change his password

$ sudo chage --maxdays 60 eva

Groups

CommandDescription
groupaddAdds a new group.
groupmodModifies an existing group’s attributes.
groupdelDeletes an existing group.

The information for groups is stored in /etc/group

$ tail -2 /etc/group
$ grep workers /etc/group

Create new group

$ addgroup workers

Check that a new group was added

$ getent group workers

Change the group name

$ groupmod -n workers new_workers

Change group ID

$ groupmod -g 7000 workers

Remove group

$ delgroup workers

Add existing user to a group

$ usermod -a -G workers eva

Remove user from the group

$ deluser eva workers

Change user primary group

$ usermod -g new_group eva