/etc/apt/sources.list" E212: Can't open file for writing

后端 未结 13 780
一生所求
一生所求 2020-12-22 15:06

I am trying to edit sources.list using vi editor but getting the following error while saving the file:

/etc/apt/sources.list\" E212: Can\'t open file for wr         


        
13条回答
  •  天涯浪人
    2020-12-22 15:34

    That happens to me all the time, I open a root file for writing:

    Instead of losing all your changes and re-opening with sudo. See this demo of how to save those changes:

    One time Setup demo to create a root owned read only file for a lower user:

    sudo touch temp.txt
    sudo chown root:root temp.txt
    sudo chmod 775 temp.txt
    whoami
    el
    

    First open the file as normal user:

    vi temp.txt
    

    Then make some changes to the file, it warns you its read only. Use this command.

    :w !chmod 777 %
    

    Then write the file:

    :wq!
    

    The permissions are expanded, and the file is saved. You need the exclamation point because you are editing a root file as a lesser user.

    Explanation of what that command does:

    The :w means write the file. The bang means start interpreting as shell. chmod means change permissions, 777 means full permissions everywhere. The percent means the current file name.

    It applies the change. And it ask if you want to re-load. Press "O" for "Ok". Don't reload or you'll lose your changes.

提交回复
热议问题