How to run a startup or boot script as root in Linux

Every once in awhile I have wanted to be able to run a specific script automatically when Linux starts regardless of the user. Typically, I have also wanted that script to run as root because I’m doing random odd things like unusual mountings or whatever. In this case, I wanted to set up an IPv4 tunnel to an IPv6 host so that I could access IPv6 only web sites.

So, while the focus of this article will be the startup script, I will also briefly discuss the IPv6 setup. So, to start, you want to create a shell script that will execute the commands you need. In this case we’ll call it LaunchIPv6Tunnel.sh and here are it’s contents:

ifconfig sit0 up
ifconfig sit0 inet6 tunnel ::209.51.181.2
ifconfig sit1 up
ifconfig sit1 inet6 add 2001:470:1f10:5ba::2/64
route -A inet6 add ::/0 dev sit1

Now, I am going to need to be root to do these next steps. Here’s how to become root:

sudo su

Then type in your password when prompted. Next you want to copy your file to the /usr/sbin folder. Now, make it so it runs always as SuperUser (root) by doing the following command:

chmod +s LaunchIPv6Tunnel.sh

Now, the real trick to launching this script is in editing the /etc/rc.local file. Simply add the path and name of your script before the return 0 and you are good to go. Mine looks like this:

#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will “exit 0” on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.
/usr/sbin//LaunchIPV6Tunnel.sh
exit 0

The next time you start your machine the script will run and away you go. You may want to test your script manually first to make sure it doesn’t cause any errors. As a side note, if you want your own IPv6 tunnel, you can go to http://www.tunnelbroker.net to get a free tunnel connection.

Advertisement

About Michael Wigle

I am a servant of Christ who is married and has two children and four grandchildren. For employment, I am the IT Manager and the Cincinnati Association for the Blind and Visually Impaired. I also have a wide variety of interests from economics and politics to hiking and caving.
This entry was posted in Computers and Internet and tagged , , , , , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s