Mac下 对readonly类型文件或者目录操作方法

mac 环境对readonly类型文件修改、目录下创建文件正常操作都会失败,如下:

vim /etc/hosts

##
# Host Database
#
# localhost is used to configure the loopback interface
# when the system is booting.  Do not change this entry.
##
127.0.0.1       localhost
255.255.255.255 broadcasthost
::1             localhost
# 100.81.197.201 hsp.aliyun-inc.com
11.164.61.51 hsp.aliyun-inc.com
# Added by Docker Desktop
# To allow the same kube context to work on the host and the container:
127.0.0.1 kubernetes.docker.internal
# End of section
~                                                                                                                                                                                                                                          
                                                                                                                                                                                                                                      
-- INSERT -- W10: Warning: Changing a readonly file


# 使用 wq! 依旧无法保存

# 使用sudo ,输入密码成功修改
sudo vim /etc/hosts
Password:

上述方法适用于mac os monterey(12.0.1)之后

12.0.1之前需要修改csrutil status 状态

1、重启mac,按住Command+R,等到系统进入安全模式。

2、选择一个账户,然后点击屏幕上方的工具栏找到命令行工具。

3、执行,命令 csrutil disable

4、重启电脑后,进入正常模式进行操作

参考文档:

1、https://www.jianshu.com/p/c5e2b29a7c19

2、https://blog.csdn.net/qq_40816795/article/details/123189262