Create custom service file
Open a new service file under /etc/systemd/system.
sudo nano /etc/systemd/system/myapp.serviceCustom local units usually belong under /etc/systemd/system.
Author, edit, override, and harden systemd .service files with practical examples.
Override vendor unit settings safely.
sudo systemctl edit nginxCreates a drop-in under /etc/systemd/system/<unit>.d/override.conf.
[Service]
Environment=NODE_ENV=production
Environment=PORT=8080Drop-ins are the preferred way to add or change settings.
[Service]
ExecStart=
ExecStart=/usr/local/bin/myapp --config /etc/myapp/config.ymlWhen overriding ExecStart, the original must often be cleared first.
Print final unit and all overrides.
systemctl cat myapp.serviceHelps verify the effective configuration.
Set service identity and startup relationships.
[Service]
EnvironmentFile=/etc/myapp/myapp.envGood for keeping secrets and config out of ExecStart.
[Service]
User=myapp
Group=myappPreferred for least privilege.
Run service from a specific directory.
[Service]
WorkingDirectory=/opt/myappUseful for relative file access and app startup.
[Unit]
Wants=network-online.target
After=network-online.targetCommon for network-dependent apps.
Fail if the required unit is absent or stopped.
[Unit]
Requires=postgresql.service
After=postgresql.serviceStronger relationship than Wants.
Tune service behavior and isolation.
[Service]
Restart=on-failure
RestartSec=5Common production resilience setting.
[Service]
Type=oneshot
ExecStart=/usr/local/bin/cleanup.sh
RemainAfterExit=yesOften paired with timers.
[Service]
Type=notify
NotifyAccess=mainUsed by daemons with sd_notify support.
[Service]
NoNewPrivileges=yes
PrivateTmp=yes
ProtectSystem=strict
ProtectHome=yes
ReadWritePaths=/var/lib/myappA starting point for service hardening.
[Service]
CapabilityBoundingSet=CAP_NET_BIND_SERVICE
AmbientCapabilities=CAP_NET_BIND_SERVICEUseful when binding privileged ports without full root access.
Score a custom service for security posture.
systemd-analyze security myapp.serviceGood final review after adding sandbox settings.