I’ve just been asked a question about changing the ownership of files from one Unix user to another, and thought it probably makes sense to have a quick post on it.
Just to give you a quick reminder, I’d like to confirm that every single file in Unix belongs to some user and some group. There simply isn’t a way to create a file without assigning ownership. I’ve briefly touched the topic of confirming file ownership in Unix before, so today I will simply build on that and show you how to change ownership of files.
Here’s a setup for today: I have created a temporary directory with a few files and made myself the owner of all the files:
As you can see from this listing, the owner (third field in each line) is my username – greys. The next field is a Unix group of each file’s owner – admin in my example.
Changing file ownership means only updating the association between a Unix user and a file, and nothing else. When you’re changing the owner of a file, no data contained in a file is changed.
To change the owner of a file, you need to use the chown command (easy enough to remember: CHange OWNer – chown), with the following syntax:
In this command, nobody is the username of the new owner for a list of files. In my example, the only file we’d like to change ownership for is file1.
It is important to realize that you can only change file ownership as a super-user (root). Any regular Unix user cannot change the ownership of any file, and I’d like to explain why.
Indeed, some people are surprised: if I’m the owner of a given file, why can’t I change the ownership for it? That’s because transferring the ownership will mean some other Unix user will become the owner of the file(s) in question. So changing ownership is like making a decision not only for yourself, but for the new owner of the files.This is only something a super-user – special administrative account in Unix – can do.
The same logic applies to other people not being able to become owners of your files, even if they’re willing to assume the new responsibilities of owning files. They cannot revoke your ownership, because each Unix user is only allowed to make decisions and take actions on his/her own behalf.
That’s why you will probably see an error like this if you attempt to change ownership of a file as your own regular Unix user:
But if we become root:
… we’ll have no problem changing owners for any files:
If you’re going to change owner of a few files, this can easily be done using either a full list of files or a mask.
First, here’s an example of updating ownership for a specified list of files (and as you can see, directories as well):
IMPORTANT: here’s one thing which is often forgotten: when you’re changing an owner of a directory, this DOES NOT automatically change owner of all the files which already exist in this directory. So, if we check the file3 in dir1 after the example above, we can see that even though dir1 now belongs to user nobody, file3 in it still belongs to me:
If your intention is to change ownership of all the files and directories of a certain location in your filesystem, you need to use a -R option of the chown command, which means recursive ownership change:
And just to further demonstrate this, I’m going to change owner of all the files and directories in /home/greys/example directory back to my own username, greys:
Similar to the chown command, there’s a command specifically helping you with changing not the owner (user) of a file.
IMPORANT: unlike chown command, chgrp can be used by non-privileged (regular) users of a system. So you don’t have to be root if you want to change a group ownership for some of your files, provided that you’re changing the ownership to a group you’re a member of.
For example, I’m a member of quite a few groups on one of my Ubuntu servers:
Now, if I create a new file, it will by default belong to my primary group (called greys, just like my username):
I can now change group ownership of this file, in this case to a group admin, which I’m also part of.
and this is just to confirm that the change actualyl happened:
That’s it for today, good luck with changing file owners on your Unix system!