The chmod command is used to change the permissions on files and directories.
It also explains how to read file and directory information.
This is written by a Japanese who can't speak English with the help of translation application. Sorry if it's not good.
Commands to change permissions
The chmod command to change file and directory permissions is used in this way.
chmod 777 target
Three numbers, from left to right.
Logged in users
group
Ohter Users
permissions.
The values are from 0 to 7 and are shown in the following table.
Table of permination values
value | binary digits | symbol | meaning |
---|---|---|---|
0 | 000 | --- | unauthorized |
1 | 001 | --x | execute |
2 | 010 | -w- | read |
3 | 011 | -wx | write, execute |
4 | 100 | r-- | read |
5 | 101 | r-x | read, execute |
6 | 110 | rw- | read, write |
7 | 111 | rwx | read, write, execute (all the authority) |
For example.
Logged in users: read, write, execute (all)
group: read, execute
Ohter Users: execute
If you want to add this permission to the sample.txt file, do this.
chmod 751 sample.txt
The non-numeric symbols can also be used, but they are easier to use as they are grouped together and require only three characters.
If you want to change the permissions of a directory, you can use the "-R" option to change the files and directories under it.
chmod -R 751 sample
How to read file and directory information in Linux
I used chmod to specify the permissions as a number 0-7. I only showed the resulting table, but I didn't explain the meaning of it.
The meaning of the numbers is easier to understand if you look at how files and directories are displayed in Linux.
Let's look at it first.
"ls" command will show you the information about the files and directories.
ls -l
-rw-rw-r-- 1 xxxxx yyyyy 1875611 Aug 16 23:57 zzzzzz
Here's the result. I'll explain the details one by one.
File Types
-rw-rw-r-- 1 xxxxx yyyyy 1875611 Aug 16 23:57 zzzzzz
The first character is the type of file.
- | file |
d | directory |
l | symbolic link |
Permission
-rw-rw-r-- 1 xxxxx yyyyy 1875611 Aug 16 23:57 zzzzzz
Next, a nine-character string follows, uninterrupted by the file type.
File access rights.
- Who did?
- What can you do?
will be displayed.
Who owns the file?
"Who?" separates 9 characters by 3 characters.
owner | group | Users other than the owner |
〇〇〇 | ××× | △△△ |
The "owner" is the user who created the file and the user belongs to some group.
The last one is a user other than the file creator.
What can a non-ownership user do?
Set this up.
What can the file do?
"What can I do? are these three.
Read from file | Write to file | Run the file |
Specify three permissions, one character for each of the three users.
There are two ways to specify permissions: alphabetic and numerical.
The table summarizes.
permission | alphabet | numerics |
---|---|---|
Read | r | 4 |
Write | w | 2 |
Execute | x | 1 |
None | - | 0 |
Alphabetical designation
The alphabetic symbol is a single letter of the word.
- read
- write
- execute
Numeric designation
The number is expressed as a binary number with three digits.
The order of the numbers looks like this
Read | Write | Execute |
Each one uses the value of a flag of 1. It's easy to remember this table.
100 | Read | 4 |
010 | Write | 2 |
001 | Execute | 1 |
Specify multiple permissions numerically
It's readable and executable.
Thus, if you want to give multiple permissions, add them together.
Then all permissions would look like this.
1 + 2 + 4 = 7
As we will see, the values we use are 0-7.
A table of all permission patterns
All permission patterns are summarized in the table.
Value | Binary digits | Symbol | Permissions to allow |
---|---|---|---|
0 | 000 | --- | No authority at all. |
1 | 001 | --x | Execute |
2 | 010 | -w- | Write |
3 | 011 | -wx | Write, Execute |
4 | 100 | r-- | Read |
5 | 101 | r-x | Read, Execute |
6 | 110 | rw- | Read, Write |
7 | 111 | rwx | Read, Write, Execute (All) |
Again, these three character permissions are assigned to the owner, group and other users.
-rw-rw-r-- 1 xxxxx yyyyy 1875611 Aug 16 23:57 zzzzzz
Red: owner
Green: group
Blue: Users other than the owner
Three color-coded permissions can be made.
Permissions error
If you try to operate on a file or directory without permission, an error occurs.
Permission denied
Number of hard links
-rw-rw-r-- 1 xxxxx yyyyy 1875611 Aug 16 23:57 zzzzzz
Next to the permissions is the number of hard links, separated by a single space.
There's not much point in knowing the details here, so this is about as much as you can remember.
- file, the value is 1
- directory, the approximate number of files and directories underneath
In Linux, there are two hard links directly under the directory that count.
. | current directory |
.. | The parent (one level up) directory |
It's a number of hard links, so symbolic links do not count.
In that sense, it won't be an exact number of files and directories.
Names of owners and groups
-rw-rw-r-- 1 xxxxx yyyyy 1875611 Aug 16 23:57 zzzzzz
Next to the number of hard links, space out one and show two names.
file owner's name
The name of the group to which the file owner belongs
Leave one space between the owner's name and the group's name.
The owner of the file is the creator of the file
File Size
-rw-rw-r-- 1 xxxxx yyyyy 1875611 Aug 16 23:57 zzzzzz
Next to the owner's name and group name is the file size, with one space left blank.
The unit is bytes and 0 for a directory.
Update Date
-rw-rw-r-- 1 xxxxx yyyyy 1875611 Aug 16 23:57 zzzzzz
Next to the file size is the date and time of the file's last modification, with a single space after the file size.
If it's a directory, it's the date and time the file was last modified when the directory configuration changed.
If it is a new file, it is the date and time of its creation.
The name of the file or directory
-rw-rw-r-- 1 xxxxx yyyyy 1875611 Aug 16 23:57 zzzzzz
Lastly, the name of the file or directory, leaving one space blank.
Finally.
The 'chmod' command has other options than '-R', but you won't use them very often. The rest of the usage is simple, so it's easy to learn.
Just look at the first table I showed you, so you don't have to struggle to remember it.