LinuxMoz

Linux Stuff && Coffee

Chown Command Linux & Examples

The chown command is used to change the ownership of one or more files & directories, here is the basic syntax:

1
chown [options] newowner files

Or you can copy the ownership details from another reference file:

chown [options] –reference=filename files

newowner is either a user ID number or a username from /etc/passwd. Chown can also change the group permission like this:

1
chown username:groupname files

Chown Examples

Here are some real word examples of the chown command in action.

Change all files in a dir to the user and group apache:

1
chown apache:apache *

Change all the files in the current dir and sub dir’s to the userand group apache:

1
chown -R apache:apache *

Change the owner of the ssh authorized_keys file for the user darth:

1
chown darth:darth /home/darth/.ssh/authorized_keys

Change the owner of the WordPress installation to the apache user to allow WordPress updates to work:

1
chown -R apache:apache /var/www/html

Chown Options

-c, –changes

Prints info about the files that are changed

–dereference 

Follow symbolic links

-f, –silent, –quiet

Do not print error messages about files that cannot be changed (stops errors displaying on the console).

–from=currown:currgroup

Only changes the owner and group if they match the –from. If currown or currgroup is omitted, a match is not required.

-v,–verbose

Print information about all files that chown attempts to change, whether or not they are actually changed.

-R, –recursive

Traverse sub dirs recursively, applying changes.

-H       With -R, traverse symbolic link to a directory.

-L        With -R, traverse every sym link that leads to a dir.

-P         With -R, do not traverse any symbolic links. This is the
default option.

–reference=filename
Change owner to the owner of filename instead of specifying a new owner explicitly.
–help
Print help message and then exit.
–version
Print version information and then exit.

Comments