Udhcpc: Difference between revisions

From Alpine Linux
mNo edit summary
(Cleaned up the formatting; clarified ADSL; some rewording for clarity)
Line 1: Line 1:
{{Expand|Please help us expand this article by contributing.}}
{{Expand|Please help us expand this article by contributing.}}


You may want to customize the behavior of the default DHCP client (udhcpc from busybox), which is called by /sbin/ifup by having "dhcp" in /etc/network/interfaces.
You may want to customize the behavior of the default DHCP client (<code>udhcpc</code> from busybox), which is called by <code>/sbin/ifup</code> by having "<code>dhcp</code>" in <code>/etc/network/interfaces</code>.


The default behavior is driven by the script /usr/share/udhcpc/default.script
The default behavior is driven by the script <code>/usr/share/udhcpc/default.script</code>


Entries in /etc/network/interfaces for dhcp interfaces will drive the udhcpc command line. For example:<br>
Entries in <code>/etc/network/interfaces</code> for DHCP interfaces will drive the <code>udhcpc</code> command line. For example:<br>
   
   
<pre>
<pre>auto eth0
auto eth0
iface eth0 inet dhcp
iface eth0 inet dhcp
         hostname myhostname
         hostname myhostname</pre>
</pre>
Will set these parameters on the command line:<br>


<b>-i eth0 -x hostname:myhostname</b><br>
will set these parameters on the command line:<br>
 
<pre>-i eth0 -x hostname:myhostname</pre>


The hostname will send the DHCP option to the server to tell the server the name of this client.   
The hostname will send the DHCP option to the server to tell the server the name of this client.   


The documentation resides [https://udhcp.busybox.net/README.udhcpc Here]<br>
The documentation for <code>udhcpc</code> can be found in [https://udhcp.busybox.net/README.udhcpc the busybox udhcpc readme file]<br>


Its default configuration may be overwritten by /etc/udhcpc/udhcpc.conf
Its default configuration may be overwritten by <code>/etc/udhcpc/udhcpc.conf</code>


Authorized key:value pairs are:
Authorized key:value pairs are:
Line 51: Line 50:
|}
|}


Example /etc/udhcpc/udhcpc.conf:
Example <code>/etc/udhcpc/udhcpc.conf</code>:


  RESOLV_CONF="no" # Prevents overwriting of /etc/resolv.conf
  RESOLV_CONF="no" # Prevents overwriting of /etc/resolv.conf


Custom scripts can be added as /etc/udhcpc/pre-* and /etc/udhcpc/post-* to be run before/after deconfig/renew/bound DHCP events - they must be marked as '''executable''' by root, e.g. chmod 744
Custom scripts can be added as <code>/etc/udhcpc/pre-*</code> and <code>/etc/udhcpc/post-*</code> to be run before/after deconfig/renew/bound DHCP events. They must be set as '''executable''' by root, e.g. <code>chmod 744</code>


As an example, /etc/udhcpc/post-bound/mtu could contain, to change the interface MTU from the default (1500) to 1492, which is useful if [https://en.wikipedia.org/wiki/Maximum_transmission_unit on ADSL] (which uses 8 bytes):
As an example, <code>/etc/udhcpc/post-bound/mtu</code> could contain, to change the interface MTU from the default (1500) to 1492, which is useful if [https://en.wikipedia.org/wiki/Maximum_transmission_unit on ADSL that uses PPPoE] (which uses 8 bytes for its own header):


  r=$(/sbin/ip route | grep ^default | head -n 1)
  r=$(/sbin/ip route | grep ^default | head -n 1)
  # Needs iproute2 package, rather than busybox's "ip", to change mtu
  # Needs iproute2 package, rather than busybox's "ip", to change mtu
  /sbin/ip route replace $r mtu 1492<br>
  /sbin/ip route replace $r mtu 1492
<b>hack alert</b>
 
I needed to restart my firewall (which replaces the iptables script from Alpine) when the client binds to a new IP, so I added the following line in the function bound(), right after 'resolvconf':<br>
<b>Hack alert:</b>
/etc/init.d/iptables reload<br>
I needed to restart my firewall (which replaces the <code>iptables</code> script from Alpine) when the client binds to a new IP, so I added the following line in the function <code>bound()</code>, right after '<code>resolvconf</code>':
<br>
 
  /etc/init.d/iptables reload
 
The reload drops all firewall rules, re-acquires the Internal and external IPs, and re-writes the rules. I'm sure there is a better way.
The reload drops all firewall rules, re-acquires the Internal and external IPs, and re-writes the rules. I'm sure there is a better way.


[[Category:Networking]]
[[Category:Networking]]
[[Category:Embedded Systems]]
[[Category:Embedded Systems]]

Revision as of 13:12, 9 December 2021

This material needs expanding ...

Please help us expand this article by contributing.

You may want to customize the behavior of the default DHCP client (udhcpc from busybox), which is called by /sbin/ifup by having "dhcp" in /etc/network/interfaces.

The default behavior is driven by the script /usr/share/udhcpc/default.script

Entries in /etc/network/interfaces for DHCP interfaces will drive the udhcpc command line. For example:

auto eth0
iface eth0 inet dhcp
        hostname myhostname

will set these parameters on the command line:

-i eth0 -x hostname:myhostname

The hostname will send the DHCP option to the server to tell the server the name of this client.

The documentation for udhcpc can be found in the busybox udhcpc readme file

Its default configuration may be overwritten by /etc/udhcpc/udhcpc.conf

Authorized key:value pairs are:

key default value possible values
NO_GATEWAY - <list of iface names>
IF_METRIC - <metric value>
IF_PEER_DNS yes <anything but yes>
RESOLV_CONF /etc/resolv.conf no ; NO ; -
NO_DNS - <list of iface names>

Example /etc/udhcpc/udhcpc.conf:

RESOLV_CONF="no" # Prevents overwriting of /etc/resolv.conf

Custom scripts can be added as /etc/udhcpc/pre-* and /etc/udhcpc/post-* to be run before/after deconfig/renew/bound DHCP events. They must be set as executable by root, e.g. chmod 744

As an example, /etc/udhcpc/post-bound/mtu could contain, to change the interface MTU from the default (1500) to 1492, which is useful if on ADSL that uses PPPoE (which uses 8 bytes for its own header):

r=$(/sbin/ip route | grep ^default | head -n 1)
# Needs iproute2 package, rather than busybox's "ip", to change mtu
/sbin/ip route replace $r mtu 1492

Hack alert: I needed to restart my firewall (which replaces the iptables script from Alpine) when the client binds to a new IP, so I added the following line in the function bound(), right after 'resolvconf':

 /etc/init.d/iptables reload

The reload drops all firewall rules, re-acquires the Internal and external IPs, and re-writes the rules. I'm sure there is a better way.