Under Linux, the ownership of created or existing files and directories is associated with a specific Linux system user, group, or other (file/directory permission access types).
However, files or directories ownership verdicts are not final as it is possible to chown (Change Ownership) of any file and/or directory within the Linux operating system.
[ You might also like: Chmod Command Examples and Chgrp Command Examples ]
The chown command determines which user, group, or other will be able to Read, Write, and/or Execute system/user directory files.
The command not only applies to files and directories but also symbolic links. This command alters the permission access rights associated with files, directories, and symbolic links.
Chown Command Examples in Linux
The following is the standard syntax for using the chown command:
$ sudo chown [Command_Option] targeted_file_name
Before changing the ownership of a file, you should be a sudoer user and be able to see the already existing file ownership settings. To view the current file ownership, use the following command syntax:
$ ls -l [targeted_file_name]
For example, the file below has the following ownership and permission settings:
$ ls -l passwd
From the above screen capture, the file passwd belongs to the owner dnyce and group dnyce. Both owner and group associated with this file have Read and Write
Changing the Owner of a File in Linux
Before we change the ownership of a targeted file, we should first make sure the new owner of the file exists.
$ cat /etc/passwd
For example, to change targeted file ownership from owner dnyce to owner root, we would implement the following chown command:
$ sudo chown root passwd $ ls -l passwd
As you have noted, the ownership of file passwd changed from dnyce to root.
Changing the Group Ownership of a File in Linux
To achieve this objective, we first need to see if the targeted user group exists.
$ cat /etc/group
To change the Group ownership of the file passwd from group dnyce to another existing user group like tutorials, the needed command can be implemented in the following manner:
$ sudo chown :tutorials passwd $ ls -l passwd
As you can see, the group ownership of the above file (passwd) successfully changed from dnyce to tutorials.
Changing a File’s Owner and Group in Linux
If you are interested in changing both the file’s owner and group settings, you could implement a single command in the following manner:
$ sudo chown dnyce:dnyce passwd $ ls -l passwdv
Working with a Symbolic Link File
For some reason, the files on your Linux system might be associated with symbolic links. They have the same functionality as shortcuts on a Windows operating system. A symbolic link file is not a real file but a pointer to that file you wish to Read, Write, or Execute.
The syntax for creating a symbolic link is as follows:
$ ln -s path_to_source_file name_of_symbolic_link
Let us create a symbolic link for the passwd file.
$ ln -s passwd passwd_link
The ownership and permission settings of this symbolic link file look like the following:
$ ls -l passwd_link
The symbolic link file points to the source file. Let us try and change the ownership of the source file through the symbolic link file to the root user.
$ sudo chown root passwd_link
On re-checking the ownership and permissions settings of this symbolic link file, nothing seems to have changed.
$ ls -l passwd_link
However, the ownership and permissions settings of the source file (passwd) changed.
$ ls -l passwd
To forcefully change the ownership of a source file associated with a symbolic link file, the chown command should include the
$ sudo chown -h root passwd_link
For multiple files, your chown command should resemble the following syntax:
$ sudo chown [USER]:[GROUP] file1 file2 file3
To change file ownership from user dnyce to user root:
$ sudo chown --from=dnyce root passwd
To copy ownership settings of file passwd to file new_data.txt:
$ sudo chown --reference=passwd new_data.txt
As a Linux administrator or superuser, the chown command gives you superiority over files managed by the chmod command. You get to decide who can perform Read, Write, and Execute operations on those files.