brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: sudoedit edits a root-owned file with your own editor settings, not as root

Linux
~/tips/tip-linux-sudoedit-safe-root-edits

sudoedit /etc/ssh/sshd_config

Edit system files safely without running a whole editor as root.

sudo vim /etc/hosts runs the whole editor, with all its plugins and shell escapes, as root. sudoedit takes a safer route and also keeps your own editor setup.

sudoedit /etc/ssh/sshd_config

The sudo man page explains what happens. sudo makes a temporary copy of the file, owned by you, and opens it in your editor with your environment unchanged. When you save and quit, the copy is written back over the original, but only if you changed something. So your editor runs without root, and your .vimrc or nano settings just work.

The editor is taken from SUDO_EDITOR, then VISUAL, then EDITOR, in that order. To pick one for this purpose only:

export SUDO_EDITOR=nano

It also fits nicely with a limited sudo rule. A sudoers line can allow sudoedit /etc/nginx/sites-available/* for a user, without giving them a root shell, which they would get with sudo vim through a shell escape.

I like that it has some deliberate limits. It refuses to edit a symbolic link, and it refuses files in a folder that you can write to yourself, because both could be used to trick root into overwriting something else. Since the original is only replaced when you save, a program that reads the file in the meantime sees the old version, which is usually what you want. Do check the syntax afterwards, for example with sshd -t, before you reload the service.

Further reading: sudoedit(8) manual page

More Linux tips a2query -m rewrite renice -n 10 -p 4242 cat config.yml | xsel -ib xvfb-run -a your-program --some-flag dpkg --get-selections > packages.txt All Linux tips (261) All topics

Link