systemd drop-in overrides (without editing vendor files)
Tabela de Conteúdo
One of the most common mistakes when tweaking a service is editing the unit file directly under:
/lib/systemd/system/
That works… until the next package update overwrites your changes.
The clean way is using drop-in overrides.
What is a drop-in override?
It is a file like:
/etc/systemd/system/<unit>.d/override.conf
systemd merges it on top of the original unit.
Example: add environment variables to nginx
Create the directory:
sudo mkdir -p /etc/systemd/system/nginx.service.d
Create the override:
sudo tee /etc/systemd/system/nginx.service.d/override.conf >/dev/null <<'EOF'
[Service]
Environment="APP_ENV=prod"
Environment="LOG_LEVEL=info"
EOF
Reload and restart:
sudo systemctl daemon-reload
sudo systemctl restart nginx
Check what systemd is using:
systemctl cat nginx.service
Example: change ExecStart (properly)
If you need to replace ExecStart, remember you must first clear it:
[Service]
ExecStart=
ExecStart=/usr/sbin/nginx -g "daemon off;"
Quick tip
To debug service errors, journalctl is your friend:
sudo journalctl -u nginx -b -n 200 --no-pager
And if you like small practical posts like this, here’s another one in: Remove multiple files.
Easy peasy! :)