2

When I try to run runuser -l mruser -c "systemctl --user status myservice.service" as root, I get the following error:

Failed to connect to bus: No such file or directory

I do not get this error when doing the same on a RHEL 8.4 system. I am able to run systemd --user commands as mruser using runuser with no problems.

Troubleshooting Performed:

  • tried running systemctl --user status myservice.service as mruser command runs with no errors.
  • I already checked loginctl for mruser, Linger=yes
  • Checked to make sure there was a systemd --user process running with ps
  • Logged into the user's account directly with separate ssh session, Then tried the same command as root in the original session, still same error.
  • mruser already has export XDG_RUNTIME_DIR=/run/user/$(id -u) in ~/.bashrc
  • Tried rebooting the system, still nothing.
  • Already read this post, was not very helpful.

On the RHEL system systemd is v239, On Ubuntu 20.04 its v245. Am I missing something? Why can I not run the systemd --user commands as root on Ubuntu?

UPDATE: I think the issue has to do with runuser not seeing exported variables. which means $XDG_RUNTIME_DIR is not being recognized or used. Anyone know why runuser is not seeing exported variables defined in mruser's ~/.bashrc file? I checked the RHEL instance, and root IS able to see and use the exported variables.

Dave
  • 242

2 Answers2

0

I found the problem. It was the ~/.bashrc file for myuser. On Ubuntu the default .bashrc file has the following line:

case $- in
      *i*);;
        *) return;;
esac

This is not in the RHEL default .bashrc file. This case statement is looking for the parameter bash -i to be used. Without it the shell is not interactive and .bashrc is not used, ergo, The defined $XDG_RUNTIME_DIR is never seen, so systemd --user command fails.

I have not figured out a way to get runuser force this case statement to recognize sending a -i.

Work around: I removed these lines with this command:

sed -i '/case $- in/,/esac/ s/^/# /' /home/mruser/.bashrc

After doing so, running runuser as root on Ubuntu works as expected. If anyone knows of a way to get this case statement to recognize -i with runuser please share, I will update this solution. But for now this is what I am doing.

Dave
  • 242
0

I had the same error (Failed to connect to bus: No such file or directory) when running the following command on Ubuntu 20.04:

systemctl --user mask tracker-store.service tracker-miner-fs.service tracker-miner-rss.service tracker-extract.service tracker-miner-apps.service tracker-writeback.service

The problem was because of the alias:

alias systemctl='sudo systemctl'

It substituted the systemctl with sudo systemctl and sudo caused the error.