bash 以非 root 用户身份编辑后将文件另存为 root 用户
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4484183/
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
Save file as root after editing as non-root
提问by Amir Raminfar
Ok so this happens to me all the time. There has to be a better solution. Let's say you do vim /etc/somefile.confand then you do ibut realize you are not sudoand you can't write. So then I lose my changes by doing :qthen sudo !!and make my changes again. Is there a better way to do this?
好吧,这一直发生在我身上。必须有更好的解决方案。假设你做了vim /etc/somefile.conf,然后你做了,i但意识到你不是sudo,你不能写。然后我会丢失我的更改,:q然后sudo !!再次进行更改。有一个更好的方法吗?
回答by Joel Spolsky
Try
尝试
:w !sudo tee %
The w !takes the entire file and pipes it into a shell command. The shell command is sudo teewhich runs teeas superuser. %is replaced with the current file name.
的w !拍摄整个文件和管道入外壳命令。shell 命令以超级用户身份sudo tee运行tee。%替换为当前文件名。
回答by Adam Maras
Save the file elsewhere (like your home folder) and then sudo mvit to overwrite the original?
将文件保存在其他地方(例如您的主文件夹),然后sudo mv覆盖原始文件?
回答by thkala
Depending on the extent of your changes, it might be faster to save (:w) your file with a different name, and then use sudoand catto overwrite the contentof the original file:
根据更改的程度,:w使用不同的名称保存 ( ) 文件,然后使用sudo和cat覆盖原始文件的内容可能会更快:
sudo sh -c 'cat changed > file'
sudo sh -c 'cat changed > file'
Note that both cpand mvwill replace the original file and its attributes (ownership, permissions, ACLs) will be lost. Do not use themunless you know how to fix the permissions afterwards.
请注意,cp和mv将替换原始文件,并且其属性(所有权、权限、ACL)将丢失。除非您知道如何在之后修复权限,否则不要使用它们。
回答by jkyle
回答by anishsane
I used this:
我用过这个:
function! SaveAsSudo()
let v=winsaveview()
let a=system("stat -c \%a " . shellescape(expand('%')))
let a=substitute(a,"\n","","g")
let a=substitute(a,"\r","","g")
call system("sudo chmod 666 " . shellescape(expand('%')))
w
call system("sudo chmod " . a . " " . shellescape(expand('%')))
call winrestview(v)
endfunction
I have mapped <F2>to :w<CR>. & <F8>to :call SaveAsSudo()<CR>
我已经映射<F2>到:w<CR>. &<F8>到:call SaveAsSudo()<CR>
Only advantage this answer provides over the sudo teeoption is: vimdoes not complain about unsaved buffer or file modified externally.
此答案提供的唯一优势sudo tee是:vim不会抱怨未保存的缓冲区或外部修改的文件。
回答by Yippie-Ki-Yay
Save the changes as another file and the make the approrpiate replacement.
将更改另存为另一个文件并进行适当的替换。
回答by Josh Lee
When vim starts up, the statusbar says [readonly], and the first time you try to edit, it says W10: Warning: Changing a readonly fileand pauses for a full second. This is enough warning for me to quit and say sudoedit /etc/somefile.conf.
当 vim 启动时,状态栏会显示[readonly],当您第一次尝试编辑时,它会显示W10: Warning: Changing a readonly file并暂停整整一秒钟。这足以警告我退出并说sudoedit /etc/somefile.conf。
You can enforce this with a plugin: Make buffer modifiable state match file readonly state.
您可以使用插件强制执行此操作:Make buffer modifiable state match file readonly state。

