Open-Source Internship opportunity by OpenGenus for programmers. Apply now.
The chown (change owner) command is used to change the ownership of a file or directory. In this article we discuss commonly used chown commands for file management.
Table of contents.
- Introduction.
- Syntax.
- Groups, GID, UID.
- Listing permissions.
- Changing ownership.
- Summary.
- References.
Introduction.
The chown (change owner) command is used to change the ownership of a file or directory.
Every file in Linux is associated with an owner and a group.
Permissions, access rights are assigned to a group, an owner and others which restricts who can read, write or execute file contents.
Users will have their properties such as a user ID, name etc.
Users are placed into groups for easier user management since groups allow setting of permissions on the group level instead of the individual level.
Syntax.
The syntax is as follows,
chown [OPTION]… [OWNER][:[GROUP]] FILE…
chown [OPTION]… –reference=RFILE FILE…
Commonly used options include,
- -v, to display a diagnostic for every processed file.
- -c, to display output like verbose.
- -f or --silent or --quiet, used to suppress error messages.
- -R or --recursive for performing the operation recursively on directories and files.
- --dereference, for affecting the referent of every symbolic link.
- -h or --no-dereference, to affect symbolic links instead of a referenced file.
Groups, GID, UID.
To get all groups, we write,
groups
To get the group id and user id, we write,
id
Listing permissions.
To list a file's permissions, we write,
ls -l file.txt
Output.
-rw-r--r-- 1 root root 0 Jan 12 07:00 test
When the command is executed we see file information such as the permissions for the users, groups and others, we can also see the owner and modification or access times.
Changing ownership.
To change the owner of a file test we write as follows,
sudo chown user test
Output.
-rw-r--r-- 1 user root 0 Jan 12 07:00 test
Now user is the owner of the file test.
To change ownership of multiple files we write,
chown user file1 file2 file3
We can also change ownership using the UID from the id command,
sudo chown 767 test
The user with the ID 767 will now own the file test.
To change a file's group, we can write,
sudo chown :group1 test
The above command changes test's group name to group1.
Output.
-rw-r--r-- 1 user group1 0 Jan 12 07:00 test
We can also use the group id GID obtained from the groups command,
sudo chown :78908 test
78908 represents the group id.
To change the group for multiple files, i.e by GID we write,
sudo chown :78908 file1 file2 file3
Where 78908 is the group id that file1, file2 and file3 belong to.
We can combine changing the user and changing the group in a single command as follows.
sudo chown user:group1 test
The above command changes the user to user and groups the file in group1.
To change ownership recursively, that is change the owner of all files in a directory, we use the -R option as follows,
sudo chown -hR user:group1 test
A symbolic link is a reference to an existing physical file. To create a symbolic link for a file test we write,
ln -s test symlink
By writing,
chown user symlink
We change the ownership of test to user but not the symbolic link's ownership.
The -h option is used to change the ownership of the symbolic link as follows,
chown -h user symlink
We can also change ownership from a specific ownership by writing,
chown --from=user root test
The above command changes ownership from user to root whereby the current owner must be user only.
We can also change ownership from a specific group as follows,
chown --from=:group1 root test
THe above command changes ownership from group1.
We use the --reference=ref_file option to change user and group ownership of specified files to the specified reference file's user and owner.
chown --reference=file1 file2
The above command changes to ownership(user and group) of file1 to that of file2.
Summary.
Every file in Linux is associated with a group ownership and an owner.
Chown stands for change owner. It can only be used by the super user.
References.
- Execute man chown in a Linux terminal or chown --help command.