如何打开Perl文件句柄以通过sudo(或以其他用户身份)写入数据 [英] How to open Perl file handle to write data via sudo (or as another user)

查看:117
本文介绍了如何打开Perl文件句柄以通过sudo(或以其他用户身份)写入数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将数据写入文件,但是应该打开文件句柄,并具有特定用户的访问权限.

I'd like to write data to a file, but the file handle should be opened with access permissions for a specific user.

因此,以下语句:

open (FH, "> $filename") or die "$@\n";

将允许以该特定用户的身份写入文件.

would allow writing to a file as that particular user.

是否有一种方法可以在Perl脚本中执行此操作,而无需使用sudo -u $username运行整个脚本?

Is there a way to do this within a Perl script, without the entire script being run with sudo -u $username?

推荐答案

有两种已建立的方法.堆垛机,我们邀请您编辑此答案以弥补每个缺点.

There are two established ways. Stackers, you are invited to edit this answer to fill in the drawbacks for each.

使用sudo运行程序.在程序中要做的第一件事是打开所需的文件并保留句柄,然后紧接着删除root特权.任何进一步的处理都必须以低特权进行. Apache httpd的工作方式是这样的,它以root的形式打开日志文件,但以nobody或类似的名称继续运行.

Run the program with sudo. The first thing you do in the program is to open the files you need and keep the handles, and then immediately afterwards drop the root privileges. Any further processing must take place with low privileges. The Apache httpd works likes this, it opens the log files as root, but continues running as nobody or similar.

如果您不喜欢这种方式,请正常运行该程序,并在需要提升时创建一个新进程,并使用配置为sudosu -kdesu/或诸如此类. CPAN客户端的工作方式是这样的,它以普通用户的身份获取,解压缩,构建和测试模块,但是在安装时调用sudo make install等.

If you don't like that way, run the program normally, and when you need to elevate, create a new process and have it run with a user configured sudo, su -, kdesu/gksu or whatnot. The CPAN client works likes this, it fetches, unpacks, builds and tests a module as a normal user, but calls sudo make install etc. when it's time to install.

这篇关于如何打开Perl文件句柄以通过sudo(或以其他用户身份)写入数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