- 2 ways to check file permissions in Linux
- Procedure to check file permissions in Linux
- Understanding file permissions in Linux
- Understanding numeric mode of file permissions in Linux
- File permission examples in Linux
- Check file permissions with ls command in Linux
- Find file Permissions with stat command in Linux
- Change file permissions in Linux
- Case study: file permissions for SSH private key file
- Case study: Permission denied error in Linux
- Conclusion
- How do you view file permissions?
- 4 Answers 4
2 ways to check file permissions in Linux
In Linux, file permissions are an important part of keeping your system secure.
In this blog post, we will discuss two ways to check file permissions in Linux- using the “ls” command and using the “stat” command.
we will also discuss what each of the permissions means, and how to change them if necessary. Stay safe out there!
- The most efficient way to check file permissions in Linux is using the “ls -l filename” command. This command will list all the information about this file, including the permissions. Each permission is represented by a single letter- r for read, w for write, and x for execute.
- To get a more detailed view of the file permissions, we can also use the “stat” command instead of ls. The “stat” command takes a few more options, so be sure to read the man pages for more information.
ls Command | stat Command |
---|---|
ls -l file.txt | stat file.txt |
ls -l /path/to/file.txt | stat /path/to/file.txt |
ls -ld /path/to/directory/ | stat /path/to/directory/ |
Procedure to check file permissions in Linux
- Open the terminal application.
- Type ls -l command
- Press Enter to run the command.
- This will give you a list of all the files in the current directory, along with their permissions.
Understanding file permissions in Linux
Every file or directory has three levels of ownership:
Level | Description |
---|---|
User | The owner of the file or directory |
Group | The group to which the file or directory belongs |
Others | All other users who are not the owner or in the group |
Each level of ownership can be assigned the following permissions:
Permission | Symbol | Description |
---|---|---|
Read | r | The ability to read the contents of a file or view the contents of a directory |
Write | w | The ability to modify the contents of a file or create, rename, or delete files within a directory |
Execute | x | The ability to execute a file or enter a directory |
In this example, the owner of the file test.txt has access to “Read and write”, while other members of its group, as well as all other users, have “Read-only” access. Therefore, they can only open the file, but cannot make any modifications.
User | rw- | The owner of the file has read and write permissions but cannot execute the file |
Group | r– | Users in the file’s group have read permission but cannot write or execute the file |
Others | r– | All other users have read permission but cannot write or execute the file |
This is called Symbolic mode. This mode is used to set permissions for a file or directory using a combination of letters and symbols.
To learn more about file permissions, I would highly recommend the following guide.
Understanding numeric mode of file permissions in Linux
There is another mode of file permissions: numeric mode.
Every file or directory on a Linux system has its own set of permissions and each permission can represented by a number.
The most common numerical representation is 3-digit notation for example 744. It consists of three numbers between 0 – 7 that represent user, group, and other respectively.
In the permission value 744, the first digit corresponds to the user, the second digit to the group, and the third digit to others. The first number is 7 then it indicates read write and execute access are granted to the owner of file.
Level | Permission | Binary | Decimal |
---|---|---|---|
Owner | rwx | 111 | 7 |
Group | r– | 100 | 4 |
Others | r– | 100 | 4 |
In this example, the owner of the file has read, write, and execute permissions (represented as rwx), which is equivalent to the binary value 111 and the decimal value 7.
The group and others only have read permission (represented as r–), which is equivalent to the binary value 100 and the decimal value 4.
Here’s a list of the octal values for different file permissions:
Decimal | Binary | Symbolic |
---|---|---|
0 | 000 | — |
1 | 001 | –x |
2 | 010 | -w- |
3 | 011 | -wx |
4 | 100 | r– |
5 | 101 | r-x |
6 | 110 | rw- |
7 | 111 | rwx |
So, if you wanted to set the owner to have read, write, and execute permissions (rwx), the group to have read and execute permissions (r-x), and others to have no permissions (—), you can use an octal value of 740
File permission examples in Linux
Here are more commonly used file permission examples.
Some file permission examples:
Permission | Owner | Group | Others |
---|---|---|---|
777 | rwx | rwx | rwx |
755 | rwx | r-x | r-x |
644 | rw- | r– | r– |
Some directory permission examples:
Permission | Owner | Group | Others |
---|---|---|---|
777 | rwx | rwx | rwx |
755 | rwx | r-x | r-x |
Check file permissions with ls command in Linux
The ls -l command is a more efficient way to check file permissions in Linux. It specifically displays file permissions in a detailed format along with other information such as file size, last modification date, owner, group, and file name.
To use this method, simply type “ls -l filename”. Replace “filename” with the name of the file or folder you are looking for.
Let’s say if I want to find the permission of a file called “testfile”, I will type “ls -l testfile”. From the following example, the permission of testfile is rw-r–r–.
% ls -l testfile
-rw-r—r— 1 howtouselinux staff 0 3 3 21:04 testfile
Property | Value |
---|---|
File type | – |
Permission settings | rw-r–r– |
User owner | howtouselinux |
Group owner | staff |
The first dash indicates that this is a regular file, and not a directory. The next three characters (rw-) indicate the owner’s permissions– in this case, the owner can read and write to the file, but cannot execute it.
The next three characters (r–) indicate the group permissions– in this case, members of the group can only read the file, and cannot write to or execute it.
The last three characters (r–) indicate the other permissions– anyone can only read the file, and cannot write to or execute it.
Find file Permissions with stat command in Linux
The “stat” command is another Linux command that can be used to get file permissions. To use “stat”, simply type “stat filename” at the command prompt. This will give you a detailed output of all the permissions for the file.
For example, the output of “stat test.txt” looks like this:
File: ‘test.txt’
Size: 0 Blocks: 0 IO Block: 4096 regular empty file
Device: fd01h/64769d Inode: 262094 Links: I-Node
uid=1001(user) gid=1001(user) mode=0644(rw-r—r—) . . .
Property | Value |
---|---|
File name | test.txt |
Size | 0 |
Blocks | 0 |
IO Block | 4096 |
File type | regular empty file |
Device | fd01h/64769d |
Inode number | 262094 |
Links | I-Node |
User ID | 1001 (user) |
Group ID | 1001 (user) |
Permission settings | 0644 (rw-r–r–) |
The first line shows the name of the file, its size, and what type of file it is. The next line shows the device number and inode number. The inode number is a unique number that identifies each file on a Linux system.
The third line shows the owner’s permissions, group permissions, and other permissions.
In this case, the file permission is 644( rw-r–r–). The owner can read and write to the file, members of the group can read the file, and anyone can read the file.
Change file permissions in Linux
Now that we know how to check file permissions. Let’s see how to change them with chmod command.
The “chmod” command stands for “change mode”. To use “chmod”, simply type “chmod +x filename” to give the file executable permissions, or “chmod u+w filename” to give the owner write permissions.
You can also use “chmod -x filename” to remove executable permissions, or “chmod u-w filename” to remove write permissions.
Command | Description |
---|---|
chmod 700 file.txt | Set permissions to read/write/execute for owner, no permissions for others |
chmod 755 file.txt | Set permissions to read/write/execute for owner, read/execute for others |
chmod 644 file.txt | Set permissions to read/write for owner, read-only for others |
chmod +x file.txt | Add execute permission for owner, group and others |
chmod u+rw,g-w,o-rwx file.txt | Add read/write for owner, remove write for group, remove all for others |
chmod -R 700 dir | Set permissions to read/write/execute for owner, no permissions for others recursively for directory ‘dir’ |
Case study: file permissions for SSH private key file
Some files in Linux have special files permissions. I will use SSH private key as an example below.
An SSH private key is an encryption key used for secure communication between a client and a server.
It is a part of the SSH (Secure Shell) protocol and is used to establish secure connections over an insecure network, such as the Internet.
The private key is kept on the client machine and is used to decrypt the data that is encrypted with the corresponding public key on the server.
If the private key file has too permissive permissions, it can be read by unauthorized users.
This would allow them to use the private key to gain access to the server, potentially compromising sensitive information.
As we can see that this is a very important file. Allowing others to read the contents of the file can be a security risk.
The permissions for a private SSH key file should be set to 600 (read/write for the owner, no permissions for others).
This ensures that only the owner of the key file can read its contents and use it for authentication.
Case study: Permission denied error in Linux
Permission denied error is a common issue I met before. This can happen for a variety of reasons, but the most common cause is that the user does not have the necessary privileges to complete an operation.
I was confused when I got this error at first time because I had always been able to access that directory without any issues.
After the cross checking, I realized that the problem was with the permissions of the directory.
I used the ls -l command to check the permissions of the directory and noticed that the x permission was not set for some reason. The x permission allows a user to enter a directory.
Then I switched to root account and used the chmod command to add the x permission.
So in order to resolve a permission denied error, the user must acquire the necessary privileges from their system administrator or root user.
Conclusion
In this blog post, I discussed two ways to check file permission in Linux- using the ls command and using the stat command. I also discussed what each of the ls output means and how you can change file permissions.
I hope you will find this guide helpful. If you face any issue while command execution or want to suggest what should I cover next, let me know in the comments.
David is a Cloud & DevOps Enthusiast. He has years of experience as a Linux engineer. He had working experience in AMD, EMC. He likes Linux, Python, bash, and more. He is a technical blogger and a Software Engineer. He enjoys sharing his learning and contributing to open-source.
howtouselinux.com is dedicated to providing comprehensive information on using Linux.
We hope you find our site helpful and informative.
How do you view file permissions?
I want to know how to see the permissions a particular file has. Which command should I type in the terminal? However, I don’t want to change it.
4 Answers 4
If you want to see the the permission of a file you can use ls -l /path/to/file command.
ls -l acroread -rwxr-xr-x 1 10490 floppy 17242 May 8 2013 acroread
What does this mean ?
First — represents a regular file. It gives you a hint of the type of object it is. It can have following values.
- d (directory)
- c (character device)
- l (symlink)
- p (named pipe)
- s (socket)
- b (block device)
- D (door)
- — (regular file)
r represents read permission.
w represents write permission and
x represents executable permission.
First combination of rwx represents permission for the owner .
Second combination of rwx represents permission for the group .
Third combination of rwx represents permission for the other of the file.
Octal notation
Permission of file can also be represented in octal notation.
In octal notation
Read or r is represented by 4,
Write or w is represented by 2
Execute x is represented by 1.
Sum of these three is use to represent the permission.
stat command can be used to view file permission in octal notation
stat -c "%a %n" acroread 755 acroread
For owner it is 4+2+1=7 (111 in binary)
For group it is 4+0+1=5 (101 in binary) and
For other it is 4+0+1=5 (101 in binary).