File and folder permissions are an important part of Linux. Without proper access, users can be locked out of important files and can’t do much in this situation.
Well, among the many useful terminal utilities that Linux offers is chown. This command is used to change the ownership of a particular file, directory or symbolic link by a user and/or group.
In this article we will see how to use the chown command.
Also read : How to delete a directory under Linux?
The basic syntax of the chown command is as follows.
chown [Flags] User[:Group] File
The user is the username or UID of the new owner, the group is the new group name or GID, and the file is the name or path to the file. Numerical indications must be preceded by a + sign.
Here is a detailed overview of the user and group syntax.
- User – if only one user is named, then that user becomes the owner of the file. The ownership structure of the Group remains unchanged.
- User: – If the user’s name is followed by a colon (:) and no group name is specified, the user takes ownership and group ownership passes to the user group.
- User:Group – In this case the owner of the file will be the specified user and group.
- Group – Only the group to which the file belongs is changed.
There is no change in ownership if only two points are specified without a group or username. By default, the chown command returns no results and returns zero.
You can use the ls -l command to determine the owners of the file.
Normal users can now change the ownership of a file only if they own the file themselves, and only in a group of which they are a member. Users with administrator or root privileges can change the ownership of all files by users and groups.
Also read : Mint vs. Ubuntu: Comparison of Linux distributions
Here is the basic syntax we will use to change the owner of the file.
user file chock-a-block
The following command transfers ownership of File1 to the CTroot user.
To change the ownership of multiple files or folders, enter the file/folder names one at a time after a space.
chown CTrool file1 file2 dir1 dir2
You can also specify the UID of the user to whom you want to transfer ownership. However, if the user account has the same name as the UID, ownership is transferred to that user account instead. To avoid this, add a + sign to the UID.
chown +1234 file1 file2
Also read : How to use the SCP command to transfer files under Linux?
To change both the user and group of file ownership, we need to specify the new user and group, separated by a colon.
The next command would be z. B. transfer ownership of File1 to a daemon user from a daemon group.
demons chow:demons file1
Note that if you omit the group name after the colon, the ownership of the group becomes that of the owner.
daemon chown : file1
Change of group ownership in file
To change only the group property of a file, use the following syntax.
chown :daemons file1
You can also use the chgrp command.
chgrp1 daemon file
Also read : How do I rename a file under Linux?
If you don’t use the recursive flag, the chown command changes the group membership of files referenced by the symbolic link, not the symbolic link itself.
Assuming you are trying to change the owner and group of a symbolic link pointing to /var/www/file1, chown would change the owner of file1, not the symbolic link itself.
chown CTroot : symlink1
You will probably get the following error message – You cannot dereference ‘symlink1’ : Permission denied.
This error occurs because most Linux distributions protect their symbolic links by default and do not allow the user to interact with the target files. You can disable this protection, but we do not recommend you do so.
To change the group membership of the symbolic link itself, use the -h option.
chown -h CTroot symlink1
Also read : How can I check the list of Linux users?
You can work recursively with all files in a given directory by using the -R option.
Directory -R User:Group
The following example changes the user and group ownership of all files in the /etc/bin directory.
Chou-Er Ctroth: /etc/bin
If the folder contains symbolic links, the -h option can also be added.
chown -hR CTroot : /etc/bin
Two other flags that can be used when changing file owners recursively are
- -H : This allows the chown command to replace the symbolic link pointing to the directory.
- -L: tells chown to bypass all symbolic links to the directory it encounters.
Using these options is not recommended, as they may cause security risks or damage something on your system.
Also read : How do I change the password under Linux?
You can use the -reference flag to assign permissions to a file that matches the specified reference file. If the link is a symbolic link, the property of the target file is used.
chown –reference=ref_file file1
Also read : How do you stop Linux from the command line?
The one who writes/cuts/films/owns all the technology, and when he’s not around, switches to virtual machine races. You can contact Yadullah at [email protected], or follow him on Instagram or Twitter.
frequently asked questions
How to use the chown command in Linux as an example?
2012/06 ‘ Examples of chowning
How to use Chown Linux?
To change both the owner and group of a file, use the chown command followed by the new owner and group, separated by a colon ( : ) with no spaces, and the target file.
Why is the chaun command used?
The chown command is used to change the owner and group of files, directories and links. By default, the owner of the file system object is the user who created it. A group is a collection of users who have the same access rights (i.e., read, write, and execute rights) to that object.
Feedback,chown command in linux,chown command in linux with examples,chown command recursive,change owner of folder linux,chown all files in directory,sudo chown,chgrp command in linux,chown command in unix