It's easy to forget the behavior of each file and directory permission on Linux, so I'll summarize it for the sake of explanation.
The one to change with chmod
$ ls -l
You can see the permissions of the files and directories in the current directory
drwxrwxrwx. 2 core core 4096 Mar 22 11:02 dir
-rwxrwxrwx. 1 core core 0 Mar 22 11:02 file
Indicates whether the most hidden d
is a directory,
The rwx
after it is a set of 3 characters.
From left to right,
Privileges of owned users, privileges of groups belonging to owned groups, privileges of other users
Represents.
r
is read
w
is written
x
runs
With the authority of
-rw-r--r--. 1 core core 0 Mar 22 11:05 foo
If it looks like the above
For the file foo
A user named core
has rw
(read / write) privileges,
Indicates that other users have only r
(read) authority.
The root user has all the privileges without any questions.
It is often used when you want to create a file that you do not want to be read by anyone other than a specific user (group) or a file that can only be updated by a specific user (group).
Creating a file in the terminal gives you 0644
privileges
core@dev ~/work $ touch file
core@dev ~/work $ ls -l
total 4
-rw-r--r--. 1 core core 0 Mar 22 11:08 file
core@dev ~/work $ chmod 444 ./file
core@dev ~/work $ ls -l
total 4
-r--r--r--. 1 core core 0 Mar 22 11:08 file
core@dev ~/work $ echo "hogehoge" > file
-bash: file: Permission denied
Used when dealing with files that you do not want anyone other than a specific user to write to. Files that are corrupted if written poorly. I get a warning when I try to delete a file that I do not have write permission for.
core@dev ~/work $ touch file
core@dev ~/work $ chmod 400 file
core@dev ~/work $ ls -l
total 4
-r--------. 1 core core 0 Mar 22 11:20 file
core@dev ~/work $ rm file
rm: remove write-protected regular empty file 'file'?
core@dev ~/work $ chmod 000 ./file
core@dev ~/work $ ls -l
total 4
----------. 1 core core 0 Mar 22 11:08 file
core@dev ~/work $ cat file
cat: file: Permission denied
It is used when dealing with a file that you do not want anyone other than the user to read. When dealing with information including confidential information.
Permission to execute the file Used for scripts and binaries
It doesn't make sense to give it to just a text file that can't be executed
core@dev ~/work $ echo "hogehoge" > file
core@dev ~/work $ chmod 700 ./file
core@dev ~/work $ ls -l
total 8
-rwx------. 1 core core 9 Mar 22 11:22 file
core@dev ~/work $ ./file
./file: line 1: hogehoge: command not found
When executed with bash
core@dev ~/work $ echo "echo hogehoge" > file
core@dev ~/work $ chmod 700 ./file
core@dev ~/work $ ls -l
total 8
-rwx------. 1 core core 14 Mar 22 11:22 file
core@dev ~/work $ ./file
hogehoge
Mostly granted so that it can be executed as a terminal or process
core@dev ~/work $ mkdir foo
core@dev ~/work $ ls -l
total 8
drwxr-xr-x. 2 core core 4096 Mar 22 11:25 foo
The default is different from the file, except for the beginning
It will be rwxr-xr-x
, so in the case of a directory
Owned users have rwx
privileges
Other users have r-x
privileges
core@dev ~/work $ mkdir foo
core@dev ~/work $ ls -l
total 8
drwxr-xr-x. 2 core core 4096 Mar 22 11:38 foo
core@dev ~/work $ chmod 500 ./foo/
core@dev ~/work $ ls -l
total 8
dr-x------. 2 core core 4096 Mar 22 11:38 foo
core@dev ~/work $ touch foo/test
touch: cannot touch 'foo/test': Permission denied
If you do not have write permission, you will not be able to place files under the directory Use when you want to protect by directory instead of by file.
core@dev ~/work $ mkdir foo
core@dev ~/work $ chmod 000 ./foo/
core@dev ~/work $ ls -l foo/
ls: cannot open directory 'foo/': Permission denied
Since the directory cannot be accessed, it cannot be referenced with ls
etc.
core@dev ~/work $ mkdir foo
core@dev ~/work $ touch foo/bar
core@dev ~/work $ chmod 600 ./foo/
core@dev ~/work $ ls -l foo/
ls: cannot access 'foo/bar': Permission denied
total 0
-????????? ? ? ? ? ? bar
If you do not have execute permission for the directory, you will not have access to the files under it.
Therefore, the file information itself cannot be accessed.
You cannot read or write because you cannot access the information in the file and the authority is unknown.
You have read and write permissions to the directory, so you can rewrite the permissions on the directory with chmod
.
Recommended Posts