Permission Command: How to use chown command?

  • Post last modified:April 27, 2021
  • Reading time:4 mins read

In the last two articles, we have explained to you the chmod & umask command on how to use and when to use it?

Through this article, we will cover chown & how to use chown command in Linux or UNIX systems.

If you want to know about chmod and umask you read our below

File Permission: What is umask & Default umask value?

What is chown?

Chown stands for change file owner and group.

Chown command is used to change ownership of files and directories or folder in the system.

Now we will explain you what is ownership?, How you can use?

What is ownership?

In the chmod articles, we have explained to you that there are three types of file permissions

  • User Permission
  • Group Permission
  • Other Permission

So, every file or directory has a specific owner and permissions by default, or you assign them.

User Permission (u):-

The user permission means when a single person or specific person has all kinds of rights to manage a file, and In other words, we also called the owner.

Like you have created a directory or file in a Unix-like system, then you will be the owner of a specific file.

Group Permission (g):-

The group permission means when a single group of the user has specific rights to manage a file, and In other words, we also called the owning groups.

A file is created or directory which is manage by owning group of users.

Other Permission (w):-

The other permission is like other users can also manage files or directory in a UNIX-like system, we also called this type of user as others or world permission.

Like you other users created a directory or file in a UNIX-like system, all the other system users have specific rights.

Type of permission

There are three types of permission that we can set on a file, and directories are as follows:

  • read
  • write
  • execute

Read permission:- When the character is “-” then the file is not allowed to read, if the character is “r” then you can read the file.

Write permission:- When the character is “-” then the file is not allowed to do changes. If the character is “w”, you can make changes in the file.

Execute permission:– When the character is “-”, the file will not execute. If the character is “x”, then you can execute the file.

Syntax

   chown [OPTION]... [OWNER][:[GROUP]] FILE...
   chown [OPTION]... --reference=RFILE FILE...

So, now we will explain to you how to use the chown command to change the owner, group, and others with examples.

There are many options that you can use with chown to check more options type in the terminal:

$chown --help

How to use chown to change the owner of the file?

If you want to change ownership to certain users, then you have to type in the terminal like

Syntax

chown [username] [filename]

In my case file owner is shen.

ls -l this will show the owner and groups with the permissions
chown:ls -l this will show the owner and groups with the permissions

Now I’ll change owner to root using below command

$chown root nameofile.txt

Current owner of file is root previously it was shen.

ls -l after change ownership
chown: ls -l after change ownership

How to use chown to change the group of the file?

If you want to change the owner to certain groups, then you have to type in the terminal like:

Syntax

chown :[group-name] [file-name]

Now we will show In my case file group is shen.

Before change of group
chown: Before change of group

So we will change group shen to root:

$ sudo chown :root nameofile
After change of group
chown: After change of group

Curent group ownfile is root not shen.

How to use chown to change both owner and group together?

From the above examples, we have changed both owner and group to revert back to the same owner and groups.

Syntax

chown [new-owner]:[new-group] [file-name]

This is my current owner and group is root.

Before change of group and owner
chown: Before change of group and owner

Now we will change owner and group both:

$ sudo chown shen:shen nameofile
chown: After change of group and owner
chown: After change of group and owner

Conclusion

Through chown command we can change owner, groups and others.

To change groups we can use both chgrp and chown.

Whenever you want to change the owner of a file or directories, you have to use sudo before chown.

The owning group file can be change roots and the owner of groups.

Like what you read? Please share it with others.
guest
0 Comments
Inline Feedbacks
View all comments