คำสั่งพื้นฐานของผู้ใช้งาน Unix Linux Shell เบื้องต้นตอนที่ 4 กำหนด Permission ด้วย chmod, umask, chown, chgrp
Permission คือ สิทธิ์ในการเข้าถึง Files หรือ Directories ภายใน Unix สามารถแบ่งออกได้เป็น 3 หมวดหมู่ ดังนี้
- user ที่เป็นเจ้าของ File
- group ที่เป็นเจ้าของ File
- Other คนอื่นๆ
โดยแต่ละหมวดหมู่ข้างต้น สามารถกำหนด permission ได้ 3 ประเภท ดังนี้
- (r)ead
- (w)rite
- 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
- '-' - Regular file
- 'd' - Directory
เมื่อนำไปใช้กับ Regular file
- Read permission อนุญาติให้ file สามารถเปิดได้
- Write permission อนุญาติให้ file สามารถแก้ไขได้
- Execute permission อนุญาติให้ file สามารถทำงานโดยตรงจาก shell ถ้าเป็น script หรือ binary file
เมื่อนำไปใช้กับ Directory
- Read permission อนุญาติให้ directory สามารถแสดงลิสต์ภายในได้
- Write permission อนุญาติให้ files หรือ directories สามารถถูกสร้างหรือลบภายใน directory
- Execute permission อนุญาติให้ข้อมูลใน directory สามารถเข้าถึงได้
โดย UNIX จะพิจารณาลำดับความสำคัญเรียงลำดับจาก user > group > other
$ chmod
คำสั่ง chmod ใช้เปลี่ยน permission ของ files และ directories สามารถใช้ได้ 2 แบบ
- symbols (ตัวอักษร)
- 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