Linux File Permissions
Every file and directory on your Unix/Linux system is assigned 3 types of owner, given below.
- Owner is the usually the creator of the files/folders. In Linux, files or folders that you created in your Home directory are usually owned by you, unless you specifically change the ownership.
- Group contains a group of users who share the same permissions and user privilege.
- Others means the general public.
Identities
- u — the user who owns the file (that is, the owner)
- g — the group to which the user belongs
- o — others (not the owner or the owner’s group)
- a — everyone or all (u, g, and o)
Permissions
As for permissions, there are 3 type of actions that you can perform on a file/folder. You can either read, write or execute.
- Read – You can only view the file, but cannot modify the content of the file. When applied on Folder, you can only view the files in the folder, but you can’t delete from or add files into the folder.
- Write – You can edit and modify the file. For Folders, you can delete and add files into the folder.
- Execute – Execute is mainly used when you need to run the file.
The characters are pretty easy to remember.
- r = read permission
- w = write permission
- x = execute permission
Changing file/directory permissions with ‘chmod’ command
We can use the ‘chmod’ command which stands for ‘change mode’. Using the command, we can set permissions (read, write, execute) on a file/directory for the owner, group and the world.
Syntax: chmod permissions filename
Absolute Mode:
How to change permissions in numeric code in Linux, so to do this you use numbers instead of “r”, “w”, or “x”.
0 = No Permission
1 = Execute
2 = Write
4 = Read
Permission numbers are:
0 = ---
1 = --x
2 = -w-
3 = -wx
4 = r-
5 = r-x
6 = rw-
7 = rwx
For example:
chmod 700 foldername – will give read, write, and execute permissions for the user only.
Symbolic Mode:
In the symbolic mode, you can modify permissions of a specific owner. It makes use of mathematical symbols to modify the file permissions.
Actions
- + — adds the permission
- – — removes the permission
- = — makes it the only permission
For example:
$ To check current file permissions ls -l admin
-rw-rw-r-- 1 home home 150 Mar 19 08:08 admin
$ Set permissions to other users – chmod o=rwx admin
-rw-rw-rwx 1 home home 150 Mar 19 08:08 admin
$ Add execute permission to usergroup – chod g+x admin
-rw-rwxrwx 1 home home 150 Mar 19 08:08 admin
$ Remove read permission for user – chmod u-r admin
--w-rwxrwx 1 home home 150 Mar 19 08:08 admin