简体   繁体   English

将文件写入mac os x中的etc目录

[英]Write file to etc directory in mac os x

I am trying to write file into /etc folder on Mac OS X.我正在尝试将文件写入 Mac OS X 上的 /etc 文件夹。

[[textView string] writeToFile:@"/etc/info.txt" atomically:YES encoding:NSUnicodeStringEncoding error:&error];

It throws error that I don't have permission to write there ( naturally ), but I don't understand how to get permission to be able to write into the System folders.它引发错误,我无权在那里写入(自然),但我不明白如何获得能够写入系统文件夹的权限。

Also can somebody provide simple example?有人也可以提供简单的例子吗?

Please help.请帮忙。 Thank you.谢谢你。

The claim "you should never write to /etc/" -folder is not valid.声明“您永远不应该写入 /etc/” - 文件夹无效。

There certainly are many many good reasons to write to system folders - if writing enterprise software that sets up things for users so that user's don't need to do that manually.写入系统文件夹当然有很多很好的理由——如果编写企业软件来为用户设置东西,这样用户就不需要手动执行这些操作。 This is required by many corporations to even approve Macs as work computers in the first place.许多公司甚至首先要求将 Mac 批准为工作计算机。 This is typically done with shell scripting or high level languages (such as Python or Ruby), but for end user usability it would be necessary that also UI apps are able to read and write changes to system folders (such as the folder owned by configuration management system).这通常是通过 shell 脚本或高级语言(例如 Python 或 Ruby)完成的,但为了最终用户的可用性,UI 应用程序也必须能够读取和写入系统文件夹(例如配置拥有的文件夹)的更改管理系统)。

I am writing professionally software that must write to system folders, change contents of configuration files etc. in folders which require root privileges - all the time.我正在编写专业软件,必须写入系统文件夹,更改需要 root 权限的文件夹中的配置文件等内容 - 一直。 I have had not 100% success in doing that since MacOSX has been made too much unfriendly for this (enterprise software for corporate internal use) use case and the approach I use seems to fail to work sometimes.我没有 100% 成功地做到这一点,因为 MacOSX 对这个(企业内部使用的企业软件)用例过于不友好,而且我使用的方法有时似乎无法奏效。 I ended up popping up admin password request dialog by using Apple Script object.我最终使用 Apple Script 对象弹出了管理员密码请求对话框。 It sucks, but worked for my main use case:它很糟糕,但适用于我的主要用例:

 NSString *shellCommand = [[NSString alloc] initWithFormat:@"do shell script \"/bin/bash /usr/bin/nameoftheshellscriptgoeshere.sh\" with administrator privileges"];

 NSAppleScript *script;

 script = [[NSAppleScript alloc] initWithSource:shellCommand];

 NSDictionary* errDict = NULL;

 [script executeAndReturnError:&errDict];

If your installer placed nameoftheshellscriptgoeshere.sh (or whatever name you give for it) shell script to /usr/bin, you are able to do your magic in the shell script.如果您的安装程序将 nameoftheshellscriptgoeshere.sh(或您为其指定的任何名称)shell 脚本放置到 /usr/bin,您就可以在 shell 脚本中发挥您的魔力。

This approach only works if the administrative thing can be done with a shell script.这种方法仅适用于可以使用 shell 脚本完成管理工作的情况。 There is a workaround, but is really not very beautiful: Needing to write configuration files to a system directory with Objective-C application, using this causes a really tedious roundtrip: you create a modified configuration file to a folder that is writable by the Cocoa application and then this app executes Apple Script which executes a one liner shell script that copies this temporary configuration file to its correct location in the system folder requiring root privileges.有一个解决方法,但确实不是很漂亮:需要使用 Objective-C 应用程序将配置文件写入系统目录,使用此方法会导致非常繁琐的往返:您将修改后的配置文件创建到 Cocoa 可写的文件夹中应用程序,然后此应用程序执行 Apple 脚本,该脚本执行单行 shell 脚本,该脚本将此临时配置文件复制到需要 root 权限的系统文件夹中的正确位置。 This is very unoptimal and clumsy and introduces dependency on the said shell script - in other words, the UI app will malfunction if the shell script is not installed on the system.这是非常不理想和笨拙的,并且引入了对上述 shell 脚本的依赖——换句话说,如果系统上没有安装 shell 脚本,UI 应用程序将出现故障。 And lots of unnecessary separated parts to maintain just for one single reason: raising privileges of Cocoa app is hard or impossible and corporate software has not apparently been a design point in designing the OSX.许多不必要的分离部分仅出于一个原因就需要维护:提高 Cocoa 应用程序的权限很难或不可能,而且企业软件显然不是设计 OSX 的设计点。

Writing to /etc/ will require root level access.写入 /etc/ 将需要根级别访问权限。 You will have to ask the user for their password and then run an appropriate helper tool, etc, to do the actual writing for you.您将不得不向用户询问他们的密码,然后运行适当的帮助工具等,为您进行实际编写。

