File Permissions
In BackSlash Linux, everything is a file. Directories are files, files are files and devices are files. Devices are usually referred to as a node; however, they are still files. All of the files on a system have permissions that allow or prevent others from viewing, modifying or executing. If the file is of type Directory then it restricts different actions than files and device nodes. The super user "root" has the ability to access any file on the system. Each file has access restrictions with permissions, user restrictions with owner/group association. Permissions are referred to as bits.
To change or edit files that are owned by root, sudo must be used.
If the owner read & execute bit are on, then the permissions are:
-r-x------
There are three types of access restrictions:
Permission | Action | chmod Option |
---|---|---|
read | (view) | r or 4 |
write | (edit) | w or 2 |
execute | (execute) | x or 1 |
There are also three types of user restrictions:
User | ls output |
---|---|
Owner | -rwx------ |
Group | ----rwx--- |
Other | -------rwx |
Note: The restriction type scope is not inheritable: the file owner will be unaffected by restrictions set for his group or everybody else.
Folder/Directory Permissions
Directories have directory permissions. The directory permissions restrict different actions than with files or device nodes.
Permission | Action | chmod option |
---|---|---|
read | (view contents, i.e. ls command) | r or 4 |
write | (create or remove files from dir) | w or 2 |
execute | (cd into directory) | x or 1 |
read restricts or allows viewing the directories contents, i.e.lscommand
write restricts or allows creating new files or deleting files in the directory. (Caution: write access for a directory allows deleting of files in the directory even if the user does not have write permissions for the file!)
execute restricts or allows changing into the directory, i.e.cdcommand
Folders (directories) must have 'execute' permissions set (x or 1), or folders (directories) will NOT FUNCTION as folders (directories) and WILL DISAPPEAR from view in the file browser (Dolphin).
Permissions in Action
user@host:/home/user$ ls -l /etc/hosts
-rw-r--r-- 1 root root 288 2005-11-13 19:24 /etc/hosts
user@host:/home/user$
Using the example above we have the file "/etc/hosts" which is owned by the user root and belongs to the root group.
What are the permissions from the above /etc/hosts ls output?
-rw-r--r--
owner = Read & Write (rw-)
group = Read (r--)
other = Read (r--)
Changing Permissions
The command to use when modifying permissions is chmod. There are two ways to modify permissions, with numbers or with letters. Using letters is easier to understand for most people. When modifying permissions be careful not to create security problems. Some files are configured to have very restrictive permissions to prevent unauthorized access. For example, the /etc/shadow file (file that stores all local user passwords) does not have permissions for regular users to read or otherwise access.
user@host:/home/user# ls -l /etc/shadow
-rw-r----- 1 root shadow 869 2005-11-08 13:16 /etc/shadow
user@host:/home/user#
Permissions:
owner = Read & Write (rw-)
group = Read (r--)
other = None (---)
Ownership:
owner = root
group = shadow
chmod with Letters
Usage: chmod {options} filename
Options | Definition |
---|---|
u | owner |
g | group |
o | other |
a | all (same as ugo) |
x | execute |
w | write |
r | read |
+ | add permission |
- | remove permission |
= | set permission |
Here are a few examples of chmod usage with letters (try these out on your system).
First create some empty files:
user@host:/home/user$ touch file1 file2 file3 file4
user@host:/home/user$ ls -l
total 0
-rw-r--r-- 1 user user 0 Nov 19 20:13 file1
-rw-r--r-- 1 user user 0 Nov 19 20:13 file2
-rw-r--r-- 1 user user 0 Nov 19 20:13 file3
-rw-r--r-- 1 user user 0 Nov 19 20:13 file4
Add owner execute bit:
user@host:/home/user$ chmod u+x file1
user@host:/home/user$ ls -l file1
-rwxr--r-- 1 user user 0 Nov 19 20:13 file1
Add other write and execute bit
user@host:/home/user$ chmod o+wx file2
user@host:/home/user$ ls -l file2
-rw-r--rwx 1 user user 0 Nov 19 20:13 file2
Add group read bit
user@host:/home/user$ chmod g-r file3
user@host:/home/user$ ls -l file3
-rw----r-- 1 user user 0 Nov 19 20:13 file3
And read, write and execute to everyone
user@host:/home/user$ chmod ugo+rwx file4
user@host:/home/user$ ls -l file4
-rwxrwxrwx 1 user user 0 Nov 19 20:13 file4
user@host:/home/user$