0

I would want to append the below line to resolv.conf of all servers in our aws env and make sure it doesn't get overwritten

options timeout:1 attempts:1 rotate

I came across dhclient enter and exit hooks, which seemed to be the way to achieve this.

I tried placing enter hook with name /etc/dhcp/dhclient-enter-hooks with content as below

 #!/bin/bash
 sed -i '$ a\options timeout:1 attempts:1 rotate' /etc/resolv.conf

And when i run dhclient, it adds the same line several times to the beginning of the file. And sometimes, few times in the beginning and few times in the end. Exit hook does the same thing.

Why does the line gets added several times to resolv.conf, instead of only once? And this behaviour is random. Sometimes it adds the same line 4 times while running dhclient. Sometimes 3 and so on. What's the correct way of doing this?

1

The man page for dhclient-script(8) defines the behaviour carefully. The script(s) will get called for each possible action:

OPERATION When dhclient needs to invoke the client configuration script, it defines a set of variables in the environment, and then invokes CLIENTBINDIR/dhclient-script. In all cases, $reason is set to the name of the reason why the script has been invoked. The following reasons are currently defined: MEDIUM, PREINIT, BOUND, RENEW, REBIND, REBOOT, EXPIRE, FAIL, STOP, RELEASE, NBI and TIMEOUT.

An easy fix for your script would be to add the line only if it doesn't already exist. A better fix would be to match on $reason for the appropriate case values.

#!/bin/bash
grep -q '^options' /etc/resolv.conf ||
    echo 'options timeout:1 attempts:1 rotate' >>/etc/resolv.conf
true

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.