18

I edited a file in /etc/ that I want to save, but forgot to open it using sudo. I remember there was a command to save such a file in vi, and want to know if there is any such way to do it in nano?

Thanks.

  • The changes are extensive. I suppose I can open a new terminal, hen-pick the changes and copy paste them. Or just copy-paste the whole thing. But I was wondering if there was a "geeky" way of doing it. – theTuxRacer Nov 30 '10 at 5:19
21

Yes you could save it temporarily to your home directory. Press Ctrl+O to change the path to your home directory or in /tmp and then press Enter to save it. Then you can sudo mv it.

alt text

Press CTRL+O will show you the path. Change that to your home directory or /tmp. For example File Name to Write: /tmp/filename and press Enter.

  • 4
    You need to sudo cp </full/temp/name> </full/original/name> and rm </full/temp/name> to maintain permissions on the original file. sudo mv will destroy them, which is not what you want, especially if it had executable permissions. – Martin Thornton Jul 11 '15 at 14:34
4

Just open a new tab in terminal, use chmod to change permissions of the file to 777, save the file in nano, then change the permissions back to what they were, probably 644. An alternative is to change owner using chown to yourself, save the file, and change ownership back to root.

  • 1
    Coupled with Ctrl + z and fg this is hands down the fastest way to do it. – Jervelund Dec 13 '14 at 11:11
  • I find this way easier and faster than the accepted answer (Y) – Sudip Bhandari Nov 2 '16 at 14:17
  • sudo chmod 777 yourfile – Josh May 7 '19 at 10:17
2

Try ctrl+o and choose to save the file to your home folder.

Then do sudo mv /home/username/file /etc/

  • yeah, i guess I could do that. thanks. but ill still hold on accepting it, to see if there is really a way. – theTuxRacer Nov 30 '10 at 5:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.