File Ownership
Tags: linux, beginners, users, groups
Published: 2021-01-20 04:58:54

In this Part, I will cover how to create and modify **Users** and **Groups**. this section will be based on hands-on learning so I'm assuming you have already gone through the previous part.

If you didn't follow me yet, go and press the follow button to get notified of my next upcoming articles. Also, don't forget to give me your precious feedback.

#<strong>Group</strong>

##Creating A Group
The <code>groupadd</code> command used to create a new group. the command requires only one parameter and that is the name of the group.

![creatingGroup](https://dev-to-uploads.s3.amazonaws.com/i/igwu9phxuozwueqt3o58.png)

*No output means the command is successfully executed.* After Creating or modifying group you can view the changes in the */etc/group* file with <code>grep</code> command or if you are working on a distributed(network-based) system then <code>getent</code> command shows you both local and network group.

![groups](https://dev-to-uploads.s3.amazonaws.com/i/gnvjb72ann43b4fiyf8x.png)

you can see the group name along with a number. this number is a unique Group ID(*GID*) which <code>groupadd</code> command itself provides, you can also specify GID with the help of <code>-g</code> option of <code>groupadd</code> command.

![Group with specific GID](https://dev-to-uploads.s3.amazonaws.com/i/cneircwasz36h2o6mf0t.png)

##Group Operations
With the help of the <code>id</code> command, you can check which user account, you are using and which groups are available to use

![id](https://dev-to-uploads.s3.amazonaws.com/i/kbseeg3mt2f8az1djj2j.png)

The output of the <code>id</code> command displays the User ID(*UID*) and user account name of the current user followed by the GID and group name of the primary group and the GIDs and group names of all group memberships.
*Notice:* Groups that we created now are not in the list because we didn't add any users yet. 

###Switch Group
The <code>id</code> commands list information along with your groups, you can also list only group name using <code>groups</code>

![Switch Group](https://dev-to-uploads.s3.amazonaws.com/i/50vtfk0gvoj9luhfr20k.png)

you can use the <code>newgrp</code> command to change your current primary group
<code>newgrp [group_name]</code>

![confirm group change](https://dev-to-uploads.s3.amazonaws.com/i/aktzrlkt0nottnz7h3os.png)

The <code>id</code> command output shows your current primary group, so it is useful for verifying that the <code>newgrp</code> command executed with no error. you can see in the second output group is changed from first id's command output. now if the user creates a file and view its details, the new file will be owned by the *sambashare* group:

![new file](https://dev-to-uploads.s3.amazonaws.com/i/uchwt408genfxp88atwy.png)

*The <code>newgrp</code> command opens a new shell; as long as the user stays in that shell, the primary group won't change. Once the user has closed the terminal the group shift back to the primary group.*

###Changing File Ownership
As we created a file in the previous command that own the *sambashare* group, we can change the group owner using <code>chgrp</code> command:

![chgrp](https://dev-to-uploads.s3.amazonaws.com/i/uok70ql3cwjlsjcm6r8r.png)

###Rename & change GID of the group
<code>groupmod</code> command uses following parameters to make to make changes in group:
• <code>-n</code> With the help of this option we can rename a group:

![rename](https://dev-to-uploads.s3.amazonaws.com/i/bowyrp34s5jzo1fts29s.png)

• <code>-g</code> option is used to change the GID of the group:

![GID change](https://dev-to-uploads.s3.amazonaws.com/i/xj60gpc5wpxkfvwi6f6i.png)

###Deleting Group
If you decide to delete a group with the <code>groupdel</code> command, remember that any files that are owned by that group will become orphaned. Only supplemental groups can be deleted, so if any group is the primary group for any user, it cannot be deleted:

![delete group](https://dev-to-uploads.s3.amazonaws.com/i/8qy8pw6sy6hwvh99830n.png) 

#<strong>User</strong>

##Creating A User
The <code>useradd</code> command used to create a new user. the command requires only one parameter that is the name of the user:

![Creating A User](https://dev-to-uploads.s3.amazonaws.com/i/xco3a061oi91l1prkcal.png)

*No output means command successfully executed.* 
When executed without any option, <code>useradd</code> creates a new user account using the default settings specified in the <code>*/etc/default/useradd*</code> file. The command adds an entry to the <code>*/etc/passwd*</code>, <code>*/etc/shadow*</code>, <code>*/etc/group*</code> and <code>*/etc/gshadow*</code> files.
Next, you have to set the password for the newly created user to login. To do that run the <code>passwd</code> command followed by the username:

![passwd](https://dev-to-uploads.s3.amazonaws.com/i/emvyp0iwahzz1wg3arwr.png)

Now you can check the home directory by listing the contents of the <code>/home</code> directory:

![listing user](https://dev-to-uploads.s3.amazonaws.com/i/dkpd6o5m38yb4dkefjvj.png)

you can also specify User Id(UID) with the help of <code>-u</code> option of <code>groupadd</code> command.

##User Operations

###Changing File Ownership
As we created a file in the group section that was owned by <code>*anonymous*</code> user and <code>*openSourceLover*</code> group:

![change file owner](https://dev-to-uploads.s3.amazonaws.com/i/qdzap2a5elio34mjuk08.png)

To change the user owner the <code>chown</code> command is used:

![chown](https://dev-to-uploads.s3.amazonaws.com/i/3xk9kisystn5v0rh7cpw.png)

The <code>chown</code> command used to change the user or group or both at the same time.
*Please refer to the manual pages by using the <code>man chown</code> command to explore in-depth*
 
###Rename User
<code>usermod</code> command is used to make changes in user with the help of <code>-l</code> option we can rename a user.

![rename user](https://dev-to-uploads.s3.amazonaws.com/i/2k5hospxl3ovp24ivjs1.png)

###Delete User
The <code>userdel</code> command is used to delete users. When you delete a user account, you also need to decide whether you want to delete the user's home directory or not. <code>-r</code> option used to delete home directory along with user.


*Hope this article will help you to create and modify user and group. See you in the next article.*

Thanks :))