Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

A lot of the time I edit a file with nano, try to save and get a permission error because I forgot to run it as sudo. Is there some quick way I can become sudo without having to re-open and re-edit the file?

share|improve this question
    
merging with this question has been suggested. Basically the same problem, but vim. I'm considering the merge... though I'm also considering the fact that vim specific instructions will not work in nano. –  xenoterracide Apr 24 '11 at 8:50
1  
@xenaterracide - I think the vim question is a special case of this one, since that one actually has a solution and it seems the general case has none. I'm not sure either though, up to you. :) –  Kit Sunde Apr 24 '11 at 9:44
    
You can make a mapping in your .vimrc to easily call the sudo tee function: <pre><code>" Allows writing to files with root priviledges cmap w!! w !sudo tee % > /dev/null</pre></code> –  jasonwryan Apr 25 '11 at 7:51
    
@xenoterracide: Caleb's answer about Nano is useful, so I think we should keep this one only about Nano. –  Mikel Apr 25 '11 at 8:15
1  
@mikel I decided that merging is invalid. nano is not vim... and no nano solutions that are similar to vim's have presented themselves. –  xenoterracide Apr 25 '11 at 8:16

2 Answers 2

up vote 15 down vote accepted

No, you can't give a running program permissions that it doesn't have when it starts, that would be the security hole known as 'privilege escalation'.

Two things you can do:

  1. Save to a temporary file in /tmp or wherever, close the editor, then dump the contents of temp file into the file you were editing. sudo cp $TMPFILE $FILE. Note that it is not recomended to use mv for this because of the change in file ownership and permissions it is likely to cause, you just want to replace the file content not the file placeholder itself.
  2. Background the editor with Ctrl+z, change the file ownership or permissions so you can write to it, then use fg to get back to the editor and save. Don't forget to fix the permissions!

Edit: See this related question for other solutions in advanced editors that allow writing the file buffer to a process pipe.

share|improve this answer
    
Excellent. Thanks Caleb. I just ran into this problem about 30 minutes ago. :) –  boehj Apr 23 '11 at 18:33
1  
@Caleb You can use the tee command from within vim to do what you describe: :w !sudo tee –  user6908 Apr 24 '11 at 16:41
    
@hellojesus Brilliant trick. If you care to write that up as an answer I can remove it from mine. I figure people other than nano users get into this situation and may find themselves here. –  Caleb Apr 25 '11 at 6:14
3  
This question is supposed to be about nano. If we start including answers about Vim here, what's the point of this question? We already have Becoming root from inside Vim for Vim. –  Mikel Apr 25 '11 at 8:13
    
Great related link Mikel. I've edited again to just link to that set of answers instead of getting off topic here. –  Caleb Apr 25 '11 at 8:24

I just tried nano, and what I found most surprising is it doesn't even warn you that the file is read-only when you start trying to edit the file. (UPDATE: Apparently nano 2.2 does warn; 2.0 doesn't.)

Here's a (basic) script that does that.

It checks if you can edit the file, and if you can't, it runs "nano" as root instead.

/usr/local/bin/edit (or ~/bin/edit)

sudo=                       # empty is false, non-empty is true
editor=nano                 # XXX check $EDITOR and $VISUAL

if test -e "$1" && test ! -w "$1"; then
    if test -t 0 && test -t 2; then
        printf "%s is not writable.  Edit with sudo? [y/n] " "$1" 1>&2
        read -n 1
        case $REPLY in
        y|Y)
            sudo=true
            ;;
        n|N)
            sudo=
            ;;
        *)
            printf "\nExpected y or n.  Exiting.\n" 1>&2
            exit 1
            ;;
        esac
    else
        printf "%s is not writable.  Fix the permissions or run \"view\" instead." "$1" 1>&2
        exit 1
    fi
fi

${sudo:+sudo} "$editor" "$1"

And a command I called view so that you can avoid the prompt if you know you aren't going to make any changes.

/usr/local/bin/view (or ~/bin/view)

editor=nano
readonlyflag=-v

"$editor" $readonlyflag "$1"

There's already a program called view that's part of Vi/Vim, so feel free to suggest a better name.
(But I think a full implementation of this program would make Vi's view redundant.)


Full versions

share|improve this answer
1  
It does actually warn you. Question would then be which version you are using. Also, on Debian-based systems the alternatives system is better suited for what you are suggesting. –  0xC0000022L May 6 '11 at 22:25
2  
For your reference: [ Read ... lines (Warning: No write permission) ] is what appears right above the two lines of help for the shortcuts (bottom of the screen). Nano version is 2.2.4. –  0xC0000022L May 6 '11 at 22:28
    
Ah, it doesn't warn in nano 2.0, which is what I was testing with. –  Mikel May 6 '11 at 22:38
    
cool we sorted that out :) –  0xC0000022L May 6 '11 at 22:44

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.