You can get the advice from IT HELP DESK

SYSTEM[LINUX] Permission command

Robert
14 Jan 2019
<head>Disk Management, Server Monitoring, System management, server monitoring, server management, system management, system monitoring, </head>

[LINUX] Permission command


Hello ~!

This time, let's look at how to change permissions and change ownership of files and directories under Linux.

It is important to understand it correctly because it is important for security!


1. Verify file and directory permissions

#ls -al (file name)

If you omit the file name, it shows information about all files in the connected directory.


  ① Permissions of files and directories

The first letter consists of "d" means directory, "l" means link, "-" means file.

The remaining nine digits represent the permissions of the files and directories.


     Ⓐ File type: "d" is a directory, "l" is a link file, and "-" indicates a regular file.

     Ⓑ Indicates the authority for the User Owner. 

     Ⓒ Indicates the authority for the Group.

     Ⓓ Represents the authority for Others. 


The definition of rwx below is a concept that is often used when using commands.

Sign
Code
Meaning
r
4
Read
w
2
Write
x
1
Execute
-
-
None


For example, "rwx" represents 7, which means that both read, write, and execute are allowed. rx is expressed as 5, meaning that only read and execute are permitted.


  ② Hard link number

Indicates the number of linked files.


  ③ Owner

Indicates the owner of the file or directory.


  ④ Owner Group

Indicates the owner group of the file or directory.


  ⑤ File size

Indicates the file size in bytes. When the capacity of a file increases, it becomes difficult to check in bytes. If this is the case, add the -h option to convert Sarim to a readable unit.


  ⑥ Time

Indicates when the file was created. If the file is modified, it indicates the most recently modified time.


  ⑦ File and directory names



 2. Change file permissions with "chmod" command. 

The default permissions are given when the file is created. If you want to change the default permissions, please follow the instructions below!

#chmod [permission value to be changed] [file to be changed]

The changed permission values can be changed individually or at once.



  2.1 Changing Permissions Individually

u(user), g(group)o(other users)a(whole), + (more privilege),(Remove Control) 

Example:

chmod g+w file_name

Grant [Write] permission to the group.


chmod o-r file_name

Takes [Read] permission to other users. 


chmod u+rwx file_name

Grant [Read&Write] permission to the User-owner. 


  2.2 Changing permissions at once

It is a numerical representation of the same value as rwxrxwrxw mentioned above.

Converts each permission symbol to a number.

For example, if rwxr-xr-x, then rwx rx rx 4 + 2 + 1 | 4 + 1 | 4 + 1 = 755 This will convert the value.


Example:

chmod 755 file_name 

7 (4 + 2 + 1) (rwx) for user (owner), 5 (4 + 0 + 1) (rx) for group and others 


chmod 700 *

Grant permissions to all files and folders in the current location to the user rwx, group and rest --- 



For directories, you can use the  -R option to change the permissions of all directories and files in a subdirectory.

Example:

chmod -R 755 www 

Modify the permissions of all files and directories within the www directory



3. Changed ownership with "chown" command

If you need to transfer the owner of the file, you can change the owner. You can change ownership as well as the owner. 

#chown [owner of change (group name)] [file to change]

Example:

chown peter file1 

Change user owner of file1 to peter 


chown haha:haha2 file2    

Change user owner to haha and owner group to haha2


chown -cR nobody:nobody dirl 

Change "dirl" folder and all the files in it, the owner of the directory, and the owning group 


As with chmod, chown can be used to change the permissions of all directories and files in a subdirectory by using the -R option.


Example:

chmod -R 755 www 

Modify the permissions of all files and directories within the www directory