Vim Sudo – Can Vim Edit a Remote File as Root?

remotesudovim

I've found this question that explains how to edit a remote file with vim using:

vim scp://user@myserver[:port]//path/to/file.txt

Is it possible to do this as root (via sudo) on the remote host?

I've tried creating a file with root permissions on the remote host and editing it with the above. Vim can see the content, can edit it, and can save it but nothing changes on the remote host (probably because vim is just saving its temp file and then giving that to scp to put back?)

When doing this with a file saved by my user it behaves as expected.

My SSH uses a key to authenticate and the remote server has NOPASSWD for my sudo access

This question is similar, but the only answer with votes uses puppet which is definitely not what I want to use.


Edit: In response to @drewbenn's comment below, here is my full process for editing:

vim scp://nagios//tmp/notouch

Where /tmp/notouch is the file owned by root, I see vim quickly show

:!scp -q 'nagios:/tmp/notouch' '/tmp/vaHhwTl/0'

This goes away automatically to yield an empty black screen with the text

"/tmp/vaHhwTl/0" 1L, 12C
Press ENTER or type command to continue

Pressing enter allows me to edit the file

Saving pops up the same kind of scp command as the beginning, which quickly and automatically goes away (it's difficult to read it in time but the scp and /tmp/… files are definitely there)

Best Answer

I'm going to say this is not possible because vim is not executing remote commands. It is simply using scp to copy the file over, edit it locally and scp it back when done. As stated in this question sudo via scp is not possible and it is recommended that you either modify permissions to accomplish what you're wanting or just ssh across to the remote machine.

Related Question