Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การแก้ไขปัญหา >> การบำรุงรักษาคอมพิวเตอร์

แก้ไข Permission Denied Errors ตอนเปิดไฟล์ผ่าน Terminal บน macOS

Zsh Permission Denied Mac

เปิดไฟล์ใน macOS ง่ายมาก แค่ Double-click — เสร็จ! ไม่ควรนานในการเปิดไฟล์บน Mac

อีกวิธีคือผ่าน Terminal รัน Command ตั้งค่า Directory ที่ไฟล์เก็บ ควรเปิดได้โดยไม่มี Hassle

เสียดายว่าไม่ได้เป็นเช่นนั้นเสมอไป มี Instance ที่ Terminal ได้ "permission denied" Error บน Mac ตอนเปิดไฟล์ Command เรียบง่ายไม่ควรให้ Error นี้ หมายถึงมีบางอย่างผิดกับ File หรือ macOS เอง

ตัวอย่าง "Permission Denied" Errors:

เหมือน Errors กับ Permissions บน Windows Devices Permission Denied Error บน Macs อาจมาพร้อม Error Messages ต่างๆ ตัวอย่างด้านล่าง:

เคล็ดลับ: สแกน Mac เพื่อตรวจสอบปัญหาประสิทธิภาพ ไฟล์ขยะ แอปที่เป็นอันตราย และภัยคุกคามด้านความปลอดภัย
ที่อาจก่อให้เกิดปัญหาระบบหรือทำให้ทำงานช้าลง

  • zsh: permission denied: ./foo.rb
  • Bash: bash: ./foo.rb: /usr/local/bin: bad interpreter: Permission denied
  • aftheers-MacBook-Air:scripts root# ./ArduinoWifiShield_upgrade.sh
  • -sh: ./ArduinoWifiShield_upgrade.sh: Permission denied

นี่เป็น Common Error ที่ทำรบกวน Mac Users นานแล้ว ไม่ใช่เรื่องใหม่ แต่มี Reports หลายรายการ gầnนี้ของ Mac Users ได้ "permission denied" Error บน Mac หลัง Upgrade เป็น Catalina และ Big Sur

มีหลายอย่างที่อาจผิดพลาดตอน Run Command ใน Terminal ดังนั้นเจอ Error นี้ไม่แปลกใจ แต่ Error นี้ยังรบกวนมาก หากคุณไม่มีทางอื่นเปิดไฟล์

เช่น คุณอยากเปิดไฟล์ที่ไม่มี Associated Program หรือไม่ตอบสนอง Clicks

ก่อนจะ Discuss ทุกอย่างเกี่ยวกับ Zsh Permission Denied Error บน Command Line ของ Mac OS Device มา Define Basic Terms ก่อน

MacOS Terminal คืออะไร?

Terminal App ของ Mac อนุญาตให้ทำหลายสิ่ง แต่วิธีที่ใช้แพร่หลายคือ Control Device ด้วย Commands ต่างๆ

App นี้คือ Mac Command Line Interface บางคนอาจ Tricky แต่บางคนทำ Tasks ให้สบายใจ ต้อง Familiar กับ Basic Functions และ Commands พิมพ์ลง Command Line

พอ Master แล้ว สามารถ Dig Deeper และ Explore Advanced Commands ทำ Complex Tasks ได้

เปิด Terminal App บน Mac

มี 2 วิธีเปิด Terminal App บน Mac

ผ่าน Applications Folder

App นี้อยู่ใน Utilities Folder ใต้ Applications Launch: เปิด Applications Folder Select Utilities Double-click Terminal App

ผ่าน Spotlight

ทางเลือก: กด Command Key และ Space Bar Launch Spotlight พิมพ์ terminal Double-click Search Result

Customizing Terminal App

ตอนนี้เห็น Small Window พื้นหลังขาว Username ใน Title Bar คำว่า "bash" และ Dimensions ของ Active Window ใน Pixels แสดงอยู่

อยากทำ Window ใหญ่ขึ้น คลิกมุมขวาล่างลากออก

อยากเปลี่ยนสี Background และ Font เปิด Shell Menu Select New Window จากนี้ Select Best Option จาก List

Basic Mac Commands ที่ควร Familiar

วิธีดีและเร็วที่สุด Familiar กับ Terminal App คือเริ่มใช้ แต่ก่อนต้อง Familiar ว่า Enter Command ยังไง ง่าย เพราะแค่ Type Command ที่ Command Line กด Return Execute

แต่ละ Command มี 3 Elements: Command, Argument บอก Command ว่าควร Run บน Resource(s) อะไร, และ Option ให้ Modify Output

เช่นอยาก Move System File จาก Folder หนึ่งไปอีก Folder ใช้ Move Command "mv" พิมพ์ Location ที่อยาก Move ไฟล์

Advanced Commands บน Terminal Command Line

ตัวอย่าง Advanced Terminal Commands:

Copy Files จาก Folder หนึ่งไปอีก Folder

Copy Files จาก Directory ไปอีก Directory:

  1. Type ditto [folder1] [folder2] Command Line เปลี่ยน folder1 เป็น Folder ที่ Host Current Files Folder2 คือ Folder ที่อยาก Move ไป
  2. อยาก View Files ที่ Copying Type -v หลัง Command

Download Files จาก Web

Download Files จาก Web ด้วย Terminal App:

  1. เปิด Terminal Type cd ~/Downloads/ Command
  2. Input Command: curl -O [URL of file you want to download]

เปลี่ยน Default Location ของ Screenshots

อยากเปลี่ยน Default Location เก็บ Screenshots:

  1. Type Command: defaults write com.apple.screencapture location [path to folder where you want screenshots to be saved]
  2. กด Return Key
  3. Type Command: killall SystemUIServer
  4. กด Return อีกครั้ง

เปลี่ยน Default File Type ของ Screenshots

อยาก Save Screenshots เป็น .jpg Format:

  1. Default Mac Screenshots Saved ด้วย .png Extension เปลี่ยนเป็น .jpg Type Command: defaults write com.apple.screencapture type JPG
  2. กด Return Key
  3. Type Command: killall SystemUIServer
  4. กด Return อีกครั้ง

ลบ Files ทั้งหมดใน Directory

ลบ Folders และ Sub-folders ใน Directory:

  1. ลบ Files ทั้งหมดใน Directory Type Command: rm -R directoryname
  2. Confirm Deletion ด้วย Command: rm -iR directoryname

Zsh คืออะไร?

Z shell หรือ Zsh เป็น Unix Shell ที่ Designed และ Built บน Bash — macOS Default Shell Experts แนะนำใช้ zsh แทน bash เพราะง่ายกว่า Config Plugins และ Themes Themes ดูสวยและเรียบร้อยกว่า สำคัญที่สุด ไม่ Require Strict User Permissions

Permission Denied ใน Terminal เปิดไฟล์ คืออะไร?

เหมือนชื่อ Error นี้เป็น Permission Error ป้องกัน Mac Users เปิดไฟล์ด้วย Commands เกิดได้กับ Sudo Commands หรือ Bash Commands ตาม Reports Permission Denied Error เกิดได้แม้ File เปิดใน Root

Reports หลายรายงาน Error ปรากฏตอนพยายามเปิด File Installer ผ่าน Terminal พอ Drag File ไป Terminal Window "Permission Denied" Error โผล่ แต่เกิดกับ File อื่นๆ ได้

ทำไมถึงเจอ "Permission Denied" Error บน Mac

มี Possible Reasons หลายอย่างที่ได้ Zsh Permission Denied Error บน Device Common Ones:

File ถูก Locked

น่าจะเจอ "Permission Denied" Error หากพยายาม Install Program หรือ Modify File ที่ Locked อาจเพราะคุณไม่มี Administrator Privileges หรือ Creator ของ File ใช้ chmod Lock File

Error นี้ Indicate ว่า Command พยายาม Write ไป Directory ที่ User Account ไม่ Own

Insufficient File Permission

เช็ค Permissions ของ File ด้วย ls -l file.ext ใน Terminal "file.ext" แทน File และ Extension ของ File ที่พยายามเปิดหรือแก้ไข ลอง Force Command ที่ต้องการ Administrator Permission ด้วย "sudo"

Incorrect Chmod X หรือ System Command

อีกเหตุผลคือคุณอาจ Enter Incorrect chmod x หรือ System Commands อื่นๆ

เช็ค Errors ใน Syntax ให้แน่ใจว่าใช้ Right Command หากพยายาม Run Application ให้แน่ใจว่า Program Command Valid และ Installed บน Computer

วิธีแก้ Permission Denied Error บน Mac

ตอนเจอ Error นี้ สิ่งแรกคือลองเปิดไฟล์ด้วย Normal Method Double-click ไฟล์ ดูว่าเปิดได้ไหม หากไม่ Successful ลอง Solutions ด้านล่าง

