Linux — View and Edit File Permissions.
When working with servers most of the time you will work with linux servers (Ubuntu, Debian, etc.). In order to change, view or create files you need the right permissions. To get a basic understanding of what that means, I will tell you how to view, read and interpret these permissions.
Viewing Permissions
An easy way to view permissions for files inside a folder you can use the ls command with the o or l flag like so:
Move to a directory that has files in it and type the above command into your terminal. It will generate multiple lines of output that look similar to this:
# permissions hard links user group of user file size change date file/folder name drwxrwxr-x 9 toscani toscani 288 11 Nov 09:07 public
- The first letter, in this case d
- Letters two to four rwx
- Letters five to seven rwx
- and finally eight to ten r-x
Group one states if the entry describes a file — , a directory d or a link l . The other three groubs describe the permissons each type of user has. On linux we distinguish between the user, the group and others — in this order. Hence drwxrwxr-x means:
You are maybe wondering what «executing a directory» means. It basically lets the you open that directory. «Reading a directory» on the other hand means «to be able to see» the directory, e.g. being able to list it with the ls command.
Changing permissions
Now that you know how to view file permissions, I will tell you how to change them. For this we use the chmod (change mode) command. There are two different methods for this. The symbolic method and the absolute method.
If you have ever seen the chmod command in a tutorial before the symbolic method is the one with the letters, the absolute method is the one with the numbers ;).
Symbolic method
The symbolic method combines an access class with an operator and an access type to set permissions. A common example is to make a file executable for everybody:
chmod a+x my_file.sh # or, as `a` is the default chmod +x my_file.sh
Access class | Operator | Access Type |
---|---|---|
u , user | + , to add access | r , read |
g , group | — , remove access | w , write |
o , other | = , set exact access | x , execute |
a , all |
These parameters can be combined in many different ways. Although I can´t tell you what the right configuration for a specific usecase is, the following examples might help you to find that out yourself.
u always means the current user and g always refers to the group the current user is in.
# remove read and write permissions for all except you chmod go-rw my_file.sh # set access to only read for everybody chmod a=r myfile.sh # remove write access from and add execution rights for the group chmod g-w+x myfile.sh # grant read permission for a directory and all files in it to all # -R is used as a flag, not as a parameter to describe the permissions chmod -R +r mydir
Absolute method
In contrast to the symbolic method, the absolute method sets all permissions at once by using a three digit number. Each of the digits is the sum of it´s individual permissions. Let´s use the following example:
Base permissions
Combinations
Permission | Number |
---|---|
7 | read, write and execute (4 + 2 + 1) |
6 | read and write (4 + 2) |
5 | read and execute (4 + 1) |
3 | execute and write (1 + 2) |
The first number represents the user, the second the group and the last represents others. Hence, the example above sets the following permissions:
- read, write and execute for the user
- read, write and execute for the group
- read for others
Now you know how to read and change file and directory permissions on linux. If you need further information just consult the man page by typing man chmod into the command line.
If you have questions or feedback, please leave a comment :).
Top comments (2)
Howtouselinux.com is a website that publishes Linux server tutorials. We will cover the Linux command line in detail, along with tutorials on cloud-related technologies, DevOps tools etc
How did you add that table to your post? Thanks.
1 like Like Comment button
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).