Linux file and directory permissions

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

How to read authority

$ 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.

File

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).

Default permissions

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

Write permission

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'?

Read permission

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.

Execution authority

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

directory

Default permissions

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

Write permission

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.

Read permission

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.

Execution authority

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

Linux file and directory permissions
[Linux] File and directory operation commands
Device and Linux file system
Meaning of Linux directory permissions
Linux permissions
Linux directory structure
Linux [directory command]
Linux directory structure
[Linux] File search
Linux directory hierarchy
Finding out about file permissions and superuser
Replace the directory name and the file name in the directory together with a Linux command.
[Linux] Zip file compression and decompression commands [Vim]
[Samba] File sharing between Linux and Windows machines
Adjust file permissions with the Linux command chmod
OS and Linux distribution
Linux (about directory path)
[Python] File / directory operations
Linux permissions in Java
Linux permissions [under investigation]
Meaning of Linux permissions
Linux command [File operation]
Hack Linux file descriptors
Linux: files and directories
Linux Study Session 1st: Virtual Console and File Manipulation
Linux Study Group 5th: Directory Manipulation, Links and Inodes
datetime and file write and backup
Studying Linux commands and frustration
Read and write csv file
CLI and Linux basic terms
Linux (about files and directories)
About LINUX files and processes
Recording and playback on Linux
Read and write a file
File access under the directory
Write and read a file
[Linux] Directory under the root
Search the file name including the specified word and extension in the directory
Linux study session 3rd: File deletion prohibition setting and search function
Dig the directory and create a list of directory paths + file names
"Chmod" command to set file and folder permissions and access permissions (setting values)
Differences between Windows and Linux directories
Basic knowledge of Linux and basic commands
Linux study session 2nd: File operation
File permissions "T" are sticky bits
Are macOS and Linux completely different?
(Windows10) Install Linux environment and gnuplot.
[linux] Swap Caps Lock and Ctrl
Switch argparse file specification and pipeline
Permission and ownership change command [Linux]
Linux archiving and compression (tar and gzip)
[Linux] Frequently used Linux commands (file operation)
Python CSV file reading and writing
Python memo ① Folder and file operations
Efficient use of Linux file system
[NFS] File sharing between Linux hosts
Note: Linux concepts and minimum commands
Linux file server construction (Ubuntu & Samba)
Device, Linux file system, FHS ① Memorandum Creation of partition and file system / maintenance of file system integrity
View the full path (absolute path) of a file in a directory in Linux Bash
A command to specify a file with a specific name in a directory with find and mv, cp, or gzip it (linux)