จากนั้น Copy Relevant Files ที่มี อาจต้อง Backup File ภายหลังหากอะไรผิดพลาด เพราะตอน Force Command ให้ทำงาน มี Potential Downsides ที่อาจ Mess Up System และ Result ใน Problems มากขึ้น Better Come Prepared

แน่ใจว่า Optimize Mac ด้วย Mac Repair App ป้องกัน Issues อื่น Pop Up ยัง Clean System ให้ Processes Run Smoothly เสร็จแล้ว Restart Mac เช็ค Troubleshooting Guide

Step 1: เช็ค Permissions

เช่นกล่าว Error นี้มักเกิดเพราะคุณไม่มี Sufficient Permissions Access File เช็คว่าเป็น Case นี้หรือไม่:

  1. Launch Terminal จาก Utilities
  2. Type Command ต่อไปนี้ เว้นวรรคหลังตัวอักษรสุดท้าย ห้ามกด Enter: ls -l
  3. Drag File ที่อยากเปิด Drop ลง Terminal Window จะ Append File Location ไป Command เดิม แต่ไม่ Move File จริง
  4. คลิก Terminal Window กด Enter

นี่จะแสดงว่าคุณมี Permission Write หรือ Modify File หรือไม่

Step 2: เปลี่ยน Ownership ของ Directory

Take Ownership ของ Directory ด้วย chmod Command ก่อน Attempt Write

Enter Command ใน Terminal กด Enter: chown -R $USER:$USER /path/to/directory

เปลี่ยน $USER ด้วย Current Logged In User และ /path/to/directory ด้วย Path ที่อยาก Write

Step 3: ให้ Terminal Full Access ถึง Disk

เป็นไปได้ว่า Terminal ไม่มี Complete Access ถึง Disk ทำให้เปิดไฟล์ด้วย Commands ไม่ได้ Fix: ไป System Preferences > Security & Privacy > Privacy Panel เพิ่ม Terminal ใน List ของ Apps ที่ Control Computer ได้

Step 4: ใช้ SUDO Commands

Sudo อาจ Simple แตทรงพอที่ Run Commands ด้วย Administrator Privilege หากเจอ Denied Commands เพราะ Permission Issues ใส่ sudo

Sudo Command Execute Commands ตามหลังด้วย Superuser หรือ Root Privileges Command นี้ควร Allow Execute เกือบ Command ใดๆ จาก Terminal วิธีทำ: ใส่ sudo หน้า Original Command ตัวอย่าง Commands:

  • sudo chmod 755 /dvtcolorconvert.rb
  • sudo chmod 755 ~/Themes/ObsidianCode.xccolortheme

จะ Prompt ใส่ Password หาก First Time ใช้ Sudo Command แต่หลังนั้นไม่ต้องใส่ทุกครั้งที่ใช้ Sudo Command

วิธีอื่นแก้ Zsh Permission Denied Error บน Mac

หากยังได้ Zsh Permission Denied Error บน Mac ตอนพยายามเปิดไฟล์บน macOS Catalina หรือเวอร์ชันอื่น ไม่ต้องกังวล นี่คือ Other Solutions:

Solution #1: Download Oh My Zsh

บางครั้ง Zsh Permission Denied Error แก้ได้โดย Improve Existing Terminal App วิธีหนึ่งคือใช้ Oh My Zsh

Oh My Zsh เป็น Open-source, User-friendly, Community-driven Framework ใช้ Manage Zsh Configurations บน MacOS Catalina หรือ macOS Versions อื่นๆ มาพร้อม Functions, Plugins, Themes, และ Relevant Stuff สำหรับ macOS Device

พอ Install Framework นี้ คุณจะเข้าถึง Hundreds ของ Powerful Plugins และ Neat Themes Terminal App ใหม่จะ Customized ตาม Preferences

Download: เยี่ยม ohmyz.sh Website พอ Executable File Downloaded Install ด้วย Command ใน Terminal: $ sh -c "$(curl -fsSL https://raw.github.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"

Solution #2: Configure .zshrc File

ยังเห็น Zsh Permission Denied Error? ลอง Configure .zshrc File File นี้มี Script ที่ต้องการ Run New Zsh Shell หาก Change File นี้ ต้อง Start New Shell เพื่อ Changes Take Effect

Customize Terminal โดย Run open ~/.zshrc Command จะเปิด .zshrc File ใน System Root Folder

Note: หากรัน macOS Catalina หรือ Later Versions อาจยังเจอ Zsh Permission Denied Error เดียวกัน สำหรับนี้ Configure Permissions ด้วยการ Open File 用 chmod +x ~/.zshrc Command

