美文网首页
当Mac文件被永久锁定后……

当Mac文件被永久锁定后……

作者: WendaoSolemn | 来源:发表于2020-09-28 21:52 被阅读0次

    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.

    相关文章

      网友评论

          本文标题:当Mac文件被永久锁定后……

          本文链接:https://www.haomeiwen.com/subject/hgvjuktx.html