Linux, root and permissions

I have been using a raspberry pi with raspbian for a long time and it has been working fine. However I recently got my hands on an older Intel NUC which is way more powerful. I have installed Debian on it. During setup, I was asked to set up both a root account and a user account which I did.

Now it seems that my user account cannot use the sudo command at all and I have to install everything using my root account. So I installed docker on my root user for example. Then I signed in to my user account but I cannot start the hello world container since it does not have sudo:

sudo docker run hello-world

So I am a little bit confused here. Do I have to treat my root account as my “regular” user now? Because running docker containers is my main usage. On my old raspberry pi, was I perhaps always using the root account already? Or did I screw up creating accounts perhaps? :smiley:

I want to remember that the setup said something that if I didnt set up a root account, I would be able to use sudo on the user account? Maybe that is how it was on rasbian? What is the preferred way?

1 Like

Debian might not come with the SUDO package /app installed, so You should be able to

su root

(Enter the root passphrase)

docker run hello world

(As root user)

Until you install the SUDO package, and use visudo to set up sudo users…

Aha ok
su root seems to work.

But if I do that, is there any difference between the root and user account in reality?

To me it feels dangerous to use the root account since its more difficult to know when sudo is actually required or not.

Edit

So I installed this:

apt-get -y install sudo

And now it seems to work as I expect it to.

Thanks, I had no idea sudo was needed to be installed. :smiley: Always learn something new…

2 Likes

I should have said “try it once as root”

My bad, but you have a good question about running stugfvas root, and I think it is good to keep to non-root / normalnuser as much as practicable, in day-to-day operations.

But, I am not an expert.

I like to use Ubuntu, because they change some default things, to compromise and make it easier, I.e. installing SUDO by default, and useradd-vs-adduser and some other things.

Raspian also does some streamlining to make it as appliance-like as they can, in a different way.

But, you basically got the right idea!
And welcome to learning, there us a lot more to discover…

4 Likes

This is intended. You should not ever use the root account for anything unless there is no feasible way to avoid it.

It’s all about isolation and sandboxing, under normal circumstances, your normal user is very restricted, this is a good thing. Sudo makes the system require authentication each time a privileged task is attempted, or at least every few minutes, that timer is changeable.
Meaning, if the normal user account becomes compromised, it’s likely to be far less severe than if the root account is compromised. Given that your password is not also compromised.

All programs (almost always) run with permissions equal to the user that ran them. The exception is anything running inside “sudo” runs as superuser. SuDo = Superuser Do.
Don’t run anything as root or with sudo unless you know you have to, and you trust said program with full access to your system.

Personally, I don’t like having sudo installed. My normal user is very locked down, and If I need to do anything with superuser permissions, I open a separate tty and login as root. People less lazy than myself actually remove sudo and disable the root account itself. This is quite secure, but makes it really complicated when eventually a privileged task needs to get done.

1 Like

Unfortunatly it seems difficult to run docker without sudo?

It is possible to add user to docker group but it is not recommended.

1 Like

I am unfamiliar with docker. I use hardware virtualization and SEV to do all my compartmentalization.
Is it possible to install docker locally to the normal user’s home folder? As opposed to a system-wide installation of Docker?

give user sudo
log in as root. then type:

usermod -aG sudo username
su - username

Tl:Dr, does the linked article show a way to add use visudo/whatever, to add the docker command to the user permission in the sudiers file?
so OP can just ‘sudo docker x’ without passphrase every time?

Not sure what you mean. Most answers talk about adding the user account to the docker group. But that seems just as bad?

“Docker containers need to be run by a superuser. You can add yourself to the docker group (e.g. by running sudo usermod -aG docker $USER), but this makes it easy for anyone with access to the $USER account to gain root access to the machine (e.g. by mounting a root volume in a privileged container).”

1 Like

Docker ends up creating containers, but many container images are built expecting to run as root.

People use the terms “rootless containers” or “docker running in rootless mode” to describe the setup where a range of UIDs - could include uid=0 gets mapped to a single non root uid.

