Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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?

share|improve this question

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
    
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

2 Answers 2

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.

share|improve this answer
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

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.

share|improve this answer
    
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
1  
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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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