- Changing File Ownership
- How to Change the Owner of a File
- Example—Changing the Owner of a File
- How to Change Group Ownership of a File
- Example—Changing Group Ownership of a File
- How to Change Permissions and Owners via Command Line
- Why You Need to Change Permissions and Owners in Linux
- How to Change File and Folder Permissions
- How to Use chmod Command
- Changing the Owners of Files and Folders
- Using Options with chmod and chown Commands
- Conclusion
- Ubuntu Documentation
- Understanding and Using File Permissions
- Folder/Directory Permissions
- Permissions in Action
- Changing Permissions
- 12 Linux Chown Command Examples to Change Owner and Group
- 1. Change the owner of a file
- 2. Change the group of a file
- 3. Change both owner and the group
- 4. Using chown command on symbolic link file
- 5. Using chown command to forcefully change the owner/group of symbolic file.
- 6. Change owner only if a file is owned by a particular user
- 7. Change group only if a file already belongs to a certain group
- 8. Copy the owner/group settings from one file to another
- 9. Change the owner/group of the files by traveling the directories recursively
- 10. Using chown command on a symbolic link directory
- 11. Using chown to forcefully change the owner/group of a symbolic link directory recursively
- 12. List all the changes made by the chown command
Changing File Ownership
This section describes how to change the ownership and group ownership of a file.
By default, the owner cannot use the chown command to change the owner of a file or directory. However, you can enable the owner to use the chown command by adding the following line to the system’s /etc/system file and rebooting the system.
For more information, see chown(1).
In addition, the owner can only use the chgrp command to change the group of a file to a group in which the owner belongs by default. For example, if the owner of a file only belongs to the staff and sysadm groups, the owner can only change the group of a file to staff or sysadm group.
However, you can enable the owner to change the group of a file to a group in which the owner doesn’t belong by adding the following line to the system’s /etc/system file and rebooting the system.
For more information, see chgrp(1).
Also, be aware that there can be other restrictions on changing ownership and groups on NFS-mounted file systems.
How to Change the Owner of a File
Use the following procedure to change the ownership of a file.
Become superuser or assume an equivalent role.
Change the owner of a file by using the chown command.
Specifies the user name or UID of the new owner of the file or directory.
Specifies the file or directory.
Verify that the owner of the file has changed.
Example—Changing the Owner of a File
In the following example, the ownership on myfile is changed to the user rimmer.
How to Change Group Ownership of a File
Use the following procedure to change the group ownership of a file.
Become superuser or assume an equivalent role.
Change the group owner of a file by using the chgrp command.
Specifies the group name or GID of the new group of the file or directory.
Specifies the file or directory.
Verify that the group owner of the file has changed.
Example—Changing Group Ownership of a File
In the following example, the group ownership on myfile is changed to the group scifi.
Источник
How to Change Permissions and Owners via Command Line
In this tutorial, you will learn how to change permissions and owners in Linux using chmod and chown commands. By doing so, you’ll have better management in team-based projects.
Why You Need to Change Permissions and Owners in Linux
Linux is a multi-user operating system, so more than one person can work on the same computer at the same time. What’s great, the system can be accessed locally or remotely. That’s why developers often use this OS for group projects.
In such a large environment, we need to set file permissions and ownership, so only specific users can access our data. This way, we can protect sensitive information and prevent unwanted changes from happening.
Fortunately, thanks to chmod and chown commands, it is easy to change permissions and owners in Linux. But before we begin to learn how to use them, make sure you have access to the command line. You can launch it by pressing Ctrl + Alt + T.
How to Change File and Folder Permissions
We will be using the chmod command to change file and folder permissions in Linux. But first, you need to be aware that there are three types of users who can interact with a file:
- Owner — the user who creates and owns a file or folder.
- Group — all users who are members of the same group.
- Others — all other users on the system who are neither the owner nor members of a group.
To see permissions and owners of a specific file, you can run this command:
The result will look like this:
Let’s break the output down to see what each field means:
- “-rwxrw-rw-“ — this part of the line represents the file permissions. To understand it better, we have to divide it into four groups: (–), (rwx), (rw-), and (rw-).
- The first group indicates the file type. Our example shows a hyphen, which represents a regular file. If we are inspecting a directory, the hyphen will be replaced by d.
- The three characters after the file type represent the owner’s file permissions. In this example, we can see that the owner can read (r), write (w), and execute (x) the file.
- The next three characters are the group’s file permissions. We can conclude that the group can read (r) and write (w), but cannot execute the file. This is because the last character is a hyphen instead of the letter x.
- The last group is others’ file permissions. Based on our example, this type of user cannot execute the file, but they are allowed to read and write.
- 1 – the number of hard links. A hard link is an additional name for an existing file.
- user user – the owner and group owner of the file.
- 0 – the size of the file in bytes.
- Jan 19 12:59 – the last modification date.
- myfile.txt – the name of the file/folder.
How to Use chmod Command
Let’s say we want to change Linux file permissions from -rwxrw-rw- to -rwx-r–r–. Simply enter this line:
By executing this command, the owner can read, write, and execute the file (rwx). However, group and others are only allowed to read (r–).
At this point, you might wonder why we are using a three-digit number (744) after the chmod command.
The number determines the file permissions. Read, write, and execute are represented by a numerical value:
- r (read) – 4
- w (write) – 2
- x (execute) – 1
So if you want to give all permissions (rwx) to a user, we need to add read (4), write (2), and execute (1). Therefore, rwx is equal to 7.
Meanwhile, since group and others are only allowed to read the file, we give them 4.
Remember, the owner’s permissions always come first, then followed by group and others. That’s why we enter 744.
If you don’t want to give any permission to a user, enter 0 into the corresponding spot.
Here is a list of the most common file permissions:
Common permissions for directories:
Changing the Owners of Files and Folders
To change the owner of a file and folder, we will be using the chown command. This is the basic syntax:
Let’s say we have a file named “myfile.txt.” If we want to set the owner of the file to “hostinger,” we can use this command:
However, if we want to change the group owner of the file to “clients,” we’ll enter this line instead:
Notice that we use a colon (:) before “clients” to indicate that it is a group owner.
Now, to change both the owner and group owner at the same time, the syntax would be like this:
The main rule is that the owner should come before the group owner, and they have to be separated by a colon.
Using Options with chmod and chown Commands
Option is an additional command to change the output of a command.
One of the most popular options that you can combine with chmod and chown is -R (Recursive). This Linux option allows you to change permissions or owners of all files and subdirectories inside a specific directory.
If you want to use an option, you have to place it right after the chmod/chown command.
Take a look at this example:
After you enter the above command, the owner can read, write, and execute all files and subdirectories inside the /etc/myfiles directory. The command also gives read and execute permissions to group and others.
Be extra careful with this option. Improper use of the command may cause critical failure, and it requires a great deal of work to reverse the changes.
Aside from -R, the following options are often used with chmod and chown commands:
- -f or force. The command line will ignore any errors and apply the chmod and chown commands.
- -v (verbose) option gives you diagnostics of all files that are processed by the command.
- -c (changes) is similar to the -v option. However, it will only provide information when changes were successfully made.
Conclusion
In this tutorial, you have learned how to use chmod and chown commands to change permissions and owners in Linux. We also provided the basic syntax and several useful options that you can combine with either of these commands.
To learn more about Linux command line, you can read our article on basic bash commands.
If you have any questions, feel free to comment below!
Domantas leads the content and SEO teams forward with fresh ideas and out of the box approaches. Armed with extensive SEO and marketing knowledge, he aims to spread the word of Hostinger to every corner of the world. During his free time, Domantas likes to hone his web development skills and travel to exotic places.
Источник
Ubuntu Documentation
Understanding and Using File Permissions
In Linux and Unix, everything is a file. Directories are files, files are files and devices are files. Devices are usually referred to as a node; however, they are still files. All of the files on a system have permissions that allow or prevent others from viewing, modifying or executing. If the file is of type Directory then it restricts different actions than files and device nodes. The super user «root» has the ability to access any file on the system. Each file has access restrictions with permissions, user restrictions with owner/group association. Permissions are referred to as bits.
To change or edit files that are owned by root, sudo must be used — please see RootSudo for details.
If the owner read & execute bit are on, then the permissions are:
There are three types of access restrictions:
Permission
Action
chmod option
There are also three types of user restrictions:
User
ls output
Note: The restriction type scope is not inheritable: the file owner will be unaffected by restrictions set for his group or everybody else.
Folder/Directory Permissions
Directories have directory permissions. The directory permissions restrict different actions than with files or device nodes.
Permission
Action
chmod option
(view contents, i.e. ls command)
(create or remove files from dir)
(cd into directory)
read restricts or allows viewing the directories contents, i.e. ls command
write restricts or allows creating new files or deleting files in the directory. (Caution: write access for a directory allows deleting of files in the directory even if the user does not have write permissions for the file!)
execute restricts or allows changing into the directory, i.e. cd command
» height=»16″ src=»/moin_static198/light/img/icon_cool.png» title=»Info » width=»16″/> Folders (directories) must have ‘execute’ permissions set (x or 1), or folders (directories) will NOT FUNCTION as folders (directories) and WILL DISAPPEAR from view in the file browser (Nautilus).
Permissions in Action
Using the example above we have the file «/etc/hosts» which is owned by the user root and belongs to the root group.
What are the permissions from the above /etc/hosts ls output?
Changing Permissions
The command to use when modifying permissions is chmod. There are two ways to modify permissions, with numbers or with letters. Using letters is easier to understand for most people. When modifying permissions be careful not to create security problems. Some files are configured to have very restrictive permissions to prevent unauthorized access. For example, the /etc/shadow file (file that stores all local user passwords) does not have permissions for regular users to read or otherwise access.
Источник
12 Linux Chown Command Examples to Change Owner and Group
The concept of owner and groups for files is fundamental to Linux. Every file is associated with an owner and a group. You can use chown and chgrp commands to change the owner or the group of a particular file or directory.
In this article, we will discuss the ‘chown’ command as it covers most part of the ‘chgrp’ command also.
Even if you already know this command, probably one of the examples mentioned below might be new to you.
1. Change the owner of a file
So we see that the owner of the file was changed from ‘himanshu’ to ‘root’.
2. Change the group of a file
Through the chown command, the group (that a file belongs to) can also be changed.
If you observe closely, the group of the file changed from ‘family’ to ‘friends’. So we see that by just adding a ‘:’ followed by the new group name, the group of the file can be changed.
3. Change both owner and the group
So we see that using the syntax ‘ : ’, the owner as well as group can be changed in one go.
4. Using chown command on symbolic link file
Here is a symbolic link :
So we see that the symbolic link ‘tmpfile_symlink’ links to the file ‘tmpfile’.
Lets see what happens if chown command is issued on a symbolic link:
When the chown command was issued on symbolic link to change the owner as well as the group then its the referent of the symbolic link ie ‘tmpfile’ whose owner and group got changed. This is the default behavior of the chown command. Also, there exists a flag ‘–dereference’ for the same.
5. Using chown command to forcefully change the owner/group of symbolic file.
Using flag ‘-h’, you can forcefully change the owner or group of a symbolic link as shown below.
6. Change owner only if a file is owned by a particular user
Using chown “–from” flag, you can change the owner of a file, only if that file is already owned by a particular owner.
- In the example above, we verified that the original owner/group of the file ‘tmpfile’ was root/friends.
- Next we used the ‘–from’ flag to change the owner to ‘himanshu’ but only if the existing owner is ‘guest’.
- Now, as the existing owner was not ‘guest’. So, the command failed to change the owner of the file.
- Next we tried to change the owner if the existing owner is ‘root’ (which was true) and this time command was successful and the owner was changed to ‘himanshu’.
On a related note, if you want to change the permission of a file, you should use chmod command.
If you are a beginner, you should start by reading the basics of file permissions.
7. Change group only if a file already belongs to a certain group
Here also the flag ‘–from’ is used but in the following way:
Since the file ‘tmpfile’ actually belonged to group ‘friends’ so the condition was correct and the command was successful.
So we see that by using the flag ‘–from=: ’ we can change the group under a particular condition.
NOTE: By following the template ‘–from= : ’, condition on both the owner and group can be applied.
8. Copy the owner/group settings from one file to another
This is possible by using the ‘–reference’ flag.
In the above example, we first checked the owner/group of the reference-file ‘file’ and then checked the owner/group of the target-file ‘tmpfile’. Both were different. Then we used the chown command with the ‘–reference’ option to apply the owner/group settings from the reference file to the target file. The command was successful and the owner/group settings of ‘tmpfile’ were made similar to the ‘file’.
9. Change the owner/group of the files by traveling the directories recursively
This is made possible by the ‘-R’ option.
So we see that after checking the owner/group of all the files in the directory ‘linux’ and its two sub-directories ‘ubuntu’ and ‘redhat’. We issued the chown command with the ‘-R’ option to change both the owner and group. The command was successful and owner/group of all the files was changed successfully.
10. Using chown command on a symbolic link directory
Lets see what happens if we issue the ‘chown’ command to recursively change the owner/group of files in a directory that is a symbolic link to some other directory.
Here is a symbolic link directory ‘linux_symlnk’ that links to the directory ‘linux’ (already used in example ‘9’ above) :
Now, lets change the owner (from himanshu to root) of this symbolic link directory recursively :
In the ouput above we see that the owner of the files and directories was not changed. This is because by default the ‘chown’ command cannot traverse a symbolic link. This is the default behavior but there is also a flag ‘-P’ for this.
11. Using chown to forcefully change the owner/group of a symbolic link directory recursively
This can be achieved by using the flag -H
So we see that by using the -H flag, the owner/group of all the files/folder were changed.
12. List all the changes made by the chown command
Use the verbose option -v, which will display whether the ownership of the file was changed or retained as shown below.
Источник