User and Group Management Tutorial for Linux User Part-2

Posted: January 20, 2017 in LINUX
Tags: , , , , ,

I have already written an article on how to create users and group in Linux and how to manage it along with few script to add users and Groups in Linux.This tutorial is enhanced form of my previous article User and Group Management Tutorial for linux User Part-1.

How to Lock and Unlock Users in Linux.

In this article we will learn some typical and conceptual thing about customizing and managing  Users and Groups in Linux Machines like changing Users home Directory,Creating Users in Linux with Custom Home Directory,Adding a user even without any Home Directory,Creating Users with custom User ID,Creating Users with Custom Group ID,Adding a User to Multiple Groups,Creating a User with Account Expiry date,Creating a User with Custom Comments and Many more.

First Understand the file which stores Linux Users important information ( i.e /etc/passwd File).

 

This file is used to keep track of every registered users created in your linux machine.In other word you can also say that this file act as a text based database for keeping users informations like there Username,encrypted Passwords,UID,GID,Comments,Home Directory,Shell used by users.

Let’s have a Look over /etc/passwd File.

/etc/passwd

Explanation of all Seventh Column:

  • 1.User name   (Tiwary)
  • 2.Encrypted password ( Represented by x)
  • 3.User ID number  (UID=506)
  • 4.User’s group ID number  (GID=508)
  • 5.Full name of the user  (Left blank)
  • 6.User home directory  (/home/tiwary)
  • 7. Login shell   (/bin/bash)

 

1.How to Create a New User in Linux with different or Custom Home Directory.

here in this example i want to create a user “shalu” with home directory “/hacking/students/shalu” .For that i have to create  the path or folder first.Then i will create the user “shalu” with custom Home Directory.

 

[root@localhost ~]# mkdir /hacking
[root@localhost ~]# mkdir -p /hacking/students
[root@localhost ~]# useradd -d /hacking/students/shalu shalu

[root@localhost ~]# passwd -d shalu
Removing password for user shalu.
passwd: Success

[root@localhost ~]#cat /etc/passwd

shivangi:x:523:526::/vit/student:/bin/bash
ram:x:524:527::/vit/student/ram:/bin/bash
shalu:x:525:528::/hacking/students/shalu:/bin/bash
[root@localhost ~]#

2.How to Create a User in Linux with specific User ID.

here in this example lab i am going to create a User name ethicalhacking with UID 888.

[root@localhost ~]# useradd -u 888 ethicalhacking
[root@localhost ~]# cat /etc/passwd|grep ethicalhacking
ethicalhacking:x:888:888::/home/ethicalhacking:/bin/bash
[root@localhost ~]#

 

3.How to create a User in Linux with custom Group ID.

 

[root@localhost ~]# useradd -u 900 -g 509 rahul
[root@localhost ~]# cat /etc/group|grep linuxtiwary
linuxtiwary:x:509:tiwary
[root@localhost ~]# cat /etc/passwd|grep rahul
rahul:x:900:509::/home/rahul:/bin/bash

 

4.How to add a User in Linux with custom comment.

here in this example lab i have created user “vishnu” and in comment section i have inserted his full name with his location.

 

[root@localhost ~]# useradd -c "vishnu sharma from vilwada" vishnu
[root@localhost ~]# cat /etc/passwd|grep vishnu
vishnu:x:907:907:vishnu sharma from vilwada:/home/vishnu:/bin/bash
[root@localhost ~]#

 

5.How to Change the information of a Linux User using CHFN Command.

Using CHFN command here i am going to change the information of user “tiwary”.

 

[root@localhost ~]# cat /etc/passwd|grep tiwary
tiwary:x:506:508::/home/tiwary:/bin/bash

[root@localhost ~]# chfn tiwary
Changing finger information for tiwary.
Name []: satish tiwary
Office []: ETHICAL HACKING TUITION CENTER JAIPUR
Office Phone []: 9509452488
Home Phone []: 8290555541
Finger information changed.

[root@localhost ~]# cat /etc/passwd|grep tiwary
tiwary:x:506:508:satish tiwary,ETHICAL HACKING TUITION CENTER JAIPUR,9509452488,8290555541:/home/tiwary:/bin/bash
[root@localhost ~]#

 

 

6.How to change the SHELL of a Linux User using CHSH command.

here in this example i am going to change the Shell of User “rahul” from “/bin/bash” to “/bin/ksh”

 

[root@localhost ~]# cat /etc/passwd|grep rahul
rahul:x:900:509::/home/rahul:/bin/bash
[root@localhost ~]# chsh rahul
Changing shell for rahul.
New shell [/bin/bash]: /bin/ksh
Shell changed.

[root@localhost ~]# cat /etc/passwd|grep rahul
rahul:x:900:509::/home/rahul:/bin/ksh
[root@localhost ~]#

 

7.How to Add a User without Home Directory.

here now i am going to create a User in Linux named “diksha” without any home Directory.

[root@localhost ~]# useradd -M diksha

[root@localhost ~]# ls -l /home/diksa
ls: /home/diksa: No such file or directory

 

 

8.How to Add a Linux User to Multiple Group.

In this example lab I am going to add User “nutan” to Multiple Groups named “redhat”  ,”ceh”  ,”python”  and  “java”

For that we have to create groups first and then add user nutan to all that groups.

 

[root@localhost ~]# groupadd redhat
[root@localhost ~]# groupadd ceh
[root@localhost ~]# groupadd python
[root@localhost ~]# groupadd java

[root@localhost ~]# useradd -G redhat,ceh,python,java nutan


[root@localhost ~]# cat /etc/group|grep nutan
redhat:x:889:nutan
ceh:x:890:nutan
python:x:891:nutan
java:x:892:nutan
nutan:x:901:
[root@localhost ~]#

 

9.How to Create a User in Linux with Account Expiry Date

In this example lab i am going to create a user  satish  which user account will expire after 15th Feb 2017.

[root@localhost ~]# useradd -e  2017-02-15 satish
[root@localhost ~]# chage -l satish
Last password change                                    : Jan 20, 2017
Password expires                                        : never
Password inactive                                       : never
Account expires                                         : Feb 15, 2017
Minimum number of days between password change          : 0
Maximum number of days between password change          : 99999
Number of days of warning before password expires       : 7
[root@localhost ~]#

 

10.How to Add a User with Custom Login Shell

In this example lab i am going to create a user “deepit” with a custom shell “/bin/csh”

[root@localhost ~]# cat /etc/shells
/bin/sh
/bin/bash
/sbin/nologin
/bin/tcsh
/bin/csh
/bin/ksh
[root@localhost ~]# useradd -s /bin/csh deepit
[root@localhost ~]# tail -1 /etc/passwd
deepit:x:906:906::/home/deepit:/bin/csh
[root@localhost ~]#

 

Very SOON USER AND GROUP MANAGEMENT for Linux User Part-3 will arrive.

Keep visiting and if you have any suggestions feel free to inform through comment and if you like our articles share this among your freinds.

 

 

 

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s