Freworld.info - (Article)

Never Do This: How Not To Give A Single Board GNU/Linux PC A Static-y IP Address

Posted 2018-03-18 by Joe.


Ed. Note, March 8, 2024: This article remains online for historical reasons, but don't follow its advice... If you want to set a static ip address in Debian and derivatives, either use /etc/network/interfaces, which is tested working properly here in Debian 12, or use another modern method of your choosing.


This method isn't limited to single board computers (such as the Olinuxino Lime2 from Olimex, which inspired this kludgy effort); it would also work on a laptop whose wifi got a random IP address on connect, for example. Or any machine running Debian GNU/Linux or a Debian derivative. Or any networked Unix(like) system with rc.something, networkmanager and the ip command.

In the "old days", one might edit the /etc/network/interfaces file and mention the desire for a static IP address, and it was almost magically so. But with the advent of networkmanager and its pretty GUI interfaces (and the CLI tool nmcli), that avenue is no more. Debian and its derivatives use networkmanager by default, and the single board ARM computers generally come with Debian (or a derivative such as Armbian). Thus, barring networking structural changes, you will need to either work with networkmanager, or around it. The instructions on this page do the latter. Never do thisTM.

This method is based on the powers and outputs of the ip command, which you probably already have installed. If not, then...
sudo apt-get install iproute2

First, make a script under /home/yourusername/bin called add-our-ip-address.sh:

#!/bin/bash
#
# Never Do This IP address script
# (c) 2018 Freworld.info
# This is free software, licensed under
# the WTDHPL version 1 or any later version:
# http://wtdhpl.info/
#
# Adds a desired static-y IP address in addition to any already present.
# Advisory: script must be run with privileges of the root user.
#
if [[ $(/bin/ip addr|/bin/grep 192.168.50.89) ]]; then
    echo Desired address 192.168.50.89 is already present.
else
    echo Adding desired address 192.168.50.89.
    echo If you are not root this will probably not work.
    ip addr add 192.168.50.89 dev eth0
fi

The address 192.168.50.89, the username yourusername, and the interface name eth0 may need to change depending on where and why you are (definitely not) doing this. This script checks whether the desired address is already in use, and if not, tries to add it to the desired network interface.

Make the script executable like so: chmod +x /home/yourusername/bin/add-our-ip-address.sh

Tip: If you don't know the name of your interface, you can ask what interfaces your system has with the following command: ip link.

Example output of ip link:

1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN mode DEFAULT group default
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
2: tunl0: mtu 1480 qdisc noop state DOWN mode DEFAULT group default
    link/ipip 0.0.0.0 brd 0.0.0.0
3: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP mode DEFAULT group default qlen 1000
    link/ether aa:ff:bb:ee:cc:dd brd ff:ff:ff:ff:ff:ff
4: usb0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc pfifo_fast state DOWN mode DEFAULT group default qlen 1000
    link/ether aa:bb:cc:dd:ee:ff brd ff:ff:ff:ff:ff:ff

Here, your interfaces are lo, tunl0, eth0, and usb0. lo is the local loopback (not a real interface), tunl0 is a vpn interface (not a real interface), and usb0 is a special interface for when the Olinuxino is connected as a USB device. That leaves the name of the real ethernet device: "eth0". If you don't have an eth0, but you do have something that looks like enp25s0, then that one's probably it.

Next, make a similar script under /home/yourusername/bin called add-our-ip-address-run-at-boot-time.sh:

#!/bin/bash
#
# Never Do This IP address script Number 2
# (c) 2018 Freworld.info
# This is free software, licensed under
# the WTDHPL version 1 or any later version:
# http://wtdhpl.info/
#
# Advisory: script must be run with privileges of the root user.
#
/bin/bash /home/yourusername/bin/add-our-ip-address.sh
sleep 15
/bin/bash /home/yourusername/bin/add-our-ip-address.sh
sleep 15
/bin/bash /home/yourusername/bin/add-our-ip-address.sh

This script simply tries a few times to assert the preferred IP address.
Again, chmod +x /home/yourusername/bin/add-our-ip-address-run-at-boot-time.sh

Third, edit the file /etc/rc.local as root and add the following line just before the 'exit 0' at the end:

/bin/bash /home/yourusername/bin/add-our-ip-address-run-at-boot-time.sh &

This causes a few blind tries at boot time to add your chosen ip address to whatever other IP address may be present (from dhcp or what-have-you). (You may need to create the file /etc/rc.local -- if so, make sure to chmod +x /etc/rc.local afterwards.)

The above should be enough. But if you find that your system doesn't reliably pick up its IP address when booting, or you find that it sometimes loses the address, then you can have the system periodically check to make sure your selected IP address is available. To do this, crontab -e as root to add a line to the end of root's crontab as follows:

*/5 * * * * /home/yourusername/bin/add-our-ip-address.sh > /dev/null

This will try once every five minutes to see if the desired IP is in use, and if not, to make it so. Maybe that's overkill, but even so, it shouldn't hurt.

After completing the above steps, you should find that you can reliably have a particular IP address on the target host. But admittedly, this isn't "the right wayTM" to do it (far from it, in fact), and so you should... Never do thisTM.

If this doesn't work for you, or you still have problems, you might leave a question in the comments section so that someone may be able to help you out.

Also See: Connecting to Your Single Board Computer with VNC

Scripts listed on this page are free software released under the terms of the Do What The Darned Heck You Want To Public License, version 1 (WTDHPL v1), or any later version. Please review the license terms (they work for your freedom); free software is important to our culture.


Leave a comment:



The text and images of this website are copyright © 2024 Freworld.info, and licensed under the terms of The Do What The Darned Heck You Want To Public License, any version, unless otherwise noted.

Page served in 0.0077 sec.