Using environment variables in systemd units

Environment directive

systemd has an Environment directive which sets environment variables for executed processes. It takes a space-separated list of variable assignments. This option may be specified more than once in which case all listed variables will be set. If the same variable is set twice, the later setting will override the earlier setting. If the empty string is assigned to this option, the list of environment variables is reset, all prior assignments have no effect. Environments directives are used in built-in Container Linux systemd units, for example in etcd2 and flannel.

With the example below, you can configure your etcd2 daemon to use encryption. Just create /etc/systemd/system/etcd2.service.d/30-certificates.conf drop-in for etcd2.service:

# Client Env Vars
# Peer Env Vars

Then run sudo systemctl daemon-reload and sudo systemctl restart etcd2.service to apply new environments to etcd2 daemon. You can read more about etcd2 certificates here.

EnvironmentFile directive

EnvironmentFile similar to Environment directive but reads the environment variables from a text file. The text file should contain new-line-separated variable assignments.

For example, in Container Linux, the coreos-metadata.service service creates /run/metadata/coreos. This environment file can be included by other services in order to inject dynamic configuration. Here's an example of the environment file when run on DigitalOcean (the IP addresses have been removed):


This environment file can then be sourced and its variables used. Here is an example drop-in for etcd-member.service which starts coreos-metadata.service and then uses the generated results:


ExecStart=/usr/bin/etcd2 \
  --advertise-client-urls=http://${COREOS_DIGITALOCEAN_IPV4_PUBLIC_0}:2379 \
  --initial-advertise-peer-urls=http://${COREOS_DIGITALOCEAN_IPV4_PRIVATE_0}:2380 \
  --listen-client-urls= \
  --listen-peer-urls=http://${COREOS_DIGITALOCEAN_IPV4_PRIVATE_0}:2380 \

Other examples

Use host IP addresses and EnvironmentFile

You can also write your host IP addresses into /etc/network-environment file using this utility. Then you can run your Docker containers following way:

Description=Nginx service
# Get network environmental variables
ExecStartPre=-/usr/bin/docker kill nginx
ExecStartPre=-/usr/bin/docker rm nginx
ExecStartPre=/usr/bin/docker pull nginx
ExecStartPre=/usr/bin/etcdctl set /services/nginx '{"host": "%H", "ipv4_addr": ${DEFAULT_IPV4}, "port": 80}'
ExecStart=/usr/bin/docker run --rm --name nginx -p ${DEFAULT_IPV4}:80:80 nginx
ExecStop=/usr/bin/docker stop nginx
ExecStopPost=/usr/bin/etcdctl rm /services/nginx

This unit file will run nginx Docker container and bind it to specific IP address and port.

System wide environment variables

You can define system wide environment variables using cloud-config as explained below:


  - path: "/etc/systemd/system.conf.d/10-default-env.conf"
    content: |
  - path: "/etc/profile.env"
    content: |
      export HTTP_PROXY=
CoreOS cloud-configs can validated using the online validator.

or using Ignition:

  "ignition": { "version": "2.0.0" },
  "files": [
      "filesystem": "root",
      "path": "/etc/systemd/system.conf.d/10-default-env.conf",
      "contents": { "source": "data:,[Manager]\nDefaultEnvironment=HTTP_PROXY=" }
      "filesystem": "root",
      "path": "/etc/profile.env",
      "contents": { "source": "data:,export%20HTTP_PROXY=" }


  • /etc/systemd/system.conf.d/10-default-env.conf config file will set default environment variables for all systemd units.
  • /etc/profile.env will set environment variables for all users logged in Container Linux.

etcd2.service unit advanced example

A complete example of combining environment variables and systemd drop-ins to reconfigure an existing machine running etcd.

More systemd examples

For more systemd examples, check out these documents:

Customizing Docker Customizing the SSH Daemon Using systemd Drop-In Units etcd Cluster Runtime Reconfiguration on Container Linux

More Information

systemd.exec Docs systemd.service Docs systemd.unit Docs