A PHP Error was encountered

Severity: Notice

Message: Only variable references should be returned by reference

Filename: core/Common.php

Line Number: 257

คำสั่ง Unix Linux Shell เบื้องต้นตอนที่ 4 กำหนด Permission ด้วย chmod, umask, chown, chgrp | Share

คำสั่ง Unix Linux Shell เบื้องต้นตอนที่ 4 กำหนด Permission ด้วย chmod, umask, chown, chgrp

คำสั่ง Unix Linux Shell เบื้องต้นตอนที่ 4 กำหนด Permission ด้วย chmod, umask, chown, chgrp

คำสั่งพื้นฐานของผู้ใช้งาน Unix Linux Shell เบื้องต้นตอนที่ 4 กำหนด Permission ด้วย chmod, umask, chown, chgrp

Jul 01, 2016

แก้ไขล่าสุด มีผู้อ่าน 48,515  |  LINUX UNIX

คำสั่งพื้นฐานของผู้ใช้งาน Unix Linux Shell เบื้องต้นตอนที่ 4 กำหนด Permission ด้วย chmod, umask, chown, chgrp

 

Permission คือ สิทธิ์ในการเข้าถึง Files หรือ Directories ภายใน Unix สามารถแบ่งออกได้เป็น 3 หมวดหมู่ ดังนี้

  1. user ที่เป็นเจ้าของ File
  2. group ที่เป็นเจ้าของ File
  3. Other คนอื่นๆ

โดยแต่ละหมวดหมู่ข้างต้น สามารถกำหนด permission ได้ 3 ประเภท ดังนี้

  1. (r)ead
  2. (w)rite
  3. e(x)ecute

 

Permission บน UNIX สามารถแสดงด้วย ตัวอักษร หรือ ตัวเลขฐานแปด (octal digits) แสดงตามตารางด้านล่าง

Octal Symbol Permission
0 --- No Permissions
1 --x Execute
2 -w- Write
3 -wx Write and Execute
4 r-- Read
5 r-x Read and Execute
6 rw- Read and Write
7 rwx Read, Write, and Execute

ผลของ Permission จะขึ้นกับว่าเป็น Files หรือ Directories 

  1. '-'    -   Regular file
  2. 'd'   -   Directory

 

เมื่อนำไปใช้กับ Regular file

  1. Read permission อนุญาติให้ file สามารถเปิดได้
  2. Write permission อนุญาติให้ file สามารถแก้ไขได้
  3. Execute permission อนุญาติให้ file สามารถทำงานโดยตรงจาก shell ถ้าเป็น script หรือ binary file

เมื่อนำไปใช้กับ Directory

  1. Read permission อนุญาติให้ directory สามารถแสดงลิสต์ภายในได้
  2. Write permission อนุญาติให้ files หรือ directories สามารถถูกสร้างหรือลบภายใน directory 
  3. Execute permission อนุญาติให้ข้อมูลใน directory สามารถเข้าถึงได้

 

โดย UNIX จะพิจารณาลำดับความสำคัญเรียงลำดับจาก user > group > other 


$ chmod

คำสั่ง chmod ใช้เปลี่ยน permission ของ files และ directories สามารถใช้ได้ 2 แบบ

  1. symbols (ตัวอักษร)
  2. octal values (ตัวเลขฐานแปด)

 

วิธีการใช้แบบ Symbols (ตัวอักษร)

$ ls -l quotations
-rwxr-xr-x         1   olan   group1  346  Apr 27 03:32  quotations
$ chmod go-rx quotations
$ ls -l quotations
-rwx------         1   olan   group1  346  Apr 27 03:32  quotations

go อ้างอิงถึงหมวดหมู่ group, other

- หมายถึงการลบหรือเอาออก

rx อ้างอิงถึง permission เป็น read, execute

มารวมกันหมายถึง "ให้ทำการลบ permission แบบ read, execute ของ group, other ที่ file quotations"
 

$ chmod ugo+rwx quotations
$ ls -l quotations
-rwxrwxrwx         1   olan   group1   346   Apr   27 03:32   quotations
ugo อ้างอิงถึงหมวดหมู่ user, group, other
+ หมายถึงการเพิ่ม
rwx อ้างอิงถึง permission เป็น read, write, execute
มารวมกันหมายถึง "ให้ทำการเพิ่ม permission แบบ read, write, execute ของ user, group, other ที่ file quotations"
 
