/etc/apt/sources.list” E212:无法打开文件进行写入
我正在尝试使用 vi 编辑器编辑sources.list,但在保存文件时出现以下错误:
/etc/apt/sources.list" E212: Can't open file for writing
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(21)
Vim 有一个内置的帮助系统。在 Vim 中运行
:h E212
会打印以下内容:您可能想以超级用户身份使用
sudo vim FILE
编辑该文件。或者,如果您不想离开现有的 vim 会话(并且现在拥有适当的 sudo 权限),您可以运行:这将保存文件。
Vim has a builtin help system. Running
:h E212
inside Vim prints the following:You might want to edit the file as a superuser with
sudo vim FILE
. Or if you don't want to leave your existing vim session (and now have proper sudo rights), you can run:Which will save the file.
这种情况经常发生在我身上,我打开一个根文件进行写入:
而不是丢失所有更改并使用 sudo 重新打开。请参阅此演示,了解如何保存这些更改:
一次性安装演示,为较低级别的用户创建 root 拥有的只读文件:
首先以普通用户身份打开文件:
< strong>然后对文件进行一些更改,它会警告您它是只读的。使用此命令。
然后写入文件:
权限被扩展,文件被保存。您需要感叹号,因为您正在以较低用户身份编辑根文件。
该命令的作用说明:
:w 表示写入文件。爆炸意味着开始解释为外壳。 chmod 表示更改权限,777 表示所有位置都有完全权限。百分比表示当前文件名。
它应用了更改。它会询问您是否要重新加载。按“O”表示“确定”。不要重新加载,否则您将丢失所做的更改。
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:
First open the file as normal user:
Then make some changes to the file, it warns you its read only. Use this command.
Then write the file:
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.
对我来说有一个非常简单的解决方案。我试图在不存在的文件夹中编辑/创建文件。因为我已经在文件夹中,所以我试图编辑/创建其中的文件。
即
pwd文件夹/文件
并正在输入
,很明显它正在寻找其中的文件夹文件夹并无法保存。
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
and rather obviously it was looking for the folder in the folder and failing to save.
我参考了2级的Zsolt,我输入:
然后我的情况还是无法修改文件,所以提示添加“!”。所以我输入
然后它就可以了
I referenced to Zsolt in level 2, I input:
and then in my situation, I still can't modify the file, so it prompted that add "!". so I input
then it works
或者也许您使用的是只读挂载的文件系统
Or perhaps you are on a readonly mounted fs
因为该目录不存在。
可以使用
:!mkdir -p /etc/apt/
来创建目录。然后
:wq
because the dir is not exist.
can use
:!mkdir -p /etc/apt/
to make the directory.then
:wq
对我来说,在运行 vim 之前将文件系统从只读更改为:
for me worked changing the filesystem from Read-Only before running vim:
1.需要创建目录,比如
2.你可以vi或vim你的文件,然后修改,保存esc> .wq,例如
1. need to create directory, such as
2. you can vi or vim you file, then modify, and save esc > .wq, such as
只需使用 - 打开文件
,然后更改文件并使用
:wq
退出Just open the file with -
Then make the file changes and quit with
:wq
将用户更改为root
浏览至etc
在用户权限部分查找root 用户。您将得到它,就像
为您的用户名输入相同的内容一样。如果您的用户名是“myuser”,那么添加
它看起来就像
保存它。将 root 用户更改为您的用户。现在尝试与您遇到 sudoers 问题的地方相同的方法
change user to root
browse to etc
look for root user in user priviledge section. you will get it like
make same entry for your user name. if you username is 'myuser' then add
it will look like
save it. change root user to your user. now try the same where you were getting the sudoers issue
尝试以 root 身份连接,然后编辑文件。这对我有用
Try to connect as root and then edit file. This works for me
使用
sudo
预先附加您的命令。例如,使用
sudo vim textfile.txt
代替vim textfile.txt
。这将解决该问题。Pre-append your commands with
sudo
.For example, Instead of
vim textfile.txt
, usedsudo vim textfile.txt
. This will resolve the issue.您正在访问的文件可能在同一目录中已有交换副本(或交换版本),
因此首先查看隐藏文件是否存在。
例如,请参阅以下类型的文件
通过使用命令
然后,使用...删除它
通常,我建议使用...开始使用超级用户权限
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
By using the command
And then, delete it using ...
Usually, I recommend to start using super user privileges using ...
当我对目录中的文件使用 git rm 时,出现此错误。
我在类似 ~/gitRepo/code/newFeature 的地方
在 newFeature 中只有一个文件。我对该文件执行了 git rm 操作,然后尝试使用 vi 创建一个新文件 myNewFile。
Ubuntu 显示我仍在 newFeature 目录中,但实际上 git rm 已删除整个目录。
我必须退出 vi,向上导航一个目录,然后重新创建 newFeature 目录。
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.
当我的目录路径不正确时,我收到此错误,请确保您的目录名称和路径正确
I got this error when my directory path is incorrect, ensure your directory names and path are correct
更改其他用户的权限
对于该类型
setfacl -mu:用户名:rw 文件名
change the permission for the other user
for that type
setfacl -m u:username:rw filename
:wq 或 :wq! vi编辑器不退出说明您对该密码没有权限,请根据您设置的权限检查权限。
例子
你有root权限
==> sudo vi /etc/ansible/hosts/
:wq 现在开始工作了
:wq or :wq! the vi editor not exit means you have no permissons on the pwd, pls check the permissons then according to that you have made.
example
you have permissions with root
==> sudo vi /etc/ansible/hosts/
:wq now its work
以“以管理员身份”打开 cmd.exe。就这样。
Open the cmd.exe as "as administrator". That's all.
我想在 WSL
vi /mnt/c/Windows/System32/drivers/etc/hosts
中编辑 Windows 文件,为此我必须安装 Windows 插件:https://github.com/gerardog/gsudo
现在我可以使用
gsudo vi /mnt/c/Windows/System32/drivers/etc/hosts
I wanted to edit a Windows file in my WSL
vi /mnt/c/Windows/System32/drivers/etc/hosts
for this to work I had to install a Windows Addon: https://github.com/gerardog/gsudo
Now i can use
gsudo vi /mnt/c/Windows/System32/drivers/etc/hosts
只需通过管理员访问文件:
sudo vim FILE
ex。
sudo filehosts
写入“hosts”文件just access the FILE via admin:
sudo vim FILE
ex.
sudo file hosts
to write to the 'hosts' file您只需要使用 root 权限访问 Gemfile。在
vi
之前:命令:
然后:
You just need to access to Gemfile with root access. Before
vi
:command:
then: