Often you will need to give user access to folder in Linux. You can easily do this using chown and chmod commands. In this article, we will look at how to give user access permission in Linux. You can use these commands on every Linux distributions since these commands are available in all of them.
How to Give User Access to Folder in Linux
Here are the steps to give user access to folder in Linux. Please note, these commands can be normally run only by admin, root or users with sudo privileges.
If you want to give user access to folder in Linux, you need to make that user the owner of your folder. For example, let us say you want to give user test_user the access to folder /home/data then run the following command to make that user the owner of this folder.
$ sudo chown -R test_user:test_user /home/data
The above command will make test_user the owner of not only /home/data but also its subfolders and files, due to -R option above which stands for recursive. If you don’t want that just remove -R option from above command.
The above command will make test_user the owner and group of /home/data. If you don’t want to change group but only the file ownership, simply use chown command as
chown username folder_path
Here is the above command modified to change only file ownership and not group ownership.
$ sudo chown -R test_user /home/data
Once you have made your user the owner of these folders, use chmod command to give user access. There are several ways to do this using chmod. We will look at them.
$ sudo chmod permission_mode folder_path
The permission modes can take any of the 3 values – read, write, execute. It can be specified in any of the 3 methods – text method, symbolic method and numeric method. Also each file/folder in Linux has separate permission for owner, group and all. File/Folder owner is denoted using u, group using g and other using o.
In this case, we specify the permission modes using alphabets – r for read, w for write, x for execute. Also, if you want to give read access to owner run the following command
$ sudo chmod -R u+r /home/data
In the above command, we specify the target user as owner (u) and the permission r for read. We use + sign to add the permission. If you use – sign it will remove the permission. We also provide -R option to recursively provide the permission for all its files & subfolders.
If you want to give read, write, execute permission to file owner, use the following command.
$ sudo chmod -R u+rwx /home/data
If you want to give read, write permission only to group & others, use the following command.
$ sudo chmod -R go+rw /home/data
You may also give user access permissions numerically. Here are the numbers equivalent to read, write & execute permissions.
- 0 = —
- 1 = –x
- 2 = -w-
- 3 = -wx
- 4 = r-
- 5 = r-x
- 6 = rw-
- 7 = rwx
Here too you need to specify a separate number for each user – owner, group and others. For example, if you want owner to have read, write, execute permission, here is the chmod command for it.
$ sudo chmod -R 700 /home/data
In the above command, the 3 digits in 700 stand for permissions for user, group and others respectively.
For example, if you also want group & others to have read, write permission, here is the command.
$ sudo chmod -R 766 /home/data
That’s it. As you can see, it is very easy to give access to user for files & folders, once you understand the notations.