/etc/apt/sources.list” E212:无法打开文件进行写入

发布于 2024-12-17 21:57:06 字数 133 浏览 7 评论 0原文

我正在尝试使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(21

謸气贵蔟 2024-12-24 21:57:06

Vim 有一个内置的帮助系统。在 Vim 中运行 :h E212 会打印以下内容:

由于某种原因,您正在写入的文件无法创建或覆盖。
原因可能是您没有该目录的写入权限
或者文件名无效。

您可能想以超级用户身份使用 sudo vim FILE 编辑该文件。或者,如果您不想离开现有的 vim 会话(并且现在拥有适当的 sudo 权限),您可以运行:

:w !sudo tee % > /dev/null

这将保存文件。

Vim has a builtin help system. Running :h E212 inside Vim prints the following:

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.

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:

:w !sudo tee % > /dev/null

Which will save the file.

沙沙粒小 2024-12-24 21:57:06

这种情况经常发生在我身上,我打开一个根文件进行写入:

而不是丢失所有更改并使用 sudo 重新打开。请参阅此演示,了解如何保存这些更改:

一次性安装演示,为较低级别的用户创建 root 拥有的只读文件:

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

首先以普通用户身份打开文件:

vi temp.txt

< strong>然后对文件进行一些更改,它会警告您它是只读的。使用此命令。

:w !chmod 777 %

然后写入文件:

:wq!

权限被扩展,文件被保存。您需要感叹号,因为您正在以较低用户身份编辑根文件。

该命令的作用说明:

: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:

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.

燃情 2024-12-24 21:57:06

对我来说有一个非常简单的解决方案。我试图在不存在的文件夹中编辑/创建文件。因为我已经在文件夹中,所以我试图编辑/创建其中的文件。

pwd文件夹/文件

并正在输入

sudo vim folder/file

,很明显它正在寻找其中的文件夹文件夹并无法保存。

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.

无需解释 2024-12-24 21:57:06

我参考了2级的Zsolt,我输入:

:w !sudo tee % > /dev/null

然后我的情况还是无法修改文件,所以提示添加“!”。所以我输入

:q! 

然后它就可以了

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

中性美 2024-12-24 21:57:06

或者也许您使用的是只读挂载的文件系统

Or perhaps you are on a readonly mounted fs

二手情话 2024-12-24 21:57:06

因为该目录不存在。

可以使用 :!mkdir -p /etc/apt/ 来创建目录。

然后 :wq

because the dir is not exist.

can use :!mkdir -p /etc/apt/ to make the directory.

then :wq

梦里寻她 2024-12-24 21:57:06

对我来说,在运行 vim 之前将文件系统从只读更改为:

bash-3.2# mount -o remount rw /

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

bash-3.2# mount -o remount rw /
ゞ花落谁相伴 2024-12-24 21:57:06

1.需要创建目录,比如

mkdir -p /var/dir1/dir2

2.你可以vi或vim你的文件,然后修改,保存esc> .wq,例如

vim /var/dir1/dir2/filename.txt

1. need to create directory, such as

mkdir -p /var/dir1/dir2

2. you can vi or vim you file, then modify, and save esc > .wq, such as

vim /var/dir1/dir2/filename.txt
梦情居士 2024-12-24 21:57:06

只需使用 - 打开文件

sudo vi filename.txt

,然后更改文件并使用 :wq 退出

Just open the file with -

sudo vi filename.txt

Then make the file changes and quit with :wq

旧时光的容颜 2024-12-24 21:57:06

将用户更改为root

sodu su -

浏览至etc

vi sudoers

在用户权限部分查找root 用户。您将得到它,就像

root ALL=(ALL:ALL) ALL 

为您的用户名输入相同的内容一样。如果您的用户名是“myuser”,那么添加

myuser ALL=(ALL:ALL) ALL

它看起来就像

root ALL=(ALL:ALL) ALL 

myuser ALL=(ALL:ALL) ALL 

保存它。将 root 用户更改为您的用户。现在尝试与您遇到 sudoers 问题的地方相同的方法

change user to root

sodu su -

browse to etc

vi sudoers

look for root user in user priviledge section. you will get it like

root ALL=(ALL:ALL) ALL 

make same entry for your user name. if you username is 'myuser' then add

myuser ALL=(ALL:ALL) ALL

it will look like

root ALL=(ALL:ALL) ALL 

myuser ALL=(ALL:ALL) ALL 

save it. change root user to your user. now try the same where you were getting the sudoers issue

小情绪 2024-12-24 21:57:06

尝试以 root 身份连接,然后编辑文件。这对我有用

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

神魇的王 2024-12-24 21:57:06

使用 sudo 预先附加您的命令。

例如,使用 sudo vim textfile.txt 代替 vim textfile.txt。这将解决该问题。

Pre-append your commands with sudo.

For example, Instead of vim textfile.txt, used sudo vim textfile.txt. This will resolve the issue.

巡山小妖精 2024-12-24 21:57:06

您正在访问的文件可能在同一目录中已有交换副本(或交换版本),

因此首先查看隐藏文件是否存在。

例如,请参阅以下类型的文件

.system.conf.swp

通过使用命令

ls -a

然后,使用...删除它

rm .system.conf.swp

通常,我建议使用...开始使用超级用户权限

sudo su

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
叫思念不要吵 2024-12-24 21:57:06

当我对目录中的文件使用 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.

浮萍、无处依 2024-12-24 21:57:06

当我的目录路径不正确时,我收到此错误,请确保您的目录名称和路径正确

I got this error when my directory path is incorrect, ensure your directory names and path are correct

我不是你的备胎 2024-12-24 21:57:06

更改其他用户的权限
对于该类型
setfacl -mu:用户名:rw 文件名

change the permission for the other user
for that type
setfacl -m u:username:rw filename

碍人泪离人颜 2024-12-24 21:57:06

: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

卷耳 2024-12-24 21:57:06

以“以管理员身份”打开 cmd.exe。就这样。

Open the cmd.exe as "as administrator". That's all.

长途伴 2024-12-24 21:57:06

我想在 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

两人的回忆 2024-12-24 21:57:06
"hosts" E212: Can't open file for writing 
Press ENTER or type command to continue

只需通过管理员访问文件:sudo vim FILE

ex。 sudo filehosts 写入“hosts”文件

"hosts" E212: Can't open file for writing 
Press ENTER or type command to continue

just access the FILE via admin: sudo vim FILE

ex. sudo file hosts to write to the 'hosts' file

真心难拥有 2024-12-24 21:57:06

您只需要使用 root 权限访问 Gemfile。在vi之前:

命令:

sudo su -

然后:

vi ~/...

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

command:

sudo su -

then:

vi ~/...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文