Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

Our sysadmins login to boxes using unprivileged accounts with sudo rights. I often, out of habit, open files in vim instead of sudoedit. I do not want to get into the habit of using sudoedit. My colleague suggested creating a vim symbolic link in the path that points to sudoedit. This just seems like a horrible idea to me.

I normally just :w /tmp/service.conf and then mv -i /tmp/service.conf /etc/

This seems clunky but I am almost inclined to say that I trust this process more than getting into the habit of using sudoedit or sudo vim.

Opinions?

Side note: We generally have easily accessible backups of these files so that is not a concern.

share|improve this question

4 Answers 4

up vote 2 down vote accepted

I would just keep using VIM, and make it do the work for you. When you open a file that you don't have write access to, you can still save it by issuing the following in VIM: <ESC>:w !sudo tee %

This will allow you to execute sudo, and tee your edits into the file you are currently editing. After you do this, the file is effectively edited outside of VIM, so it will ask you to load the new contents.

share|improve this answer
    
Just tried it. Awesome. –  Belmin Fernandez Dec 2 '10 at 5:09
1  
Is there a way to save this as a macro perhaps? –  Belmin Fernandez Dec 2 '10 at 5:09
    
I haven't messed with macros or maps before, but I discovered you can create a "cmap" to accomplish this. I entered the following in my .vimrc cmap w!! w !sudo tee %^M. I chose "w!!" because w! is a force overwrite, and w!! is REALLY REALLY force :), but you can call it whatever you want. Also note that "^M" has to be entered as <CTRL-V><ENTER>, not literally ^M. –  Alex Dec 2 '10 at 16:28

With sudo.vim, you can do vim sudo:path-to-file. If you've already opened a file without sudo:, you can do :f sudo:% to switch to using sudo to read/write. (You may want to :set noro too, to avoid warnings when you try to edit/write the file that Vim thought was read-only when first opened.)

Be warned that both Alex's :w !sudo tee % solution and sudo.vim suffer from shell escaping issues. Do not use unless you know that the filenames are safe.

share|improve this answer
    
Thanks for the warning. Wasn't aware. –  Belmin Fernandez Dec 2 '10 at 12:23

If you have sudo rights such that you can run any command as root, sudoedit doesn't really give any benefits. It exists so that otherwise unprivileged users can be safely granted the ability to edit certain files, without letting them run a whole editor as root in some sort of restricted mode (which is difficult to get right).

So, your approach seems fine, although I think remembering to type sudo vim isn't that awful, given that you need to type sudo whatever to execute other commands as root.

A note on your side-note: I think I'd be more worried about accidentally overwriting another file by accident (-i not withstanding — you're expecting to say "yes" to that question, so it's easy to get in the habit of doing so) than I would about munging the file you're intending to edit. So make sure you have a backup of everything else, as well.

share|improve this answer
1  
...I kid you not: I just deleted a file accidentally even after using the -i option because of your very point about expecting to say "yes" (rm -i *). And this was after my habit of pausing to read the whole question and "processing" it in my head. Luckily it was just on my personal box and I have hourly snapshots. And an embarrassed +1 for you my good sir. –  Belmin Fernandez Dec 2 '10 at 2:22
sudo su -

Failing that, write a shell script, something like this. Note that it'll break if you want to pass arguments into vim (you could extend it to work though, it's more of a PoC)

#!/bin/sh
# Pretend to be vim, write to /tmp then sudo copy to original file
$TMPFILE = $(mktemp)
cp $1 $TMPFILE
vim $TMPFILE
sudo mv -i $TMPFILE $1

This might do what you're after? You could also easily set it up to create two copies of the file, and before doing the move, warn you if they're not still identical (meaning someone has since changed the original)

share|improve this answer

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.