ERROR: “E212: Can’t open file for writing” when unable to update /etc/hosts file on Linux system with root user

on

ERROR: “E212: Can’t open file for writing” when unable to update /etc/hosts file on Linux system with root user

Description:
Unable to update /etc/hosts file on Linux system with root user. Error while saving:

“E212: Can’t open file for writing”

Permissions are already intact for root:

ls -ltr /etc/hosts
-rw-r–r– 1 root root 224 Nov 15 23:20 /etc/hosts

Though there are permissions shown, the file attribute could be configured.

To check, you can do the following:

lsattr /etc/hosts
—-i——–e- /etc/hosts

of

lsattr /etc/hosts
——–a—-e- /etc/hosts

This shows that the file level attribute is set to be non-editable.

Solution
To be able to edit a file, you can change the attribute as follows:

chattr -i /etc/hosts
or
chattr -a /etc/hosts
[root@hotromaytinhit]# ls -lt /etc/hosts

Now, the file could be editable as follows:

[root@hotromaytinhit]# lsattr /etc/hosts
————-e- /etc/hosts

ls -lt /etc/hosts
-rw-r–r– 1 root root 224 Nov 15 23:20 /etc/hosts

Leave a Reply

Your email address will not be published. Required fields are marked *