Static network IP with systemd

One of the few real hurdles I’ve found with systemd is setting up a static network configuration. This is a solution that cuts the middleman, be it ifupdown, netcfg, NetworkManager or wicd and uses systemd to do it. This comes originally from the Archlinux Wiki on systemd services, but it seems to be deleted and reinstated at the whim of some God entitled pedant so you may not find it there tomorrow.

Make sure that iputils2 are installed. Create a service file network-static.service and a confguration file network-static as shown below and install in the suggested places. Then enable the service with systemctl:

;; /etc/systemd/system/network-static.service
[Unit]
Description=Static Network Service
Wants=network.target
Before=network.target

[Service]
Type=oneshot
RemainAfterExit=yes
EnvironmentFile=/etc/conf.d/network-static
ExecStart=/usr/sbin/ip link set dev ${IF} up
ExecStart=/usr/sbin/ip address add ${ADDR}/${NM} broadcast ${BC} dev ${IF}
ExecStart=/usr/sbin/ip route add default via ${GW} dev ${IF}
ExecStop=/usr/sbin/ip addr flush dev ${IF}
ExecStop=/usr/sbin/ip link set dev ${IF} down

[Install]
WantedBy=multi-user.target
;; /etc/conf.d/network-static
IF=eth0
ADDR=AAA.BBB.CCC.DDD
NM=24
BC=AAA.BBB.CCC.255
GW=AAA.BBB.CCC.EEE

And that’s all there is to it.

Advertisements