View Single Post
Join Date: Oct 2007
Posts: 399
Reputation: eggi will become famous soon enough eggi will become famous soon enough 
Solved Threads: 47
eggi eggi is offline Offline
Posting Whiz

Re: Change DNS from DHCP to static in Ubuntu

 
0
  #5
Nov 18th, 2008
Hey Ed,

Yes, it appears that I totally misunderstood you Sorry about that.

Probably your best bet here is to either have the multiple resolv.conf's so that you can replace them when you get to your alternate location. Something like:

cp /etc/resolv.conf /etc/resolv.conf.bak.`date +%m%d%y`
cp /etc/resolv.conf.static /etc/resolv.conf

with /etc/resolv.conf.static being your regular resolv.conf - the DHCP servers elsewhere shouldn't care if you go outside to resolve.

If they do, for some reason, require that you resolve through them first, the answer is a bit more complicated, but fairly easy to understand once you've done it a time or to. Basically, all you need to do in your script would be to check and see if your DNS servers are still in /etc/resolv.conf. If they've been removed, add them after the initial nameserver in the new resolv.conf (which you won't have to back up since they'll happily overwrite it anyway - although it never hurts to back up and make sure you have an options line (and if it already exists remove it and replace it with your own.

Something like this, for a script (just spitballing here, so this may not be perfect for you - /bin/bash can be /bin/dash if you like that better for Ubuntu - the script should run the same either way):

Shell Scripting Syntax (Toggle Plain Text)
  1. #!/bin/bash
  2.  
  3. sed '1,/^nameserver.*$/ {/^nameserver.*$/a\
  4. nameserver 10.10.10.10
  5. }' resolv.conf >resolv.tmp
  6. mv resolv.tmp resolv.conf
  7. sed '/^options.*/d' resolv.conf >>resolv.tmp
  8. echo "options retrans:1 retry:1" >> resolv.tmp
  9. mv resolv.tmp resolv.conf

That would change this resolv.conf

domain hithere.com
nameserver 10.10.1.199
nameserver 10.10.1.889
search here there everywhere
options retrans:555555 retry:100
to this

domain hithere.com
nameserver 10.10.1.199
nameserver 10.10.10.10
nameserver 10.10.1.889
search here there everywhere
options retrans:1 retry:1
only substituting your 10.10.1.99 nameserver after their initial entry (if that's a requirement) and changing the timeout on retrans and retry to (hopefully) skip past theirs and failover directly to yours - you can add more, just by modifying the sed expression.

Hope that helps out and is more germane to what you're looking to do that my last response was!

Best wishes,

Mike
Last edited by eggi; Nov 18th, 2008 at 10:34 pm. Reason: typo
Linux and Unix Tips, Tricks and Individual Advice - The Linux and Unix Menagerie!
------------------------------------------------------------------------
The greatest viral marketing idea of all time, get your copy of this Free Report now!
Reply With Quote