ABOUT THE FILE SYSTEM AND FILE PERMISSIONS IN LINUX

(C) 2004 Daniel Brodzik
This document is released under the terms of the GNU Free Documentation License as published by the Free Software Foundation, either version 1.2 or (at your option) any later version, with no front-cover texts, no back-cover texts, and no invariant sections.

INTRODUCTION
Have you ever wondered how the file system is organized in Linux? Or, have you ever wondered how file permissions and ownership work? This guide, unlike most of the others I wrote, is a 'how it works' and 'why' guide rather than a 'how-to' guide. Here, you'll find an explanation of the organization of the file system, how it is different from Micro$oft Windows file systems, and how file permissions work. A note for those of you who use/used Windows: in Linux, and most other Unices (plural of Unix), nobody or almost nobody uses the term 'folder' to refer to a directory, unless you use Nautilus... So, I will be using the term 'directory' to refer to what you might call a 'folder'.


PART 1: FILE SYSTEM DIRECTORY HIERARCHY
Let's start this guide with an explanation of how the file system is arranged. First, a few notes. Linux uses a '/' instead of a '\' in pathnames. This follows the Unix tradition. Another thing you should be aware of is that Linux is case-sensitive! This means that 'foo.bar', 'Foo.bar', 'FOO.BAR', and 'fOo.BaR' are all different filenames. In fact, all of those can exist in the same directory because of that.

In Linux, everything starts at the root, '/'. However, there are no drive letters; all drives become part of a single file system. On the filesystem, there are several important directories. These are:

For more information on the file system hierarchy, visit http://www.tldp.org and look for the Linux file system hierarchy guide, in the guides section, by Binh Nguyen. Many thanks go to him for writing that guide. I looked at it a lot while I was writing this section. His guide goes into much greater detail than my guide goes.


PART 2: DIFFERENCES BETWEEN THE LINUX AND WINDOWS FILESYSTEMS

Despite the fact that I grew up using Windows, I personally prefer the Unix way of doing things. I think it's very nice to be able to just type a the name of a program's executable from any directory and have the program run. I know it's possible to set that up in Windows, but in Windows, registry entries telling where the program executable is and/or a search path is needed. Under Unix, the executables are all in four directories that contain just executables. Another nice thing is that, since the files are arranged by function, absolute paths are allowed when referencing a program or its data, and the installation directory is going to be the same on every system, thus eliminating the need to keep track of where the user installed it.


PART 3: FILE PERMISSIONS

Linux, like Unix, is a very secure, multiuser operating system. This adds a little bit of complexity to the way file permissions are handled. If you have problems with file permissions, you can have some nasty problems... Thus, it is a good idea to learn about how file permissions work in Linux.


FILE OWNERSHIP

Each file is owned by a user and a group. The user is usually someone's login name. The group may consist of multiple users, but it is often just a one-person group with the user's name. To see who owns a file, you can look in your file manager's properties window, or you can use 'ls -l filename' at the command prompt. Here's what the 'ls -l' method might look like on my computer for these Linux guides:


djb@tty1:~/linux-guides$ ls -l

total 192k

-rw-r--r-- 1 djb djb 7.3k Jun 15 23:49 00Readme.html

-rw-r--r-- 1 djb djb 26k Jul 7 21:55 Command-Line-BASH.html

-rw-r--r-- 1 djb djb 12k Jul 7 16:05 Compiling-the-Linux-Kernel-in-Debian.html

-rw-r--r-- 1 djb djb 6.2k Jul 9 14:34 Disk-Images.html

-rw-r----- 1 djb users 6.8k Jun 15 10:04 FAT-partitions.html

-rw-r--r-- 1 djb users 18k Jul 9 14:46 Filesystem-and-Permissions.html

-rw-r--r-- 1 djb djb 9.2k Jun 15 10:29 Installing-Debian-486.html

-rw-r--r-- 1 djb djb 8.8k Jun 15 10:49 Installing-Debian-Pentium.html

-rw-rw-rw- 1 djb djb 5.7k Jul 7 14:00 Installing-from-source-code.html

-rw-r--r-- 1 djb djb 5.2k Jun 15 21:57 TiMidity.html

-rw-r--r-- 1 djb djb 6.4k Jun 15 11:18 Using-TrueType-Fonts.html

-rw-r--r-- 1 djb djb 30k May 31 22:26 fdl.html

-rw-r--r-- 1 djb djb 26k May 31 22:17 gpl.html


The format for the display is:

permission #itm user group size date/time filenamename


In the example, the owner and group for most files are both 'djb', which is my login name. Some have the group name 'users'. The owner can be changed with the 'chown' command as root. For example, I can change the owner of 'fdl.html' to user 'rms' and group 'fsf':


root@tty1:/home/djb/linux-guides# chown rms:fsf fdl.html


I can also just change the owner of the file without changing the group:


root@tty1:/home/djb/linux-guides# chown rms fdl.html


Alternatively, I can change the group without changing the owner via the 'chgrp' command:


root@tty1:/home/djb/linux-guides# chgrp fsf fdl.html


Note that, unless you currently own the file(s) you're working with, you must be root to use these commands.


By default, the owner of a file is who created it.


FILE PERMISSIONS

In order to understand file permissions, you need to first know how file ownership works. That was the purpose of the last section.


File permissions come in five varieties: r, w, x, s, and t. Here's a table that outlines what each permission means for files and directories:

Table 1: Permissions and Their Meanings

Permission

Files

Directories

r

Read the file

Read directory entries

w

Write to the file

Write entries

x

Execute file

Enter directory

s

Set user/group ID when executing

Sets a default group ID for files in a directory

t

