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

左心房为你撑大大i 提交于 2019-12-04 07:20:35

问题


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 writing

回答1:


For some reason the file you are writing to cannot be created or overwritten.
The reason could be that you do not have permission to write in the directory
or the file name is not valid.

Vim has a builtin help system. I just quoted what it says to :h E212.

You might want to edit the file as a superuser as sudo vim FILE. Or if you don't want to leave your existing vim session (and now have proper sudo rights), you can issue:

:w !sudo tee % > /dev/null

Which will save the file.

HTH




回答2:


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.




回答3:


For me there was was quite a simple solution. I was trying to edit/create a file in a folder that didn't exist. As I was already in the folder I was trying to edit/create a file in.

i.e. pwd folder/file

and was typing

sudo vim folder/file

and rather obviously it was looking for the folder in the folder and failing to save.




回答4:


Or perhaps you are on a readonly mounted fs




回答5:


I referenced to Zsolt in level 2, I input:

:w !sudo tee % > /dev/null

and then in my situation, I still can't modify the file, so it prompted that add "!". so I input

:q! 

then it works




回答6:


for me worked changing the filesystem from Read-Only before running vim:

bash-3.2# mount -o remount rw /



回答7:


Try to connect as root and then edit file. This works for me




回答8:


It might be possible that the file you are accessing has a swap copy (or swap version) already there in the same directory

Hence first see whether a hidden file exists or not.

For example, see for the following type of files

.system.conf.swp

By using the command

ls -a

And then, delete it using ...

rm .system.conf.swp

Usually, I recommend to start using super user privileges using ...

sudo su



回答9:


I got this error when I used git rm on a file in a directory.

I was in something like ~/gitRepo/code/newFeature

In newFeature there was only one file. I did a git rm on that file then tried to create a new file myNewFile using vi.

Ubuntu showed me as still being inside the newFeature directory but actually git rm had removed the whole directory.

I had to exit out of vi, navigate up one directory and then recreate the newFeature directory.




回答10:


You just need to access to Gemfile with root access. Before vi:

command:

sudo su -

then:

vi ~/...


来源:https://stackoverflow.com/questions/8253362/etc-apt-sources-list-e212-cant-open-file-for-writing

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!