TIL: sudoedit

· 166 words · 1 minute read

We use the sudo command when we need to temporarily do something as root (or less commonly as another user). One common use case is to edit system configuration files owned by root. For the longest time, this is how I do it:

sudo hx /etc/ssh/sshd_config

In this case, I am using the Helix editor. I noticed that Helix did not load my configurations. This makes sense since we are running hx as root, it would not know to load my configuration.

I found sudoedit (or sudo -e) as an alternative to editing files owned by root:

sudoedit /etc/ssh/sshd_config

The way this works is that sudo would make a temporary copy of the file owned by your user, edit the file as your user, then replace the original file (the one owned by root) with that temporary file. sudoedit would use SUDO_EDITOR, VISUAL, or EDITOR to edit the file. Since the editor is running as your user, it would also correctly load the correct configuration files.