Until now, when I executed the command and the characters `Pemission denied``` were displayed, I knew that I should add
sudo``` for the time being (not good).
I think that it is not good as it is, and since I learned about access authority this time, I would like to write down my memorandum.
All files handled by Linux have an owner.
You can check it with the ls -l
command. The ls
command is a command to list files and directories, but by adding the
-l``` option, you can display not only the file name but also detailed information. I will.
$ ls -l sample.txt
-rw-r--r-- 1 morimorimokenpi staff 0 4 19 22:24 sample.txt
The leading `-`
indicates the file type.
There are three types of files, and in this case it is `` `-```, so it represents a normal file.
symbol | meaning |
---|---|
- | Normal file |
d | directory |
l | Symbolic link |
The following `rw-r--r--``` is called file mode and represents the file permissions. ```rw-``` ```r--``` ```r--```And 3 are separated, each is an "owner","group",Represents the permissions of "other users". Only
r``` and ``
w are mentioned here, but there is another `` `x
as a symbol for permission.
symbol | meaning |
---|---|
r | reading(read) |
w | writing(write) |
x | Run(execute) |
The permissions of sample.txt this time are as follows.
User type | reading | writing | Run |
---|---|---|---|
Owner | Permit | Permit | Ban |
group | Permit | Ban | Ban |
Other users | Permit | Ban | Ban |
Now that you know what the file type and file mode are, how do you change the permissions? Use the chmod (short for change mode) command to change permissions. There are two ways to specify the chmod command, so I will introduce them in order.
$ chmod [ugoa] [+-=] [rwx]file name
[ugoa]
Represents which user to target.
This user specification can be omitted, but if omitted, it is considered that a
is specified.
symbol | meaning |
---|---|
u | Owner |
g | group |
o | Other users |
a | ugo all |
[+-=]
Specifies the addition or prohibition of privileges.
symbol | meaning |
---|---|
+ | Add permissions |
- | Prohibit authority |
= | Make the specified authority |
[rwx]
Represents the meaning of permissions, as explained above.
Here, as an example, let's add write permission for the group to sample.txt.
$ chmod g+w sample.txt
Before execution
$ ls -l sample.txt
-rw-r--r-- 1 morimorimokenpi staff 0 4 19 22:45 sample.txt
After execution
$ ls -l sample.txt
-rw-rw-r-- 1 morimorimokenpi staff 0 4 19 22:47 sample.txt
Added write permission for the group.
It is also possible to set the privileges of multiple users at once. Let's add execute permission to the owner and other users.
$ chmod uo+x sample.txt
$ ls -l sample.txt
-rwxr--r-x 1 morimorimokenpi staff 0 4 19 22:50 sample.txt
Execute permission has been added to the owner and other users.
In numeric mode, the permissions granted in `` `rwx``` are replaced with the numbers in the table below and added together to represent the permissions numerically.
$chmod octadecimal number filename
Numbers | meaning |
---|---|
4 | reading(read) |
2 | writing(write) |
1 | Run(execute) |
It looks like this in the figure (I'm sorry it's dirty lol)
Let's actually change the permissions of sample.txt.
$ chmod 755 sample.txt
Before execution
$ ls -l sample.txt
-rw-r--r-- 1 morimorimokenpi staff 0 4 19 23:14 sample.txt
After execution
$ ls -l sample.txt
-rwxr-xr-x 1 morimorimokenpi staff 0 4 19 23:15 sample.txt
In this way, the numeric mode is an absolute specification that changes the value to the new permission regardless of the original permission. On the other hand, the symbol mode is a relative specification that does not change the permissions other than those specified. It may be better to use it properly according to the changed part.
[New Linux Textbook](https://www.amazon.co.jp/%E6%96%B0%E3%81%97%E3%81%84Linux%E3%81%AE%E6%95%99% E7% A7% 91% E6% 9B% B8-% E4% B8% 89% E5% AE% 85-% E8% 8B% B1% E6% 98% 8E / dp / 4797380942)