FUNDAMENTALS A Complete Guide for Beginners
The Linux operating system offers several ways to give users access permissions to its file system. Hence, you can give permission to as many users as you like. For this purpose, there are multiple commands that will help you achieve this task. In this article, I will demonstrate the possible methods in order to give permission to a user in Linux from the Command Line Interface(CLI).
Key Takeaways
- Giving permissions to a specific user.
- Giving permissions to a specific group.
- Changing permission modes.
Requirements
- Need to be a root user or have root/sudo privileges to change permissions of files/folders.
Process Flow Chart
Distro Used Throughout the Tutorial: Ubuntu 22.04.1 LTS
Watch 4 Methods to Give Permission to a User in Linux
4 Methods to Give Permission to a User in Linux
The best approach for giving permission to a user in Linux is to use the CLI since Linux distributions provide four basic commands. These commands are as follows: the chown command, the chgrp command, the chmod command, and the setfalc command for altering user permissions. Furthermore, for your convenience, I have divided the application of these commands into four main Methods.
Method 01: Give Direct Permission to a User in Linux
You can directly give file/folder access permissions to a user by specifying the desired username. To specify the user, you can use either the chown command which will change the ownership of a file in Linux. The new owner will have all the access permissions as the previous owner. However, you must be a sudo user or have sudo privileges to apply such changes. You can see in the following image, the user “anonnya” does not have permission to access the “file1.txt”. In this example, I will give the user “anonnya” permission to run this file by changing the owner. Follow the steps below to do the same.
Steps to Follow >
➊ At first, press CTRL+ALT+T to open the Ubuntu Terminal.
➋ After that, copy the following command:
sudo chown anonnya file1.txt
- sudo: Grants administrative privileges.
- chown: Changes file ownership.
- anonnya: Name of the new owner.
- file1.txt: Name of the file.
➌ Then, press the ENTER button.
➍ Moreover, to check the updated permission run the command below:
cat file1.txt
- cat: Displays file contents on the terminal.
- file1.txt: Name of the file.
➎ Finally, hit ENTER. In the image above, you can see that I gave permission to the user “anonnya” to access the desired file using chown command.
Method 02: Changing Group to Give Permission to a User in Linux
Apart from giving direct access, you can also give permission to a user through its group. When you allow some group to access the files and folders of your system every member of the group will have the exact access. The chgrp command in Linux can be used to change the group ownership of a file/directory. Moreover, you have to be a sudo user or have sudo privileges to change ownership. In the above image, you can see the user “anonnya” does not have permission to access the “file1.txt”. I will give the file access to the group “anonnya” so that the user “anonnya” can have permission to run it. Follow the process below to give permission to a user with the help of chgrp command.
Steps to Follow >
➊ At first, go to the Ubuntu Terminal.
➋ After that, copy the following command:
sudo chgrp anonnya file1.txt
- sudo: Grants administrative privileges.
- chgrp: Changes group ownership.
- anonnya: Name of the new group.
- file1.txt: Name of the file.
➌ Then, press the ENTER button.
➍ Furthermore, to check the updated permission run the command below:
cat file1.txt
- cat: Displays file contents on the terminal.
- file1.txt: Name of the file.
➎ Finally, hit ENTER. In the above image, you can see that I gave permission to the user “anonnya” to access the desired file through the group “anonnya”.
Method 03: Changing Permission Modes to Give Permission to a User in Linux
You can change Linux file/folder permissions by simply running the chmod command. It does not matter which user or group is the owner of the file/folder. If you give read/write/execute permissions to the other user class using this command, you are giving permission to any user on your system. You may use either the symbolic mode or the absolute mode of permissions to achieve this task. In the above you can that the user “anonnya” does not have permission to access the “file2.txt”. I will change this permission to give read, write, and execute access to everyone so the user “anonnya” also has permission.
Steps to Follow >
➊ At first, press CTRL+ALT+T to open the Ubuntu Terminal.
➋ After that, copy any of the following commands:
sudo chmod 777 file2.txt
- chmod: Changes folder permissions.
- 777: Sets read, write and execute permissions to all user classes.
- file1.txt: Name of the file.
➌ Then, press the ENTER button.
➍ Additionally, to check the updated permission run the command below:
cat file2.txt
- cat: Displays file contents on the terminal.
- file1.txt: Name of the file.
➎ Furthermore, view the updated permission modes with the following command:
ls -l
- ls -l: Shows a detailed list along with permissions of the files and folders in the current directory.
In the above image, you can see that I have given permission to read, write and execute the “file2.txt” file to the user “anonnya” by giving the same access permission to every other user.
- How to Change Folder Permissions in Linux? [2 Methods]
- 2 Ways to Change Folder Permissions from Root to User in Linux
Method 04: Modifying ACL to Give Permission to a User in Linux
You can give permission to a user with the setfalc command. In brief, this command is used to modify Access Control Lists (ACLs) for files and directories. While giving permission to a specific user with this command you will need to mention the read/write/execute permissions as well.In the above image, you can see the user “anonnya” does not have permission to access the “file1.txt”. In this example, I will give my “anonnya” user all the permissions for the “file1.txt” file through ACL. To do the same you can follow the steps below.
Steps to Follow >
➊ At first, open the Ubuntu Terminal.
➋ Then, type the following command:
sudo setfacl -m u:anonnya:rwx file1.txt
- setfacl -m: Sets the file’s Access Control List (ACL).
- u: Indicates username to add to ACL.
- anonnya: Name of the new owner.
- rwx: Adds read, write, and execute.
- file1.txt: Name of the file.
➌ Now, press the ENTER button.
➍ Additionally, to check the updated permission run the command below:
cat file1.txt
- cat: Displays file contents on the terminal.
- file1.txt: Name of the file.
In the image above, you can see that I successfully gave permission to the user “anonnya” for accessing the “file1.txt” file by modifying the ACL.
Comparative Analysis of Methods
Since this article introduces multiple methods for a single task, you might get confused about which one to pick. Therefore, in this section, I am presenting a comparative analysis of the three different approaches for your convenience. You can learn the advantages and disadvantages of the available methods and choose which is the best for you.
Methods | Pros | Cons |
---|---|---|
Give Direct Permission (Method 1) |
|
|
Change Group (Method 2) |
|
|
Change Permission Modes (Method 3) |
|
|
Modify ACL (Method 4) |
|
|
To sum up, it can be said that all methods come with their own pros and cons. Therefore, the suitable method varies from user to user. However, If you want to keep the permission modes unchanged but want to change the owner or the group user then you can adopt Method 1 or, Method 2 as per your requirement.
On the other hand, if you wish to change the permission modes only, then you can go for Method 3. Furthermore, if you are willing to change both the permission modes and the user access then you must choose Method 4.
Complementary Information
While learning about how to give permission to a user in Linux, you will find the below information helpful.
Permission Modes in Linux
From the Linux CLI, changes in folder permissions are achieved by using the chmod command. Moreover, the altered permissions can be represented either in symbolic mode or in absolute mode.
Applying Symbolic mode you can change permissions for any desired user class. Whereas with Absolute mode you have to set permissions for all types of user classes separately. In the section below, you will find detailed information on each of the modes.
A. Symbolic Mode
Using symbolic mode, you can add, remove or set access permissions for a particular user class with logical operators. Since, chmod takes multiple sets of symbols as arguments, changing permissions of multiple user classes requires comma (,) separated logical expressions.
User Class | Operator | Access Type |
---|---|---|
u (user) | + (add access) | r (read) |
g (group) | – (remove access) | w (write) |
o (other) | = (set exact access) | x (execute) |
a (all classes: u, g, and o) |
- How to Change File Permissions to 777 in Ubuntu?
- 2 Ways to Change Folder Permissions Recursively in Linux
B. Absolute Mode
On the other hand, in absolute mode, to remove all permissions you can assign a “0” (zero) or, to give permissions you must assign one of the following octal values for each user class.
Permissions | Read
(r) |
Write
(w) |
Execute
(x) |
Read
& Write (r+w) |
Read
& Execute (r+x) |
Write
& Execute (w+x) |
ALL
( r+w+x) |
---|---|---|---|---|---|---|---|
Octal value | 4 | 2 | 1 | 6
(= 4+2) |
5
(= 4+1) |
3
(= 2+1) |
7
(= 4+2+1) |
Conclusion
To conclude, this article presents you with the possible ways to give permissions to users in Linux. In summary, it includes feasible approaches from CLI with a total of four different commands. Furthermore, I have also illustrated the extensive permission modes in Linux. I hope following this guide will help you through your journey with the command line and make you a power user of Linux.
People Also Ask
Related Articles
- How to Change Permissions of All Files in a Folder in Linux?
- Manage File Permissions in Ubuntu Using Command?
- How to Change File Permissions in Linux? [6 Examples]
- 2 Cases to Give User Permission to Folder and Subfolders in Ubuntu
- How to Change Directories and Files Recursively with “chmod”
- Change Permissions on Mounted Drive in Linux