Setting Environment Variables for a systemd Service

From NovaOrdis Knowledge Base
Revision as of 17:51, 16 February 2016 by Ovidiu (talk | contribs)
Jump to navigation Jump to search

Internal

Overview

This document describes the procedure of configuring a systemd service by injecting custom environment variables into its environment. The document assumes we're configuring a service named "myservice".

Create the Configuration Directory

Create a directory /etc/systemd/system/myservice.service.d, and inside that directory create a file whose name ends in .conf, and in this file you can add to or override any part of the unit shipped by the distribution.

For instance, in a file /etc/systemd/system/myservice.service.d/myenv.conf:

[Service] Environment="SECRET=pGNqduRFkB4K9C2vijOmUDa2kPtUhArN" Environment="ANOTHER_SECRET=JP8YLOc2bsNlrGuD6LVTq7L36obpjzxd" Also note that if the directory exists and is empty, your service will be disabled! If you don't intend to put something in the directory, ensure that it does not exist.