This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Linux / UNIX and the fly likes Confusion in chown  unix commands Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Engineering » Linux / UNIX
Bookmark "Confusion in chown  unix commands" Watch "Confusion in chown  unix commands" New topic
Author

Confusion in chown unix commands

Vinod Vijay
Ranch Hand

Joined: Sep 13, 2011
Posts: 137

Hi, I'm learning UNIX. I have started with the basics, introduction of unix like how and when did it came, its architecture, file systems, file attributes, file permissions, etc, etc.

I'm at chown and chgrp.
chown -> changing the ownership of a file to someone else.
chgrp -> changing the group ownership of file to any other group only to whom I belong to..

Please clear my following doubts:-
chown -> Suppose I have a file say abc.txt in my home directory, now can I change its ownership to someone else irrespective of the group he belongs to (I mean does it matter whether he belongs to one of my group or default group or not). If it doesn't matter then ofcourse when I do ls -l, it will show his id under file ownership column but what about column which shows group? Will that also get updated to the default gid or group id of that person to whom I have given the ownserhip of file abc.txt?

For instance-> at my home, i have ubuntu installed. I switched to root, created a file and then listed it (using ls -l option) which diplayed root as both owner and group. Then I gave the ownership to my user id like . Now it displayed vijay827 as file owner but group didn't change, it was still root. But don't you think that it has discrepancy now because file ownership to someone else and group ownership to someone else? If no discrepancy, does it mean that both file ownership and group ownership works independently?


Vinod Vijay Nair
Anayonkar Shivalkar
Bartender

Joined: Dec 08, 2010
Posts: 1509
    
    5

Hi,

While using chown command, if you only specify user name, then original group of file remains unchanged.

If you want to change group of file as well, then it can be done by user:group syntax.

e.g. chown vijay827:programmers abc.txt

Vinod Vijay wrote:Will that also get updated to the default gid or group id of that person to whom I have given the ownserhip of file abc.txt?

No. As I mentioned above, you'll have to explicitly provide group name/id. This is because, if you don't provide group info, and that user belongs to more than one groups, how would chown command know about to which group it should assign ownership?

I hope this helps.


Regards,
Anayonkar Shivalkar (SCJP, SCWCD, OCMJD, OCEEJBD)
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16101
    
  21

chgrp is a convenience, incidentally. The same effect can be done using only the group ID on chown. Like so:


since the format is owner:group, this changes the group while leaving the owner alone.


Customer surveys are for companies who didn't pay proper attention to begin with.
Vinod Vijay
Ranch Hand

Joined: Sep 13, 2011
Posts: 137

Anayonkar Shivalkar wrote:Hi,

While using chown command, if you only specify user name, then original group of file remains unchanged.



Could you please answer to my first question that can we give the ownership of our file to any person who is registered on UNIX system irrespective of the group he/she belongs to....... means does it matter whether that person belongs to one of my group( or there is a common group between us) or not?

can we conclude with that that both file ownership and group ownership works independently.
Anayonkar Shivalkar
Bartender

Joined: Dec 08, 2010
Posts: 1509
    
    5

Vinod Vijay wrote:Could you please answer to my first question that can we give the ownership of our file to any person who is registered on UNIX system irrespective of the group he/she belongs to

Well, the answer is in your post itself:
Here, you didn't give group name for user vijay827 (or you didn't even care to which group that user belongs), and it still worked, right?

Actually, your question is the exact reason why user need root permissions to run chown (otherwise, normal users will end up owning files which those shouldn't)

I hope this helps.
Vinod Vijay
Ranch Hand

Joined: Sep 13, 2011
Posts: 137

Anayonkar Shivalkar wrote:
Vinod Vijay wrote:Could you please answer to my first question that can we give the ownership of our file to any person who is registered on UNIX system irrespective of the group he/she belongs to

Well, the answer is in your post itself:
Here, you didn't give group name for user vijay827 (or you didn't even care to which group that user belongs), and it still worked, right?

Actually, your question is the exact reason why user need root permissions to run chown (otherwise, normal users will end up owning files which those shouldn't)

I hope this helps.


Nicely explained. But just to give you some information following statement worked because at that moment I was a root user(at my personal laptop)
chown vijay827 abc.txt

and root has super powers
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16101
    
  21

Anayonkar Shivalkar wrote:Actually, your question is the exact reason why user need root permissions to run chown (otherwise, normal users will end up owning files which those shouldn't)


That would be true, except that as I mentioned, chown can also chgrp. And it's perfectly permissible for a user to change the group to any one of several values. Hopefully, those values are only the groups that the user him/herself is a member of, though.

 
GeeCON Prague 2014
 
subject: Confusion in chown unix commands