Donate : Link
Medium Blog : Link
Applications : Link
chown = Change Owner
chown used to change the owner of file system files, directories.
The ownership of any file in the system may only be altered by a super-user. A user cannot give away ownership of a file, even when the user owns it. Similarly, only a member of a group can change a file’s group ID to that group.
Each user has some properties associated with them, such as a user ID and a home directory. We can add users into a group to make the process of managing users easier.
A group can have zero or more users. A specified user can be associated with a “default group”. It can also be a member of other groups on the system as well.
Ownership and Permissions: To protect and secure files and directory in Linux we use permissions to control what a user can do with a file or directory. Linux uses three types of permissions:
- Read: This permission allows the user to read files and in directories, it lets the user read directories and subdirectories stores in it.
- Write: This permission allows a user to modify and delete a file. Also it allows a user to modify its contents (create, delete and rename files in it) for the directories. Unless the execute permission is not given to directories changes does do affect them.
- Execute: The write permission on a file allows it to get executed. For example, if we have a file named php.sh so unless we don’t give it execute permission it won’t run.
Types of file Permissions:
- User: These type of file permission affect the owner of the file.
- Group: These type of file permission affect the group which owns the file. Instead of the group permissions, the user permissions will apply if the owner user is in this group.
- Other: These type of file permission affect all other users on the system.
~/codeFactory$ chown --help
Usage: chown [OPTION]... [OWNER][:[GROUP]] FILE...
or: chown [OPTION]... --reference=RFILE FILE...
Change the owner and/or group of each FILE to OWNER and/or GROUP.
With --reference, change the owner and group of each FILE to those of RFILE.
-c, --changes like verbose but report only when a change is made
-f, --silent, --quiet suppress most error messages
-v, --verbose output a diagnostic for every file processed
--dereference affect the referent of each symbolic link (this is
the default), rather than the symbolic link itself
-h, --no-dereference affect symbolic links instead of any referenced file
(useful only on systems that can change the
ownership of a symlink)
--from=CURRENT_OWNER:CURRENT_GROUP
change the owner and/or group of each file only if
its current owner and/or group match those specified
here. Either may be omitted, in which case a match
is not required for the omitted attribute
--no-preserve-root do not treat '/' specially (the default)
--preserve-root fail to operate recursively on '/'
--reference=RFILE use RFILE's owner and group rather than
specifying OWNER:GROUP values
-R, --recursive operate on files and directories recursively
The following options modify how a hierarchy is traversed when the -R
option is also specified. If more than one is specified, only the final
one takes effect.
-H if a command line argument is a symbolic link
to a directory, traverse it
-L traverse every symbolic link to a directory
encountered
-P do not traverse any symbolic links (default)
--help display this help and exit
--version output version information and exit
Owner is unchanged if missing. Group is unchanged if missing, but changed
to login group if implied by a ':' following a symbolic OWNER.
OWNER and GROUP may be numeric as well as symbolic.
Examples:
chown root /u Change the owner of /u to "root".
chown root:staff /u Likewise, but also change its group to "staff".
chown -hR root /u Change the owner of /u and subfiles to "root".
GNU coreutils online help: <https://www.gnu.org/software/coreutils/>
Report chown translation bugs to <https://translationproject.org/team/>
Full documentation at: <https://www.gnu.org/software/coreutils/chown>
or available locally via: info '(coreutils) chown invocation'
/home/codefactory# ls -l
total 4
drwxr-xr-x 1 root root 0 Aug 21 10:34 dir1
-rw-r--r-- 1 root root 3 Aug 21 10:34 file1
/home/codefactory# chown codefactory file1
/home/codefactory# ls -l
total 4
drwxr-xr-x 1 root root 0 Aug 21 10:34 dir1
-rw-r--r-- 1 codefactory root 3 Aug 21 10:34 file1
where the codefactory is another user in the system. Assume that if you are user named cf1 and you want to change ownership to root (where your current directory is cf1). use “sudo” before syntax.
sudo chown root file1
Options:
- -c: Reports when a file change is made.
/home/codefactory# ls -l
total 4
drwxr-xr-x 1 root root 0 Aug 21 10:34 dir1
-rw-r--r-- 1 root root 3 Aug 21 10:34 file1
/home/codefactory# chown -c codefactory file1
changed ownership of 'file1' from root to codefactory
/home/codefactory# ls -l
total 4
drwxr-xr-x 1 root root 0 Aug 21 10:34 dir1
-rw-r--r-- 1 codefactory root 3 Aug 21 10:34 file1
- -v: It is used to show the verbose information for every file processed.
/home/codefactory# ls -l
total 4
drwxr-xr-x 1 root root 0 Aug 21 10:34 dir1
-rw-r--r-- 1 root root 3 Aug 21 10:34 file1
/home/codefactory# chown -v codefactory file1
changed ownership of 'file1' from root to codefactory
/home/codefactory# ls -l
total 4
drwxr-xr-x 1 root root 0 Aug 21 10:34 dir1
-rw-r--r-- 1 codefactory root 3 Aug 21 10:34 file1
- -f: It supresses most of the error messages. When you are not permitted to change group permissions and shows error, this option forcefully/silently changes the ownership.
Examples:
1. To Change group ownership
/home/codefactory# ls -l
total 4
drwxr-xr-x 1 root root 0 Aug 21 11:19 dir1
-rw-r--r-- 1 root root 2 Aug 21 11:19 file1
/home/codefactory# chown -v :codefactory file1
changed ownership of 'file1' from root:root to :codefactory
/home/codefactory# ls -l
total 4
drwxr-xr-x 1 root root 0 Aug 21 11:19 dir1
-rw-r--r-- 1 root codefactory 2 Aug 21 11:19 file1
2. To change the owner as well as group
/home/codefactory# ls -l
total 4
drwxr-xr-x 1 root root 0 Aug 21 11:19 dir1
-rw-r--r-- 1 root root 2 Aug 21 11:19 file1
/home/codefactory# chown -v codefactory:cf file1
changed ownership of 'file1' from root:root to codefactory:cf
/home/codefactory# ls -l
total 4
drwxr-xr-x 1 root root 0 Aug 21 11:19 dir1
-rw-r--r-- 1 codefactory cf 2 Aug 21 11:19 file1
3. To change the owner from particular ownership only
/home/codefactory# ls -l
total 4
drwxr-xr-x 1 root root 0 Aug 21 11:19 dir1
-rw-r--r-- 1 codefactory cf 2 Aug 21 11:19 file1
/home/codefactory# chown -v --from=codefactory cf file1
changed ownership of 'file1' from codefactory to cf
/home/codefactory# ls -l
total 4
drwxr-xr-x 1 root root 0 Aug 21 11:19 dir1
-rw-r--r-- 1 cf cf 2 Aug 21 11:19 file1
4. To change group from a particular group
/home/codefactory# ls -l
total 4
drwxr-xr-x 1 root root 0 Aug 21 11:19 dir1
-rw-r--r-- 1 cf cf 2 Aug 21 11:19 file1
/home/codefactory# chown -v --from=cf :codefactory file1
changed ownership of 'file1' from cf:cf to :codefactory
/home/codefactory# ls -l
total 4
drwxr-xr-x 1 root root 0 Aug 21 11:19 dir1
-rw-r--r-- 1 cf codefactory 2 Aug 21 11:19 file1
5. To copy ownership of one file to another
/home/codefactory# ls -l
total 4
drwxr-xr-x 1 root root 0 Aug 21 11:19 dir1
-rw-r--r-- 1 cf codefactory 2 Aug 21 11:19 file1
/home/codefactory# chown -v --reference=file1 dir1
changed ownership of 'dir1' from root:root to cf:codefactory
/home/codefactory# ls -l
total 4
drwxr-xr-x 1 cf codefactory 0 Aug 21 11:19 dir1
-rw-r--r-- 1 cf codefactory 2 Aug 21 11:19 file1
6. To change ownership of multiple files
/home/codefactory# ls -l
total 4
drwxr-xr-x 1 cf codefactory 0 Aug 21 11:19 dir1
-rw-r--r-- 1 cf codefactory 2 Aug 21 11:19 file1
/home/codefactory# chown -c root:root dir1 file1
changed ownership of 'dir1' from cf:codefactory to root:root
changed ownership of 'file1' from cf:codefactory to root:root
/home/codefactory# ls -l
total 4
drwxr-xr-x 1 root root 0 Aug 21 11:19 dir1
-rw-r--r-- 1 root root 2 Aug 21 11:19 file1

