Linux and servers · Intermediate · 14 min
Change a packaged service safely with systemd drop-in files
By Padding · Updated 18 Nov 2026
Ubuntu 26.04.1 LTS · systemd 259 · Nginx 1.28 (Ubuntu archive)
You will change how Nginx runs under systemd with drop-in files that survive package upgrades, replace a start command correctly, list every local override on the server and put a unit back to its packaged state.
On this page
When you install Nginx, PostgreSQL or any other service from a package, its unit file lands in /usr/lib/systemd/system/. That directory belongs to the package manager. Edit nginx.service there and the next apt upgrade that ships a new version of the file writes over your change, usually without telling you. The service then restarts with the packaged settings, and the problem shows up weeks later as a crash that no longer restarts itself or a limit you thought you had raised.
systemd has a better place for local changes: drop-in files. A drop-in is a small .conf file in a directory named after the unit, under /etc/systemd/system/. systemd reads the packaged unit first, then applies each drop-in on top, so you keep only your differences and the package keeps its file.
In this tutorial you will use the Nginx package from the Ubuntu archive to give Nginx a restart policy and a higher open-file limit, replace its start command the right way, list every override on the server, apply one drop-in to a group of units and then undo it all. Ubuntu 26.04 ships systemd 259.
Step 1 · Find the packaged unit file
Install Nginx if it is not there yet. If you installed it from nginx.org, as in the Nginx install tutorial, the steps are the same, but the lines inside the unit file differ, so copy commands from your own systemctl cat output rather than from this page.
sudo apt update
sudo apt install -y nginxAsk systemd which files make up the unit:
systemctl cat nginxThe first line of the output is a comment with the file's path, # /usr/lib/systemd/system/nginx.service. Below it you see the packaged settings, including Type=forking, an ExecStartPre= line that tests the configuration and an ExecStart= line that starts /usr/sbin/nginx. There is no Restart= line, so by default systemd does not restart Nginx if it crashes.
Confirm that a package owns that file:
dpkg -S nginx.serviceCheck it worked
dpkg -S prints a package name, something like nginx: /usr/lib/systemd/system/nginx.service. A file with an owning package is one you leave alone: any change you make to it is the package's to overwrite.
Step 2 · Add an override with systemctl edit
The usual way to make a drop-in is systemctl edit. It opens an editor on an empty override, with the current unit shown below as comments for reference:
sudo systemctl edit nginxType these lines in the space between the two marker comments at the top, then save and close the editor:
[Service]
Restart=on-failure
RestartSec=5s
LimitNOFILE=65535Restart=on-failurerestarts Nginx after a crash or a non-zero exit, but not aftersystemctl stop.RestartSec=5swaits five seconds between attempts, so a broken config does not spin the CPU.LimitNOFILE=65535raises the number of files and sockets each Nginx process may hold open. Every client connection uses one, and many distributions start services with a soft limit of 1024. Nginx'sworker_connectionssetting cannot usefully go above this number.
Every setting in a drop-in must sit under the same section header ([Service], [Unit], [Install]) it would use in a full unit file. A line with no section header is ignored with a warning.
systemctl edit saves the result as /etc/systemd/system/nginx.service.d/override.conf and reloads systemd's configuration for you. If you close the editor without adding anything, the empty file is discarded and nothing changes.
In a script or a configuration management tool, write the same file yourself. Do this instead of systemctl edit, not as well:
sudo mkdir -p /etc/systemd/system/nginx.service.d
sudo tee /etc/systemd/system/nginx.service.d/override.conf > /dev/null <<'CONF'
[Service]
Restart=on-failure
RestartSec=5s
LimitNOFILE=65535
CONFCheck it worked
Look at the file and ask systemd to parse the unit with its drop-ins:
cat /etc/systemd/system/nginx.service.d/override.confsudo systemd-analyze verify /usr/lib/systemd/system/nginx.serviceThe cat shows your four lines. systemd-analyze verify prints nothing when the unit and its drop-ins are valid. A misspelt option, such as Restart=onfailure, is reported with the drop-in's file name and line number.
Step 3 · Apply the change and confirm it took effect
If you wrote the file with tee, tell systemd to reread its unit files. Then restart Nginx so the running processes pick up the new limit:
sudo systemctl daemon-reload
sudo systemctl restart nginxCheck it worked
Read the values systemd is now using:
systemctl show nginx -p Restart -p RestartUSec -p LimitNOFILEThe output is three lines: Restart=on-failure, RestartUSec=5s and LimitNOFILE=65535. Then check the running master process itself:
sudo grep 'Max open files' /proc/$(systemctl show -p MainPID --value nginx)/limitsBoth numbers on that line read 65535. To prove the restart policy, kill the master process and look again after the delay:
sudo kill -9 $(systemctl show -p MainPID --value nginx)
sleep 7
systemctl is-active nginxIt prints active. Without the drop-in it would print failed, and your site would stay down until someone noticed.
Step 4 · Replace a start command with an empty assignment
Most settings take one value, and a drop-in's value replaces the packaged one. Some settings are lists, and a new line adds to the list instead. ExecStart=, ExecStartPre=, Environment= and ExecReload= all behave this way. Suppose you want Nginx to start with an explicit -c /etc/nginx/nginx.conf (in practice you would point it at a different file). The obvious drop-in is wrong:
[Service]
ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf -g 'daemon on; master_process on;'That gives the unit two ExecStart= commands. Only Type=oneshot services may have more than one, so systemd refuses to load Nginx at all. See it for yourself, with a separate drop-in file so your override from step 2 stays as it is:
sudo tee /etc/systemd/system/nginx.service.d/20-start-command.conf > /dev/null <<'CONF'
[Service]
ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf -g 'daemon on; master_process on;'
CONFsudo systemd-analyze verify /usr/lib/systemd/system/nginx.serviceThe output says the service has more than one ExecStart= setting and that systemd is refusing it. Do not reload or restart while the unit is in this state.
The fix is an empty assignment first. ExecStart= with nothing after it clears every earlier value, and the next line sets the only one. Change the matching test command in ExecStartPre= the same way, so the configuration systemd checks before starting is the one it starts:
sudo tee /etc/systemd/system/nginx.service.d/20-start-command.conf > /dev/null <<'CONF'
[Service]
ExecStartPre=
ExecStartPre=/usr/sbin/nginx -t -q -c /etc/nginx/nginx.conf -g 'daemon on; master_process on;'
ExecStart=
ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf -g 'daemon on; master_process on;'
CONFIf your new command changes how Nginx is reloaded or stopped, reset ExecReload= and ExecStop= the same way.
Check it worked
sudo systemd-analyze verify /usr/lib/systemd/system/nginx.serviceNo output this time. Apply it with sudo systemctl daemon-reload and sudo systemctl restart nginx, then systemctl show nginx -p ExecStart shows one command containing -c /etc/nginx/nginx.conf.
Step 5 · See every local change on the server
systemctl cat shows the merged view of one unit, in the order systemd applies it:
systemctl cat nginxYou see the packaged file, then # /etc/systemd/system/nginx.service.d/20-start-command.conf, then # /etc/systemd/system/nginx.service.d/override.conf. Drop-ins are applied in alphabetical order of file name, so name them with number prefixes (10-, 20-) when the order matters. override.conf sorts after any file starting with a digit.
On a server someone else set up, you do not know which units have been changed. systemd-delta scans every unit directory and lists the local changes:
systemd-delta --no-pager --type=extended,overridden,masked[EXTENDED]lines are drop-ins: a unit with a change layered on top.[OVERRIDDEN]lines are whole unit files in/etcthat hide the packaged one, followed by a diff of the two.[MASKED]lines are units linked to/dev/nullso they cannot start.
Check it worked
Two [EXTENDED] lines name /usr/lib/systemd/system/nginx.service and point to your two drop-in files. Run the same command after every package upgrade or before you hand a server over, so nobody is surprised by a change they cannot see in /usr/lib.
When to use systemctl edit --full
sudo systemctl edit --full nginx copies the whole unit to /etc/systemd/system/nginx.service and edits the copy. systemd then ignores the packaged file completely, and systemd-delta lists it as [OVERRIDDEN]. That is the right tool only when you need to remove settings that cannot be reset with an empty value. The cost is that fixes the package makes to its unit file no longer reach you, so check the diff from systemd-delta after each upgrade. For changing a few settings, a drop-in is always the safer choice.
Step 6 · Apply one drop-in to many units
Drop-in directories also work on groups of units. For a unit with dashes in its name, systemd also looks in directories named after each prefix up to a dash. A drop-in in /etc/systemd/system/demo-.service.d/ applies to demo-web.service, demo-worker.service and every other service whose name starts with demo-.
Create two small throwaway units to see it work:
sudo tee /etc/systemd/system/demo-web.service > /dev/null <<'UNIT'
[Unit]
Description=Demo web process
[Service]
ExecStart=/usr/bin/sleep infinity
UNIT
sudo tee /etc/systemd/system/demo-worker.service > /dev/null <<'UNIT'
[Unit]
Description=Demo background worker
[Service]
ExecStart=/usr/bin/sleep infinity
UNITNow give both of them the same restart policy and environment with one file:
sudo mkdir -p /etc/systemd/system/demo-.service.d
sudo tee /etc/systemd/system/demo-.service.d/10-common.conf > /dev/null <<'CONF'
[Service]
Restart=on-failure
Environment=APP_ENV=production
CONFsudo systemd-analyze verify /etc/systemd/system/demo-web.service /etc/systemd/system/demo-worker.service
sudo systemctl daemon-reloadA plain service.d/ directory (no prefix at all) is a top-level drop-in: it applies to every service on the machine, including SSH. Use it only for settings you have tested against all of them, because one bad line there can stop the service you need to log in and fix it.
Check it worked
systemctl cat demo-worker
systemctl show demo-worker -p Environment -p Restartsystemctl cat lists # /etc/systemd/system/demo-.service.d/10-common.conf under the unit, although the file name does not mention demo-worker. systemctl show prints Environment=APP_ENV=production and Restart=on-failure.
Step 7 · Put everything back
systemctl revert removes a unit's drop-in directory and any full copy in /etc that hides the packaged file, and unmasks the unit. The packaged unit stays. This deletes both of your Nginx drop-ins, so copy any you want to keep first:
sudo systemctl revert nginxrevert does not remove units that have no packaged version, or the shared demo- prefix directory, so delete the demo files by hand. These paths are the ones you created above:
sudo rm -r /etc/systemd/system/demo-.service.d
sudo rm /etc/systemd/system/demo-web.service /etc/systemd/system/demo-worker.servicesudo systemctl daemon-reload
sudo systemctl restart nginxCheck it worked
systemd-delta --no-pager --type=extended,overridden | grep -E 'nginx|demo-' || echo "no local changes to nginx or demo units"It prints no local changes to nginx or demo units, and systemctl show nginx -p Restart is back to Restart=no. If you want to keep the restart policy and file limit, run sudo systemctl edit nginx again and add only the override from step 2.
Next steps
- Write a unit for your own app, where you own the file in
/etc/systemd/system/and drop-ins are optional: /tutorials/run-an-app-as-a-systemd-service. - Use a drop-in to lock a packaged service down with options such as
ProtectSystem=andNoNewPrivileges=: /tutorials/sandbox-a-systemd-service-with-security-options. - When a restart policy kicks in, find out why the service failed: /tutorials/search-the-systemd-journal-with-journalctl.
- Set up Nginx from the nginx.org repository instead of the Ubuntu archive: /tutorials/install-nginx-on-ubuntu-26-04.
Related tutorials
- Write safer Bash scripts with strict mode and trapsYou will build a small Bash release script that stops on the first error, says which line failed, survives file names with spaces and always removes its temporary files.
- Write least-privilege sudo rules with visudoYou will replace blanket sudo access with checked rule files in /etc/sudoers.d that let two operators restart one service and edit one file, give an automation account a password-free command list, and audit what every account can run.
- Upgrade a server from Ubuntu 24.04 LTS to 26.04 LTSYou will take a backed-up Ubuntu 24.04 server through do-release-upgrade to 26.04 LTS over SSH with a fallback login, then confirm services, repositories and language runtimes work on the new release.
Need this done for you?
Padding does this work for growing companies, from the first diagnosis to a system that keeps working.
Get new tutorials