system administration with emacs






When performing system administration tasks, one often needs to edit files
owned by root.



For both security and safety reasons, it's a good idea to do as little as
possible as root (or with root privileges). For that reason, you probably
don't want to run Emacs as 'root', because it's simply too powerful. I often
see people use vi (usually, vim) instead – but since it allows you to do
just about anything as well (like running shell commands), that's not much of
an improvement.



Now, a while back we discussed editing files owned by root with tramp – you
can use emacs with your normal user-account, and use sudo to write the
root-owned file. That makes it much harder to screw things up.



Another reason people use vi for little editing jobs is because its startup
time is significantly shorter than the startup time for a new emacs
instance. For that, however, we have emacs daemon.



Combining tramp and emacs daemon and a shell function:





# edit file with root privs
function E() {
emacsclient -c -a emacs "/sudo:root@localhost:$1"
}





Now, we can very quickly edit any file owned by root using 'E' --






$ E /etc/hosts





So, if you prefer emacs, there's little reason to use vi, even for editing
system files – although I have to admit that it takes time to evict 'sudo vi
<system file>' from my muscle memory.



Update reader Yi Wang mentions that, in fact, we can make this a bit more
general using sudoedit; so, instead of using Tramp, we can use:





# edit file with root privs
alias E="SUDO_EDITOR=\"emacsclient -c -a emacs\" sudoedit"






This works also without absolute paths.


No comments:

Post a Comment

Followers

Popular Posts