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.

I made lengthy changes to a configuration file on a Ubuntu Linux computer with the vi editor. Unfortunately, I forgot to sudo first, so now I'm in the editor, but can't save my changes because of missing rights. Can I retroactively sudo the user on that terminal, or what would be the best course of action to take?

share|improve this question
2  
See also stackoverflow.com/questions/1005/… –  therefromhere Oct 10 '09 at 20:00

3 Answers 3

up vote 10 down vote accepted

In this case, I write the file with :w /tmp/tmpfile. Then I go out and move /tmp/tmpfile to my old file with sudo rights.

share|improve this answer
    
this is what i end up doing, but i like wfaulk's answer .. if i can only remember it the next time i do this! :) –  quack quixote Apr 19 '10 at 14:59
    
I hope you copy the file rather than moving it. Moving replaces the file’s mode (permissions) with vi’s default (which is probably 666, ANDed with the inverse of your “umask”), replaces the file’s owner with your UID (it was “root”, wasn’t it?), and breaks hard links. –  Scott Jun 23 at 23:20
    
A couple of issues: (1) If the file is supposed to be confidential, and your “umask” is 22 (rather than 66), this procedure exposes the file’s contents to other users who might be monitoring the /tmp directory. (2) If you have truly evil users on your system, they could replace your /tmp/tmpfile between when you write it (from vi) and when you copy it over the system configuration file that you were editing. It’s safer to put the temporary file into a directory that only you have access to. –  Scott Jun 23 at 23:33

From SO:

:w !sudo tee %

I actually find myself using this way to do it more frequently now:

:%!sudo tee %

I think it's a little more intuitive, as I know what :%! does, whereas I don't have a visceral understanding of :w !. Also, it's easy to miss the very important space between the w and the !.

share|improve this answer
    
If it helps your intuition any, remember that vi commands can be multiple letters, so there could theoretically be a “wfoo” command, so if you want to write to a file called “foo”, you must say “:w foo”. I.e., you need a space after “:w”. As far as “:w !” is concerned – you know what “:!” is, right? “:!date” runs a “date” command. So “:w !xyz” writes the buffer, but to a command rather than to a file. –  Scott Jun 23 at 23:26

Couldn't you open another terminal and temporarily change the file's access rights?

share|improve this answer
1  
This is a bad idea. Probably wont ever be a big security problem, but it could be, and there's better and simpler solutions (like writing to a temporary file, or better, the :w !sudo tee % solution wfaulk posted –  dbr Oct 10 '09 at 20:07

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.