- How to change the file owner and group in Linux?
- Syntax
- Checking the ownership
- Changing the ownership of a file
- How to Change File/Group Owner with chown Command in Linux
- How to view file permissions
- How to change file owner with chown command
- How to change the group owner with chown command
- How to change both file owner and group owner of a file
- How to recursively change file ownership
- How to change ownership using a reference file
- Chown Command: Change Owner of File in Linux
- Linux Chown Command Syntax
- How to Check Ownership of a File in Linux
- How to Change the Owner of a File
- Change the Owner of a File With UID
- Change Ownership of Multiple Linux Files
- How to Change the Group of a File
- Change the Group of a File Using GID
- Change Owner and the Group
- Change Group to a Users Login Group
- Transfer Ownership and Group Settings from One File to Another
- Check Owner and Group Before Making Changes
- Check Owner Only
- Check Group Only
- How to Recursively Change File Ownership
- Chown Command and Symbolic Links
- Display Chown Command Process Details
- Suppress Chown Command Errors
How to change the file owner and group in Linux?
To change the file owner and group, we use the chown command in the Linux operating system.
We know that Linux is a multiuser operating system so every file or directory belongs to an owner and group.
To change ownership of files or directories we use chown command in the Linux system. This command is also available in the IBM i operating system. The chgrp command is also used to change only the group ownership of the file in the Linux system.
Syntax
The general syntax of the chown command is as follows
A brief description of options available in the chown command —
Sr.No. | option & Description |
---|---|
1 | -c, —changes Gives a diagnosis for all the files that actually changed. |
2. | -f, —silent, —quite It suppresses most of the error messages. |
3 | -v, —verbose Give a diagnosis for all the processed files. |
4 | -R, —recursive It changes files and directories recursively. |
5 | —help Displays a help message and then exits. |
6 | —version It gives info about the version and then exits. |
Checking the ownership
To check the ownership of a file in the Linux system, we use the ls -l command as shown below.
Changing the ownership of a file
To change ownership of a file in the Linux system we need to administrative permission or sudo privilege.
The general syntax for changing ownership of a file is as follows:
Here, we will change the ownership of a file ‘file.txt’ Vikash to Gautam using the chown command.
First, we will check the ownership of ‘file.txt’ using the below command.
We can see that the owner of ‘file.txt’ is vikash and the group ownership of the ‘file.txt’ is vikash. To change the ownership, we will execute command as shown below.
After changing the ownership of the file, we will again check again the ownership of files to ensure that ownership is changed or not.
To check more information and available options in the chown command, we use the —help option with the chown command as follows:
Conclusion: In this article, we learned to change the ownership of files using the chown command in the Linux operating system with available options and suitable examples. To change only the group ownership of the file, we use the chgrp command in the Linux system.
Источник
How to Change File/Group Owner with chown Command in Linux
Short for change ownership, Chown command is a command-line utility that is used to change the user or group ownership of a file or directory and even links. The Linux philosophy is such that every file or directory is owned by a specific user or group with certain access rights.
Using different examples, we will try and see the various use cases of the chown command . Chown command employs quite a simple and straight forward syntax.
$ chown OPTIONS USER: GROUP file(s)
Let’s briefly flesh out the parameters:
The attribute USER refers to the username of the user that will own the file. You can specify either the username or the UID ( User ID). Meanwhile, the GROUP option indicates the name of the new group that the file will acquire after running the command. The file option represents a regular file or a directory or even a symbolic link. These are the three entities whose permissions can be altered.
A few points to note:
1) When the USER option is specified alone, ownership of the file/directory changes to that of the specified user while the group ownership remains unchanged. Here’s an example:
In the above command, user ownership of the file file1.txt changes from the current user to the user john.
2) If the USER option is proceeded by a full colon i.e. USER: and the group name is not provided, then the user takes ownership of the file but the file’s group ownership switches to the user’s login group. For example:
In this example, the user john takes ownership of the file file1.txt, but the group ownership of the file changes to john’s login group.
3) When both the user and group options are specified separated by a colon i.e USER:GROUP – without any spaces therein – the file takes ownership of the new user and the group as specified
In the above example, the file takes the user and group ownership of user john.
4) When the USER option is left out and instead the group option is preceded by the full colon :GROUP then, only the group ownership of the file changes.
How to view file permissions
To view file permissions, simply use the ls -l command followed by the file name
From the output, we can see that the file is owned by user linuxtechi which and belongs to the group linuxtechi in the 3rd and 4th columns respectively.
How to change file owner with chown command
Before changing permissions, always invoke sudo if you are not working as the root user. This gives you elevated privileges to change user and group ownership of a file.
To change file ownership, use the syntax:
$ sudo chown user filename
From the output, you can clearly see that the ownership of the file has changed from linuxtechi to user james .
Alternatively, instead of using the username, you can pass the UID of the user instead. To get the UID, view the /etc/passwd file.
From the example below, we can see that the UID of user linuxtechi is 1002
To change the file ownership back to linuxtechi user, we shall execute the command:
How to change the group owner with chown command
As earlier discussed, to change the group owner of a file, omit the user and simply prefix the group name with a full colon.
$ sudo chown :group file
For example, to change the group owner of file1.txt from linuxtechi to docker , we executed the command:
How to change both file owner and group owner of a file
If you want to change both the owner and group that a file belongs to, specify both the user and group options separated by a full colon as shown in the syntax below. Be sure that there are no spaces between the options and the colon.
$ sudo chown user:group filename
For example, the following command changes the ownership of the file file1.txt to user james and group redis as verified using the ls command.
How to recursively change file ownership
When applying permissions to directories, you might want to apply changes recursively i.e make the ownership changes to descend and apply to files and sub-directories. To achieve this, user the recursive option -R or –recursive directive.
$ sudo chown -R user:group directory
For example, the command below assigns all files and folders in the /var/www directory ownership to the www-data group.
The example below assigns ownership of the directory reports alongside all the files and folders in the directory to the user linuxtechi.
How to change ownership using a reference file
Lastly, there’s a nifty way that you can use to change ownership of a file, and that is by using a reference file. Using the chown command, you can change the user and group ownership of a file using another file as the point of reference.
The syntax is shown below:
$ chown –reference=ref_file file
Suppose you want to assign user and group ownership of file1.txt to another file file2.txt. How would you go about it? This is illustrated in the command below.
The output above confirms that file2.txt inherits the user and group ownership of file1.txt .In the command, file1.txt is the reference file.
Conclusion
Chown command is a powerful tool that is used for managing file and directory ownership. For additional information, check out the chown man pages.
Источник
Chown Command: Change Owner of File in Linux
Home » SysAdmin » Chown Command: Change Owner of File in Linux
The chown command changes user ownership of a file, directory, or link in Linux. Every file is associated with an owning user or group. It is critical to configure file and folder permissions properly.
In this tutorial, learn how to use the Linux chown command with examples provided.
- Linux or UNIX-like system
- Access to a terminal/command line
- A user with sudo privileges to change the ownership. Remember to run the commands with sudo to execute them properly.
Linux Chown Command Syntax
The basic chown command syntax consists of a few segments. The help file shows the following format:
- [OPTIONS] – the command can be used with or without additional options.
- [USER] – the username or the numeric user ID of the new owner of a file.
- [:] – use the colon when changing a group of a file.
- [GROUP] – changing the group ownership of a file is optional.
- FILE – the target file.
Superuser permissions are necessary to execute the chown command.
In this guide, we tested the command examples with the chown version 8.28 in Ubuntu 18.04.2 LTS.
To check the chown version on your machine, enter:
The output will look similar to this:
How to Check Ownership of a File in Linux
First, you need to know the original file owner or group before making ownership changes using the chown command.
To check the group or ownership of Linux files and directories in the current location, run the following command:
An example output of the ls command looks like this:
How to Change the Owner of a File
Changing the owner of a file with chown requires you to specify the new owner and the file. The format of the command is:
The following command changes the ownership of a file sample from root to the user test:
Use the same format to change the ownership for both files and directories.
Change the Owner of a File With UID
Instead of a username, you can specify a user ID to change the ownership of a file.
Make sure there is no user with the same name as the numeric UID. If there is, the chown command gives priority to the username, not the UID.
Note: To check a user’s ID, run id -u USERNAME from the terminal.
Change Ownership of Multiple Linux Files
List the target file names after the new user to change the ownership for multiple files. Use single spaces between the file names.
In the following example, root will be the new owner of files sample2 and sample3.
Combine file names and directory names to change their ownership with one command. For example:
Do not forget that the commands are case sensitive.
How to Change the Group of a File
With chown, you can change a group for a file or directory without changing the owning user. The result is the same as using the chgrp command.
Run the chown command using the colon and a group name:
The following example changes the group of the file sample3 from grouptest to group3.
List multiple names of files or directories to make bulk changes.
Change the Group of a File Using GID
Similar to UID, use a group ID (GID) instead of a group name to change the group of a file.
Change Owner and the Group
To assign a new owner of a file and change its group at the same time, run the chown command in this format:
Therefore, to set linuxuser as the new owner and group2 as the new group of the file sample2:
Remember that there are no spaces before or after the colon.
Change Group to a Users Login Group
The chown command assigns the owner’s login group to the file when no group is specified.
To do so, define a new user followed by a colon, space, and the target file:
The following example changes the group ownership to the login group of linuxuser:
Transfer Ownership and Group Settings from One File to Another
Rather than changing the ownership to a specific user, you can use the owner and a group of a reference file.
Add the —reference option to the chown command to copy the settings from one file to another:
Remember to type in the names of the files correctly to avoid the error message:
Check Owner and Group Before Making Changes
The chown command —from option lets you verify the current owner and group and then apply changes.
The chown syntax for checking both the user and group looks like this:
The example below shows we first verified the ownership and the group of the file sample3:
Then chown changed the owner to linuxuser and the group to group3.
Check Owner Only
The option —from can be used to validate only the current user of a file.
Check Group Only
Similar to the previous section, you can validate only the group of a file using the option —from .
Here is an example where we verified the current group before changing it:
Remember to use the colon for both group names to avoid error messages.
How to Recursively Change File Ownership
The chown command allows changing the ownership of all files and subdirectories within a specified directory. Add the -R option to the command to do so:
In the following example, we will recursively change the owner and the group for all files and directories in Dir1.
Chown Command and Symbolic Links
To change the owner of a symbolic link, use the -h option. Otherwise, the ownership of the linked file will be changed.
The following image shows how symbolic links behave when -h is omitted.
The owner and group of the symbolic link remain intact. Instead, the owner and the group of the file textfile changed.
To push the changes to the link, run the chown command with the -h flag:
In the following example, we changed the owner and group of a symbolic link.
Display Chown Command Process Details
By default, the terminal does not display the chown process information. To see what happens under the hood, use one of the two command line flags:
- The option –v produces the process details even when the ownership stays the same.
- The option –c displays the output information only when an owner or group of the target file changes.
For example, if we specify the current owner as a new owner of the file:
The terminal produces the following output:
Switch from -v to -c and there will be no messages in this case. This happens because there are no owner or group changes.
The information is particularly useful with the recursive chown command:
In this example, the output lists all objects affected after running the command.
Suppress Chown Command Errors
To avoid seeing potential error messages when running the chown command, use the -f option:
The example below shows the error message for a non-existent file or directory:
Adding the -f flag suppresses most error messages. However, if you specify an invalid username, the error message appears:
Now you know how to use chown command in Linux to change a file’s user and/or group ownership.
Take extra caution when changing the group or ownership of a file or directories.
Источник