Often Linux developers and system administrators need to modify file and folder permissions to give and revoke user access in Linux. You can easily do this using chown and chmod command. In this article, we will learn how to give user access to folder in Linux. These commands are freely available on all Linux distributions and can be used accordingly.
How to Give User Access to Folder in Linux
Please note, only an admin or superuser or privileged user can modify file and folder permissions in Linux.
1. Using Chown
Here is a sample command of chown command to easily change the ownership of folder.
$ chown -R test_user:test_group /home/data
The above command sets the owner of /home/data directory to test_user and group to test_group. We have used -R option to recursively change ownership of all sub directories and files in the /home/data directory.
The user group is optional. If you want to change only file owner and not user group, you can omit it.
$ sudo chown -R test_user /home/data
2. Using chmod
Once you have changed the ownership of file or directory, you can change its permissions using chmod command. Here is the syntax of chmod command.
$ chmod permission_mode folder_path
The permission mode can have read, write and execute values represented by text, numeric and symbolic methods. Each file/directory has separate permissions for user, group and all. The owner of file/directory is denoted using u, group using g and other using o.
In the text method, we specify permissions using alphabets r, w and x for read, write and execute respectively. We use + sign to add permission and – sign to
remove permission. Here is the command to give read, write and execute permission to file owner.
$ sudo chmod u+rwx /home/data
If you want to give these permissions to group and others, then modify the above command as shown.
$ sudo chmod -R go+rwx /home/data
You can also give the permission numerically. Here are the different numbers to specify different kinds of permissions.
● 0 = — ● 1 = –x ● 2 = -w● 3 = -wx ● 4 = r-
● 5 = r-x
● 6 = rw-
● 7 = rwx
Here also you need to specify separate number for user, group and others. Here is the command to assign read, write & execute permissions for file owners but no permission for group or others.
$ sudo chmod -R 700 /home/data
In this article, we have learnt how to change ownership & permissions for files & directories.
Originally published at https://techosha.com/how-to-give-user-access-to-folder-in-linux/