0

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?

stackbiz
  • 267
  • 4
    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. – Artur Meinild Aug 19 '23 at 10:31
  • Because root has the highest privileges. – stackbiz Aug 19 '23 at 10:33
  • 5
    Maybe you don't understand the principle - but you should do it the other way around. – Artur Meinild Aug 19 '23 at 10:37
  • 1
    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. – Raffa Aug 19 '23 at 11:12
  • 1
    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. – Organic Marble Aug 19 '23 at 11:36

1 Answers1

4

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

Raffa
  • 32,237