Different users of the operating system have the ownership and permission to ensure that files are safe and put restrictions on who can modify the contents of files. In Linux there are different users who use the system:
- Each user has some properties associated with them, such as a user ID and a home directory. We can add users to a group to facilitate the user management process.
- A group can have zero or more users. A specified user can be associated with a “default group”. You can also be a member of other groups in the system.
Ownership and permissions: To protect and secure files and directories on Linux, we use permissions to control what a user can do with a file or directory. Linux uses three types of permissions
: Read: This permission allows the user to read files and in directories
- , allows the user to read directories and subdirectories stored in it. Write: This permission allows a user to
- modify and delete a file. It also allows a user to modify their content (create, delete, and rename files in it) for directories. Unless directories are not granted execute permission, changes do affect them.
- Execute: This permission on a file allows it to run. For example, if we have a file named php.sh unless we don’t give it execute permission, it won’t run.
File permission types
: User: This type of file permission affects
- the file owner. Group: This type of file permission affects
- group that owns the file. Instead of group permissions, user permissions will apply if the owner user is in this group.
- Other: This type of file permission affects all other users on the system.
the
Note: To see the permissions we use:
The ls -l
chown command is used to change the owner or group of the file. Whenever you want to change ownership, you can use the chown command.
Syntax:
chown [OPTION]… [OWNER] [:[GROUP]] FILE… chown [OPTION]… -reference=RFILE FILE…
Example: To
change the owner of the file: chown owner_name file_name In
our case we have files
as follows: Now if I use file1.txt
In my case, to change the owner I will use the following syntax: chown master file1.txt
where The teacher is another user in the system. Suppose if you are a user named user1 and you want to change the property to root (where your current directory is user1). Use “sudo” before the syntax.
sudo chown root file1.txt
Options
:
- -c: Informs when a file change is made.
example:
chown -c master file1.txt
- -v: Used to display the detailed information of each processed file.
example:
chown -v master file1.txt
- -f: Suppresses most error messages. When you are not allowed to change group permissions and it displays an error, this option changes ownership forcefully or silently.
Examples:
- To change group ownership In our case, I’m using group1 as a group in the system. To change the property we
will use chown :group1 file1.txt
You can see that the permissions of the group changed to group1 from root, if you use the -v option it will inform you of that. We just need to add a “:” to switch groups.
To change the owner and group:
- Again taking master as user and group1 as group in the system chown master:group1
greek1
Here, greek1 is a file.
- To change the owner of private property only: Suppose we want to change the master property to root where the current owner should be master only.
chown -from=master root greek1
- To change the
group of a particular group:chown -from=:group1 root greek1
<img src
=”https://media.geeksforgeeks.org/wp-content/uploads/chown7.png”
alt=”” />
Here, greek1’s group is changed to root.
To copy the property from
- one file to another
:chown -reference=greek1 greek2
- To change ownership of multiple files
:chown master:group greek2 greek3