Skip to main content
new findings
Source Link
Alex
  • 201
  • 1
  • 6

I have a service that runs as root:

[Unit]
Description=my service
After=docker.service network-online.target
Requires=docker.service network-online.target

[Service]
TimeoutStartSec=0
RestartSec=60
Restart=always
User=root
Group=root

ExecStart=/usr/bin/myservice

[Install]
WantedBy=multi-user.target

I am getting some unexpected behavior with this:

The environment variable $HOME is set to /root when I look at the environment variable that myservice is getting which is causing it to fail.

However, as the root user I am getting:

$ echo $HOME
/home/root

This is in line with what I see in /etc/passwd

root:x:0:0:root:/home/root:/bin/sh

When I run myservice manually, it gets the correct $HOME env variable and it works.

I would like $HOME to be /home/root, not /root. So...

  1. Where is systemd getting /root from, and why is $HOME getting overwritten?
  2. How do I prevent systemd from doing this globally, aside from explicitly setting the $HOME environment variable in the service file? <-- upon reviewing the source code this doesn't seem like it'll make a difference

I have a service that runs as root:

[Unit]
Description=my service
After=docker.service network-online.target
Requires=docker.service network-online.target

[Service]
TimeoutStartSec=0
RestartSec=60
Restart=always
User=root
Group=root

ExecStart=/usr/bin/myservice

[Install]
WantedBy=multi-user.target

I am getting some unexpected behavior with this:

The environment variable $HOME is set to /root when I look at the environment variable that myservice is getting which is causing it to fail.

However, as the root user I am getting:

$ echo $HOME
/home/root

This is in line with what I see in /etc/passwd

root:x:0:0:root:/home/root:/bin/sh

When I run myservice manually, it gets the correct $HOME env variable and it works.

I would like $HOME to be /home/root, not /root. So...

  1. Where is systemd getting /root from, and why is $HOME getting overwritten?
  2. How do I prevent systemd from doing this globally, aside from explicitly setting the $HOME environment variable in the service file?

I have a service that runs as root:

[Unit]
Description=my service
After=docker.service network-online.target
Requires=docker.service network-online.target

[Service]
TimeoutStartSec=0
RestartSec=60
Restart=always
User=root
Group=root

ExecStart=/usr/bin/myservice

[Install]
WantedBy=multi-user.target

I am getting some unexpected behavior with this:

The environment variable $HOME is set to /root when I look at the environment variable that myservice is getting which is causing it to fail.

However, as the root user I am getting:

$ echo $HOME
/home/root

This is in line with what I see in /etc/passwd

root:x:0:0:root:/home/root:/bin/sh

When I run myservice manually, it gets the correct $HOME env variable and it works.

I would like $HOME to be /home/root, not /root. So...

  1. Where is systemd getting /root from, and why is $HOME getting overwritten?
  2. How do I prevent systemd from doing this globally, aside from explicitly setting the $HOME environment variable in the service file? <-- upon reviewing the source code this doesn't seem like it'll make a difference
Source Link
Alex
  • 201
  • 1
  • 6

systemd $HOME environment variable different from expected

I have a service that runs as root:

[Unit]
Description=my service
After=docker.service network-online.target
Requires=docker.service network-online.target

[Service]
TimeoutStartSec=0
RestartSec=60
Restart=always
User=root
Group=root

ExecStart=/usr/bin/myservice

[Install]
WantedBy=multi-user.target

I am getting some unexpected behavior with this:

The environment variable $HOME is set to /root when I look at the environment variable that myservice is getting which is causing it to fail.

However, as the root user I am getting:

$ echo $HOME
/home/root

This is in line with what I see in /etc/passwd

root:x:0:0:root:/home/root:/bin/sh

When I run myservice manually, it gets the correct $HOME env variable and it works.

I would like $HOME to be /home/root, not /root. So...

  1. Where is systemd getting /root from, and why is $HOME getting overwritten?
  2. How do I prevent systemd from doing this globally, aside from explicitly setting the $HOME environment variable in the service file?