-
Notifications
You must be signed in to change notification settings - Fork 1
/
systemd-hosts.d.service
37 lines (32 loc) · 1.07 KB
/
systemd-hosts.d.service
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
[Unit]
Description=Keep your hosts records in logically separated files
After=local-fs.target
[Service]
Type=oneshot
Environment="HOSTS_FILE=/etc/hosts"
Environment="HOSTS_DIRECTORY=/etc/hosts.d"
Environment="HOSTS_HEADER=### Generated by systemd-hosts.d ###"
# Ensure hosts directory exist
ExecCondition=/bin/sh -c 'test -e "$HOSTS_DIRECTORY"'
# Ensure preexisting hosts file isn't going to be overwritten
ExecCondition=/bin/sh -c '\
test ! -f "$HOSTS_FILE" || head -n1 -- "$HOSTS_FILE" | grep -q "$HOSTS_HEADER"\
'
# Assemble hosts file
ExecStart=/bin/sh -c '(\
echo "$HOSTS_HEADER";\
echo;\
for conf_file in "$HOSTS_DIRECTORY"/*.conf; do\
test -e "$conf_file" || continue;\
echo "## From $conf_file ##";\
while read -r line; do\
printf "%%s\\n" "$line";\
done <"$conf_file";\
echo;\
done;\
) >"$HOSTS_FILE"'
# Additionally, unset write permissions while at rest
ExecStartPre=-/usr/bin/env chmod ug+w -- "$HOSTS_FILE"
ExecStartPost=-/usr/bin/env chmod ugo=r -- "$HOSTS_FILE"
[Install]
RequiredBy=network-pre.target