I am trying to edit sources.list using vi editor but getting the following error while saving the file:
我正在尝试编辑源代码。使用vi编辑器列表,但在保存文件时得到以下错误:
/etc/apt/sources.list" E212: Can't open file for writing
9 个解决方案
#1
175
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 like sudo vim FILE
. Or if you don't want to leave your existing vim session (and know have proper sudo rights), you can issue:
Vim有一个内置的帮助系统,我只是引用了它所说的:h E212。您可能想要编辑该文件作为超级用户,比如sudo vim文件。或者,如果你不想离开现有的vim会话(并且知道有适当的sudo权限),你可以发布:
:w !sudo tee % > /dev/null
Which will save the file.
这将保存文件。
HTH
HTH
#2
12
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:
而不是失去所有的改变和重新开放与sudo。请参见演示如何保存这些更改:
One time Setup demo to create a root owned read only file for a lower user:
一次安装演示,为较低的用户创建一个root拥有的只读文件:
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.
w表示写入文件。“砰”的一声意味着开始解释为“炮弹”。chmod意味着更改权限,777意味着在任何地方都有完全的权限。百分比表示当前文件名。
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.
它适用于变化。它会问你是否想重新加载。按“O”“Ok”。不要重载,否则你会失去你的改变。
#3
6
Or perhaps you are on a readonly mounted fs
或者,您可能在一个只读挂载的fs上。
#4
4
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
即pwd文件夹/文件
and was typing
并输入
sudo vim folder/file
and rather obviously it was looking for the folder in the folder and failing to save.
很明显,它是在寻找文件夹中的文件夹并没有保存。
#5
0
Try to connect as root and then edit file. This works for me
尝试连接为根目录,然后编辑文件。这适合我
#6
0
I referenced to Zsolt in level 2, I input:
我在第2级参考了Zsolt,我输入:
: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
那么它工作
#7
0
for me worked changing the filesystem from Read-Only before running vim:
对于我来说,在运行vim之前,我将文件系统从只读更改为:
bash-3.2# mount -o remount rw /
#8
0
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
-2
You just need to access to Gemfile with root access. Before vi
:
您只需要访问具有根访问权限的Gemfile。前六世:
command:
命令:
sudo su -
then:
然后:
vi ~/...
#1
175
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 like sudo vim FILE
. Or if you don't want to leave your existing vim session (and know have proper sudo rights), you can issue:
Vim有一个内置的帮助系统,我只是引用了它所说的:h E212。您可能想要编辑该文件作为超级用户,比如sudo vim文件。或者,如果你不想离开现有的vim会话(并且知道有适当的sudo权限),你可以发布:
:w !sudo tee % > /dev/null
Which will save the file.
这将保存文件。
HTH
HTH
#2
12
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:
而不是失去所有的改变和重新开放与sudo。请参见演示如何保存这些更改:
One time Setup demo to create a root owned read only file for a lower user:
一次安装演示,为较低的用户创建一个root拥有的只读文件:
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.
w表示写入文件。“砰”的一声意味着开始解释为“炮弹”。chmod意味着更改权限,777意味着在任何地方都有完全的权限。百分比表示当前文件名。
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.
它适用于变化。它会问你是否想重新加载。按“O”“Ok”。不要重载,否则你会失去你的改变。
#3
6
Or perhaps you are on a readonly mounted fs
或者,您可能在一个只读挂载的fs上。
#4
4
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
即pwd文件夹/文件
and was typing
并输入
sudo vim folder/file
and rather obviously it was looking for the folder in the folder and failing to save.
很明显,它是在寻找文件夹中的文件夹并没有保存。
#5
0
Try to connect as root and then edit file. This works for me
尝试连接为根目录,然后编辑文件。这适合我
#6
0
I referenced to Zsolt in level 2, I input:
我在第2级参考了Zsolt,我输入:
: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
那么它工作
#7
0
for me worked changing the filesystem from Read-Only before running vim:
对于我来说,在运行vim之前,我将文件系统从只读更改为:
bash-3.2# mount -o remount rw /
#8
0
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
-2
You just need to access to Gemfile with root access. Before vi
:
您只需要访问具有根访问权限的Gemfile。前六世:
command:
命令:
sudo su -
then:
然后:
vi ~/...