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

发布于 2024-11-27 09:37:48 字数 276 浏览 3 评论 0原文

我正在尝试将文件写入 Mac OS X 上的 /etc 文件夹。

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

它会抛出错误,指出我没有权限在那里写入(自然),但我不明白如何获得能够写入系统的权限文件夹。

另外有人可以提供简单的例子吗?

请帮忙。 谢谢。

I am trying to write file into /etc folder on Mac OS X.

[[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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

清旖 2024-12-04 09:37:49

如何创建可写入的 /logs 目录:

  1. 禁用 SIP(​​恢复模式,csrutil 禁用
  2. command + R 打开恢复模式,直到看到 Apple 徽标
  3. 重新启动
  4. 打开终端
  5. echo 'logs' | sudo tee -a /etc/synthetic.conf
  6. 重新启动
  7. 或者您可以手动创建一个文件 /etc/synthetic.conf
  8. synthetic.conf
    • #在 / 处创建一个名为“logs”的空目录,可以将其挂载到
    • 日志
  9. 然后重新启动系统
  10. ,确保 **/etc/synthetic.conf** 具有以下权限:
    • root:读、写
    • 轮子:阅读
    • 大家:阅读

How to create a /logs directory you can write to:

  1. Disable SIP (recovery mode, csrutil disable)
  2. Open Recovery mode by pressing command + R, until u see Apple Logo
  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
  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:
    • root: read, write
    • wheel: read
    • everyone: read
可遇━不可求 2024-12-04 09:37:48

“您永远不应该写入 /etc/”文件夹的说法无效。

当然有很多很好的理由写入系统文件夹 - 如果编写为用户设置内容的企业软件,以便用户不需要手动执行此操作。许多公司甚至首先要求批准 Mac 作为工作计算机。这通常是通过 shell 脚本或高级语言(例如 Python 或 Ruby)完成的,但为了最终用户的可用性,UI 应用程序还需要能够读取和写入对系统文件夹(例如配置拥有的文件夹)的更改管理系统)。

我正在编写专业软件,必须始终写入系统文件夹、更改需要根权限的文件夹中的配置文件内容等。我在这方面并没有 100% 成功,因为 MacOSX 对于这个(供公司内部使用的企业软件)用例来说太不友好了,而且我使用的方法有时似乎不起作用。我最终使用 Apple Script 对象弹出了管理员密码请求对话框。它很糟糕,但适用于我的主要用例:

 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];

如果您的安装程序将 nameoftheshellscriptgoeshere.sh (或您为其指定的任何名称)shell 脚本放置到 /usr/bin,您就可以在 shell 脚本中发挥您的魔力。

仅当管理工作可以使用 shell 脚本完成时,此方法才有效。有一个解决方法,但确实不是很漂亮:需要使用 Objective-C 应用程序将配置文件写入系统目录,使用此方法会导致非常繁琐的往返:您将修改后的配置文件创建到 Cocoa 可写的文件夹中应用程序,然后该应用程序执行 Apple 脚本,该脚本执行一个单行 shell 脚本,将该临时配置文件复制到需要 root 权限的系统文件夹中的正确位置。这是非常不理想且笨拙的,并且引入了对上述 shell 脚本的依赖 - 换句话说,如果系统上未安装 shell 脚本,UI 应用程序将出现故障。许多不必要的独立部分仅出于一个原因而需要维护:提高 Cocoa 应用程序的权限很难或不可能,而且企业软件显然并不是 OSX 设计的一个设计点。

The claim "you should never write to /etc/" -folder is not valid.

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. 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).

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. 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. I ended up popping up admin password request dialog by using Apple Script object. 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.

This approach only works if the administrative thing can be done with a shell script. 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. 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. 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.

国际总奸 2024-12-04 09:37:48

写入 /etc/ 需要 root 级别访问权限。您将必须询问用户的密码,然后运行适当的帮助工具等来为您进行实际的写入。

请参阅授权服务任务文档。

一般来说,无论出于何种原因,您都不应该写入 /etc/。那是系统拥有和控制的目录。当然,考虑到操作系统的 unix 基础,可以通过这样做来完成一些事情,但只能作为最后的手段。

Writing to /etc/ will require root level access. 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. 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.

陌路终见情 2024-12-04 09:37:48

对于在谷歌搜索答案时遇到此问题的其他人:

如果您只使用“sudo nano directory/file.name”命令,它将打开 nano 文本编辑器,然后您可以将文件写入 /etc/ 目录(或任何目录)你想要的)所以我所做的是:

'sudo nano /etc/krb5.conf'

打开nano文本编辑器并同时在etc目录中创建文件'krb5.conf'然后我需要做的就是输入代码并写入文件。花了大约30秒。

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 /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. Took about 30s.

清眉祭 2024-12-04 09:37:48

好吧,经过大量研究后,我相信我找到了一种或多或少简单的方法。
首先查看链接: Cocoa - 获得 NSFileManager 的根访问权限

他们建议使用已经创建了“BLAuthentication”类 - 它为您处理大量身份验证编码。谷歌下载,添加到项目并在代码中使用。这是一个多么简单的例子:

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];

当然,上述方法并不能完全回答我自己的问题,因为在上面的示例中,您不会直接写入“/etc”。相反,首先我必须将文件写入“/tmp”(或您选择的其他目录),然后在unix中使用“cp”命令,将其复制到“/etc”(使用“BLAuthentication”进行身份验证)。

这是我设法找到的最简单的方法。

*注意:此方法可能不适用于 Lion (Mac os 10.7),因为“BLAuthentication”使用已弃用的函数“AuthorizationExecuteWithPrivileges”。另外,我建议观看 WWDC 2011,他们谈论安全性和完成任务的正确方法。但是,如果您需要原型和/或尝试,“BLAuthentication”应该可以解决问题。

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

They suggest using already made class "BLAuthentication" - which takes care of lots of authentication coding for you. 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". 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).

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". Also I would suggest watching WWDC 2011 where they talk about security and proper ways of accomplishing the task. However if you need to prototype and/or play around, "BLAuthentication" should do the trick.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文