Mastering User Management on Linux


Are you working as Linux admin? Do you create/delete users in Linux Command line? If yes, then this article is for you guys! After reading the below content, you will be able to manipulate users and group permissions in Linux system.

In the below example sai is the username.

usermod

The usermod command modifies the approach account records to reflect the alterations which can be targeted on the command line.

To get more information about usermod, use the following command –

$ usermod --help

The sample output should be like this –

-c, --comment        COMMENT new value of the GECOS field
-d, --home HOME_DIR  new home directory for the user account
-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 as new primary group
-G, --groups         GROUPS new list of supplementary GROUPS
-a, --append         append the user to the supplemental GROUPS
                     mentioned by the -G option without removing
                     him/her from other groups
.....................................................................

Usage of usermod

The usage of usermod should be like this –

$sudo usermod [options] [user Name]

Home directory for New user

To create a new home directory for the user account, use the following command –

$ sudo usermod --home /home sai

Account expiration date

To set account expiration date to EXPIRE_DATE, use the following command –

$ sudo usermod --expiredate 20017-2-24 sai

Lock the user account

To lock the user account, use the following command –

$ sudo usermod --lock sai

Unlock the user account

To unlock the user account, use the following command –

$ sudo usermod --unlock sai

Password for The user

To set the password for the user, use the following command –

$ sudo usermod --password 123456 sai

Adding the user to group

To add the user to group, use the following command –

$ sudo usermod --append --groups root,users sai

Useradd

Useradd is a low degree utility for adding users. On Debian, Admins normally use adduser to create the users on Linux system.

To get more information about user add, use the following command –

$ sudo useradd --help

The sample output should be like this –

-b, --base-dir    BASE_DIR base directory for the home directory of the new account
-c, --comment     COMMENT GECOS field of the new account
-d, --home-dir    HOME_DIR home directory of the new account
-D, --defaults    print or change default useradd configuration
-e, --expiredate  EXPIRE_DATE expiration date of the new account
-f, --inactive    INACTIVE password inactivity period of the new account
-g, --gid         GROUP name or ID of the primary group of the new
account
.....................................................................................

Usage of useradd

The usage of useradd, should be like as shown below –

$ sudo useradd [options] [user name]

Base directory for new account

To create base directory for new account, use the following command –

$ sudo useradd --base-dir /tmp sai

Home directory for new account

To create home directory of the new account, use the following command –

$ sudo useradd --home-dir /home sai

Account expiration date

To set account expiration date to EXPIRE_DATE, use the following command –

$ sudo useradd --expiredate 20017-2-24 sai

Password for the user

To set the password for the user, use the following command –

$ sudo useradd --password 123456 sai

Adding the user to group

To add the user to group, use the following command –

$ sudo usermod --append --groups root,users sai

userdel

Userdel is a low-level utility for disposing of users On Debian, Linux admins most of the time use deluser to delete a consumer on Linux.

To get the more information about userdel, use the following command –

$ userdel --help

The sample output should be like this –

-f, --force         force removal of files,
                    even if not owned by user
-h, --help          display this help message and exit
-r, --remove        remove home directory and mail spool
-R, --root          CHROOT_DIR directory to chroot into
-Z, --selinux-user  remove any SELinux user mapping for the user

Usage of userdel

The usage of userdel should be like this –

$sudo userdel [options] [user name]

Delete the user

To delete the user, use the following command –

$ sudo userdel sai

Deleting the user with files

To delete the user with files, use the following command –

$ sudo userdel --force sai

Remove the user of home directory

To remove the user of home directory, use the following command

$ sudo userdel --remove sai

Changing password expiration time

To change the time the user’s password will expire, use the following command as shown below –

$ sudo chage sai

The sample output should be like this –

Changing the aging information for sai
Enter the new value, or press ENTER for the default

   Minimum Password Age [0]: 20
   Maximum Password Age [99999]: 20
   Last Password Change (YYYY-MM-DD) [2017-02-01]:
   Password Expiration Warning [7]: 7
   Password Inactive [-1]: -1
   Account Expiration Date (YYYY-MM-DD) [-1]: 2017-02-28

Changing User full name information

To change the user full name field finger information, use the following command as shown below –

$ sudo chfn sai

The sample output should be like this –

Changing the user information for sai
Enter the new value, or press ENTER for the default
   Full Name [sai]: sairamkrishna mammahe
   Room Number []:
   Work Phone []:
   Home Phone []:
   Other []:

Changing the owner files to another user

To change the owner of file(s ) to another user.In the below command, we are changing owner file of home directory to sai(user) as shown below –

$ sudo chown sai /home

Examine Faillog

To examine faillog, use the following command as shown below –

$ sudo faillog -a

The sample output should be like this –

Login         Failures      Maximum          Latest        On

root          0              0             01/01/70    05:30:00 +0530
daemon        0              0             01/01/70    05:30:00 +0530
bin           0              0             01/01/70    05:30:00 +0530
sys           0              0             01/01/70    05:30:00 +0530
sync          0              0             01/01/70    05:30:00 +0530
games         0              0             01/01/70    05:30:00 +0530
man           0              0             01/01/70    05:30:00 +0530
lp            0              0             01/01/70    05:30:00 +0530
mail          0              0             01/01/70    05:30:00 +0530
news          0              0             01/01/70    05:30:00 +0530
uucp          0              0             01/01/70    05:30:00 +0530
.......................................................................

Create a new Group

To create a new group, use the following command as shown below –

$ sudo groupadd sai

In the above command sai is the group name.

Verifying the integrity of groupd files

To verify the integrity of group files. use the following command as shown below –

$ sudo grpck

Deleting a group

To delete a group, use the following command as shown below –

$ sudo groupdel sai

Printing the groups

To print the groups a user is in, use the following command as shown below –

$ sudo groups

The sample output should be like this –

root

Printing user ids and group ids

To print real and effective user id and group ids, use the following command as shown below –

$ sudo id

The sample output should be like this –

uid=0(root) gid=0(root) groups=0(root)

Last user logged information

To display the last users logged on and how long, use the following command as shown below –

$ sudo last

The sample output should be like this –

linux tty7 :0 Wed Feb 1 09:49 gone - no logout
reboot system boot 4.4.0-59-generic Wed Feb 1 09:48 still running
guest-er tty8 :1 Mon Jan 30 15:47 - down (00:00)
linux tty7 :0 Fri Jan 27 09:33 - down (3+06:15)
reboot system boot 4.4.0-59-generic Fri Jan 27 09:32 - 15:48 (3+06:16)
linux tty7 :0 Mon Jan 23 09:10 - down (2+08:51)
reboot system boot 4.4.0-59-generic Mon Jan 23 09:09 - 18:01 (2+08:52)
linux tty7 :0 Fri Jan 20 09:17 - crash (2+23:51)
reboot system boot 4.4.0-59-generic Fri Jan 20 09:17 - 18:01 (5+08:44)
linux tty7 :0 Wed Jan 18 14:21 - down (03:38)
...........................................................................

Failed login attempts information

To should the failed login attempts, use the following command as shown below –

$ sudo lastb

The sample output should be like this –

linux tty8 :1 Wed Jan 25 11:16 - 11:16 (00:00)
sai tty8 :1 Wed Jan 25 11:15 - 11:15 (00:00)
sai Wed Jan 25 11:15 - 11:15 (00:00)
linux tty8 :1 Wed Jan 25 11:15 - 11:15 (00:00)
sai tty8 :1 Wed Jan 25 11:15 - 11:15 (00:00)
sai tty8 :1 Wed Jan 25 11:15 - 11:15 (00:00)
sai Wed Jan 25 11:14 - 11:14 (00:00)
linux tty7 :0 Mon Jan 9 11:04 - 11:04 (00:00)

btmp begins Mon Jan 9 11:04:59 2017

Login with root

To login with root, use the following command as shown below –

$ sudo sulogin

The sample output should be like this –

Give root password for maintenance
(or press Control-D to continue):

Logged information and usage

To display users logged in and what they are doing, use the following command as shown below –

$ sudo w

The sample output should be like this –

11:45:38 up 1:57, 1 user, load average: 0.10, 0.39, 0.37
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
linux tty7 :0 09:49 1:57m 3:22 0.28s /sbin/upstart -

User information

To display the users logged in, use the following command as shown below –

$sudo who

The sample output should be like this –

linux tty7 2017-02-01 09:49 (:0)

User id information

To display the user id infromation, use the following command as shown below –

$sudo whoami

The sample output should be like this –

root

In this article, we have learnt about – Mastering user management on Linux, we will come up with more Linux based tricks and tips. Keep reading!

karthikeya Boyini
karthikeya Boyini

I love programming (: That's all I know

Updated on: 23-Jan-2020

154 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements