當Mac文件被永久鎖定后……

1. 解決「解鎖Mac文件」的歷程

1.1 個人遇到的問題

為了防止歐路詞典對 .plist 文件進行更改,從一篇論壇問答《[Mac] 歐路詞典破解及擴充辭典》中找到以下代碼:

sudo chmod 0400 ~/Library/Preferences/com.eusoft.eudic.plist
sudo chflags -R schg ~/Library/Preferences/com.eusoft.eudic.plist

復制粘貼到終端中運行后,永久鎖定了文件com.eusoft.eudic.plist。但是后來發現自己無法對該文件相應的軟件進行配置,遂需要解鎖該文件,在配置后重新鎖定。

文件被永久鎖定時的狀態.png

1.2 第一輪QA——先搜索可能原因及其解決辦法再實踐驗證

首先搞清楚我需要解決的問題是什么,我的搜索目標是什么。

我需要解決的問題是:解鎖由sudo chmod 0400命令造成鎖定狀態的文件。目標是解鎖文件,補充信息是該文件的鎖定狀態是由sudo chmod 0400命令造成的。

所以我的首要搜索目標是:搞清楚怎樣解鎖一個被鎖定的文件

通過搜索查詢,了解到「解鎖」其實就是對文件的讀寫權限進行改寫,0400是只允許所有者讀(所有者都不能寫),所以我需要將其權限改為所有者可寫,經查詢,0755,0777等都可以。


改寫文件的讀寫權限.png

使用 sudo chmod 0755 myfilesudo chmod -R 0755 myfile 進行嘗試,但顯示操作不被允許。

1.3 第二輪QA

這是一個衍生出來的新問題,針對該問題我的搜索目標是:搞清楚sudo chmod命令不被允許的可能原因

通過搜索查到這樣一個問題《chmod/chown on mac not working》,其中有一個回答部分如下:

With certain files you may run into: [sudo] chmod: Operation Not Permitted. This indicates another problem which is down to the OSX/BSD flags system functionality. Any file can have these flags set using the chflags tool, and viewed using ls -lO yourfile (on OSX) ls -lo yourfile (on BSD). The one that causes this particular error is the 'immutable' flag. Which can be unset using (prefixing it with sudo as necessary): chflags nouchg yourfile.

這個回答中指出,當執行[sudo] chmod 相關命令顯示不允許操作時,就表明問題可能取決于OSX / BSD 的標志(flags)系統功能。任何文件都可以使用 chflags 工具來設置這些標志(flags),并使用ls -lO yourfile(在OSX上)和ls -lo yourfile(在BSD上)查看,就會發現導致上述特定錯誤的是 “immutable” 標志。可以使用取消設置(根據需要使用sudo前綴):chflags nouchg yourfile.

立即根據回答最后的代碼進行嘗試,仍然無效。

1.4 第三輪QA

盡管代碼無效,但是知道了chflags的作用。使用chflags命令是解決自己問題的大方向,當下則是怎樣使用這個命令的問題。

針對該問題我的搜索目標是:搞清楚chflags命令的使用方法

于是轉去查詢該命令工具的使用。經過搜索篩選找到中英各一篇文章,《Mac 命令學習 - chflags》、《Pro Terminal Commands: Working with chflags in macOS》,從中文文檔中了解了chflags相關的基本知識,從英文文檔中了解得更清晰,進而搞清楚了自己在終端中的代碼問題。

因為鎖定文件時設置的標志是schg,所以取消設置時也要針對該標志——noschg,得到以下代碼:

sudo chflags -R noschg ~/Library/Preferences/com.eusoft.eudic.plist
sudo chmod 0755 ~/Library/Preferences/com.eusoft.eudic.plist

解鎖成功!


文件未被鎖定時的狀態.png

2. 知識點

2.1 博客園——《su、sudo、su - root的區別

su 和 sudo 的區別

共同點:都是root用戶權限;

不同點:

  • su:su只獲得root權限,工作環境不變,還是在切換之前用戶的工作環境;sudo是完全獲得root的權限和root的工作環境。

  • sudo:表示獲取臨時的root權限執行命令。

sudo執行命令的流程:

① 當前用戶切換到root(或其他指定切換到的用戶),
② 以root(或其他指定的切換到的用戶)身份執行命令,
③ 執行完成后,直接退回到當前用戶,而這些的前提是要通過sudo的配置文件/etc/sudoers來進行授權。

su - root 和 su root 區別

  • su - root:以root身份登錄,then the shell is login shell, .bash_profile and .bashrc will be sourced.
  • su root/其他命令:與root建立一個連接,通過root執行命令。then only .bashrc will be sourced.

最直接的區別是su目錄還是原先用戶目錄,su - root后目錄就變為root用戶的主目錄。因此,可以在.bashrc修改PATH、PHP、APACHE、Mysql等路徑。

Mac上的sudo -i

  • sudo -i :切換用戶到root

2.2 Apple官網——《如何在 Mac 上啟用 root 用戶或更改 root 密碼

名為「root」的用戶帳戶是一個超級用戶,擁有更多系統區域(包括 macOS 用戶帳戶中的文件)的讀寫權限。默認情況下,root 用戶處于停用狀態。

與啟用 root 用戶相比,在“終端”中使用 sudo 命令更為安全。要了解 sudo,請打開“終端”應用,然后輸入 man sudo

2.3 《Mac下su命令提示su:Sorry的解決辦法

mac上,一開始創建的具有管理員權限的用戶,但是那個用戶密碼,不是進入root的密碼,當運行su -這個命令時,會提示輸入密碼,而輸入自己用戶密碼后報su :sorry,其實,需要使用sudo su -命令可以進入root用戶,或者輸入sudo su命令,進入sh-3.2#,這里注意這個#號,#在linux中就是代表root權限的,這時在sh-3.2#環境下輸入su - 命令,也可以進入root。

2.4 《Pro Terminal Commands: Working with chflags in macOS

Only one flag can be set or removed per chflags command.

  • opaque set the folder to appear opaque when viewed through a union mount, an old-fashioned way of viewing multiple directories simultaneously. This isn’t a relevant flag for more users.

  • nodump prevents the file or folder from being dumped during use of the dump command to back up your system. If you don’t use dump, this flag has no effect on its own. However, it is often set in concert with other change-restriction flags like uappnd or schg.

  • sappnd, sappend set the system append-only flag, which allows the file to be added to but not modified or deleted. In general,the use of the system-level flags is rare. They’re restricted to only the most important files or the laziest developers. The same goes for any flags preceded with a s, which all stand for “system” level flags. To remove this flag, you’ll need to enter Single User Mode by holding “X” at your Mac’s startup chime.

  • schg, schange, simmutable set the system immutable flag, which locks out all file changes by all users of any privilege level. To remove this flag, you’ll need to enter Single User Mode by holding “X” at your Mac’s startup chime.

  • uappnd, uappend set the user append-only flag. This can be set by the file owner and can be unset by the owner without escalating privileges. Since it locks the file the same was as sappnd or schg at lower security, it’s used far more frequently.

  • uchg, uchange, uimmutable set the user immutable flag, which has the same relationship to the system immutable flag as the uappnd flag has to sappnd.

  • hidden sets the hidden flag. This hides the item within the Finder GUI and ls commands.

To clear a given flag, set its opposite. In most cases, this means prepending “no” to your command. In the case of nodump, set the dump flag to clear, like so:

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

More standard terms can be reversed with the “no” prefix, like so:

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

As with chmod, a recursive flag is available:

$ chflags  -R nohidden ~/Desktop

Once flags have been cleared, you’ll be free to change the ownership and permissions of the file as expected.

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。