7

I am using Nano.

Sometimes I make changes to a file and then after attempting to save I realize that I need sudo permissions. Then I need to close the file, reopen it as sudo and make my changes again...

Is there a way to save anyways? I remember a colleague closed the file without saving and then executed "sudo !!" or something and then it saved the changes.

How does it work?

7
  • 1
    What tool do you use to edit the file? I see you've tagged with nano, is that what you use? Please edit and clarify.
    – terdon
    Apr 25, 2022 at 9:24
  • 2
    While it's tedious, when I do this, I save it under a different file name and mv it afterwards. At least saves me from having to make the changes twice.
    – zenzelezz
    Apr 25, 2022 at 9:42
  • @terdon Seems likely to be a duplicate no matter what -- either of the general case (e.g. nano) or vi, right? Apr 25, 2022 at 14:52
  • 1
    Write the file out to /tmp, then sudo mv /tmp/file to /realpath/file. Check permissions and ownership afterwards.
    – mpez0
    Apr 25, 2022 at 21:35
  • 1
    Yes, the body is the best option, absolutely. Tags are there to help categorize the questions, and also they are often misused (I've seen loads of people slap seemingly random tags on posts). So clearly mentioning it in the body is the way to go, thanks.
    – terdon
    Apr 26, 2022 at 10:59

4 Answers 4

6

The command sudo !! in shells such as zsh or bash that implement csh-style history expansion would re-execute the most recent command but prefix it with sudo. If the most recent command was opening a text editor, then this will be done as the root user. You could then go on and re-do the changes and save. However, the changes that you introduced the first time around would be lost.

Assuming you are using the nano editor as a non-root user, and you find yourself needing to save the current file as root. You may then do so by filtering the current buffer's contents through sudo tee some/file/path. That command would run tee as root and would overwrite or create the file at some/file/path.

From the nano editor, you could invoke this filter by pressing ^R^X, i.e., Ctrl+R+Ctrl+X, and then type

|sudo tee some/file/path

(The initial | signals to nano that you want to pass the current document to that command.)

If sudo needs to prompt you for your password, it will do so. This will mess up the display a bit, but you may redraw the screen by pressing ^L (Ctrl+L).

Note that care must be taken to specify the correct pathname of the file that you want to write to, as it would irreversibly overwrite that file with the contents of the editor's buffer. Since you write as root, permissions will not stop you from destroying data. This is therefore a sort of "hack" that may possibly be good to know about, but that you don't want to rely on.

The ability to filter text from nano to an external command like this was added in release 2.9.8 (2018).

5

Try this...

:w !sudo tee %

Explanation...

  • :w – writes the contents of buffer
  • !sudo – pipes it to the stdin of sudo
  • tee % – sudo executes tee to write to “%” file
  • % – Vim expands “%” to current file name
2
  • 8
    This is for vim, what makes you think the OP is using vim?
    – terdon
    Apr 25, 2022 at 9:25
  • Oops. Good point.
    – quetzalito
    Apr 26, 2022 at 5:15
2

sudo !! will recall last command (!!) with sudo. You'll have to remade the change anyway (supposing last command was the editing).

Depending on file size and sensivity (confidentials informations, clear text password, etc ...), you may save file in a temporary location, and re import it from sudo.

sample code from a vi session :

:w /tmp/file.txt 
sudo vi /some/path/file.txt
:%d (this will delete all content of file.txt under sudo)
:r /tmp/file.txt (this will include from /tmp/file.txt )
:x (exit vi)
rm -f /tmp/file.txt (delete file)

note that /tmp/file.txt is exposed.

0

The general solution is "write the modified file to somewhere permitted, exit the editor, copy/move the file to the Right Place".

If you set up the VISUAL and EDITOR environment variables to the full path of your favorite editor (use, e.g. type -p nano) , you can use the sudoedit command. Read man sudoedit.

sudoedit uses its root powers to make a temporary copy of the file you're editing, lets you ($USER) edit the copy. When you finish editing, and the temporary file has been changed, sudoedit copies the temporary file to the file you wanted to edit.

Not the answer you're looking for? Browse other questions tagged .