IoT Bytes

Bits and Bytes of IoT

Configure Tiny Core Linux as DHCP Server using udhcpd

Pradeep Singh | 18th Aug 2017

DHCP.png

Dynamic Host Configuration Protocol (DHCP) is a client/server protocol that automatically provides an IP Host with its IP address and other related configuration information such as the subnet mask and default gateway.

udhcpd is the DHCP Server demon which is part of BusyBox and is included by default with core OS (no need to install any additional package). It is a perfect candidate for simple and lightweight DHCP Server. Let’s explore how to create a Tiny Core based DHCP Server –

1. Configure the Ethernet Interface with Static IP Address:

1.1 Create the ethernet config script using the “sudo vi /opt/eth0.sh” command with the following contents  (For more details on this you can refer to this Article) –

#!/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.10.1 netmask 255.255.255.0 broadcast 192.168.10.255 up

# Start the DHCP Server Process once the Interface is Ready with the IP Add
sleep .1
sudo udhcpd /etc/udhcpd.conf &

1.2 Assign execute permission to “eth0.sh” script –

sudo chmod 777 /opt/eth0.sh

1.3 Execute the script to configure the “eth0” Ethernet interface using the following command –

sudo /opt/eth0.sh

1.4 Check the Ethernet interface “eth0” –

 

2. Create “/etc/udhcpd.conf” File:

Create a DHCP Configuration file with the “sudo vi /etc/udhcpd.conf” command and fill it will following template (make the changes according to your DHCP Range requirements) –

start 192.168.10.100
end 192.168.10.200
interface eth0
option subnet 255.255.255.0
option router 192.168.10.1
option lease 43200
option dns 192.168.10.1
option domain local

3. Start DHCP Daemon:

Start the DHCP process using the following command –

sudo udhcpd /etc/udhcpd.conf

Note: In case your DHCP Server is not working try using “-f” option with “udhcpd” command to run it in the foreground and see the error messages on the console. For Example – “sudo udhcpd -f /etc/udhcpd.conf

4. Verify the DHCP Server Process:

You can use following commands to verify if the DHCP process is running properly using the following commands –

ps -ef | grep udhcpd
sudo netstat -anp | grep udhcpd

You should see something like following console output (DHCP Server listens at UDP port 67) –

tc@box:~$ ps -ef | grep udhcpd
  913 root     udhcpd /etc/udhcpd.conf
  933 tc       grep udhcpd
tc@box:~$
tc@box:~$ sudo netstat -anp | grep udhcpd
netstat: /proc/net/tcp6: No such file or directory
udp        0      0 0.0.0.0:67              0.0.0.0:*                           913/udhcpd
netstat: /proc/net/udp6: No such file or directory
netstat: /proc/net/raw6: No such file or directory
tc@box:~$ 

4. Make DHCP Server Configuration Persistent:

As Tiny Core runs from system RAM, it would loose the DHCP configuration on system reboot. To make your DHCP configuration persistent use the following commands –

sudo echo '/etc/udhcpd.conf' >> /opt/.filetool.lst
sudo echo '/opt/eth0.sh' >> /opt/.filetool.lst
sudo echo '/opt/eth0.sh &' >> /opt/bootlocal.sh
filetool.sh -b

Following is the console output with sample contents of “.filetool.lst” and “bootlocal.sh” files –

tc@box:~$ sudo echo '/etc/udhcpd.conf' >> /opt/.filetool.lst
tc@box:~$ sudo echo '/opt/eth0.sh' >> /opt/.filetool.lst
tc@box:~$
tc@box:~$ cat /opt/.filetool.lst 
opt
home
/etc/udhcpd.conf
/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:~$

5. Reboot the Server:

With this your DHCP Server is ready. You may test the configuration persistence by rebooting the server.


References:

/etc/udhcpd.conf – Man Page

/etc/udhcpd.conf – Sample Configuration File 1

/etc/udhcpd.conf – Sample Configuration File 2

Leave a comment