Using an operating system of the Linux family, you have to deal with access rights more than once. Sometimes, if it is necessary to change or delete a file or folder, the system gives us an error. It will not take much time to solve it, if, of course, you have the necessary knowledge.
Moving from Windows to Linux (Ubuntu), it was not clear to me what these symbols and numbers mean. And how do you get these rights in general. Indeed, in Windows, this kind of problem is solved much easier, with a couple of clicks in a graphical environment. And in Linux, unfortunately, maybe fortunately, it is done in a slightly different way.
And now, after reading the official documentation, having read a couple of interesting publications. I decided to write this article, maybe it will be useful to someone. I will try to explain everything as simply and clearly as possible. So, we change the access rights with the command chmod in the terminal.
Command syntax chmod.
chmod options mode file
Now let’s see what value is substituted for options, mode, file.
options takes the following values (you can not use it at all)
-R
recursive change of rights (i.e. executed on a given directory and all its contents)
-f
prohibition of error output for files, if the rights have not been changed
-v
describes in detail the changes in permissions for each file
mode – this is where exactly what rights will be set for all types of users. First, let’s take a closer look at the users. The system is divided into 3 types of users.
- current user – u isshort for user
- other users included in the group of the current user – ge. group
- all other users – o i.e. other
We got ugo, that is, user group other . Now, for each type of user, we determine which rights will be installed. Let’s get acquainted with the use of symbolic commands.
r
(read) read a file or directory contents
w
(write) write to file or directory
x
(execute) execute the file or read it
X
(special execute) execute if the file is a directory or already has execute permission for another user
s
(setuid / gid) the set SUID or SGID attributes allow the file to be run as the file owner or group owner
t
(sticky) sets a certain t-bit on a directory, changes the rule so that only the owner of this file can delete a file
Read also: How to install a LAMP server on Ubuntu 16.04
There are three rules for each type of user. The main rules used are rwx.
And the last from the chmod syntax:
file – the name of the file or folder to which these rights are set.
So let’s look at an example of use:
sudo chmod rwxr – r– filename
The rights are set by three rules to three users.
For the current user u , all rights are set – read, write and execute. For the group g and other users o are allowed read only, the dash (-) sign means no rights, we get the group to write and execute and we do not set other users.
Let’s look at an example using a comment folder.
We have considered using the command in symbolic form, and now we will consider it in numerical form.
Numerically, the file or directory is assigned absolute rights. Check out the table just below
Options for recording user rights.
octal | symbolic | rights |
– | no rights | |
1 | –X | execution only |
2 | -w- | write only |
3 | -wx | recording and execution |
4 | r– | only reading |
5 | rx | read and execute |
6 | rw- | read and write |
7 | rwx | all rights |
The following conclusion suggests itself from the table: a symbolic record in the form r– (read-only) can be replaced with only one digit 2 . Write rw- (read and write) digit 6 . Etc.
Let’s set full rights to the user on the file, and allow groups and other users to read only this file. The next two rules are completely identical.
sudo chmod rwxr – r– file
sudo chmod 744 file
To view the rights, use the command
sudo ls -l
Lists files and directories and detailed user rights information.
In the left column – access rights. Prefix – represents the file (highlighted), the prefix d denotes the directory.
The default rights are:
for files: 644 (-rw-r – r–)
for directories: 755 (drwxr-xr-x.