• There is NO official Otland's Discord server and NO official Otland's server list. The Otland's Staff does not manage any Discord server or server list. Moderators or administrator of any Discord server or server lists have NO connection to the Otland's Staff. Do not get scammed!

Linux Permissions - Chmods

klekSu

Stroke my ego.
Joined
Nov 4, 2008
Messages
1,285
Reaction score
18
Hello OtLanders!

I decided to make a tutorial for every new and not only Unix user. This tutorial explains everything you would need about permissions - called chmods.

First of all you have to know what is chmod, this is a linux command which lets you tell the system how much access it should permit to a file.

Let's take a look on the permissions in the directory you are now by typing ls -la.
Code:
total 28
drwxr-xr-x  4 kleksu kleksu 4096 lut 24 22:05 .
drwxr-xr-x 22 kleksu kleksu 4096 lut 24 19:50 ..
drwx------  2 kleksu kleksu 4096 lut 24 19:49 .aptitude
-rw-------  1 kleksu kleksu 2888 mar  8 01:34 .bash_history
-rw-r--r--  1 kleksu kleksu 412 gru 15  2004 .bashrc
drwxr-xr-x  2 kleksu kleksu 4096 lut 24 19:50 .debtags
-rw-r--r--  1 kleksu kleksu 140 lis 19  2007 .profile
Comand total tells you how many blocks are there, 1024 bytes per block.

In the first collumn you can see chmods. Chmods are divided into 3 groups
Code:
|d|owner|group|others|
|-|owner|group|others|
| |r w x|r w x|r w x |
First what you can see is d or -, d is a directory and - is a file. Then, you can see groups such as owner, group, others. If we for example see a file with chmods:
Code:
|-|r w x|r w x|r w x |
Then every group has access to everything, what is Everything? Character r means read, w means write, and x means execute. This file has chmods 777. Why 777? Let me explain.
r, w or x is a number 1 in binary system and - is a 0. So in the file above we see chmods |-|111|111|111| 111 in binary is 7 in hexadecimal.
Code:
hexadecimal - binary - chmod
0           - 000    - ---
1           - 001    - --x
2           - 010    - -w-
3           - 011    - -wx
4           - 100    - r--
5           - 101    - r-x
6           - 110    - rw-
7           - 111    - rwx
If we for example want to protect our file with users cat /etc/passwd then we have to give it chmods -rw-r--r-- why? Because we want owner to read the file and write changes to it, we want groups to only read the file and others read too. Why not letting them modifty this file? Because in this file you can see the following
Code:
root:x:0:0:root:/root:/bin/bash
kleksu:x:1000:1000:kleksu,,,:/home/kleks:/bin/bash
userName : password : UID : GID... So if we let users write to this file then user kleksu could make himself UID and GID 0:0 which is a main administrator - root. Then user kleksu could change roots name or even give kleksu administrators access. Alright so, it was a little examples for chmods, but now how to use/give them? Here we go.
Code:
chmod 1 file
What does it mean? Set chmods 1 to file, what is 1? It's the access we want to add, for example.
Code:
chmod a+r file
This means, we add read access for a (all - owner, groups, others).

Let's make a file and test it. Type touch file and start giving chmods. Type ls -la to see it current chmods, now we have that:
Code:
-rw-r--r--  1 root root    0 mar  8 09:15 file
Which is file|110|100|100| so now we have chmods 644 you can now type chmod 644 file to see that I'm not lieing and nothing has changed. Now type chmod 664 file which should remain as:
Code:
-rw-rw-r--  1 root root    0 mar  8 09:15 file
So now we have set read and write for owner and groups, read only for others. Now let's make it as it was so remove write for groups, type chmod g-w file and it should be back to normal.

Chmod commands
Who:
u - user who owns the file which probably is root, you.
g - groups the file belongs to.
o - other users.

What:
r - access to read the file
w - access to write (modify) or delete the file.
x - access to execute (run) the file.

So we give chmods by the following: chmod who-what file, chmod who+what file, chmod who=what file. We could also do chmod who+whatWhatWhat file. This is easy to remember, but if you are good in the math or remember the table written above, you could always use numbers instead of characters, but then just like this chmod 777 file which is same as chmod a+rwx file

I hope this will be clear for everyone now! :) if you have any questions please don't hesitate to ask :)
 
A little hint if you want something more advance then owner,ownergroup and others. Is to use setfacl.

setfacl -x user:username:rwx filename (equal to 7 for that user)
setfack -x group:groupname:r-x filenane (qual to 5 for that group.


Also you can use letters instead of numbers when you chmod.

-R can be used to set it to all files and underfolders
(filename can also be a foldername)
 
That's also useful, thanks stian. Especially the -R :)
 
Back
Top