Linux administration - File and directory ownership

Linux file and directory ownerships guide

Managing file and directory ownerships are important administration task in Linux. Setting up ownership means give security to a file or directory.

Linux has a very special ownership and permission system. Each files or directories has 2 ownership which is user and group. That means, a certain file or a directory has its owner and group responsible for it. Let's see more about these in details with examples. We will create a new directory named 'owner' in /home/bill directory. We will use mkdir command to create a new directory or folder (as you called it in Windows). Type as follows:

[email protected]:~$ mkdir /home/bill/owner
[email protected]:~$ cd /home/bill/owner/
[email protected]:~/owner$

Now create a new file named ownership.txt. The command to create a new file (simple file) is touch <filename>.

[email protected]:~/owner$ touch ownership.txt
[email protected]:~/owner$ ls -l
total 0
-rw-r--r-- 1 bill
users 0 2006-01-16
[email protected]:~/owner$

The first column (-rw-r--r--) is a permission of the subject.

The second column (1) is the number of link to the subject.

The third column (bill) is the owner of the subject.

The fourth column (users) is the group owner of the subject.

The column (0) is the size of the subject.

The next column (2006-01-16 13:02) is the date and time when the subject is last updated.

Finally, the last column (ownership.txt) is the subject.

The size of the subject ownership.txt is 0. We can edit the file with vim text editor to add some text to it. Try vim ownership.txt now. Press i to go to insert mode. Type something:

You'll have a similar screen like this.
Something Something Something Something Something

Now press Esc to go back to the vi command mode. Save what we did by entering ':w and :q to exit vim editor. Don't worry to much about vim editor now. You can learn more in Linux vi editor tutorial later if you want.

Linux chown command

Ok now, the command to change the owner of the subject is chown. Oh you can view the format and info about chown with man chown. Let's try it if you are ready.

[email protected]:~/owner$ chown root ownership.txt
chown: changing ownership of `ownership.txt': Operation not permitted

[email protected]:~/owner$

You can't change the owner of 'ownership.txt' file to root because you don't have the power. Who's the most powerful user in Linux operating system? Well, switch to root now. The command is su, means switch user or super user. Type su - to switch to root environment. Enter the root password and you are root now.

[email protected]:~/owner$ su -
[email protected]:~#

Now issue the following command:

[email protected]:~# chown root /home/bill/owner/ownership.txt
[email protected]:~# ls -l /home/bill/owner/
total 0
-rw-r--r-- 1 root users 0 2006-01-16 13:02 ownership.txt
[email protected]:~#

Now the owner of the subject is root.

Linux chgrp command

The command chgrp is used to change the group owner. The example is as follows:

[email protected]:~# chgrp root /home/bill/owner/ownership.txt
[email protected]:~# ls -l /home/bill/owner/
total 0
-rw-r--r-- 1 root root 0 2006-01-16 13:02 ownership.txt
[email protected]:~#

We have changed the owner and group of the subject. Now press Ctrl + D or type exit to exit from root.

[email protected]:~# exit
[email protected]:~/owner$

View the content again with 'vi ownership.txt'. Press'i' and insert some text. After you finished, try force save and exit with ':wq!'

You can't overwrite the file this time. Why? That's ownership ;-)

To exit from vim press ':q!'. This option is to force quit vi.

Add new comment