Member-only story
How to Edit Root Owned Files on Linux Without Running Your Editor as Root
sudoedit explained, and why it is safer than sudo vim
You need to change one line in /etc/hosts. You open it in your editor. You make the edit. You try to save.
E212: Can't open file for writingThe file belongs to root. A normal user cannot write to it. You do what everyone does. You press Up and put sudo in front of the same command. The editor opens again and this time you can save.
It works. It also quietly breaks a safety rule. You just started a large and complicated program with full administrator rights. Linux has a better way. A single command lets you edit protected files while your editor runs as you. The command is sudoedit.
Why You Get Permission Denied
Every file on Linux has an owner and a set of permissions. Look at the hosts file.
ls -l /etc/hosts-rw-r--r-- 1 root root 221 Oct 29 09:10 /etc/hostsThe first column shows the permissions. The third column names the owner. Here the owner is root. The letters rw- in the first group mean the owner can read and write. The next two groups show r--. Everyone else can only read.





