Permission&Ownership#
This page provides information on setting up permissions and ownership for files.
Check#
Use the -l
flag with the ls
command to view information associated with file permissions and ownership. We are interested in the first, third, and forth attributes for each position in the output, which refer to: permissions, user, and group respectively.
The first attribute for each file contains 10 symbols that describe whether the file is a directory and its permissions. The first character defines whether the is a deroctoty. The following characters are separated separated into 3 groups of 3 symbols each and specify the rights for the user, group, and other. The following schema illustrates this concept:
The rwx
combination means read
, write
and execute
.
If a digit is preceded by -
, it means that the position is not a directory or that there are no corresponding permisisons.
The following cell creates the folder that we’ll use as an example throught the section and the file that we will consider.
rm -fr /tmp/check_permission
mkdir /tmp/check_permission
cd /tmp/check_permission
echo "hello" > my_file
The following cell displays the output of the command ls -l
for the folder.
ls -l
total 4
-rw-r--r-- 1 root root 6 Sep 23 20:28 my_file
According to the output, the file my_file
belongs to the root
user and root
group. The root
user can read and write to the file; others are only allowed to read it.
Chmod#
The chmod
command allows you to adjust file permissions.
The general syntax is:
chmod [MODE] [FILE]
The most imporant point for consideration here is MODE
. It can be specified in the symbolic and numeric formats.
Symbolic#
The MODE
in the symbolic format breaks down in the following format:
chmod [WHO][OPERATOR][PERMISSIONS] [FILE]
The meaning of each element is described in the following table:
Element |
Symbols |
Description |
---|---|---|
WHO |
|
User, Group, Others and All |
OPERATOR |
|
add, remove, overwrite |
PERMISSIONS |
|
read, write, execute |
Consider a few examples. The following cell creates the file and displays its default permissions.
rm -rf /tmp/chmod_symbolic
mkdir /tmp/chmod_symbolic
cd /tmp/chmod_symbolic
echo "Hello" > some_file
ls -l
total 4
-rw-r--r-- 1 root root 6 Sep 27 09:31 some_file
The following cell add x
permission for the user.
chmod u+x some_file
ls -l
total 4
-rwxr--r-- 1 root root 6 Sep 27 09:31 some_file
The following code shows removing reading permission for others.
chmod o-r some_file
ls -l
total 4
-rwxr----- 1 root root 6 Sep 27 09:31 some_file
The last example overwrites the permissions for all userse to r-x
.
chmod a=rx some_file
ls -l
total 4
-r-xr-xr-x 1 root root 6 Sep 27 09:31 some_file
Octal#
THe Ocatal syntax uses the format [0-7][0-7][0-7]
to set the mode. Each number corresponds to the user/group/others as ususal.
To memorize which number corresponds to which permissions, assign a score to each permission type: 4-r, 2-w, and 1-x. Each permissions combination is uniquely identified by the sum of the scores. The following table shows all permissions and their correspondig number:
Permissions |
Octal Value |
---|---|
|
4 + 2 + 1 = 7 |
|
4 + 2 + 0 = 6 |
|
4 + 0 + 1 = 5 |
|
4 + 0 + 0 = 4 |
|
0 + 2 + 1 = 3 |
|
0 + 2 + 0 = 2 |
|
0 + 0 + 1 = 1 |
|
0 + 0 + 0 = 0 |
The following cell creates the file that we’ll use as an example and displays its permissions.
rm -rf /tmp/chmod_octal
mkdir /tmp/chmod_octal
cd /tmp/chmod_octal
echo "Hello" > some_file
ls -l
total 4
-rw-r--r-- 1 root root 6 Sep 27 09:58 some_file
The following cell changes the permissions into:
--x
to 1 for user.-wx
to 3 for group.rwx
to 7 for others.
chmod 137 some_file
ls -l
total 4
---x-wxrwx 1 root root 6 Sep 27 09:58 some_file