Logged in to vi, made changes, forgot to sudo first - now what

11

5

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?

simon

Posted 2009-10-10T19:50:42.200

Reputation: 1 375

Answers

11

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.

Dom

Posted 2009-10-10T19:50:42.200

Reputation: 361

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 – 2014-06-23T23:20:18.453

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 – 2014-06-23T23:33:06.940

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 – 2010-04-19T14:59:39.590

39

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

wfaulk

Posted 2009-10-10T19:50:42.200

Reputation: 5 692

2If 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 – 2014-06-23T23:26:28.157

This should be made the accepted answer as the current existing answer is a rather long work-around whereas this is an immediate solution. OP? – bschlueter – 2015-03-19T04:15:34.350

-2

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

Terje Mikal

Posted 2009-10-10T19:50:42.200

Reputation: 4 867

1This 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 – 2009-10-10T20:07:39.937