Thursday 17 October 2013

Linux Chown Command Examples to Change Owner and Group


The concept of owner and groups for files is fundamental to Linux. Every file is associated with an owner and a group. You can use chown and chgrp commands to change the owner or the group of a particular file or directory.

In this article, we will discuss the ‘chown’ command as it covers most part of the ‘chgrp’ command also.

1. Change the owner of a file
$ls -l filename
-rw-r--r-- 1 srinu chilukuri 0 2013-10-16 20:03 filename

$ chown root filename

$ls -l filename
-rw-r--r-- 1 root chilukuri 0 2013-10-16 20:05 filename

So we see that the owner of the file was changed from ‘srinu’ to ‘root’.


2. Change the group of a file
Through the chown command, the group (that a file belongs to) can also be changed.
$ls -l filename
-rw-r--r-- 1 srinu chilukuri 0 2013-10-16 20:03 filename

$ chown :friends filename

$ls -l filename
-rw-r--r-- 1 root friends 0 2013-10-16 20:06 filename

If you observe closely, the group of the file changed from ‘chilukuri’ to ‘friends’. So we see that by just adding a ‘:’ followed by the new group name, the group of the file can be changed.

3. Change both owner and the group
$ls -l filename
-rw-r--r-- 1 root friends 0 2013-10-16 20:03 filename

$ chown srinu:chilukuri filename

$ls -l filename
-rw-r--r-- 1 srinu chilukuri 0 2013-10-16 20:06 filename
So we see that using the syntax newOwner:newGroup, the owner as well as group can be changed in one go.

No comments:

Post a Comment