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?
root
? This practice is generally discouraged, and you should instead login as a normal user, and usesudo
to execute commands asroot
. – Artur Meinild Aug 19 '23 at 10:31