[LINUX] About Permission

Caution </ b>: This article is for making security changes. We are not responsible for any problems or damages caused by this article, so please be sure to be sure to do so within your own responsibility </ b> when executing commands. I will. </ font>

Terminology used in this article

file_put_contents () → Adds contents to the file with the specified path when PHP is executed. file_get_contents () → When PHP is executed, the contents of the file with the specified path are called. Permission → Permission to operate directories and files. (Recursive in Japanese)


The other day, when I was learning PHP, I had a scene where I used file_put_contents () and file_get_contents (), but I got hooked there, so I will leave a solution.

In conclusion, it was a Permission problem.

How to check Permission

I'm using MacOS, so first open a terminal. Next, move up one level in the directory where you want to check Permission, and enter the following command.

$ ls -la

Then, the following notation will appear in a row for each directory and file.

drwxrwxrwx 1 username admin 1918 4 21 14:22 example.html

The leftmost column shows the Permission of the directory or file displayed on the right. (Other than that, you don't have to worry too much.) The leftmost d seems to be complicated, so ignore it, and the important part is the part where three rwxs continue.

  • The first `rwx` is read permission
  • The second `rwx` is write access
  • The third `rwx` is execute permission

And the permissions that each of `r`,` w`, and `x` shows are as follows.
  • `r` is the creator
  • `w` is the editor under the author group
  • `x` is neither the creator nor the editor (unspecified number).


## How to change Permission (everything under the current directory) After successfully checking `Permisson`, it's time to change the Permission of the directory or file in question. Execute the following command while keeping the current directory you moved earlier.
$ chmod 765

First, type chmod (change mode) to make Permission change </ b> possible. Then you can change the Permission of all directories and files under the current directory by entering any number </ b> and executing.
By the way, arbitrary numbers </ b> can be determined as follows.

rwx rwx rwx r-- -w- --x rwx rw- r-x
 ↓    ↓    ↓   ↓  ↓  ↓   ↓   ↓   ↓
 7      7      7  4    2    1  7     6     5
        ↓       ↓        ↓
       777      421       765

It's easy to understand if you look at the middle column of the table, but each `rwx` is numbered.
  • `r` is 4
  • `w` is 2
  • `x` is 1
Therefore, if you want to make it `rwx`, you can calculate 7 with 4 + 2 + 1 . And if you want to make it `rwxrwxrwx`, you can type 777 with 7 , 7 and 7 . ..
* However, the 777 introduced above is a security that anyone can read, write, and execute . It is deprecated because it is extremely dangerous above.
Also, `Permission` is often saved in some necessary meaningful state by the creator, so if you make a trial change, use` chmod` again to restore the original `Permission`. It's a good idea to keep it.

How to change Permission (specified file only)

If you want to change the Permission of any directory or only the file specified here instead of changing all the Permission under the current directory, execute the following command.

$ chmod 765 example.html

In this case, change Permission of example.html under the current directory to 765 </ b>.

In my case, I could change the Permission of only one file like this to allow the execution offile_put_contents ()andfile_get_contents (), which was the problem this time, and solve it. I did.

Summary

This is a list of commands introduced this time.

$ ls -la
$ chmod 765
$ chmod 765 example.html

Again, the Permission setting is an important security-related setting </ b>, so carefully when changing . / b> Please go. </ font>
Thank you for reading until the end!

Recommended Posts