Solution #3: เปลี่ยน Theme ของ Terminal App

หาก Install Oh My Zsh Framework แล้ว ดำเนินการ Solution นี้ เปลี่ยน Current Terminal App Theme ด้วย ZSH_THEME="theme_name" Command

Solution #4: เปลี่ยน Default Shell ของ System

อีกวิธีแก้ Zsh Permission Denied Error คือเปลี่ยน Default Shell ของ System วิธีทำ:

  1. ไป Apple Menu Select Preferences
  2. Navigate ไป Users and Groups
  3. คลิก lock Icon ใส่ User Account Name และ Password
  4. Control+Click User Account Name จาก List of Users
  5. Choose Advanced Options
  6. Select Shell จาก Login Shell Menu
  7. กด OK Apply Changes

วิธีป้องกัน Permission Issues บน Mac

เพื่อหลีกเลี่ยง Permission Problems ในอนาคต สามารถเปลี่ยน Permission Settings กำหนดว่าใคร View และ Alter Files บน Device ได้ บางวิธีด้านล่าง:

Method #1: Assign Permissions ให้ Users และ Groups

Assign Permissions ให้ Users และ Groups ต่างๆ หลีกเลี่ยง Permission Issues บน macOS Device:

  1. บน Mac Device เลือก Folder หรือ Directory
  2. ไป File Select Get Info
  3. หาก Information ไม่ Available กด Arrow
  4. Select User หรือ Group Select Privilege จาก Pop-up Menu
  5. กด OK Save Changes

Method #2: Apply Permissions ทุก Items

Apply Permissions ทุก Items ให้แน่ใจว่า Access ได้เฉพาะคนที่ถูกต้อง:

  1. Select Directory ไป File
  2. คลิก Get Info
  3. เช็ค Action Pop-Up Menu Select Apply to Enclosed Items
  4. กด OK Save Changes

Method #3: เปลี่ยน Owner ของ Item

หาก Item Owned by Another User จะมี Problems กับ Permission Fix ได้โดย:

  1. Select Item ไป File > Get Info
  2. หาก New Owner Name ไม่อยู่ใน List คลิก Add Button Add New Owner
  3. Choose New Owner คลิก Action Pop-Up Menu
  4. คลิก Make <new owner> the owner
  5. Apply และ Save Changes

Method #4: Add หรือ Remove User หรือ Group

Add หรือ Remove User หรือ Group ก็ Resolve Permission Issues ได้ วิธีทำ:

  1. Select Item
  2. ไป File คลิก Get Info
  3. Navigate ไป Sharing and Permissions Section
  4. Select Add a User or Group Add User/Group หรือ Remove a user or group Remove User/Group
  5. Save Changes

สรุป

ได้ "permission denied" Error บน Mac ตอนพยายาม Access File ไม่ใช่ปัญหาใหญ่หากเปิดไฟล์ได้ด้วย Double-click แต่ส่วนใหญ่ Users ที่เจอ Error นี้เกือบไม่สามารถเปิดไฟล์ด้วย Conventional Methods

เนื่องจากเป็น Permission Error ต้องแน่ใจว่ามี Privileges พอ Access Steps ข้างต้นควร Guide ว่าต้องทำอะไร Quite Helpful สำหรับ Mac Users ที่ไม่ Familiar กับ Commands หรือ Terminal

เพื่อ Resolve Zsh Permission Denied Error บน Mac ต้องเช็ค File Permissions ก่อน อาจเป็นที่ Current User Account มี Insufficient Permissions Access File

หากไม่ได้ผล เปลี่ยน Ownership ของ Directory ด้วย chown Command

ลองให้ Terminal App Full Access Disk หรือใช้ SUDO Command ให้ Superuser/Root Privileges

หาก Basic Troubleshooting Methods ไม่ได้ผล ดำเนินการ Other Solutions ก่อน ลอง Download Oh My Zsh Framework Method นี้ Work สำหรับ Affected Users หลายคน Framework นี้ Customize Terminal App ได้

ต่อไป Configure zshrc File เอง หรือเปลี่ยน Theme Terminal App หาก Error Persists เปลี่ยน System Default Shell

ตอนนี้ หากลองทุกอย่างแต่ No Avail ขอความช่วยเหลือจาก Mac Expert พวกเขา Diagnose Problem ให้คุณไม่ต้องทำเอง

คุณเจอ Mac-related Errors อื่นๆ อะไรบ้าง? แชร์ใน Comments นะครับ