Automatically Assign a Public IP on Boot Using systemd

In certain server environments (such as cloud VMs or NAT-based setups), public IP addresses may not persist after a reboot. This can break services like licensing systems or firewalls that rely on that IP.

This guide walks you through creating a systemd service and bash script that ensures your public IP is automatically reassigned every time the system boots.


Use Case

You need to bind a public IP (e.g., 103.20.235.24) to a specific network interface (e.g., eth0) at boot time.


Overview

This method includes:

  • A bash script that checks and assigns the IP.

  • A systemd service that runs this script at every boot.

  • Automatic enabling and verification of the service.


Step-by-Step Instructions

1. Run the Setup Script

Create and execute the following script as root.

#!/bin/bash

# CONFIGURATION
PUBLIC_IP="103.20.235.24"
INTERFACE="eth0"
SCRIPT_PATH="/usr/local/bin/add-public-ip.sh"
SERVICE_PATH="/etc/systemd/system/add-public-ip.service"

if [ "$EUID" -ne 0 ]; then
  echo "This script must be run as root."
  exit 1
fi

# Step 1: Create IP assignment script
echo "Creating IP assignment script at $SCRIPT_PATH..."
cat <<EOF > "$SCRIPT_PATH"
#!/bin/bash
PUBLIC_IP="$PUBLIC_IP"
INTERFACE="$INTERFACE"

if ip addr show "\$INTERFACE" | grep -q "\$PUBLIC_IP"; then
    echo "IP \$PUBLIC_IP already assigned to \$INTERFACE."
else
    echo "Assigning \$PUBLIC_IP to \$INTERFACE..."
    ip addr add "\$PUBLIC_IP/32" dev "\$INTERFACE"
    echo "IP assigned."
fi
EOF

chmod +x "$SCRIPT_PATH"

# Step 2: Create systemd service
echo "Creating systemd service at $SERVICE_PATH..."
cat <<EOF > "$SERVICE_PATH"
[Unit]
Description=Assign public IP to interface
After=network-online.target
Wants=network-online.target

[Service]
Type=oneshot
ExecStart=$SCRIPT_PATH
RemainAfterExit=yes

[Install]
WantedBy=multi-user.target
EOF

# Step 3: Enable and start the service
systemctl daemon-reexec
systemctl daemon-reload
systemctl enable add-public-ip.service
systemctl start add-public-ip.service

# Step 4: Show status
echo "Service status:"
systemctl status add-public-ip.service --no-pager

# Step 5: Verify IP assignment
echo "Verifying IP assignment on $INTERFACE:"
ip addr show "$INTERFACE" | grep "$PUBLIC_IP" && echo "IP successfully assigned." || echo "IP not found!"
2. Make It Executable & Run
chmod +x setup-public-ip.service.sh
sudo ./setup-public-ip.service.sh

Result

  • The public IP will be assigned automatically at boot.

  • You’ll no longer need to manually re-add the IP after reboots.


Notes

  • Update the PUBLIC_IP and INTERFACE variables as needed for your system.

  • Ensure the public IP is routed correctly through your provider or cloud control panel.

  • This script is safe to run multiple times; it only adds the IP if it's missing.


Verifying the Setup

Check assigned IP:

ip addr show eth0 | grep 103.20.235.24

Check service status:

systemctl status add-public-ip.service
  • 2 Users Found This Useful
Was this answer helpful?

Related Articles

How to restrict direct root access in Linux

We can do it just in two steps. Step One: At first we will create new root user as follows (for...

How to extract .tar.gz files in Linux/UNIX OS

A tarball is a group of files that are bundled together using the tar command. Use the...

How to add welcome message when SSH start?

You need to change the contents of /etc/motd. Unfortunately, by default, /etc/motd is a link to...

How to change root password when SSH logged in

Run the following command: passwd Now type your new passwordOnce done, retype new passwordDone!...

How to install Pinguzo on any Linux/UNIX OS

Login to Pinguzo panel using Softaculous account or create an account of Pinguzo To add new...