DEV Community

Siva
Siva

Posted on • Originally published at byteshiva.Medium on

How to Create an Init Script for Tailscaled in Linux

If you are using a Linux distribution that does not use Systemd, you may need to create an init script to manage Tailscaled, the network daemon for the Tailscale VPN.

Here are the steps to create an init script for Tailscaled:

Step 1:

Determine the location of the Tailscaled binary The Tailscaled binary is typically installed in /usr/bin/tailscaled. Use the which command to determine the location of the tailscaled binary:

which tailscaled
Enter fullscreen mode Exit fullscreen mode

If tailscaled is not in your PATH, you may need to provide the full path to the binary in your init script.

Step 2:

Create the init script Create a new file in the /etc/init.d directory with a name that reflects the purpose of the script. In this case, tailscaled would be a good name:

sudo nano /etc/init.d/tailscaled
Enter fullscreen mode Exit fullscreen mode

Add the following script to the tailscaled file:

#!/bin/sh
### BEGIN INIT INFO
# Provides: tailscaled
# Required-Start: $local_fs $network $syslog
# Required-Stop: $local_fs $network $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: tailscaled daemon
# Description: tailscaled daemon
### END INIT INFO

DAEMON=/sbin/tailscaled
PIDFILE=/var/run/tailscaled.pid
USER=tailscale
GROUP=tailscale
test -x $DAEMON || exit 0
case "$1" in
  start)
    echo "Starting Tailscaled"
    start-stop-daemon --start --background --pidfile $PIDFILE --make-pidfile --chuid $USER:$GROUP --startas $DAEMON -- start
    ;;
  stop)
    echo "Stopping Tailscaled"
    start-stop-daemon --stop --pidfile $PIDFILE --retry 10
    ;;
  *)
    echo "Usage: /etc/init.d/tailscaled {start|stop}"
    exit 1
    ;;
esac
exit 0
Enter fullscreen mode Exit fullscreen mode

This script provides basic functionality for starting and stopping tailscaled. You may need to modify it based on your specific needs, such as adding command-line arguments to tailscaled or changing the user and group that the daemon runs under.

Save the file and exit the text editor.

Step 3:

Make the init script executable Make the tailscaled init script executable with the following command:

sudo chmod +x /etc/init.d/tailscaled
Enter fullscreen mode Exit fullscreen mode

Step 4:

Test the init script Test the init script by starting and stopping tailscaled with the following commands:

sudo /etc/init.d/tailscaled start
sudo /etc/init.d/tailscaled stop
Enter fullscreen mode Exit fullscreen mode

Step 5:

Configure the init script to start automatically Configure the tailscaled init script to start automatically on system boot with the following command:

sudo update-rc.d tailscaled defaults
Enter fullscreen mode Exit fullscreen mode

This will create the necessary symlinks in the /etc/rc?.d directories to start tailscaled during the boot process.

You now have an init script for tailscaled that can be used on Linux distributions that do not use Systemd.

Top comments (0)