1. 解決「解鎖Mac文件」的歷程
1.1 個(gè)人遇到的問(wèn)題
為了防止歐路詞典對(duì) .plist 文件進(jìn)行更改,從一篇論壇問(wèn)答《[Mac] 歐路詞典破解及擴(kuò)充辭典》中找到以下代碼:
sudo chmod 0400 ~/Library/Preferences/com.eusoft.eudic.plist
sudo chflags -R schg ~/Library/Preferences/com.eusoft.eudic.plist
復(fù)制粘貼到終端中運(yùn)行后退渗,永久鎖定了文件com.eusoft.eudic.plist
须眷。但是后來(lái)發(fā)現(xiàn)自己無(wú)法對(duì)該文件相應(yīng)的軟件進(jìn)行配置栅螟,遂需要解鎖該文件憨栽,在配置后重新鎖定。
1.2 第一輪QA——先搜索可能原因及其解決辦法再實(shí)踐驗(yàn)證
首先搞清楚我需要解決的問(wèn)題是什么玉凯,我的搜索目標(biāo)是什么奋蔚。
我需要解決的問(wèn)題是:解鎖由sudo chmod 0400
命令造成鎖定狀態(tài)的文件她混。目標(biāo)是解鎖文件,補(bǔ)充信息是該文件的鎖定狀態(tài)是由sudo chmod 0400
命令造成的泊碑。
所以我的首要搜索目標(biāo)是:搞清楚怎樣解鎖一個(gè)被鎖定的文件坤按。
通過(guò)搜索查詢(xún),了解到「解鎖」其實(shí)就是對(duì)文件的讀寫(xiě)權(quán)限進(jìn)行改寫(xiě)馒过,0400是只允許所有者讀(所有者都不能寫(xiě))臭脓,所以我需要將其權(quán)限改為所有者可寫(xiě),經(jīng)查詢(xún)腹忽,0755谢鹊,0777等都可以。
使用 sudo chmod 0755 myfile
或 sudo chmod -R 0755 myfile
進(jìn)行嘗試留凭,但顯示操作不被允許。
1.3 第二輪QA
這是一個(gè)衍生出來(lái)的新問(wèn)題偎巢,針對(duì)該問(wèn)題我的搜索目標(biāo)是:搞清楚sudo chmod
命令不被允許的可能原因蔼夜。
通過(guò)搜索查到這樣一個(gè)問(wèn)題《chmod/chown on mac not working》,其中有一個(gè)回答部分如下:
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
.
這個(gè)回答中指出压昼,當(dāng)執(zhí)行[sudo] chmod
相關(guān)命令顯示不允許操作時(shí)求冷,就表明問(wèn)題可能取決于OSX / BSD 的標(biāo)志(flags)系統(tǒng)功能。任何文件都可以使用 chflags 工具來(lái)設(shè)置這些標(biāo)志(flags)窍霞,并使用ls -lO yourfile
(在OSX上)和ls -lo yourfile
(在BSD上)查看匠题,就會(huì)發(fā)現(xiàn)導(dǎo)致上述特定錯(cuò)誤的是 “immutable” 標(biāo)志〉穑可以使用取消設(shè)置(根據(jù)需要使用sudo前綴):chflags nouchg yourfile
.
立即根據(jù)回答最后的代碼進(jìn)行嘗試韭山,仍然無(wú)效。
1.4 第三輪QA
盡管代碼無(wú)效,但是知道了chflags
的作用钱磅。使用chflags
命令是解決自己?jiǎn)栴}的大方向梦裂,當(dāng)下則是怎樣使用這個(gè)命令的問(wèn)題。
針對(duì)該問(wèn)題我的搜索目標(biāo)是:搞清楚chflags
命令的使用方法盖淡。
于是轉(zhuǎn)去查詢(xún)?cè)撁罟ぞ叩氖褂媚昴=?jīng)過(guò)搜索篩選找到中英各一篇文章,《Mac 命令學(xué)習(xí) - chflags》褪迟、《Pro Terminal Commands: Working with chflags in macOS》冗恨,從中文文檔中了解了chflags
相關(guān)的基本知識(shí),從英文文檔中了解得更清晰味赃,進(jìn)而搞清楚了自己在終端中的代碼問(wèn)題掀抹。
因?yàn)殒i定文件時(shí)設(shè)置的標(biāo)志是schg
,所以取消設(shè)置時(shí)也要針對(duì)該標(biāo)志——noschg
洁桌,得到以下代碼:
sudo chflags -R noschg ~/Library/Preferences/com.eusoft.eudic.plist
sudo chmod 0755 ~/Library/Preferences/com.eusoft.eudic.plist
解鎖成功渴丸!
2. 知識(shí)點(diǎn)
2.1 博客園——《su、sudo另凌、su - root的區(qū)別》
su 和 sudo 的區(qū)別
共同點(diǎn):都是root
用戶(hù)權(quán)限谱轨;
不同點(diǎn):
su
:su只獲得root權(quán)限,工作環(huán)境不變吠谢,還是在切換之前用戶(hù)的工作環(huán)境土童;sudo是完全獲得root的權(quán)限和root的工作環(huán)境。sudo
:表示獲取臨時(shí)的root權(quán)限執(zhí)行命令工坊。
sudo執(zhí)行命令的流程:
① 當(dāng)前用戶(hù)切換到root(或其他指定切換到的用戶(hù))献汗,
② 以root(或其他指定的切換到的用戶(hù))身份執(zhí)行命令,
③ 執(zhí)行完成后王污,直接退回到當(dāng)前用戶(hù)罢吃,而這些的前提是要通過(guò)sudo的配置文件/etc/sudoers來(lái)進(jìn)行授權(quán)。
su - root 和 su root 區(qū)別
-
su - root
:以root身份登錄昭齐,then the shell is login shell, .bash_profile and .bashrc will be sourced. - su root/其他命令:與root建立一個(gè)連接尿招,通過(guò)root執(zhí)行命令。then only .bashrc will be sourced.
最直接的區(qū)別是su目錄還是原先用戶(hù)目錄阱驾,su - root后目錄就變?yōu)閞oot用戶(hù)的主目錄就谜。因此,可以在.bashrc修改PATH里覆、PHP丧荐、APACHE、Mysql等路徑喧枷。
Mac上的sudo -i
- sudo -i :切換用戶(hù)到root
2.2 Apple官網(wǎng)——《如何在 Mac 上啟用 root 用戶(hù)或更改 root 密碼》
名為「root」的用戶(hù)帳戶(hù)是一個(gè)超級(jí)用戶(hù)虹统,擁有更多系統(tǒng)區(qū)域(包括 macOS 用戶(hù)帳戶(hù)中的文件)的讀寫(xiě)權(quán)限弓坞。默認(rèn)情況下,root 用戶(hù)處于停用狀態(tài)窟却。
與啟用 root 用戶(hù)相比昼丑,在“終端”中使用 sudo
命令更為安全。要了解 sudo
夸赫,請(qǐng)打開(kāi)“終端”應(yīng)用菩帝,然后輸入 man sudo
。
2.3 《Mac下su命令提示su:Sorry的解決辦法》
mac上茬腿,一開(kāi)始創(chuàng)建的具有管理員權(quán)限的用戶(hù)呼奢,但是那個(gè)用戶(hù)密碼,不是進(jìn)入root的密碼切平,當(dāng)運(yùn)行su -
這個(gè)命令時(shí)握础,會(huì)提示輸入密碼,而輸入自己用戶(hù)密碼后報(bào)su :sorry
悴品,其實(shí)禀综,需要使用sudo su -
命令可以進(jìn)入root用戶(hù),或者輸入sudo su
命令苔严,進(jìn)入sh-3.2#定枷,這里注意這個(gè)#號(hào),#在linux中就是代表root權(quán)限的届氢,這時(shí)在sh-3.2#環(huán)境下輸入su - 命令欠窒,也可以進(jìn)入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.