Creating an rc.local
file involves a few straightforward steps to enable a legacy service that allows you to run custom commands at system boot-up on Linux distributions primarily using systemd
.
rc.local
is a script executed once at the end of the traditional boot sequence, offering a simple way to automate tasks that need to run before users log in. While systemd
is the modern init system, rc.local
functionality can still be enabled for specific use cases.
How to Create and Enable RC Local
Here's a step-by-step guide to create and enable rc.local
on your system:
Step 1: Check the Status of the rc-local
Service
Before creating the file, you can check if the rc-local
service unit is already present and its current status. This helps in understanding if the system is configured to handle rc.local
scripts.
sudo systemctl status rc-local
If the service is not found or is inactive, you'll proceed to create the necessary files and enable it.
Step 2: Create the rc.local
Script File
First, you need to create the rc.local
script itself in the /etc/
directory. This file will contain all the commands you want to execute at boot.
-
Open a text editor with root privileges to create the file. Using
nano
is a common choice for its simplicity:sudo nano /etc/rc.local
-
Add the necessary content to the file. A basic
rc.local
script should always start with a shebang line to specify the interpreter (#!/bin/bash
) and end withexit 0
to indicate successful execution.
Here's an example of what to put in your/etc/rc.local
file:#!/bin/bash # Add your custom commands here that you want to execute at boot. # For example, to print a message to the system log: # echo "rc.local script executed!" >> /var/log/syslog # Or to start a specific service/application: # /opt/my_application/start.sh # Make sure to include the full path to any commands or scripts you run. exit 0
#!/bin/bash
: This line ensures the script is executed bybash
.exit 0
: This line is crucial. It tells the system that the script executed successfully. Without it, therc-local
service might report an error.
-
Save and close the file. In
nano
, pressCtrl+O
to write out, thenEnter
to confirm the filename, andCtrl+X
to exit.
Step 3: Make the rc.local
Script Executable
The system needs permission to execute the script. You must grant executable permissions to the /etc/rc.local
file:
sudo chmod +x /etc/rc.local
This command makes the script executable for all users, which is necessary for the system to run it.
Step 4: Enable the rc-local
Service on System Boot
For systemd
to recognize and run your rc.local
script at every boot, you need to enable the rc-local
service. This will ensure the service starts automatically.
sudo systemctl enable rc-local
If rc-local.service
unit file doesn't exist, systemd
might automatically create a symlink to /etc/rc.local
or you might need to manually create a systemd
service unit to wrap the rc.local
functionality if your distribution doesn't provide one by default. Modern distributions often include a default rc-local.service
unit that simply executes /etc/rc.local
if it exists and is executable.
You can then start the service immediately without rebooting to test it:
sudo systemctl start rc-local
Verify its status again:
sudo systemctl status rc-local
It should now show as active (exited)
or active (running)
depending on your distribution's setup and if your script has long-running processes.
Understanding the rc-local.service
Unit
On systemd
based systems, the execution of /etc/rc.local
is managed by a systemd
service unit, typically named rc-local.service
. This service unit ensures that the rc.local
script is run at the appropriate stage during boot. If your distribution does not have this unit file by default, you might need to create one (e.g., /etc/systemd/system/rc-local.service
). However, most major distributions like Ubuntu and Debian provide it, often as a symlink to /lib/systemd/system/rc-local.service
or similar.
A typical rc-local.service
unit file looks like this:
[Unit]
Description=/etc/rc.local compatibility
ConditionPathExists=/etc/rc.local
[Service]
Type=forking
ExecStart=/etc/rc.local start
TimeoutSec=0
StandardOutput=tty
RemainAfterExit=yes
[Install]
WantedBy=multi-user.target
This unit ensures that if /etc/rc.local
exists, it will be executed before multi-user.target
is reached (meaning, before most services are fully up and running).
Best Practices and Considerations
- Full Paths: Always use absolute (full) paths for commands and files within your
rc.local
script (e.g.,/usr/bin/python
instead ofpython
,/var/www/html/myscript.sh
instead ofmyscript.sh
). The environmentrc.local
runs in might not have the samePATH
variable as your interactive shell. - Logging: Redirect output from your commands to a log file (
>> /var/log/my_custom_log.log 2>&1
) to help with debugging. - Error Handling: Be mindful of commands that might fail.
rc.local
runs early in the boot process, and some services or network connectivity might not be fully available yet. - Alternatives: For more complex or critical services,
systemd
unit files are the recommended modern approach. They offer better control over dependencies, logging, and error handling. Learn more about systemd service units. - Security: Only add commands from trusted sources to
rc.local
, as it runs with root privileges.
Summary of Commands
Here's a quick reference table for the commands involved:
Command | Purpose |
---|---|
sudo systemctl status rc-local |
Check if the rc-local service is active. |
sudo nano /etc/rc.local |
Create or edit the rc.local script file. |
sudo chmod +x /etc/rc.local |
Make the rc.local script executable. |
sudo systemctl enable rc-local |
Enable rc-local to start at boot time. |
sudo systemctl start rc-local |
Start the rc-local service immediately. |
sudo systemctl disable rc-local |
Disable rc-local from starting at boot. |
By following these steps, you can effectively create and utilize rc.local
to execute custom scripts and commands at system startup.