How to add file permissions in Ubuntu via command line

navya , Credit to  volkotech-solutions Nov 19
File Permissions in Ubuntu Banner Image

Find the list of instructions to control file access using file permissions in Ubuntu. Learn how to set permissions for users and groups with chown commands.

Pre-requisites:

  1. Laptop/PC with Ubuntu OS installed
  2. Active Terminal

If you don’t have Ubuntu on your Laptop/PC and if you feel to install it, then please refer to this blog

https://mycode.blog/naveen-golla/how-install-ubuntu-your-computer

File permissions:

In any project, all files may not have access to every developer, some authenticated files could have access only for the project manager. So for a well-organized project, the admin needs to set the permissions to protect the project data. Here is a clear explanation of how the file permissions work.

We can change the permissions in two methods,

  1. Numeric method and
  2. Symbolic method

1. Numeric Method 

In this method, we have numbers from 0 to 7, and for every number, there is permission status. Look below the table for detailed information.

Number Permission type Symbol
0 No permission for anyone -
1 Only execute -x
2 Only write -w-
3 Execute + Write -wx
4 Read r-
5 Read + Execute r-x
6 Read + Write rw-
7 Read + Write + Execute rwx

So with the combination of the above numbers, we can change the permissions for a file in this format user group others.

Listing file permissions using Ubuntu terminal


From the above screenshot consider the first permission, that is drwxr-xr-x 2 krishna krishna 4096 Nov 14 18:44

Diagramatic representation of File Permissions
In the above diagram, you can see the division of different roles like user, others, and group. So with the combination of numbers in the above table, we can change the permissions.

Now we will give all the permissions for all the rolls to the user Krishna so that anyone can access the files and do the operations.

From the above table, the number that has been used to give all the three permissions is 7, now for all the rolls give the number 7 with the help of below command.

Command: chmod [user][group][others] fileName/userpath

So for our condition, the command is chmod 777 /home/krishna
The above code says that
The owner can read write and execute
User and group also can read write and execute
The world also can read, write and execute.

So this type of permission is not allowed for all types of files.

Updating File Permissions for the user using Ubuntu terminal
If you can observe the above screenshot, for the user krishna, the permissions have changed like this drwxrwxrwx 2 krishna krishna 4096 Nov 14 18:44 .

So if the user wants to protect his files from other people, he can change the permissions. In the above example, we have seen how to change the permissions for users, the same steps for files and directories also.

If you observe the below screenshot, the user navya is the admin of the current computer, that user has all the permissions and the user krishna is the created user by the user navya, the user krishna has respective given permissions.

Updating File permissions for the directory using Ubuntu terminal

So now, the project folder has access only for the user krishna but not the user navya, the user navya can only read the file and is unable to do any actions in the project folder.

2. Symbolic Method 

The purpose of the symbolic method is also for changing the file/folder permissions but in a different way like using symbols.In symbolic mode, the available operations and available roles are

Operations: Roles: symbols
+->  Adds permissions
- -> Removes permissions 
u -> user/owner
g -> group
o -> others
a -> all
-x  -> execute
-w- -> write
r-    -> read

The command is chmod [role]= symbols+operations fileName 
The symbolic code to change the group permissions for the available folder is chmod g+x folderName

Symbolic method of updating File Permissions using Ubuntu terminal


Just observe the file permissions of the folder project, the user permissions have been updated like this drwxr-xr-- 2 navya navya   4096 Nov 16 08:04 project.

Finally, I want to conclude that, to protect the files from corruption or loss of data in the project we have to restrict the access of files/folders. So with these simple commands, you can update the permissions accordingly.

FAQ

Is it possible to change the user of the file?

Yes, you can change the user of the file with the following command chown userName fileName/folderName.

Is it possible to change both the user and group of the file?

Yes, you can change the user of the file with the following command chown userName:groupName fileName/folderName

Can a created user login to the system and save files?

Yes, the user can log in with the respective credentials and save the files as he has respective permissions. 

Comments

Authors

Read Next