IoT Bytes

Bits and Bytes of IoT

Assign Persistent Static IP Address to Tiny Core Linux

Pradeep Singh | 19th Aug 2017

ip-address

The static IP Address assignment process for TinyCore or MicroCore is a bit different from other Linux distros as Tiny Core runs into the system RAM directly and has limited set of network packages.

If you wish to assign a static IP Address to your Tiny Core instace from the CLI interface, you may follow this article –

1. Create Static Interface Config:

Create a new script file using “sudo vi /opt/eth0.sh” command with the following contents (chante the settings in this script according to your network config) –

#========================================================
#!/bin/sh
# kill dhcp client for eth0
if [ -f /var/run/udhcpc.eth0.pid ]; then
kill `cat /var/run/udhcpc.eth0.pid`
sleep 0.1
fi
# configure interface eth0
ifconfig eth0 192.168.0.1 netmask 255.255.255.0 broadcast 192.168.0.255 up
route add default gw 192.168.0.254
echo nameserver 192.168.0.254 >> /etc/resolv.conf
#========================================================

2. Change Interface Script File Permission:

Add the execute permissions to the file you created in previous step. To keep things simple I have assigned all the permissions to this file. If you are concerned about security, feel free to fine tune the file permissions –

sudo chmod 777 /opt/eth0.sh

tc@box:~$ ls -ltr | grep /opt/eth0.sh 
-rw-r--r--    1 root     staff          178 Aug 19 16:02 eth1.sh
tc@box:~$
tc@box:~$ sudo chmod 777  /opt/eth0.sh 
tc@box:~$ 
tc@box:~$ ls -ltr /opt/ |grep eth0.sh
-rwxrwxrwx 1 root staff 178 Aug 19 16:02 eth0.sh
tc@box:~$

3. Make the Ethernet Interface Configuration Persistent:

At this stage if you reboot the server all the changes that you made in previous steps would be lost as the Tiny Core OS runs directly from the system RAM. To make the configuration presistent, use following commands –

sudo echo ‘/opt/eth0.sh’ >> /opt/.filetool.lst
sudo echo ‘/opt/eth0.sh &’ >> /opt/bootlocal.sh
filetool.sh -b

tc@box:~$ sudo echo '/opt/eth0.sh' >> /opt/.filetool.lst 
tc@box:~$ 
tc@box:~$ cat /opt/.filetool.lst 
opt
home
/opt/eth0.sh
tc@box:~$ 
tc@box:~$ sudo echo '/opt/eth0.sh &' >> /opt/bootlocal.sh 
tc@box:~$
tc@box:~$ cat /opt/bootlocal.sh 
#!/bin/sh
# put other system startup commands here
/opt/eth0.sh &
tc@box:~$ 
tc@box:~$ filetool.sh -b
Backing up files to /mnt/sda1/tce/mydata.tgztc@box:~$ 
tc@box:~$

Troubleshooting:

NOTE 1: If you are running Tiny Core from a very fast Storage device such as SSD Disk (specially NVMe Drives), you may see errors like following during boot process and the IP Address will not get assigned to the Ethernet interface –

ifconfig: SIOCSIFADDR: No such Device

route: SIOCADDRT: Network is unreachable

To fix this error, you may add a “sleep” statement in the first line of  “/opt/eth0.sh” file that you created in first step of this article. You can start with “sleep .2” and can increase or decrease the sleep time accordingly. After this change your “/opt/eth0.sh” file should look something similar to this –

tc@box:~$ cat /opt/eth0.sh 
#!/bin/sh

# If you are booting Tiny Core from a very fast storage such as SSD / NVMe Drive and getting 
# "ifconfig: SIOCSIFADDR: No such Device" or "route: SIOCADDRT: Network is unreachable"
# error during system boot, use this sleep statemet, otherwise you can remove it -
sleep .2

# kill dhcp client for eth0
sleep 1
if [ -f /var/run/udhcpc.eth0.pid ]; then
 kill `cat /var/run/udhcpc.eth0.pid`
 sleep 0.1
fi

# configure interface eth0
ifconfig eth0 192.168.0.1 netmask 255.255.255.0 broadcast 192.168.0.255 up
route add default gw 192.168.0.254
echo nameserver 192.168.0.254 >> /etc/resolv.conf
tc@box:~$

Don’t forget to backup your configuration using “filetool.sh -b” command –

tc@box:~$ filetool.sh -b
Backing up files to /mnt/sda1/tce/mydata.tgztc@box:~$ 
tc@box:~$ 

NOTE 2: If you mess-up DHCP Client configuration for any of your NIC card, You can enable DHCP Client for it using “sudo /sbin/udhcpc -i ” command.

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

%d bloggers like this: