21

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.

1
  • 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. Nov 30, 2010 at 5:19

3 Answers 3

23

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.

1
  • 5
    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. Jul 11, 2015 at 14:34
7

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.

3
  • 1
    Coupled with Ctrl + z and fg this is hands down the fastest way to do it.
    – Jervelund
    Dec 13, 2014 at 11:11
  • 1
    I find this way easier and faster than the accepted answer (Y) Nov 2, 2016 at 14:17
  • sudo chmod 777 yourfile
    – Josh
    May 7, 2019 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/

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

You must log in to answer this question.

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