<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>33dots &#187; access rights</title>
	<atom:link href="http://www.33dots.com/index.php/tag/access-rights/feed" rel="self" type="application/rss+xml" />
	<link>http://www.33dots.com</link>
	<description></description>
	<lastBuildDate>Wed, 12 Oct 2011 17:07:54 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.3</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Revisiting File Access Modes in Linux</title>
		<link>http://www.33dots.com/index.php/linux/revisiting-file-access-modes-in-linux.html</link>
		<comments>http://www.33dots.com/index.php/linux/revisiting-file-access-modes-in-linux.html#comments</comments>
		<pubDate>Tue, 04 Aug 2009 14:26:36 +0000</pubDate>
		<dc:creator>tony</dc:creator>
				<category><![CDATA[Cent OS]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[access rights]]></category>
		<category><![CDATA[chmod]]></category>
		<category><![CDATA[file access modes]]></category>
		<category><![CDATA[file permissions]]></category>
		<category><![CDATA[sticky bit]]></category>

		<guid isPermaLink="false">http://www.33dots.com/?p=106</guid>
		<description><![CDATA[I recently found out that I&#8217;ve a messed up understanding of linux file access mode [or file permissions] 
Like, i thought it was possible to rename/delete a file if we have the write permission on that file. Also i thought it was not possible to delete/rename a file if we[or our groups] don&#8217;t own, cant [...]]]></description>
			<content:encoded><![CDATA[<p>I recently found out that I&#8217;ve a messed up understanding of linux file access mode [or file permissions] <img src='http://www.33dots.com/blog/wp-includes/images/smilies/icon_sad.gif' alt=':(' class='wp-smiley' /><br />
Like, i thought it was possible to rename/delete a file if we have the write permission on that file. Also i thought it was not possible to delete/rename a file if we[or our groups] don&#8217;t own, cant read or write a file. Both of them seems to be false.. pretty bad right?</p>
<p>Back to learning basics,<br />
A file or directory(folder) is owned by a <strong>User(u)</strong> and a <strong>Group(g)</strong>. There are three permissions <strong>read(r)</strong>, <strong>write(w)</strong> and <strong>execute(x)</strong>, which has different meaning for files and directories. Each of these permissions can be set for the user(u), the group(g) and others(o).<br />
The root user, the one with UID = 0 has full access irrespective of any set permissions.</p>
<p><strong>Lets take the case of files,</strong><br />
<strong>r(read)</strong> : Read the file, and also copy the file.<br />
See the shell examples. Comments are included beginning with a #</p>
<pre>[john@localhost test]$ ls -l
total 4
-r-------- 1 john john 9 Jul 29 12:18 testfile		#we only have read permission
[john@localhost test]$ cat testfile			#we read it
abcde...
[john@localhost test]$ cp testfile testfile2		#we copy it
[john@localhost test]$ ls -l
total 8
-r-------- 1 john john 9 Jul 29 12:18 testfile
-r-------- 1 john john 9 Jul 29 12:20 testfile2</pre>
<p><strong>w(write)</strong> :Write to the file. Add/change/append/remove content to it.</p>
<pre>[john@localhost test]$ ls -l
total 8
--w-r-xr-x 1 john john 9 Jul 29 12:18 testfile	 #we have only write set on these files
--w-r-xr-x 1 john john 9 Jul 29 12:20 testfile2
[john@localhost ~]$ echo "abcde" &gt; testfile
[john@localhost ~]$ echo "abcdef" &gt;&gt; testfile
[john@localhost ~]$ cat testfile		#we can write to the file
abcde
abcdef</pre>
<p>But this does not mean we have the rights to rename/move/delete the file. This is actually controlled by the write permissions in the directory on which the file resides.</p>
<pre>[john@localhost test]$ chmod u-w .	#we remove the write on the current directory
[john@localhost test]$ ls -l
total 8
-rwxr-xr-x 1 john john 9 Jul 29 12:18 testfile    #we have write access on these files
-rwxr-xr-x 1 john john 9 Jul 29 12:20 testfile2
[john@localhost test]$ mv testfile testfile3
mv: cannot move `testfile' to `testfile3': Permission denied
[john@localhost test]$ rm testfile
rm: remove write-protected regular file `testfile'? y
rm: cannot remove `testfile': Permission denied   #we cant rename/delete the file</pre>
<p><strong>x(Execute)</strong> : Run the script or program.</p>
<pre>[john@localhost ~]$ touch script	#we create a file and try to run but fails
[john@localhost ~]$ ./script
-bash: ./script: Permission denied
[john@localhost ~]$ chmod u+x scrip	#we also set execute and runs it fine
[john@localhost ~]$ ./script
[john@localhost ~]$</pre>
<p>Note that it is also required to have read permission for execution.</p>
<pre>[john@localhost ~]$ chmod u-r script	#we remove the read
[john@localhost ~]$ ./script
bash: ./script: Permission denied	#not allowed</pre>
<p><strong>Now,  the case of directories,</strong></p>
<p><strong>r(read) </strong> : List the contents of the directory. Also this enables TAB completion feature.</p>
<pre>[john@localhost ~]$ chmod u=r test/	#we set only read access to directory test/
[john@localhost ~]$ ls  test/
testfile  testfile2			#listing works fine</pre>
<p>Listing doesn&#8217;t mean we can read/access the files inside. Nor we could cd into the directory. These are controlled by the execute permission of the directory.</p>
<pre>[john@localhost ~]$ cat test/testfile
cat: test/testfile: Permission denied
[john@localhost ~]$ cd test/
-bash: cd: test/: Permission denied
[john@localhost ~]$ find test/
test/
find: test/: Permission denied		#as you can see find/cat/cd etc not allowed.
[john@localhost ~]$ ls -l test/
total 0					#ls -l doesnt show details bcoz it cant
?--------- ? ? ? ?            ? testfile	#access the file for more details</pre>
<p>This read permission only affects the directory which it is set and not its subdirectories. We can have an unreadable directory with readable subdirectories. However the execute permission must be set on the parent directory.</p>
<pre>[john@localhost ~]$ chmod u-r test/	#remove read on directory test/
[john@localhost ~]$ ls test/		#we cant list test/
ls: test/: Permission denied
[john@localhost ~]$ ls test/sub/	#but we can list test/sub/
sub  subfile  subfile2</pre>
<p><strong>w(write)</strong> : Create, rename, move, delete files/folders. But we also need the Execute permission set, to do all these operations.</p>
<pre>[john@localhost ~]$ chmod u=wx test/	#we enable both write and execute for test/
[john@localhost ~]$ touch test/testfile4
[john@localhost ~]$ mv test/testfile4 test/testfile5	#we are able to create and rename files</pre>
<p>Its possible to delete/move/rename the files/folders that we[or our groups] don&#8217;t own, cant read or write.</p>
<pre>[john@localhost ~]$ su
Password:
[root@localhost john]# touch test/testfile6	#we create a file as root user
[root@localhost john]# chmod a= test/testfile6	#we remove all the permission on this file
[root@localhost john]# ls -l test/testfile6
---------- 1 root root 0 Jul 30 13:00 test/testfile6
[root@localhost john]# exit
exit
[john@localhost ~]$ rm test/testfile6
rm: remove write-protected regular empty file `test/testfile6'? y
[john@localhost ~]$		#as you can see we were able to delete it as john.</pre>
<p>A user can remove directories created by anyone, if he has write and execute permission on its parent directory. However, if it contains files, this is not possible.</p>
<pre>[john@localhost test]$ ls -ld .		#we see that user john has write and execute
drwxr-xr-x 3 john john 4096 Jul 30 13:20 .
[john@localhost test]$ su
Password:
[root@localhost test]# mkdir test1 test2	#we make an empty and
[root@localhost test]# touch test1/testfile	#a non empty directory as root
[root@localhost test]# ls -l
total 20
drwxr-xr-x 2 root root 4096 Jul 30 17:17 test2
drwxr-xr-x 2 root root 4096 Jul 30 17:17 test1
[root@localhost test]# exit
exit
[john@localhost test]$ rm -R test2
rm: remove write-protected directory `test2'? y		#delete success as normal user
[john@localhost test]$ rm -R test1
rm: descend into write-protected directory `test1'? y
rm: remove write-protected regular empty file `test1/testfile'? y
rm: cannot remove `test1/testfile': Permission denied	#we cant delete the one with contents</pre>
<p>The write permission too only affects the directory on which it is set and not its subdirectories. We can have an unwritable directory with writable subdirectories. However the execute permission must be set on the parent directory.</p>
<pre>[john@localhost ~]$ chmod u=x test1/		#we enable only execute for the directory 'test1/'
[john@localhost ~]$ touch test1/subfile3	#cant write to test1/
touch: cannot touch `test1/subfile3': Permission denied
[john@localhost ~]$ touch test1/sub/subfile	#still we are able to create files in test1/sub/
[john@localhost ~]$ mkdir test1/sub/sub
[john@localhost ~]$ ls -l test1/sub
total 8
drwxrwxr-x 2 john john 4096 Aug  3 12:09 sub
-rw-rw-r-- 1 john john    0 Aug  3 12:39 subfile
-rw-rw-r-- 1 john john    0 Aug  3 12:04 subfile2</pre>
<p><strong>x(execute) </strong> :Access the files in the directory for reading, writing etc. Enables cd to it.</p>
<pre>[john@localhost ~]$ chmod u=x test/		#we set execute on the directory
[john@localhost ~]$ cd test
[john@localhost test]$ cat testfile
abcde...
[john@localhost test]$ ls -l testfile		#cd/reading files  are possible
-rwxr-xr-x 1 john john 9 Jul 29 12:18 testfile
[john@localhost test]$ ls
ls: .: Permission denied		#listing not possible as we dont have read set</pre>
<p>This also enables accessing the directory for write operations.</p>
<pre>[john@localhost test]$ chmod u=wx .	# we set write and create items
[john@localhost test]$ mkdir test
[john@localhost test]$</pre>
<p>Though directory execute permissions only affects the directory on which it is set, we are not allowed to access its subdirectories even if we have the appropriate permissions on the subdirectory. So we can say that execute permission on a directory has a fall through effect to all its child directories at any depth.</p>
<pre>[john@localhost ~]$ ls -ld test1/
drwxrwxr-x 3 john john 4096 Aug  3 12:04 test1/
[john@localhost ~]$ ls -ld test1/sub/
drwxrwxr-x 4 john john 4096 Aug  3 12:41 test1/sub/
[john@localhost ~]$ ls -ld test1/sub/sub/
drwxrwxr-x 2 john john 4096 Aug  3 12:09 test1/sub/sub/
[john@localhost ~]$ chmod u-x test1/		#we remove execute on test1/
[john@localhost ~]$ touch test1/testfile
touch: cannot touch `test1/testfile': Permission denied
[john@localhost ~]$ touch test1/sub/testfile
touch: cannot touch `test1/sub/testfile': Permission denied
[john@localhost ~]$ touch test1/sub/sub/testfile
touch: cannot touch `test1/sub/sub/testfile': Permission denied
[john@localhost ~]$ ls test1/sub/
ls: test1/sub/: Permission denied
[john@localhost ~]$ mkdir test1/sub/sub2
mkdir: cannot create directory `test1/sub/sub2': Permission denied
[john@localhost ~]$	#as you can see we cant do anything in its child directories</pre>
<p>By default read and execute permission is given  in all directories for all users, except /root and user&#8217;s home directories and a few others. With read permission we can list the items and with the execute permission we can cd to them and access the items.<br />
Without the execute bit set in / and /home we would not be able to create any files/folders in our home directory ~/ even though we have full permissions on ~/.</p>
<p>A directory with only execute set allows us to access the files but not list or write to it. This scenario is particularly useful in a ~/public_html/ directories where we allow a webserver to access our files but not list them.</p>
<pre>[john@localhost ~]$ chmod u=x test1/
[john@localhost ~]$ ls test1/
ls: test1/: Permission denied
[john@localhost ~]$ cat test1/testfile
abcde...
[john@localhost ~]$ ls -l test1/testfile	#ls -l works bcoz ls can access the file
-rw-rw-r-- 1 john john 0 Aug  3 11:54 test1/testfile	#file to get its details..</pre>
<p><strong>Sticky bit</strong><br />
The sticky bit is used to avoid the default behavior of delete/rename/move the items inside a directory. When sticky bit is set, items inside the directory can be renamed or deleted only by the item&#8217;s owner, the directory&#8217;s owner, or the superuser. Without the sticky bit set, as we&#8217;ve seen earlier, any user with write and execute permissions for the directory can rename or delete contained files, regardless of owner.<br />
Sticky bit is set on the /tmp directory to prevent ordinary users from deleting or moving other users&#8217; files</p>
<pre>[john@localhost ~]$ ls -ld /tmp			#/tmp has full permission for everyone
drwxrwxrwt 32 root root 4096 Jul 30 13:18 /tmp	#the last t shows that sticky bit is set
[john@localhost ~]$ touch /tmp/test		#we create files with two users
[john@localhost ~]$ su tony
Password:
[tony@localhost john]$ touch /tmp/test1
[tony@localhost john]$ exit
exit
[john@localhost ~]$ rm /tmp/test	#john is able to remove the file created by him
[john@localhost ~]$ rm /tmp/test1	#but not the one created by tony
rm: remove write-protected regular empty file `/tmp/test1'? y
rm: cannot remove `/tmp/test1': Operation not permitted
[john@localhost ~]$</pre>
<p>Thanks to,<br />
<a title="Introduction to Linux by Machtelt Garrels at tldp.org" href="http://tldp.org/LDP/intro-linux/html/sect_03_04.html" target="_self">Introduction to Linux</a> by Machtelt Garrels<br />
<a title="An article in linx-noob.com" href="http://www.linux-noob.com/forums/index.php?showtopic=1766" target="_self">Understanding Access Permissions</a> by znx<br />
And the Linux Info pages</p>
]]></content:encoded>
			<wfw:commentRss>http://www.33dots.com/index.php/linux/revisiting-file-access-modes-in-linux.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

