Understanding File or Folder Permissions in Linux
In Linux nothing is taken for granted. Linux is secure and it will always continue to be. What are file/folder permissions in Linux?
It becomes very boring when you can’t even open, delete or modify your own files and folders on Linux as a beginner. When I started using Ubuntu and doesn’t know about file/folder permissions I only allow these stubborn secured files and folders to just have their way on my computer.
Most people have gone through this before, where files and folders become immutable to them due to file permissions. Is file or folder permission setting necessary at all? YES. Want to know why, continue reading.
Today am going to show what file and folder permissions is all about.
Let’s get started.
The command that will help us solve this is known as chmod.
chmod command is used to set the permission of a file or folder. chmod command uses three digits as a parameter to assign permissions to files or folders. chmod changes the file mode bits of each given file according to mode, which can be either a symbolic representation of changes to make, or an octal number representing the bit pattern for the new mode bits.
$ sudo chmod XXX file/folder name
Understanding the example above,
The XXX in the command are the digits used in manipulating of bits to change permissions.
The first X represents the Owner (current user)
The second X represents Group (set by owner)
The third X represents anyone else
Here we dealing with 3-bits xxx where one of the 3-bits is set to 1 it means you are permitted to do something, when set to 0 you are not.
for these bits xxx, they stand for x-Read, x- Write and x-Execute (in their respective order).
The list below shows the Decimals, Bits and Meanings
0 = 000 = No permission, this person can neither read, write nor execute
1 = 001 = Execute only (because last bit set to 1)
2 = 010 = Write only (because middle bit set to 1)
3 = 011 = Write and Execute only (because last 2 bits set to 1)
4 = 100 = Read only (because first bit set to 1)
5 = 101 = Read and Execute only (because first and last bits set to 1)
6 = 110 = Read and Write only (because first and second bits set to 1)
7 = 111 = Read, Write and Execute (because all bits set to 1)
Now let’s try our hands on a real file. Using ls -l to list files and folders in a directory displays the permission of files with
- r (Read)
- w (Write)
- x (Execute)
The first, second and third part corresponds Owner, Group and anyone permissions.
Looking at the codes below, only the Owner have permission to the file.
enock@enock-pc:~/unixmen$ ls -l total 0 -rwx------ 1 enock enock 0 Aug 25 18:05 unixmen.txt
Looking at the codes below, only the owner and anyone have permission to the file.
enock@enock-pc:~/unixmen$ chmod 707 unixmen.txt enock@enock-pc:~/unixmen$ ls -l total 0 -rwx---rwx 1 enock enock 0 Aug 25 18:05 unixmen.txt
Looking at the codes below, everybody have permissions to the file.
enock@enock-pc:~/unixmen$ chmod 777 unixmen.txt enock@enock-pc:~/unixmen$ ls -l total 0 -rwxrwxrwx 1 enock enock 0 Aug 25 18:05 unixmen.txt
Now you can change the permission of files or folders.
NOTE: It is not good to set permission of confidential files or folders to 777.