How To Use chmod and chown Command

How To Use chmod and chown Command

chown: to change file owner and group information.
chmod: to change file access permissions such as read, write, and access.
 

chown command

to changes the user and/or group ownership of for given file. The syntax is:

chown owner-user file
chown owner-user:owner-group file
chown owner-user:owner-group directory
chown options owner-user:owner-group file

Examples
First, list permissions for demo.txt, enter:

ls -l demo.txt

Sample outputs:

-rw-r--r-- 1 root root 0 Aug 31 05:48 demo.txt

In this example change file ownership to vivek user and list the permissions, run:

chown vivek demo.txt
 ls -l demo.txt

Sample outputs:

-rw-r--r-- 1 vivek root 0 Aug 31 05:48 demo.txt

In this next example, the owner is set to vivek followed by a colon and a group ownership is also set to vivek group, run:

chown vivek:vivek demo.txt
ls -l demo.txt

Sample outputs:

-rw-r--r-- 1 vivek vivek 0 Aug 31 05:48 demo.txt

In this example, change only the group of file. To do so, the colon and following GROUP-name ftp are given, but the owner is omitted, only the group of the files is changed:

chown :ftp demo.txt
ls -l demo.txt

Sample outputs:

-rw-r--r-- 1 vivek ftp 0 Aug 31 05:48 demo.txt

Please note that if only a colon is given, or if NEW-OWNER is empty, neither the owner nor the group is changed:

chown : demo.txt

In this example, change the owner of /foo to “root”, execute:

chown root /foo

Likewise, but also change its group to “httpd”, enter:

chown root:httpd /foo

Change the owner of /foo and subfiles to “root”, run:

chown -R root /u

Where:
-R Recursively change ownership of directories and their contents.

 

chown command

Change file owner and group. Consider following example:

$ ls -l foo.txt

Output:

-rw-r--r-- 1 vivek webgroups 8 2006-08-08 17:57 foo.txt

Change files ownership to tony user:

chown tony foo.txt
ls -l foo.txt

Output:

-rw-r--r-- 1 tony webgroups 8 2006-08-08 17:57 foo.txt

Change foo.txt group to ftpusers:

chown tony:ftpusers foo.txt
ls -l foo.txt

Output:

-rw-r--r-- 1 tony ftpuseers 8 2006-08-08 17:57 foo.txt

You can also operate on files and directories recursively using -R option.

For example setup /var/www/html ownership to user lighttpd including subdirectories:

chown -R lighttpd /var/www/html
About This Author

I'm a software engineer with many years of experience, open source enthusiast, now I'm creating and contributing to awesome PHP web projects. I love coding as much as learning, and I enjoy trying new languages and patterns. My passion revolves around (but is not limited to) back-end development.

You are not signed in. Sign in to post comments.