If you know what youâre doing, running commands as root is perfectly fine. In order to ease usage, logging in as root or suâing into root is acceptable. I find myself in many situations where I have scripts that should only run as root (for example for backing up an entire fs, which you need super user to back up all users data and the OS). Or when you have to pipe outputs to files.
When trying to look at these kind of scripts or directories, you have to go like:
sudo ls -l /root
sudo ls -l /root/subfolder
sudo ls -l /root/subfolder/other/
sudo ls -l /root/subfolder/folder
sudo cat /root/subfolder/folder/file
This is because you cannot autocomplete stuff that you are not allowed to read from. When logged in as root, itâs as easy as using tab autocompletion and getting it right the first time (excluding shells that donât autocomplete, like dash or ash).
sudo -i
cat /root/subfolder/folder/file
With that said, on my own systems, I still do the workaround of having to run sudo doas ls -l /path/where/users/don't/have/access a bunch of times, until I find the full folder and file. At worst, I may do a doas nnn, but that would be after Iâm already getting desperate.
The other example of piping to files. Best example is adding a new wifi AP SSID and password to your headless wireless WAN connected router.
wpa_passphrase SSID passowrd >> /etc/wpa_supplicant/wpa_supplicant.conf
If you run the above with sudo as an unprivileged user, it will fail, because the pipe is for your own user to output to that file, file which is owned by root alone, with no write permissions for other users. To get around it, you could do what I do, but in the annoying way:
wpa_passphrase SSID passowrd | doas tee -a /etc/wpa_supplicant/wpa_supplicant.conf
(replace doas with sudo)
Doing it logged in as root is easier and you know that the commands are safe to run. Of course, donât run random scripts you get from the internet, unless you understand them. If you have to, try running them as an unprivileged user that doesnât even have sudo permissions and see if it works. Otherwise, donât run it, unless you trust it.
The myth of âdonât log in a rootâ is a great rule of thumb for all linux newbies. It is better to use a lesser privileged account and make sure that when you want to run commands as root, you use sudo. That way you know what things may do more stuff to your system. Itâs a good rule of thumb, but by no means unbreakable.
On systems that arenât mine, I just su into root and get it done, especially when Iâm not on my own time. Iâm more cautious with other peopleâs systems, so I donât run everything that just comes to mind and I always ask if itâs ok to run X, or especially install software (even if they are from the running distroâs main repos).
Lastly, for remote systems, itâs a good idea to follow the alpine user setup wiki. Disable sshd password authentication (only use keys), ssh into a non-privileged user that is not even a sudoer, then su into a sudoer user with a strong password and use that user whenever you need to run sudo commands. Cumbersome, makes you use the sudoer anyway, but if it doesnât have the key in its authorized_keys, you wonât be able to work around by logging to the sudoer directly without first going through the non-privileged user. And making it annoying to access (like using a long rng password) make you less likely to go into that user.
Similarly, programs, if you can, should be ran under a user that isnât a sudoer. Docker is not one of them, because it requires root. But its RHEL alternative, podman, can run rootless. Stuff like webservers (apache, nginx) should run as a user without sudo rights. Everything that can be ran as a user should be run as a user. And if a program requires root, try to move it to a container that pretends itâs root, but itâs actually a non-privileged user. Sometimes that may not work with some workarounds (like a DNS server, because you would have to port forward the DNS port to the container anyway, so youâd still be running something as root).
Anyway, rant over.