วิธีการใช้แบบ octal values (ตัวเลขฐานแปด)
$ chmod 700 quotations
$ ls -l quotations
-rwx------         1   olan   group1   346   Apr   27 03:32   quotations

ตัวแรก 7 เป็นการกำหนด permission ให้ user เป็น rwx

ตัวแรก 0 เป็นการกำหนด permission ให้ group เป็น ---

ตัวแรก 0 เป็นการกำหนด permission ให้ other เป็น ---

 

$ chmod 754 quotations
$ ls -l quotations
-rwxr-xr--         1   olan   group1   346   Apr   27 03:32   quotations

ตัวแรก 7 เป็นการกำหนด permission ให้ user เป็น rwx

ตัวแรก 5 เป็นการกำหนด permission ให้ group เป็น r-x

ตัวแรก 4 เป็นการกำหนด permission ให้ other เป็น r--

 

ถ้าต้องการเปลี่ยนแปลง Permission ทั้งหมดใน Directory ปัจจุบันไม่ลงไปถึง sub directories สามารถใช้ * ช่วยได้ครับ

$ chmod go-rwx *
$ chmod 700 *

 


ถ้าต้องการเปลี่ยนแปลง Permission ทั้งหมดใน Directory ปัจจุบันและ sub directories ทั้งหมด สามารถใช้ Option : -R (Recursive) ช่วยได้ครับ

$ chmod -R u+r Email

 


$ umask

คำสั่ง umask ใช้สำหรับตั้งค่าเมื่อมีการสร้าง files หรือ directories ใหม่จะมีค่าตามที่กำหนดในคำสั่ง umask

$ umask 022

หลังจากเรียกใช้คำสั่ง umask 022 แล้ว file หรือ directories ที่สร้างใหม่จะมี permission เป็น 022 เสมอ

* 022 = -----w--w-


$ chown

 
คำสั่ง chown ใช้สำหรับการเปลี่ยนแปลง Owner
$ chown north contact_info

ถ้าต้องการเปลี่ยนแปลง Owner ทั้งหมดใน Directory สามารถใช้ Option : -R (Recursive) ช่วยได้ครับ

$ chown -R north Project

 


$ chgrp

คำสั่ง chgrp ใช้ในการเปลี่ยน group 
$ chgrp students data_file
$ ls -l data_file
-rwxrwx---          1   north   students   812   Jan   27 11:20   data_file

ตอนอื่นๆ

คำสั่ง Unix Linux Shell เบื้องต้นตอนที่ 1 | date, cat, touch, who, finger, exit

คำสั่ง Unix Linux Shell เบื้องต้นตอนที่ 2 | ls, pwd, mv เกี่ยวกับ Files และ Directories

คำสั่ง Unix Linux Shell เบื้องต้นตอนที่ 3 | cp, ln, rm, mkdir, rmdir, file เกี่ยวกับ Files และ Directories

คำสั่ง Unix Linux Shell เบื้องต้นตอนที่ 4 กำหนด Permission ด้วย chmod, umask, chown, chgrp

คำสั่ง Unix Linux Shell เบื้องต้นตอนที่ 5 วิธีดู File ขนาดยาว | pg, more, less, head, tail


แบ่งปัน

ชอบ +1

บันทึก

ฝากคำถาม คำแนะนำ ได้ที่

Facebook : Share.OlanLab.Com
LINE ID : @olanlab
อีเมล์ : olan@olanlab.com

หลักสูตร

สอนคำสั่ง Unix Linux Shell เบื้องต้นที่ควรรู้

รวมคำสั่ง Unix Linux Shell สำหรับผู้ดูแลระบบ นักพัฒนาโปรแกรม พร้อมตัวอย่างการใช้งานจากเหตุการณ์จริง
Software Engineer, Inventor, Writer, Trainer
Share คลังความรู้ด้านเทคโนโลยี สารสนเทศ นวัตกรรมคอมพิวเตอร์ สอนเขียนโปรแกรม Php Java Html CSS Javascript C C++ Objective-C และอื่นๆ บนระบบปฏิบัติการ Window Linux Unix CentOS IOS Android
โดยผู้เขียนที่มีความเชี่ยวชาญเฉพาะ ให้บริการพื้นที่สำหรับนักเขียนที่ต้องการแบ่งปันความรู้ พร้อมให้คำปรึกษาแก่ผู้ที่สนใจ โดยไม่เสียค่าใช้จ่าย