Linux 尝试使用 sudo 将文件附加到根拥有的文件时权限被拒绝

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13778857/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 18:00:00  来源:igfitidea点击:

Permission denied when trying to append a file to a root owned file with sudo

linuxbashshellcommand-linesudo

提问by Desmond Hume

This is the shell command that results in "Permission denied" when I'm trying to append the data in a file to another file with sudo:

当我尝试使用 sudo 将文件中的数据附加到另一个文件时,这是导致“权限被拒绝”的 shell 命令:

sudo cat add_file >> /etc/file

The file at /etc/fileis owned by root(i.e. me) and its permissions are rw-r--r--. Should I become rootfor a moment to make it work or is there a workaround for sudo?

文件 at/etc/fileroot(即我)所有,其权限为rw-r--r--. 我应该root暂时让它工作还是有解决方法sudo

采纳答案by Chris Seymour

Run bashas sudo:

运行bashsudo

$ sudo bash -c "cat add_file >> /etc/file"

$ whoami;sudo bash -c "whoami";whoami
iiSeymour
root
iiSeymour

回答by Gilles Quenot

Try doing this instead :

尝试这样做:

sudo tee -a /etc/file < add_file

It's lighter than running bash or sh -c command

它比运行 bash 或 sh -c command

回答by Maxime Chéramy

You are trying to write the result of the command sudo cat add_fileto the file /etc/file. And apparently you don't have that right.

您正在尝试将命令的结果写入sudo cat add_file文件 /etc/file.conf 。显然你没有那个权利。

man sudogives that example :

man sudo给出那个例子:

   To make a usage listing of the directories in the /home partition.  Note
   that this runs the commands in a sub-shell to make the cd and file
   redirection work.

    $ sudo sh -c "cd /home ; du -s * | sort -rn > USAGE"
   To make a usage listing of the directories in the /home partition.  Note
   that this runs the commands in a sub-shell to make the cd and file
   redirection work.

    $ sudo sh -c "cd /home ; du -s * | sort -rn > USAGE"

So you should try :

所以你应该尝试:

sudo sh -c "cat add_file >> /etc/file"

sudo sh -c "cat add_file >> /etc/file"

回答by gniourf_gniourf

A funny possibility is to use ed(the standard editor):

一个有趣的可能性是使用ed(标准编辑器):

sudo ed -s /etc/file <<< $'r add_file\nwq'

I know I won't get upvoted for this wonderful answer, but I wanted to include it here anyway (because it's funny). Done!

我知道我不会因为这个精彩的答案而获得赞誉,但无论如何我还是想把它包括在这里(因为它很有趣)。完毕!

回答by Montells

try

尝试

sudo bash -c 'cat add_file >> /etc/file'

or

或者

cat add_file | sudo tee -a /etc/file > /dev/null

回答by kenorb

Similar approach to @gniourf_gniourfanswer, but using ex:

@gniourf_gniourf答案类似的方法,但使用ex

sudo ex +"r in.txt" -cwq out.txt

which is equivalent to vi/vimEx-mode (-e).

相当于 vi/ vimEx-mode ( -e)。

This in-place edit example is simple, safe and convenient approach, because it doesn't use any shell piping, FIFOs or shell within the shell workarounds.

这个就地编辑示例是简单、安全和方便的方法,因为它在外壳变通方法中不使用任何外壳管道、FIFO 或外壳。

To boost performance for scripting purposes, consider using silence mode (-s).

为了提高脚本编写的性能,请考虑使用静音模式 ( -s)。