See the Authorization Services Tasks documentation.请参阅授权服务任务文档。

In general, you should never write to /etc/ for any reason.通常,您不应该出于任何原因写入 /etc/ That is a system owned and controlled directory.这是一个系统拥有和控制的目录。 Certainly, given the unix underpinnings of the OS, there are things that can be done by doing so, but only as a means of last resort.当然,鉴于操作系统的 unix 基础,这样做可以完成一些事情,但只能作为最后的手段。

For anyone else who comes across this while Googling for answers:对于在谷歌搜索答案时遇到这个问题的其他人:

If you just use the "sudo nano directory/file.name" command it will open the nano text editor and then you can write files to the /etc/ directory (or whichever directory you want) so what I did was:如果您只使用“sudo nano directory/file.name”命令,它将打开 nano 文本编辑器,然后您可以将文件写入 /etc/ 目录(或您想要的任何目录),所以我所做的是:

'sudo nano /etc/krb5.conf' '须藤纳米/etc/krb5.conf'

which opened the nano text editor and simultaneously created the file 'krb5.conf' in the etc directory then all I needed to do was input the code and write the file.打开nano文本编辑器并同时在etc目录中创建文件'krb5.conf'然后我需要做的就是输入代码并编写文件。 Took about 30s.花了大约 30 秒。

Okay, after researching quite a bit I believe I found a more/less simple method.好的,经过大量研究后,我相信我找到了一种更简单/更简单的方法。 First check out the link: Cocoa - Gaining Root Access for NSFileManager首先查看链接: Cocoa - Gaining Root Access for NSFileManager

They suggest using already made class "BLAuthentication" - which takes care of lots of authentication coding for you.他们建议使用已经创建的类“BLAuthentication”——它会为你处理大量的身份验证编码。 Google it to download, add to the project and use in your code.谷歌下载,添加到项目并在您的代码中使用。 Here is a example of how simple that is:这是一个多么简单的例子:

id blTmp = [BLAuthentication sharedInstance];

NSString *myCommand = [[NSString alloc] initWithString:@"/bin/cp"];

NSArray *para = [[NSArray alloc] initWithObjects:fileName, @"/etc/", nil];

[blTmp authenticate:myCommand];

if([blTmp isAuthenticated:myCommand] == true) {
    NSLog(@"Authenticated");
    [blTmp executeCommandSynced:myCommand withArgs:para];

} else { NSLog(@"Not Authenticated"); }

[fileName release];
[myCommand release];
[para release];

Naturally above approach doesn't exactly answers my own question, since with above example you don't directly write to "/etc".自然,上述方法并不能完全回答我自己的问题,因为在上面的示例中,您不会直接写入“/etc”。 Instead first I have to write file to "/tmp" (or other directory of your choice) and then using "cp" command in unix, copy it over to "/etc" (using "BLAuthentication" for authentication).相反,我必须首先将文件写入“/tmp”(或您选择的其他目录),然后在 unix 中使用“cp”命令,将其复制到“/etc”(使用“BLAuthentication”进行身份验证)。

This is the simplest to do it, that I managed to find.这是我设法找到的最简单的方法。

*Note: This method might not work in Lion (Mac os 10.7) since "BLAuthentication" uses deprecated function "AuthorizationExecuteWithPrivileges". *注意:此方法在 Lion (Mac os 10.7) 中可能不起作用,因为“BLAuthentication”使用已弃用的函数“AuthorizationExecuteWithPrivileges”。 Also I would suggest watching WWDC 2011 where they talk about security and proper ways of accomplishing the task.此外,我建议观看 WWDC 2011,在那里他们谈论安全性和完成任务的正确方法。 However if you need to prototype and/or play around, "BLAuthentication" should do the trick.但是,如果您需要原型和/或玩耍,“BLAuthentication”应该可以解决问题。

How to create a /logs directory you can write to:如何创建可以写入的 /logs 目录:

  1. Disable SIP (recovery mode, csrutil disable )禁用 SIP(​​恢复模式, csrutil disable
  2. Open Recovery mode by pressing command + R , until u see Apple Logocommand + R打开恢复模式,直到你看到 Apple 标志
  3. Restart重新开始
  4. Open a terminal打开终端
  5. echo 'logs' | sudo tee -a /etc/synthetic.conf
  6. Reboot重启
  7. Or you can manually create a File /etc/synthetic.conf或者你可以手动创建一个文件/etc/synthetic.conf
  8. synthetic.conf合成文件
    • #create an empty directory named "logs" at / which may be mounted over
    • logs
  9. Then Reboot the system然后重启系统
  10. Make sure **/etc/synthetic.conf** has the following permissions:确保**/etc/synthetic.conf**具有以下权限:
    • root: read, write根:读,写
    • wheel: read轮:阅读
    • everyone: read大家:读

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM