Score:0

How to execute command as "non_root" user in Ubuntu

bw flag

When logging into the Ubuntu system, the login user is "root".

And then, I want to execute some bash script on behalf of the "non_root":

root@test_pc:~# echo $USER
root
root@test_pc:~# sudo -E -u non_root -g non_root -H /bin/bash -c "echo $USER"
root
root@test_pc:~# 

But the output is still "root", in other words, the command is still execute under "root" user instead of "non_root".

Here is the expected output:

root@test_pc:~# sudo -E -u non_root -g non_root -H /bin/bash -c "echo $USER"
non_root
root@test_pc:~#

Here is the real operation:

sudo -E -u non_root -g non_root -H /bin/bash -c "systemctl --user disable pulseaudio.service"

But got the following error:

Failed to connect to bus: Operation not permitted (consider using --machine=<user>@.host --user to connect to bus of other user)

How to execute any bash command as "non_root" when logged in with "root" user?

Artur Meinild avatar
vn flag
The big question is: Why are you logging in as `root`? This practice is generally discouraged, and you should instead login as a normal user, and use `sudo` to execute commands as `root`.
stackbiz avatar
bw flag
Because root has the highest privileges.
Artur Meinild avatar
vn flag
Maybe you don't understand the principle - but you should do it the other way around.
Raffa avatar
jp flag
Double quotes allow for parameter expansion in the current shell on the current command line before the command gets executed … To expand $USER in the sub-shell instead use single quotes around the command string instead … And pulseaudio is a user service that requires user login to run.
Organic Marble avatar
us flag
Short answer: don't log in as root. You are making it difficult for yourself for no reason. Instead, examine why you think you need to do it, and ask about the correct way to do whatever that is.
Score:4
jp flag

Needless to say that what you're doing i.e. logging in as the user root is discouraged and therefore the root user is disabled by default on Ubuntu.

That said, ...

Double quotes around the Bash command string allow for parameter expansion in the current shell on the current command line before the command gets executed … see it in action:

root@Lenovo:~# whoami
root
root@Lenovo:~# set -x
root@Lenovo:~# sudo -E -u ubuntu -g ubuntu -H /bin/bash -c "echo $USER"
+ sudo -E -u ubuntu -g ubuntu -H /bin/bash -c 'echo root'
root

While your command is actually executed as the intended user:

root@Lenovo:~# sudo -E -u ubuntu -g ubuntu -H /bin/bash -c "whoami"
ubuntu

To expand $USER in the sub-shell created to run that Bash command string instead use single quotes around the command string instead:

root@Lenovo:~# sudo -E -u ubuntu -g ubuntu -H /bin/bash -c 'echo $USER'
ubuntu

Notice as well that pulseaudio is a user service that requires user login to run ... See for example https://askubuntu.com/a/1465201 and Dbus is also part of the user’s user-runtime environment and requires a user login as well ... See for example https://askubuntu.com/a/1470118

I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.