DEV Community

Cover image for File Ownership
Fazal ur Rehman
Fazal ur Rehman

Posted on

File Ownership

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.

Group

Creating A Group

The groupadd command used to create a new group. the command requires only one parameter and that is the name of the group.

creatingGroup

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

groups

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

Group with specific GID

Group Operations

With the help of the id command, you can check which user account, you are using and which groups are available to use

id

The output of the id 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 id commands list information along with your groups, you can also list only group name using groups

Switch Group

you can use the newgrp command to change your current primary group
newgrp [group_name]

confirm group change

The id command output shows your current primary group, so it is useful for verifying that the newgrp 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

The newgrp 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 chgrp command:

chgrp

Rename & change GID of the group

groupmod command uses following parameters to make to make changes in group:
-n With the help of this option we can rename a group:

rename

-g option is used to change the GID of the group:

GID change

Deleting Group

If you decide to delete a group with the groupdel 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

User

Creating A User

The useradd command used to create a new user. the command requires only one parameter that is the name of the user:

Creating A User

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

passwd

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

listing user

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

User Operations

Changing File Ownership

As we created a file in the group section that was owned by anonymous user and openSourceLover group:

change file owner

To change the user owner the chown command is used:

chown

The chown command used to change the user or group or both at the same time.
Please refer to the manual pages by using the man chown command to explore in-depth

Rename User

usermod command is used to make changes in user with the help of -l option we can rename a user.

rename user

Delete User

The userdel 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. -r 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 :))

Top comments (0)