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

etc/apt/sources.list, On a standard, clean install of Kali Linux, with network access, you should have the following entry present in /etc/apt/sources.list : sudo sh -c "echo 'deb http://ppa.launchpad.net/person/ppa/ubuntu karmic main' >> /etc/apt/sources.list" However, instead of editing /etc/apt/sources.list, it is simpler to add a new *.list file to /etc/apt/sources.list.d.

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.

How do I add a line to my /etc/apt/sources.list?, SourcesList file i.e. /etc/apt/sources.list contains the list of repositories from where Advanced Package Tool could download the packages for us. sudo nano /etc/apt/sources.list Use <Ctrl>+x to exit, then press "y" and Return to save the modifications. Last edited by Head_on_a_Stick; 01-31-2015 at 05:30 AM .

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.

Kali Network Repositories (/etc/apt/sources.list), list somehow had not been updated. It was still referring to xenial everywhere, now bionic entry was to be seen anywhere. I had to replace the source.list manually� Very new to raspberry pi and I am interested in using opencpn to build a autopilot for a boat. While attempting to download opencpn (trying to follow directions and not succeeding). I have lost my /etc/apt/sources.list. I was told to add this PPA to the end and I think I deleted some important lines in the process

Or perhaps you are on a readonly mounted fs

/etc/apt/sources.list - SourcesList file in Ubuntu distribution, Back up your current sources.list, make a new one and start editing. sudo mv /etc/ apt/sources.list /� The file ' /etc/apt/sources.list' in Debian contains the list of the 'sources' from which the packages can be obtained. sources.list file can vary depending on various factors (from which medium the Debian was installed, was it updated from previous release, etc) This is the list we want on our system:

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

/etc/apt/sources.list for Ubuntu 18.04.1 LTS Bionic Beaver � GitHub, Ubuntu uses apt for package management. Apt stores a list of repositories or software channels in the file /etc/apt/sources.list. and in any file� The easiest way is to edit /etc/apt/sources.list. Remove or comment each line with # in front and add the following line in the terminal window: leafpad /etc/apt/sources.list . After this command line, the screen will appear: Now, remove the old command line and add a new one. Repo for Kali Linux versions. For Kali Linux 1.x users, use the repo

How To Configure Apt Sources.List, Since you have sudo working, you should use sudoedit : SUDO_EDITOR="gedit - w" sudoedit /etc/apt/sources.list. Site of reference for sources.list for different linux distro that use APT as package management system: Ubuntu, Kubuntu, Linux Mint, Debian. But there are also sources.list for Linux O.S. in which APT isn't native.

Repositories/CommandLine, The APT package manager and all the graphical front ends (Ubuntu Software Center, Muon, aptitude etc) uses the sources.list file to learn about which package� Chapter 2 - Basic Configuration 2.1 The /etc/apt/sources.list file As part of its operation, APT uses a file that lists the 'sources' from which This file is /etc/apt/sources.list.

How to edit sources.list as root under Debian 9?,

Comments
  • I got this when the directory I was trying to write the file to exceeded its max. disc usage
  • As a comment mentioned in one of the answers, you might get this if folder in which the file resides has been deleted or didn't exist in the first place. Look for commands for changing directory or creating a directory from vi, depending on the situation.
  • Thanks! In my case it was trying to edit a non-existing file in a non-existing directory. Turns out, while you can "open" non-existing file and then create it by saving it, this doesn't work with non-existing directory. And now I know about the Vim's built-in help system! :)
  • Thanks! In my case, I was trying to write to a network drive, and my network permissions had partially crashed for that screen session. I ended up writing out to a temporary file in /var and copying that from another ssh instance over to my home directory.
  • Rolled back an invalid edit.
  • @Xinus see this: stackoverflow.com/questions/2600783/…
  • :h E212 says: ".bashrc" E212: Can't open file for writing E433: No tags file E149: Sorry, no help for E212
  • This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post.
  • @DmitryG There is no question being asked here, just a situation being stated. This was the reason for which I got into the very same situation and I think people would find it useful to know about it.
  • doesn't this just quits the file, and not save it?
  • This is bad practice. root should be used as a last resort.
  • Thanks it helped!