Networking in rootless containers becomes a bit more interesting.


Is the sole purpose of running this machine, so that you can run containers, or do you want to use it as a machine where you login as a userish user and userish stuff like browse the internet and process email and what not?

If former, then it’s a server and you , the “human”, don’t need a “user level” account… just run docker as root.

2 Likes

The sole purpose of it is to act as a home assistent/pi hole machine that runs as docker containers. I guess you could call it a server of Some kind. :joy:

Since I ran it the same way on my pi surely it will work. But perhaps there is a more secure way to do it. :crazy_face:

1 Like

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.

1 Like

But how does it work on Raspbian? The default user there is called pi. That user can run docker containers without sudo and it can also use sudo with for example apt without typing the password. It seems almost like that user is root? Seems dangerous to do that in a standard installation but maybe it’s because they want it to be as simple as possible.

It’s just some fancy command aliases and passwordless sudo on certain commands. If you cat /etc/sudoers, or /etc/sudoers.d/pi, depending on where it is, you should be able to find what commands are allowed to run passwordless. Likely apt update and apt install is an exception.

Because raspberry pi os is meant for beginners, the devs made a few QoL things. Not best practices, because it’s meant for learning, meaning you can break it and remake / reinstall it, which is what it’s likely meant for.

I’m pretty sure if you run ps -ef | grep -i doc, you will see the user will be root for the docker service. You can run docker commands without sudo because you are probably in the docker group. Run id on your user to confirm it. Otherwise. it might be just a fancy alias yet again for sudo docker without having to type it. Use the alias command to list the aliases, env to list your environment variables and cat ~/.*rc ~/.*profile to verify that these options are there.

I can’t think of other places for these things, unless the docker and apt commands are actually scripts pointing to a renamed apt and docker that actually call upon sudo or something (which would be a roundabout way of doing it, aliases are generally what’s going on).

If you are running docker services for people you don’t trust, or if you expose your services to the internet, then I would change the defaults of the pi user. I would in fact create a new user and delete the pi user actually, because many automated bots will try to get into your system using the pi user.

But if you only run it in-house, then I would not worry about any of the qol improvements the rpi foundation made for the os, as long as you exercise caution when running things you download from the internet.

1 Like

Yes the difference is that root permissions allow you to alter the system configurations at critical levels.
User accounts by default are denied the major system configuration.
A workaround for this is to enter the root account, and modify the users permissions to administrator levels ( access, read/write) to the various sections.
If you are the only user thats fine, but if other users have access its a recipe for headaches.

Also web surfing with the root account opens the door for malware or Spyware.
User account web browsing has some firewall protection set up by the os.

Your root account is your toolbox to repair or modify if needed.

1 Like

On the flip side, before the UAC prompt, Windows was effectively run as “root”* as in, any user could run damaging software / change configs and mess up the machine…

*when talking about a home install/not business, default single user mode. And yeah, there are higher level system etc that one does not log in to, but it’s an equivalent. And yes, one could set up non-admin users on windows, to use day-to-day. how many home users do that, apart from kids accounts? UAC prompt more like the sudo command in my mind, as in it brings attention to potential damage, but still allows user to change stuff. no passphrase required by default, so easier user acceptance

2 Likes

Unfortunately for windows, most programs are made with admin accounts in mind. A few years ago, I tried setting up a non-admin user in my windows VM, but even steam games would require admin to launch, despite them not needing it, so I ended up elevating the privileges of the user anyway. Unless you run exclusively stuff from the windows store, you’d be hard pressed for stuff not to ask for admin rights. Windows software is junk.

3 Likes

that’s because you have misunderstood a fundamental of windows.
to run most stuff without needing admin privs just requires you to install somewhere other than c:
games that require admin rights only need them to have write access the program files/programs (x86).

so if you dont install everything in c: you can reduce admin requests dramatically.

1 Like

Wait, I see UAC as a Good thing!

It makes the user slow down, and maybe realise they didn’t necessarily mean to open a particular setting panel or some such, and possibly reduce problems for them