chown command Linux to Change owner with examples

chown Command linux is an administrator command is used to change the owner user and/or group of each given file or directory.

chown Command syntax

				
					chown [options] newowner directoryname/filename

				
			

chown options

  • -c, –changes : like verbose but report only when a change is made.
  • –reference=RFILE :use RFILE’s owner and group rather than specifying OWNER:GROUP values
  • -f, –silent, –quiet:suppress most error messages.
  • –dereference : affect the referenced file of each symbolic link rather than the symbolic link itself. This is the default.
  • -v, –verbose :output a diagnostic for every file processed.
  • –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.
  • -h, –no-dereference:affect symbolic links instead of any referenced file. This is useful only on systems that can change the ownership of a symlink.
  • –preserve-root:Do not operate recursively on ‘/’.
  • –no-preserve-root:do not treat ‘/’ (the root directory) in any special way. This is the default.
  • -R, –recursive:operate on files and directories recursively.
  • -L :traverse every symbolic link to a directory encountered.
  • -H :if a command line argument is a symbolic link to a directory, traverse it.
  • -P :do not traverse any symbolic links. This is the default.
  • –help :display this help and exit.
  • –version :output version information and exit.

chown Command Examples

Changing owner for directories recursively

The ownership of the directories and files contained in them can be changed recursively with -R option.
				
					chown -R exaccount /home/example/
				
			

chown Verbose output

The –verbose option shows all the ownership changing. It outputs the diagnostics for each file processed

				
					chown -R –verbose david /home/david/
#changed ownership of ‘/home/david/emails’ to david
#changed ownership of ‘/home/david/blogs’ to david
#changed ownership of ‘/home/david/plugins’ to david
				
			

Preserve root

To prevent changing the ownership of / directory recursively, –preserve-root is used.
				
					chown -R –preserve-root david /
#chown: changing ownership of ‘/proc/1/tasks/1/fd’: Permission denied
#chown: changing ownership of ‘/proc/1/tasks/1/fd/status’: Permission denied

				
			

chown Silent operation

Let’s consider the following scenario .As normal users cannot change the ownership of files owned by others. Then an error is displayed when a that user tries to change the ownership.

				
					chown david /etc/

				
			
chown: changing ownership of `/etc/’: Operation not permitted But if by passing -f or –silent or –quiet option, the error is not displayed.
				
					chown -f david /etc/
				
			
Share on facebook
Share on twitter
Share on linkedin
Share on telegram
Share on whatsapp

Related Posts