0

I'm running Ubuntu 18.04.2 LTS, and have a fresh install of transmission-daemon. It runs fine as a Systemd service. But when I try to run it standalone (with the service stopped), I see:

~ ⌘ sudo -u debian-transmission transmission-daemon -f --log-error --log-info --log-debug
[2019-05-14 11:53:19.167] Couldn't read "/home/jason/.config/user-dirs.dirs": Permission denied
[2019-05-14 11:53:20.419] Couldn't create "/home/jason/.config/transmission-daemon": Permission denied (file-posix.c:189)
[2019-05-14 11:53:20.419] Couldn't create "/home/jason/.config/transmission-daemon": Permission denied (file-posix.c:189)
[2019-05-14 11:53:20.419] Couldn't create "/home/jason/.config/transmission-daemon": Permission denied (file-posix.c:189)
[2019-05-14 11:53:20.419] Transmission 2.92 (14714) started (session.c:740)
[2019-05-14 11:53:20.419] Couldn't read "/home/jason/.config/transmission-daemon/stats.json": No such file or directory (utils.c:238)
[2019-05-14 11:53:20.419] Couldn't read "/home/jason/.config/transmission-daemon/stats.benc": No such file or directory (utils.c:238)

Note that ~ does resolve to /home/jason as the user jason (me). But I'm running it as the user debian-transmission. So why is Transmission trying to access "my" user directories? How can I stop it doing that?

detly
  • 3,382
  • 1
    If you want sudo to change $HOME to that of the target user, you need to add -H I think? – steeldriver May 14 '19 at 04:43
  • @steeldriver aaa(character limit satisfied here)aaaaaaaaaaargh – detly May 14 '19 at 10:50
  • @steeldriver That was the problem, I wonder if the default policy changed recently? Anyway, post it as an answer, it really was that simple. (Not sure what happens when Systemd runs it, but not really important.) – detly May 14 '19 at 10:50

1 Answers1

1

At least in recent versions of Ubuntu, sudo is configured to preserve the invoking user's HOME environment variable1. You can verify this either by executing something like

$ sudo -u testuser sh -c 'echo $HOME'
/home/steeldriver

or by running sudo sudo -V and looking at the Environment variables to preserve section.

In order to set HOME to that of the target user, you can add -H (--set-home) option:

 -H, --set-home
             Request that the security policy set the HOME environment
             variable to the home directory specified by the target user's
             password database entry.  Depending on the policy, this may
             be the default behavior.

Ex.

$ sudo -Hu testuser sh -c 'echo $HOME'
/home/testuser

  1. See also Why should users never use normal sudo to start graphical applications?
steeldriver
  • 136,215
  • 21
  • 243
  • 336