How To Run A Command On StartUp in Linux (works on Centos & Ubuntu)

Published: December 1, 2025 (Updated: Dec 1, 2025)

Enjoying this content? Subscribe to the Channel!

Run Commands Automatically at Startup on Any Linux System (The Easy rc.local Method!)

Welcome to Darren’s Tech Tutorials!

Are you tired of manually starting the same script or service every time your Linux box reboots? Automation is key in the tech world, and today we’re tackling one of the most fundamental needs for any administrator or hobbyist: running a command automatically when your system starts up.

This incredibly quick and easy tutorial will show you exactly how to leverage the classic /etc/rc.local file. While we’re using CentOS 7 for the demonstration, this method works seamlessly across most major distributions, including Ubuntu and even the Raspberry Pi!

Ready to make your system smarter? Let’s dive in.


👉 Need a quick reference guide? Grab the Free Linux Cheat Sheet [here] (http://eepurl.com/dkRNM9). All commands used in this tutorial are also available on the official post [here] (https://darrenoneill.eu/?p=648).


Step 1: Accessing and Editing the rc.local File

The /etc/rc.local file is a special shell script that the system traditionally executes after all standard multi-user services have started. Although modern Linux distributions (like those using Systemd) often prefer other methods, rc.local remains the quickest, simplest way to run a command early in the boot sequence.

We will use the vi editor to modify the file. If you prefer, you can substitute vi with nano or your favorite editor.

Open the file using elevated privileges:

sudo vi /etc/rc.local

Step 2: Adding Your Startup Command

Once the file is open, navigate to the end of the existing content. You will likely see comments suggesting that you should use other methods (like Systemd unit files), but we’re going with the reliable quick-fix here!

We need to place the commands we want to execute before the final exit 0 (if that line exists).

For this tutorial, we are going to use a simple command that logs the boot time to a file in our home directory. This gives us a perfect way to test if the command ran successfully after a reboot.

Paste the following command into your rc.local file:

echo "Computer booted up at $(date)" >> /home/YOUR_USERNAME/bootup.log

Note: Be sure to replace /home/YOUR_USERNAME/ with the actual path to your home directory (e.g., /home/darren/).

This command does two things:

  1. echo "...": Generates a string of text, including the current date and time ($(date)).
  2. >>: Appends that output to the specified log file (bootup.log).

Saving the File

Once you have added your command, save and exit the vi editor:

  1. Press the Escape key.
  2. Type :wq (write and quit).
  3. Press Enter.

Step 3: Granting Execute Permissions

The system must have permission to execute this script when booting up. If the script does not have the executable bit set, your command will not run. This is a crucial step that is often missed!

Run the following command to give the /etc/rc.local file the necessary execute permission:

sudo chmod +x /etc/rc.local

You are now ready to test!

Step 4: Testing and Verification

To ensure our command runs successfully, we must reboot the machine.

Execute the reboot command:

sudo init 6

(Alternatively, you can use sudo reboot)

Wait for your machine to fully restart. Once you are logged back into your system, we can check if the bootup.log file was successfully created in your home directory.

Checking the Log File

First, confirm the file exists:

ls ~/bootup.log

If the file is present, let’s use cat to view its contents:

cat ~/bootup.log

You should see output similar to this, proving the command ran automatically on startup!

Computer booted up at Thu Dec 14 10:35:12 UTC 2023

Success! You have now configured your Linux system to execute a command automatically upon every startup.

Conclusion

The rc.local method remains one of the simplest and most distribution-agnostic ways to ensure critical commands or small scripts run immediately when your system comes online. Whether you’re setting up a robust CentOS server, an Ubuntu workstation, or a fun Raspberry Pi project, this technique ensures your automation requirements are met.

If this tutorial helped you simplify your workflow, please take a moment to Like this post and Subscribe to Darren’s Tech Tutorials for more clear, practical, and enthusiastic tech guides.

If you encountered any issues or have a question about adapting this technique, drop a comment below—I’m always happy to help! Thanks for watching!