Linux-for-Sys-Ads/C2/Modifying-and-Deleting-Groups/English

From Script | Spoken-Tutorial
Revision as of 11:49, 14 February 2019 by Pravin1389 (Talk | contribs)

Jump to: navigation, search

Title of script: Modifying and Deleting Groups

Author: Antara Roy Choudhury

Keywords: groupmod, groupdel, change group name, change GID, delete group

Visual Cue Narration
Slide 1: Introduction Hello and welcome to the spoken tutorial on Modifying and Deleting Groups in Linux.
Slide 2: Learning Objectives In this tutorial we will learn about
  • groupmod command and
  • groupdel command

We will do this through some examples.

Slide 3: System requirement To record this tutorial, I am using Ubuntu Linux 16.04 OS
Slide:4 Prerequisite To practice this tutorial,
  • you should have gone through the Linux System Administration tutorials on this website and
  • you must have root access to your computer.
Press Ctrl+Alt+T Open the Terminal by pressing Ctrl, Alt and T keys simultaneously on the keyboard.
Type sudo su [Enter] First, you should login as superuser or root user.
Only narration Here onwards, please remember to press the Enter key after typing each command.
Type

grep "electronics" /etc/group

[Enter]

In the series, we had created the group electronics.

Let us check the details of the group from the /etc/group file.

Highlight GID in the output Notice that group electronics has GID 1006.
Highlight last field(list of members, it should be empty) Also, the list of members field is empty.

So, no user has group electronics as a supplementary group.

Type: cat /etc/passwd [Enter] Let’s check the /etc/passwd file to know how many users belong to the primary group electronics.
Highlight 1006 in the fourth field of the user testmod Notice that, the users testmod and amit have 1006 in the fourth field.

It means the users testmod and amit belong to the group electronics

Let me create a document testfile.txt in the user account testmod
Type su - testmod [Enter] So, login as user testmod
Highlight in the output Now if we create any document, then that document will be owned by the group electronics.Let's do that.
Type vi testfile.txt [Enter] First we’ll create a text file named testfile.txt

I am going to use vi text editor to do so.

But you can use any other text editor of your choice.

At the command prompt type vi space testfile.txt

Press i Press the I key to switch to insert mode.
Type: Hi I am user testmod Now type this text, “Hi I am user testmod
Press Esc Press the Escape key on the keyboard to exit from the insert mode.
press wq Then press colon w q

w is used to save the typed text in the text file.

And q is used to quit the editor.

Press Enter Now press Enter.
On getting command prompt We are back to the command prompt
Type ls -l testfile.txt [Enter] Now type, ls space hyphen l space testfile.txt
Highlight the third and fourth field Notice the third and fourth fields.

The document belongs to user testmod and group electronics.

Retain same screen Now let us start modifying the group with the groupmod command.
Slide 5

groupmod space [options] space groupname

  • The groupmod command is used to modify the definition of the group specified in the command.
  • This is the syntax
  • Here the options field is not mandatory.
Slide 6 groupmod command can be used to modify-
  • the group name
  • GID
  • GID to a non-unique number
[Terminal]

Type exit [Enter]

Suppose, we want to modify our group’s name from electronics to electrical.

To do so, let me switch back to the root login

Exit from the user testmod

Type groupmod -n electrical electronics [Enter] Now type groupmod space hyphen n space electrical space electronics
cat /etc/group [Enter] Then type cat space /etc/group
Highlight the entry with GID value 1006 Notice that the group with GID 1006 is now named as electrical.
Slide 7
  • You can also change the GID of a group to a new unique non-negative number.
  • Suppose, you are planning to use number 1100 as the new GID value.
  • So, first ensure, that the number 1100 is not present as GID in any other group.
Switch to terminal

Highlight GID values in group file

From the output of /etc/group 1100 is not set as a GID value to any of the group.

This type of manual checking is not a good idea, if you have a large number of groups.

Instead we can use the grep command to check if the number is present or not.

As an assignment, try this out on your own.

Type: groupmod -g 1050 electrical [Enter] Now to change GID value, type

groupmod space hyphen g space 1100 space electrical

cat /etc/group [Enter] Now check the details of the file /etc/group
Highlight 1100 for electrical Notice that GID of electrical is now 1100.
grep "testmod " /etc/passwd [Enter] We know that user testmod belongs to the group electrical.

Let’s check /etc/passwd file for the entry of the user testmod.

Highlight the fourth field Notice that the GID value in the fourth field is also updated.

It is showing the current value 1100.

Slide 8
  • The group ID has changed.
  • But the files belonging to this group will retain the old group ID value.
  • So we must change their group ID manually.

Type find /home -gid 1100 [Enter]

To find all files belonging to the group with GID 1100, type

find space slash home space hyphen gid space 1100

Show the output This may take some time.

We have not got any output.

So, it is clear that file testfile.txt has not automatically set to the group ID 1100.

Change 1100 to 1006

find /home -gid 1006 [Enter]

Recall the previous command.

Change the group ID value to 1006 that is the previous group ID.

Highlight in the output Notice, the file testfile.txt still belongs to group ID 1006.

