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)
ssh
to the server and edit the file there (withvisudo
, or whatever). Anything else has security implications. – Satō Katsura Jun 27 '16 at 16:44.vimrc
file – Mitch Jun 27 '16 at 16:47ssh -t host sudo vim file
? – Kusalananda Jun 27 '16 at 17:02vim
as opposed to my local version, whereas thevim scp...
uses my local version of vim:ssh -t nagios sudo vim /tmp/notouch \n sudo: vim: command not found
(this is a centos server that hasvi
notvim
– Mitch Jun 27 '16 at 17:04scp://root@myserver/...
...? – user253751 Jun 28 '16 at 11:35