How to check and change Linux permissions (permissions) (chmod) (I want to be saved from Permiss on denied)

Introduction

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.

How to check file permissions

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

File type

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

File mode

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.

Permission symbol and meaning

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

How to change permissions

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.

Change by symbol mode

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

Change by numerical mode

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) 5QuIWXyCRbuVBSR1%OZU8w_thumb_21f8.jpg

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.

reference

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

Recommended Posts

How to check and change Linux permissions (permissions) (chmod) (I want to be saved from Permiss on denied)
I want to use Linux on mac
I want to format and check Python code to my liking on VS Code
I want to log file I / O on Linux
I want to be notified when the command operation is completed on linux!
I want to use OpenJDK 11 on Ubuntu Linux 18.04 LTS / 18.10
I want to change the symbolic link destination of / lib64 from / usr / lib64 to / my-lib64 on CentOS
I want Sphinx to be convenient and used by everyone
I want to use shortcut translation like DeepL app on Linux
How to display PDF resolution and detailed information on Linux (pdfinfo)
I want to hack Robomaster S1 ① Rooting and file configuration check
How to install VMware-Tools on Linux
How to check Linux OS version
I want to pass an argument to a python function and execute it from PHP on a web server
[Linux] How to subdivide files and folders
How to install aws-session-manager-plugin on Manajro Linux
How to update php on Amazon linux 2
How to display emoji on Manjaro Linux
How to install packages on Alpine Linux
How to install Anisble on Amazon Linux 2
How to operate Linux from the console
How to update security on CentOS Linux 8
How to install php7.4 on Linux (Ubuntu)
How to find large files on Linux
I want to develop Android apps on Android
Useful for changing permissions on Linux! How to count up to 31 with one hand.
I want to visualize where and how many people are in the factory
I want to drop a file on tkinter and get its path [Tkinter DnD2]
I want to write an element to a file with numpy and check it.
I want to convert horizontal text to vertical text and post it on Twitter etc.
I want to realize something like AutoHotkey with AutoKey on Ubuntu (Kali Linux)