rc.local

On this page 7

rc.local is a shell script executed once at the end of the boot process that lets an administrator run arbitrary commands without writing a full service definition. In Linux system initialization, it originated in the BSD and SysVinit tradition as the last script called after all runlevel scripts finished, and it survives today mainly as a compatibility mechanism under systemd.

The file usually lives at /etc/rc.local or, on older Red Hat systems, at /etc/rc.d/rc.local. It is simple to use, which explains its long popularity, but it offers no dependency handling, no restart logic and limited logging, so modern practice treats it as a last resort rather than a default tool.

How rc.local runs during boot

rc.local runs as root, once per boot, after the main services have started. Under SysVinit, the init process walked through the scripts of the target runlevel in /etc/rc2.d or a similar directory and then called rc.local as the final step before login prompts appeared. Because it ran last, administrators could assume that networking and file systems were already available.

Under systemd, the script is handled by a generator and a compatibility unit called rc-local.service. The generator systemd-rc-local-generator checks whether /etc/rc.local exists and is executable; only then does it pull the unit into the boot transaction. The unit is ordered after network.target, which does not guarantee that the network is fully configured, a detail that causes many startup failures.

The following table summarizes how the main init systems treat the script.

Init systemHow rc.local is invokedTypical location
SysVinitCalled by the last runlevel script/etc/rc.d/rc.local or /etc/rc.local
systemdrc-local.service via a generator/etc/rc.local, must be executable
OpenRCReplaced by local.d scripts/etc/local.d/*.start
Upstartrc-sysinit job ran the legacy chain/etc/rc.local
BSD initCalled from /etc/rc after base services/etc/rc.local
Handling of rc.local by init system

Structure and requirements of the script

A working rc.local needs a valid interpreter line, execute permission and a zero exit status. The first line is normally #!/bin/sh -e, where the -e option makes the shell stop at the first failing command. The script traditionally ends with exit 0, because a non-zero exit marks the systemd unit as failed.

Many current distributions, including Debian and Ubuntu, no longer ship the file at all, so an administrator must create it and set permissions with chmod +x /etc/rc.local. The table below lists the conditions that decide whether the script actually runs.

RequirementWhy it mattersSymptom if missing
Shebang lineDefines the interpreterExec format error in the journal
Execute permissionThe generator ignores non-executable filesUnit never starts, no error shown
Exit status 0systemd records successrc-local.service shown as failed
Absolute pathsPATH is minimal at bootCommand not found
Background long jobsAvoids blocking the unit timeoutBoot delay or killed process
Conditions for a working rc.local

Long-running programs started from rc.local should be sent to the background with an ampersand or, better, moved into a proper service. Otherwise the unit waits for them and systemd may terminate the process when the start timeout expires, which by default is about 90 seconds.

rc.local compared with modern alternatives

The modern replacement for rc.local is a dedicated systemd unit, usually a Type=oneshot service with explicit ordering. Such a unit declares what it needs, for example After=network-online.target together with Wants=network-online.target, and its output goes to the journal where journalctl -u can show it.

Other mechanisms cover narrower needs. A cron @reboot entry runs a command per user after the cron daemon starts, a systemd timer can delay execution, and udev rules react to devices appearing. The comparison helps pick the right tool.

MechanismStrengthsWeaknesses
rc.localTrivial to write, familiarNo dependencies, weak logging
systemd oneshot unitOrdering, logging, sandboxingRequires unit file syntax
cron @rebootWorks for unprivileged usersRuns early, no ordering control
systemd timerDelayed or repeated runsTwo files to maintain
udev ruleTriggered by hardware eventsNot for long-running tasks
Ways to run commands at startup

When rc.local is still a reasonable choice

rc.local remains acceptable for small, idempotent, quick commands on systems an administrator fully controls. Typical examples are writing a value into /sys, loading a kernel tunable that has no sysctl equivalent, or setting a CPU frequency governor on a test machine.

It is a poor choice for anything that must be reliable, observable or portable. The decision table below reflects common operational practice.

SituationRecommended methodReason
One-line tweak on a lab hostrc.localFast and good enough
Daemon that must restart on failuresystemd serviceRestart policies built in
Task needing a configured networkUnit with network-online.targetExplicit dependency
Kernel parameter at bootsysctl.d or kernel command lineNative and documented
Fleet managed by configuration toolsPackaged unit fileReproducible deployment
Choosing a startup method

Common mistakes and failure modes

Most rc.local problems come from assumptions about boot timing and environment. Because the script inherits a minimal environment, commands that work in an interactive shell often fail silently at boot.

  • Missing execute bit: on systemd the script is simply ignored, and nothing obvious reports it.
  • Network not ready: network.target only means networking setup has started, so mounts or downloads fail intermittently.
  • Blocking commands: a foreground loop delays the unit and can be killed at the timeout.
  • Assuming user environment: variables from login profiles are absent, so relative paths break.
  • No logging: output is lost unless redirected, so add logger calls or check journalctl -u rc-local.

A further risk is security: because the script runs as root, any writable file it calls becomes a privilege escalation path. Permissions on rc.local and everything it invokes should be restricted to root.

A non-technical view

Think of rc.local as a sticky note left for the computer that says: once you have finished starting up, please also do these few things. The computer reads the note every time it boots and follows the instructions in order.

The note is easy to write, which is why people liked it. But it cannot say what to do if something goes wrong, and it does not keep a good record of what happened. Modern Linux systems prefer proper service descriptions, which are like detailed work orders instead of sticky notes.

A concrete example

A system administrator runs a small Ubuntu server used as a home lab router. After each reboot, the server needs IP forwarding enabled on one interface and a disk set to spin down after 20 minutes of inactivity. Writing two systemd units for this feels excessive, so the administrator creates /etc/rc.local with a shebang, the two commands using absolute paths and a final exit 0, then runs chmod +x.

On the next boot, systemctl status rc-local shows the unit as active and exited, and the settings are in place. Later, when the administrator adds a command that mounts a network share, it fails on some boots because the network is not yet configured. The fix is to move that mount into a dedicated unit ordered after network-online.target, while the two simple tweaks stay in rc.local.