[TUTORIAL] Automate IP routing at power outings/reboot for lxc

papatikka

Member
Jun 26, 2023
31
0
6

Prologue​

I googled a lot, and, there are so many scattered solutions that are poorly explained because everyone assumes that you are a linux expert.

The problem​

Some of the inconveniences that I had, with lxc running with vpn network interface, is that you cannot access your service because you need to add ip route in order to access it inside your LAN or by your own VPN server. This especially coupled with reboots, because at reboots pve removed all earlier IP routes that you've added.

By simply adding a bash script and attach systemd directive to it, the problem is solved with reboots. Disclaimer, I consider myself average homelabber and you are welcome to add any advices if you have, in order to improve the tutorial. This solution should work for all debian based lxc's such as Ubuntu.

1. Preparing bash script​

Create a script file at i.e. /usr/local/sbin/ip-route-add.sh
Code:
nano /usr/local/sbin/ip-route-add.sh

Inside the file, write following:
Bash:
#!/bin/bash

#This example assumes that you wrote the command below
#ip route add 10.10.10.0/24 via 192.168.20.20 dev eth0

#Route is where you are coming from, the subnet/VLAN you are accessing from in order to access the service
#Gateway is commonly the node where LXC resides, in this case Proxmox VE node is at 192.168.20.20
#Interface is the network device the node use, in most case it is eth0.

#Start by defining the route and gateway
ROUTE="10.10.10.0/24"
GATEWAY="192.168.20.20"
INTERFACE="eth0"

#Check if the route already exists
ip route | grep -q "$ROUTE via $GATEWAY dev $INTERFACE"

#If the route does not exist, add it
if [ $? -ne 0 ]; then
    ip route add $ROUTE via $GATEWAY dev $INTERFACE
    echo "Route added: $ROUTE via $GATEWAY dev $INTERFACE"
else
    echo "Route already exists: $ROUTE via $GATEWAY dev $INTERFACE"
fi

Because of the script that have "do nothing" action if the ip route exist, the systemctl won't throw errors.

2. Make sure system have right to execute the script​

Add these commands
Code:
chmod a+x /usr/local/sbin/ip-route-add.sh

3. Preparing systemd ini​

Create a systemd file at /etc/systemd/system/ip-routing-custom.service by writing following commands
Code:
nano /etc/systemd/system/up-routing-custom.service

Inside the file, add following

Bash:
[Unit]
Description=automate the ip route add for every reboot
After=network.target
StartLimitIntervalSec=0
[Service]
Type=simple
Restart=always
RestartSec=5
ExecStart=/bin/bash /usr/local/sbin/ip-route-add.sh
[Install]
WantedBy=multi-user.target

Then you have to run following:

Code:
systemctl daemon-reload
systemctl start ip-routing-custom.service
systemctl status ip-routing-custom.service

Also try reboot.

Enjoy not having to manually add ip routes anymore.

 
Last edited:

About

The Proxmox community has been around for many years and offers help and support for Proxmox VE, Proxmox Backup Server, and Proxmox Mail Gateway.
We think our community is one of the best thanks to people like you!

Get your subscription!

The Proxmox team works very hard to make sure you are running the best software and getting stable updates and security enhancements, as well as quick enterprise support. Tens of thousands of happy customers have a Proxmox subscription. Get yours easily in our online shop.

Buy now!