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.
File ownership in Unix
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:
ubuntu$ ls -al /home/greys/example/ total 12 drwxr-xr-x 3 greys admin 4096 Feb 9 03:55 . drwxr-xr-x 13 greys greys 4096 Feb 9 03:54 .. drwxr-xr-x 2 greys admin 4096 Feb 9 03:55 dir1 -rw-r--r-- 1 greys admin 0 Feb 9 03:54 file1 -rw-r--r-- 1 greys admin 0 Feb 9 03:55 file2
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 owner of a file in Unix
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:
ubuntu$ chown nobody file1
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:
ubuntu$ id uid=1000(greys) gid=113(admin) groups=33(www-data),113(admin) ubuntu$ chown nobody file1 chown: changing ownership of `file1': Operation not permitted
But if we become root:
ubuntu$ sudo -i [sudo] password for greys: ubuntu#
… we'll have no problem changing owners for any files:
ubuntu# cd /home/greys/example ubuntu# chown nobody file1 ubuntu# ls -l file1 -rw-r--r-- 1 nobody admin 0 Feb 9 03:54 file1
Changing owner for multiple 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):
ubuntu# chown nobody file2 dir1 ubuntu# ls -al total 12 drwxr-xr-x 3 greys admin 4096 Feb 9 03:55 . drwxr-xr-x 13 greys greys 4096 Feb 9 03:54 .. drwxr-xr-x 2 nobody admin 4096 Feb 9 03:55 dir1 -rw-r--r-- 1 nobody admin 0 Feb 9 03:54 file1 -rw-r--r-- 1 nobody admin 0 Feb 9 03:55 file2
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:
ubuntu# ls -l dir1/file3 -rw-r--r-- 1 greys admin 0 Feb 9 03:55 dir1/file3
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:
ubuntu# chown -R nobody dir1 ubuntu# ls -l dir1/file3 -rw-r--r-- 1 nobody admin 0 Feb 9 03:55 dir1/file3
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:
ubuntu# chown -R greys /home/greys/example/ ubuntu# ls -l /home/greys/example/ total 4 drwxr-xr-x 2 greys admin 4096 Feb 9 03:55 dir1 -rw-r--r-- 1 greys admin 0 Feb 9 03:54 file1 -rw-r--r-- 1 greys admin 0 Feb 9 03:55 file2
Changing group ownership for a file
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:
ubuntu$ id greys uid=1000(greys) gid=1000(greys) groups=1000(greys),4(adm),20(dialout),24(cdrom),46(plugdev),114(lpadmin),115(sambashare),116(admin)
Now, if I create a new file, it will by default belong to my primary group (called greys, just like my username):
ubuntu$ touch file ubuntu$ ls -al file -rw-r--r-- 1 greys greys 0 2012-09-20 10:48 file
I can now change group ownership of this file, in this case to a group admin, which I'm also part of.
ubuntu$ chgrp admin file
and this is just to confirm that the change actualyl happened:
ubuntu$ ls -al file -rw-r--r-- 1 greys admin 0 2012-09-20 10:48 file
That's it for today, good luck with changing file owners on your Unix system!
Tim McCormack says
I understand why changing owner is only permitted for the superuser, but why would changing *group* also be locked down?
scouser73 says
This is a great tutorial, I tend to install tarballs rather than debs and this is an extremely quick way of changing permissions rather than gksudo nautilus and changinging each and every single file.
Rob says
What happens if I get the error "Changing ownership of [drive]: Operation not permitted"?
I've tried Googling, but the answers I find talk about "fstab" which I don't undderstand and as a result are reluctant to mess with.
pushparaj says
file1 owner : XYZ group :g1
when I change the ownership of a file1 to say ABC and ABC was not in group g1. will I am able to change the ownership??
Gleb Reys says
Hi Pushparaj, you'll be able to change group or user ownership for as long as you're doing this under superuser privileges. As a regular user (ABC in your example), you won't be able to change ownership, regardless if you were a member of a group g1 or not.
In short: only root (privileged user) can change ownership of files. Regular users can't change ownership, but can change access permissions.
Kevin Kohrt says
How would I pipe the output of a find command so that I only changed the ownership of particular files. Specifically, I want to change the owner of all files I owned under my old account, foouser, to now be owned by my user in my new account, baruser; but not touch any files owned by root or www or whatever other odd things may have been set up within my home directory.
dich thuat says
I want chown folder and files in sub folder??
How to ??
chown congtydichthuat drupal/*/*/*
or
>????