Is it possible to save as sudo from nano after you've forgotten to run as sudo?

46,586

Solution 1

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!

¹ Some editors are actually able to do this by launching a new process with different permissions and passing the data off to that process for saving. See for example this related question for other solutions in advanced editors that allow writing the file buffer to a process pipe. Nano does not have the ability to launch a new process or pass data to other processes, so it's left out of this party.

Solution 2

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:
46,586

Related videos on Youtube

Kit Sunde
Author by

Kit Sunde

CTO at Media Pop, hire us for DevOps and product development

Updated on September 18, 2022

Comments

  • Kit Sunde
    Kit Sunde over 1 year

    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?

    • Admin
      Admin about 13 years
      @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. :)
    • Admin
      Admin about 13 years
      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>
    • Admin
      Admin about 13 years
      @xenoterracide: Caleb's answer about Nano is useful, so I think we should keep this one only about Nano.
    • Admin
      Admin about 13 years
      @mikel I decided that merging is invalid. nano is not vim... and no nano solutions that are similar to vim's have presented themselves.
  • boehj
    boehj about 13 years
    Excellent. Thanks Caleb. I just ran into this problem about 30 minutes ago. :)
  • Bohemian
    Bohemian about 13 years
    @Caleb You can use the tee command from within vim to do what you describe: :w !sudo tee
  • tcoolspy
    tcoolspy about 13 years
    @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.
  • Mikel
    Mikel about 13 years
    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.
  • tcoolspy
    tcoolspy about 13 years
    Great related link Mikel. I've edited again to just link to that set of answers instead of getting off topic here.
  • Mikel
    Mikel about 13 years
    cat TMPFILE > FILE will obviously need you to do su - or similar first. sudo cp TMPFILE FILE would be easier. cp actually truncates the file if it already exists and doesn't change it's permissions.
  • Mikel
    Mikel about 13 years
    @Caleb: The related link was actually suggested by xenoterracide.
  • 0xC0000022L
    0xC0000022L about 13 years
    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
    0xC0000022L about 13 years
    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.
  • Mikel
    Mikel about 13 years
    Ah, it doesn't warn in nano 2.0, which is what I was testing with.
  • 0xC0000022L
    0xC0000022L about 13 years
    cool we sorted that out :)
  • Stanley Wintergreen
    Stanley Wintergreen about 7 years
    @Caleb whenever I look for an answer regarding bash, I always see your answers :D
  • Anand Rockzz
    Anand Rockzz about 6 years
    Save to a temporary file in /tmp - How to save to temp file while inside nano ?
  • baptx
    baptx over 5 years
    @user6908 +1 but the correct command to use with vim is :w !sudo tee %
  • Admin
    Admin about 2 years
    Don't forget to quote the expansion of "$@" on nano's command line, otherwise it breaks for files with spaces in their names.