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:

bill@slackware:~$ mkdir /home/bill/owner
bill@slackware:~$ cd /home/bill/owner/
bill@slackware:~/owner$

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

bill@slackware:~/owner$ touch ownership.txt
bill@slackware:~/owner$ ls -l
total 0
-rw-r--r-- 1 bill
users 0 2006-01-16
13:02
ownership.txt
bill@slackware:~/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.

bill@slackware:~/owner$ chown root ownership.txt
chown: changing ownership of `ownership.txt': Operation not permitted

bill@slackware:~/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.

bill@slackware:~/owner$ su -
Password:
root@slackware:~#

Now issue the following command:

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

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:

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

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

root@slackware:~# exit
logout
bill@slackware:~/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

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
By submitting this form, you accept the Mollom privacy policy.