How can I edit a remote file with sudo?

I have ssh/scp access to remote server, and passwordless sudo on it.

I want to edit /etc/some/file as remote root, so I ran:

  $ Vi scp://server/etc/some/file

It doesn't work, however, citing I don't have enough permissions. How can I do this?

migrated from stackoverflow.com Nov 26 '11 at 15:42

This question came from our site for professional and enthusiast programmers.

  • 2
    You can log into remote system using ssh remoteserver, and on remote server execute sudo vim /etc/myfile. OR one command: ssh -t remoteserver sudo vi /etc/myfile – taro Nov 26 '11 at 8:12
  • @taro What if he does not have a login shell on that machine? – Swiss Nov 26 '11 at 8:15
  • If he does not have login, he can scp remote:/etc/myfile ~/myfile, edit vim ~/myfile, but it will be a problem to overwrite remote file with changed one. – taro Nov 26 '11 at 8:21
  • 2
    The main aim is to use local vim (with my settings, my keymap etc) on remote. Yes, i can install on every host my settings -- but that ugly way. – datacompboy Nov 26 '11 at 11:32
  • I am having the exact same problem, and have the exact same motivation, to use one single central VIM program – puk Jul 16 '15 at 5:42

According this wiki page you should specify your username and host like that:

vim scp://user@server//etc/some/file

And thanks for the tip, I didn't know that vim is capable of editing files via scp.

  • Sorry, seems like it doesn't solve the problem with permissions unless you log in as root. – KL-7 Nov 26 '11 at 8:18
  • The problem with permissions is not a problem. If you want to do root-level stuff you just login as root, that's a good thing in my book. Your solution works well. – romainl Nov 26 '11 at 10:12
  • I have already said -- that doesn't allows to access sudo-accessible files. – datacompboy Nov 26 '11 at 11:32
  • 1
    Remote editing in Vim is not done on the remote server. The file is downloaded locally and re-uploaded after save – romainl Nov 26 '11 at 15:37
  • 2
    I'm know. Just want to find netrw plugin/mod that allows not just scp, but ssh+sudo. – datacompboy Nov 26 '11 at 17:09

To expand on my comment…

Vim doesn't do that kind of fancy thing on the remote machine, no matter what account you use. It's limited to downloading and uploading: reading a remote file means downloading the file in a temporary location on your local machine, writing a remote file means uploading the local copy to the remote machine.

It means that you won't be able to sudo before editing on the remote machine.

If the account used to write/upload the file is not root you can't overwrite the root-owned file. That's basic UNIX/server security.

Instead, ssh into the server and use Vim there or use KL-7's snippet with the right account.

See :help netrw-xfer, and :help netrw-quickmap for additional features such as directory listing. No sudo in sight.

  • 1
    You can do that in emacs, his trampoline able to work via ssh+sudo. I'm just search for plugin, that implements same thing for vim. – datacompboy Nov 26 '11 at 17:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.