Search the web with Google

Android install

Android installation weblog


Enter your email address:


Jeroen´s weblog

Ubuntu install

Ubuntu installation weblog


Thursday, August 5, 2010

How To Change Ownership of Files and Directories in Unix

On unix tutorial I found this post:

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

Linux change or rename user name and UID (user-id)

Use the usermod command to change user name under Linux. it modifies the system account files to reflect the changes that are specified on the command line. Syntax is as follows
usermod -l login-name old-name
Where,
=> The name of the user will be changed from old-name to login_name. Nothing else is changed. In particular, the user's home directory name should probably be changed to
reflect the new login name.
usermod -u UID username
Where,
=> The numerical value of the user's ID (UID) . This value must be unique, unless the -o option is used. The value must be non-negative. Values between 0 and 99 are typically reserved for system accounts. Any files which the user owns and which are located in the directory tree rooted at the user's home directory will have the file user ID changed automatically. Files outside of the user's home directory must be altered
manually.

Task: Change username from john to harry

Type usermod command as follows:
# id john
# usermod -l harry john
# id harry
# id john

Task: Change user tom UID from 1001 to 1002

Type usermod command as follows
# id john
# usermod -u 1002 john
# id john

Read man page of usermod for more information.

No comments:

Post a Comment