Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> ระบบ >> Mac

วิธีใช้ File Flags แก้ไขพฤติกรรมไฟล์บน macOS

วิธีใช้ File Flags แก้ไขพฤติกรรมไฟล์บน macOS

Flags ใช้โดย macOS จำกัดวิธีเปลี่ยนแปลงไฟล์ ตั้งค่าต่างจาก Permissions ทำงานขนานกับคำสั่ง chown และ chmod ใช้ chflags เปลี่ยน Flags และ ls ดู Flags Unix ระบบคล้ายกัน แตมี Options มากกว่า เรียกว่า "Attributes"

ดู Flags ที่ตั้งไว้บน macOS

ใน Terminal ใช้ Flag ls ดู Flags ที่มี

ls -lO ~/Library
drwx------@ 88 alexander staff hidden 2992 Jan 25 14:01 Library

หากไม่มี Flags จะแสดง Dash (-)

ls -lO ~/Library/Caches
drwx------+ 234 alexander staff - 7956 Jan 25 13:03 Caches

Flags ที่ตั้งไว้ ลบได้ด้วย Inverse ตามด้านล่าง

ตั้งและลบ Flags บน macOS

วิธีใช้ File Flags แก้ไขพฤติกรรมไฟล์บน macOS

Flags มักเป็นปัญหา เพราะตั้ง Extra-permissions ง่ายหลุดหาย เจอไฟล์ลบไม่ได้ ไม่ว่าสิทธิ์ User สูงแค่ไหน? อาจต้องลบ Flags 1-2 ตัว

Flags ที่ตั้งได้บน macOS รายการเต็ม พร้อมคำอธิบาย ฟังก์ชัน ส่วนใหญ่ตั้งได้เฉพาะ Owner หรือ Superuser sappnd และ schg เฉพาะ Superuser เพราะเป็น System-level Flags มีแค่ Hidden Flag ที่ตั้งได้โดยไม่ต้องยกระดับสิทธิ์

ตั้ง Flags บน macOS

คำสั่งตั้ง Flag: recursive flag -R สำหรับ Directory-level

sudo chflags -R [flag] /usr/bin/local
sudo chflags [flag] /usr/bin/local/mnt.sh
chflags -R hidden ~/Desktop

ตัวอย่าง ตั้ง Nodump Flag:

sudo chflags nodump /usr/bin/local/tty.sh

ตั้ง/ลบ Flag ได้ทีละตัวต่อคำสั่ง chflags

  • opaque ตั้งโฟลเดอร์เป็น Opaque ผ่าน Union Mount (วิธีเก่า View หลาย Directory พร้อมกัน)
  • nodump ป้องกันไฟล์/โฟลเดอร์ไม่ถูก Dump ระหว่างคำสั่ง dump Backup ระบบ
  • sappnd, sappend System Append-only Flag: เพิ่มข้อมูลได้ แตแก้ไข/ลบไม่ได้ ลบ Flag นี้ต้องเข้า Single User Mode
  • schg, schange, simmutable System Immutable Flag: ล็อกการเปลี่ยนแปลงไฟล์ทุก User ทุกสิทธิ์ ลบ Flag ต้อง Single User Mode
  • uappnd, uappend User Append-only Flag: Owner ตั้งได้ Owner ลบได้โดยไม่ยกระดับสิทธิ์ ใช้บ่อยกว่า sappnd/schg เพราะ Security ต่ำกว่า
  • uchg, uchange, uimmutable User Immutable Flag: ความสัมพันธ์กับ System Immutable เหมือน uappnd กับ sappnd
  • hidden ตั้ง Hidden Flag: ซ่อนไอเทมใน Finder GUI และคำสั่ง ls

ลบ Flags บน macOS

ลบ Flag: ตั้งค่าตรงข้าม โดยทั่วไปคือเติม "no" นำหน้า สำหรับ nodump ใช้ dump flag แทน:

sudo chflags dump /usr/bin/local/oty.sh

Terms มาตรฐานย้อนกลับด้วย "no":

sudo chflags nosappnd /usr/bin/local/oty.sh

เหมือน chmod มี Recursive Flag:

chflags -R nohidden ~/Desktop

หลังลบ Flags แล้ว จะเปลี่ยน Ownership และ Permissions ได้ตามปกติ

ใช้ Attributes บน Unix

วิธีใช้ File Flags แก้ไขพฤติกรรมไฟล์บน macOS

Unix ระบบคล้ายกัน Backend แตคำสั่งต่าง Linux ยอดนิยมใช้ chattr และ lsattr สำหรับ Attributes (Flags บน Unix อื่นๆ)

ดู Attributes: lsattr /path/to/file.txt

เปลี่ยน Attributes: รหัสย่อ ตาม man page chattr:

"ตัวอักษร 'acdeijstuADST' เลือก Attributes ใหม่: Append only (a), Compressed (c), No dump (d), Extent format (e), Immutable (i), Data journalling (j), Secure deletion (s), No tail-merging (t), Undeletable (u), No atime updates (A), Synchronous directory updates (D), Synchronous updates (S), Top of directory hierarchy (T)"

คำสั่ง:

chattr +s /file/name.txt

ตั้ง Secure Deletion Attribute สำหรับ Path นั้น

สรุป

Flags มีประโยชน์ตอนจำกัดใครเปลี่ยนไฟล์ได้ ด้วยการ Lock ไฟล์ ป้องกัน Tampering หรือแก้ไขโดยบังเอิญ ระดับ Filesystem โดยไม่ Escalate ไป Root หรือ Owner สิทธิ์เหล่านี้เปลี่ยนไม่ได้ จึงปลอดภัยปานกลาง