When writing a configuration file, often forget to switch to root, resulting in file editing, typing: Wq exit save, appear E212: Unable to open and write file error prompts. This is due to the fact that the current user does not have write permissions in this directory.
The solution is as follows:
"1" Saves the file to the user directory, changes the owner, and moves to the configuration directory as follows:
1 Save exit with : Wq! ~/tmp/file.conf
2 su switch to root user, will file.conf change the owner, chown Root file.conf
3) Mobile configuration file to target directory,MV file.conf [pathname]
The second solution of "2" is relatively simple
Save file with : W! sudo tee%
Tee is used to read input files while saving
% represents the current edit file
(however, one requirement for this approach is that the current editing user must be in the Sudoers file, which is also required to execute the sudo command)