Please note the complete path of the file testfile.txt

Type chgrp 1100 /home/newdir/testfile.txt [Enter] To change the group ID for the file testfile.txt to 1100, type

chgrp space 1100 space

Then type the full path of file testfile.txt.

The full path can be seen by the previous command that is /home/newdir/testfile.txt

chgrp command is used to change the group of the file.

On getting # prompt back The command has executed without any error.
Type find /home -gid 1100 [Enter] Recall the command to display all files belonging to group ID 1100.
Highlight the output Notice the output shows testfile.txt file.

Please note, you may have multiples file belonging to old group ID value.

Changing group ID manually one by one will be a tedious job in that case.

Try writing shell script for this.

Retain same screen Now we know how to change the GID of a group to another unique value.

Let’s move on.

cat /etc/group [Enter] Recall the command to display /etc/group.
Show the entry of the group literature_club

Type groupmod -g 1007 electrical [Enter]

Notice, literature club has GID 1007

Let us try to assign this 1007 as the new GID of electrical.

Show the output Notice, it is showing an error, GID 1007 already exists.

So, we cannot change the group ID to a non-unique value with only hyphen g option.

Slide 9
  • To change the group ID to a non-unique value,
  • you will need hyphen o option along with hyphen g option
Type groupmod -g 1007 electrical [Enter] Let me try to set the GID to 1007 now.

Recall the previous command.

Type groupmod -g 1007 -o electrical] [Enter] Now, after 1007, give space and type hyphen o
On getting command prompt back The command has executed successfully.
cat /etc/group [Enter] Next, execute the cat /etc/group command
Highlight GID of both electrical and literature_club Notice that both electrical and literature_club now have same GID 1007.
Retain same screen So, we have learnt about groupmod command.

Now let’s learn about groupdel command.

Slide 10
  • groupdel command is used by the root or the superuser to remove a group.
  • Syntax is groupdel space groupname
Type: groupdel electrical [Enter] Let me remove the group named electrical.

Type groupdel space electrical

Show the output and highlight appropriately Notice that Linux has thrown some error.

It is telling - cannot remove primary group of the user amit

So, you cannot delete electrical because it is the primary group of any existing user amit.

You must first delete the user amit and then you can remove electrical.

Let’s try this.

Type: userdel -r amit [Enter] For deleting user testmod, type

userdel space hyphen r space amit

Highlight

"userdel: testmod mail spool (/var/mail/testmod) not found"

The command may report that the user's mail spool file was not found.

This warning can be ignored.

Type: groupdel electrical [Enter] Now execute the groupdel command.
Show the output and highlight appropriately Once again we got the error.

Says, cannot remove primary group of the user testmod

Type: userdel -r testmod [Enter] Let me delete the user testmod
Highlight

"userdel: testmod mail spool (/var/mail/testmod) not found"

Once again we got the user's mail spool file was not found.
Type: groupdel electrical [Enter] Now execute the groupdel command for the group electrical.
On getting back command prompt This time we did not get any error.
cat /etc/group [Enter] Now execute the cat /etc/group command
Show the file opened We can see that there is no entry for electrical.

The group named electrical is deleted now.

Slide 11 The important point you have to keep in mind is:
  • You are not permitted to remove the primary group of any existing user.
  • You must remove the user first.
  • Then you can remove that user's primary group.
Slide 12 Now what about the files owned by the deleted group?
  • groupdel will not automatically check for files owned by the deleted group.
  • You should manually search all file systems for such files.
  • You can use the find command for this search.
  • You should ensure that no files remain owned by this deleted group.
  • You can either delete those files or change the group ownership using chgrp command.

I leave it as an exercise for you.

Type: exit [Enter] Switch to the terminal.

Exit from the root user access

This brings us to the end of this tutorial.

Let us summarize.

Slide 13: Summary In this tutorial we learnt about-
  • groupmod command to modify
    • the group name
    • GID
    • GID to a non-unique number
  • and groupdel command to delete a group
Slide 14: Assignment As an assignment:
  1. Create two new groups
    1. civil with GID 1201
    2. mechanical with GID 1202
  2. Add a user suman with primary group civil
  3. Let suman create a document test.txt and type any text of your choice.
Slide 14: Assignment (Cont.)
  1. Modify the GID of the group civil to 1203
  2. Change the group ownership of the file test.txt.
  3. Delete group civil
  4. Delete file test.txt
Slide 15: About Spoken Tutorial project The video at the following link summarises the Spoken Tutorial project.

Please download and watch it.

Slide 16: Spoken Tutorial workshops The Spoken Tutorial Project team conducts workshops using spoken tutorials and gives certificates.

For more details, please write to us.

Slide 17: Forum for specific questions: Pls post your timed queries in this Forum.
Slide 18: Acknowledgement Spoken Tutorial Project is funded by NMEICT, MHRD, Government of India.

More information on this mission is available at this link.

The script has been contributed by Antara.

And this is Praveen from IIT Bombay signing off.

Thanks for joining.

Contributors and Content Editors

Nancyvarkey, Pravin1389