Sample Header Ad - 728x90

Setting `xhost` from systemd service file

0 votes
1 answer
1627 views
I would like to run DISPLAY=:0 xhost + at every system boot. (I'm aware of the security implications of xhost +.) This command, executed locally, works just fine:
username@hostname:`$ DISPLAY=:0 xhost +
access control disabled, clients can connect from any host
But when I would like to run it through the following systemd service file, it fails as noted below.
username@hostname:/etc/systemd/system$ cat set-xhost.service 
[Unit]
Description=Set access control for X server

[Service]
Type=oneshot
Environment=DISPLAY=:0
ExecStart=/usr/bin/xhost +

[Install]
WantedBy=multi-user.target
username@hostname:~$ sudo systemctl start set-xhost.service
Job for set-xhost.service failed because the control process exited with error code. See "systemctl status set-xhost.service" and "journalctl -xe" for details.

username@hostname:~$ sudo journalctl -u set-xhost.service
Jun 09 12:09:28 hostname systemd: Starting Set access control for X server...
Jun 09 12:09:28 hostname xhost: No protocol specified
Jun 09 12:09:28 hostname xhost: /usr/bin/xhost:  unable to open display ":0"
Jun 09 12:09:28 hostname systemd: set-xhost.service: Main process exited, code=exited, status=1/FAILURE
Jun 09 12:09:28 hostname systemd: Failed to start Set access control for X server.
Jun 09 12:09:28 hostname systemd: set-xhost.service: Unit entered failed state.
Jun 09 12:09:28 hostname systemd: set-xhost.service: Failed with result 'exit-code'.
What is the reason behind this? From all I know, this should work. That it doesn't work indicates I'm missing some important knowledge in this area.
Asked by Multisync (514 rep)
Jun 9, 2020, 10:16 AM
Last activity: Jun 9, 2020, 12:53 PM