Does nothing

Sticky


There are three classes of users when dealing with permissions: the user who owns the file (user, u), users in the group that owns the file (group, g), and everyone else (others, o). Let's refer back to the directory listing I gave earlier in the file ownership section. To refresh your memory:


djb@tty1:~/linux-guides$ ls -l

total 192k

-rw-r--r-- 1 djb djb 7.3k Jun 15 23:49 00Readme.html

-rw-r--r-- 1 djb djb 26k Jul 7 21:55 Command-Line-BASH.html

-rw-r--r-- 1 djb djb 12k Jul 7 16:05 Compiling-the-Linux-Kernel-in-Debian.html

-rw-r--r-- 1 djb djb 6.2k Jul 9 14:34 Disk-Images.html

-rw-r----- 1 djb users 6.8k Jun 15 10:04 FAT-partitions.html

-rw-r--r-- 1 djb users 18k Jul 9 14:46 Filesystem-and-Permissions.html

-rw-r--r-- 1 djb djb 9.2k Jun 15 10:29 Installing-Debian-486.html

-rw-r--r-- 1 djb djb 8.8k Jun 15 10:49 Installing-Debian-Pentium.html

-rw-rw-rw- 1 djb djb 5.7k Jul 7 14:00 Installing-from-source-code.html

-rw-r--r-- 1 djb djb 5.2k Jun 15 21:57 TiMidity.html

-rw-r--r-- 1 djb djb 6.4k Jun 15 11:18 Using-TrueType-Fonts.html

-rw-r--r-- 1 djb djb 30k May 31 22:26 fdl.html

-rw-r--r-- 1 djb djb 26k May 31 22:17 gpl.html


The first column, the file permissions, is what we're focusing on now. Here's how it's arranged, broken up to make it easier to show:


type | user | group | other

- rwx rwx rwx


(Permissions 's' and 't' shows up in the 'x' column when it's set.)


For example, the file above named 'FAT-partitions.html' has a permissions column of 'rw-r-----', meaning the owner has the read and write permission, members of the group have read-only permission, while everyone else cannot even read the file.


The command 'chmod' changes file permissions, provided you are either root or the user who owns the file. The syntax is 'chmod mode file'. The mode can be an octal number or in the form of a user class (u for user, g for group, o for others, a for all), a + or – sign (for on or off), and then a permission (r for read, w for write, x for execute, X to make it so that execute is changed only if the file is a directory OR if the execute permission is already set). The file can be a wildcard. Here are some examples


Does the explanation look familiar? I 'borrowed' it from my command line guide.


The octal representation of the file permissions is shorthand. There are three digits, one for each of user, group, and other. Each permission type (r, w, x) has a certain value. When the values of the active or desired permission(s) are added together, the sum produces a digit. The value of each permission follows.


Table 2: The Values of the Permissions in Octal Representation

Permission

Value

Read [r]

4

Write [w]

2

Execute [x]

1


Thus, the meaning of each possible digit:


Table 3: The Possible Digits of Octal Representation

Digit

Meaning

0

no permissions at all (common)

1

execute only (seems unusual)

2

write only (seems unusual)

3

write and execute (seems unusual)

4

read only (common)

5

read and execute (common)

6

read and write (common)

7

read, write, and execute (common).


A common octal permission is 666: read and write permission for everybody. For some people, this is proof enough that Linux, as well as any Unix, is the work of the devil... >:-O


Octal representation is base 8, and it comes from the binary representation of the permissions. Here's an example (and another table).


Table 4: Example: Binary and Octal Representations of Permissions

Representation

User

Group

Others

Complete

ls -l

rwx

r-x

--x

rwxr-x--x

Binary

111

101

001

111101001

Octal

7

5

1

751


You never have to use octal format for specifying permissions, but it may come in handy.


Now, about the 's' permission. The 's' permission is special. It is used to make a program 'think' it's being run by a different user (i.e., the owner of the executable file), and, as a result, provide any special permissions that the user may have. This is most commonly used to give a program superuser (root) permissions even when run as a normal user. This is called 'setting suid'. This is most often required on X servers, svgalib programs, and other software that needs direct access to the hardware. To set a program suid, type the following as root, substituting filename for the name of the executable you wish to set SUID:


chmod u+s filename


Make sure you either specify a path to the executable or be in the directory that the executable is in.


To make an executable file set the group ID (SGID), use


chmod g+s filename


WARNING: Making a program SUID can be dangerous! If a program is SUID, the results can be disastrous if the program has any poorly-written or malicious code in it! This is not a problem in svgalib programs because svgalib drops the root permissions for the program at the beginning (svgalib needs root permissions to control the video card). Use caution! Suid programs do not have any security! Don't suid too many executables, either. I will NOT be responsible for anything that happens as a result of suiding a program that shouldn't be suid. You have been warned!


The 's' permission does a different thing on a directory. When used as a SUID bit, it has no effect. When a directory is SGID, it sets the default group to be used for files created in that directory.


The 't' permission is another special permission. If a directory is accessible (r, w, and x are all allowed) by more than one user, setting the 't' bit makes any move or delete operations fail if the current user doesn't own those files.


ACKNOWLEDGEMENTS

I referenced two guides extensively while writing this guide. For the file system hierarchy information, I would like to thank Binh Nguyen for his wonderful Linux File System Hierarchy guide, which is available at http://www.tldp.org. For the information on file permissions, I would like to thank the authors of the Linux Newbie Administrator Guide, which is available at http://linux-newbie.sunsite.dk. Both of those guides explain in further detail the way the filesystem works (among other things).


For the program I used to write this guide, I would like to thank the many authors of OpenOffice.org.

1