Change DNS from DHCP to static in Ubuntu

Please support our Shell Scripting advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Nov 2008
Posts: 14
Reputation: EdTheUniqueGeek is an unknown quantity at this point 
Solved Threads: 0
EdTheUniqueGeek EdTheUniqueGeek is offline Offline
Newbie Poster

Change DNS from DHCP to static in Ubuntu

 
0
  #1
Nov 18th, 2008
I'm still learning Linux, and have been for the past 7 years, but am still rusty on scripting.
Can someone tell me if it is possible to create a script that will change the DNS IP address of your interface from dynamic (from DHCP) to a specific static IP address? If so, can someone point me to how this can be done?
I know you can change these settings from command line using VI on the /etc/resolve.conf file but I wasn't sure if there was a way to automatically change that with a script that I can simply launch from the GUI desktop in Ubuntu 8.10.
Thanks in advance.
-----------------------------------
http://www.edwardcrosby.com
http://www.theuniquegeek.com
Reply With Quote Quick reply to this message  
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
  #2
Nov 18th, 2008
Hey there,

It sounds like you want to change ethernet settings and reset your DNS.

For DNS, the easiest way would be to create an additonal resolv.conf - /etc/resolv.conf.static or something like that - and backup your current resolv.conf and copy that one over it.

For your ethernet address, you should be able to do something close to the following (setting up a file in /etc/network/interfaces would make this easier, but not as easy to explain

Assuming eth0

ifdown eth0
ifconfig eth0 10.10.10.10 netmask 255.255.255.0 up


should do it - just change 10.10.10.10 to your static IP and replace the 255.255.255.0 subnet mask with whatever is appropriate for your system.


Hope that helps

, Mike
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 Quick reply to this message  
Join Date: Nov 2008
Posts: 14
Reputation: EdTheUniqueGeek is an unknown quantity at this point 
Solved Threads: 0
EdTheUniqueGeek EdTheUniqueGeek is offline Offline
Newbie Poster

Re: Change DNS from DHCP to static in Ubuntu

 
0
  #3
Nov 18th, 2008
Originally Posted by eggi View Post
Hey there,

It sounds like you want to change ethernet settings and reset your DNS.

For DNS, the easiest way would be to create an additonal resolv.conf - /etc/resolv.conf.static or something like that - and backup your current resolv.conf and copy that one over it.

For your ethernet address, you should be able to do something close to the following (setting up a file in /etc/network/interfaces would make this easier, but not as easy to explain

Assuming eth0

ifdown eth0
ifconfig eth0 10.10.10.10 netmask 255.255.255.0 up


should do it - just change 10.10.10.10 to your static IP and replace the 255.255.255.0 subnet mask with whatever is appropriate for your system.


Hope that helps

, Mike
Thanks for the reply.
Actually, the only thing I want to change is my DNS IP.
Here's the scenario. I have Ubuntu 8.10 running on my Dell Mini 9 and I take this machine everywhere. At home I have DHCP setup with my router serving OpenDNS as my DNS for all of my client machines on my LAN. When I leave my house, when I go to a wifi hotspot, like Starbucks or whatever, I would like to continue to use OpenDNS IP's as my DNS servers.
So, when I enter the wifi hotspot, I would like to launch a script on my desktop to automatically change my DNS IPs from the IPs that were given to me by that public wifi to the OpenDNS IPs instead.
Is that possible? Should I just create a script to rename that resolve.conf file every time I want to use a static DNS IP?
Thanks in advance.
-----------------------------------
http://www.edwardcrosby.com
http://www.theuniquegeek.com
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 146
Reputation: Stylish is an unknown quantity at this point 
Solved Threads: 14
Stylish's Avatar
Stylish Stylish is offline Offline
Junior Poster

Re: Change DNS from DHCP to static in Ubuntu

 
0
  #4
Nov 18th, 2008
I used Google with the following terms:
resolv.conf and dhcp

There were too many results to link and I do not have a machine to test on. But, plenty of talk on how dhclient overwrites resolv.conf and ways to stop it.

Good luck!
I NEED AN ADULT!
Reply With Quote Quick reply to this message  
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 Quick reply to this message  
Join Date: Nov 2008
Posts: 14
Reputation: EdTheUniqueGeek is an unknown quantity at this point 
Solved Threads: 0
EdTheUniqueGeek EdTheUniqueGeek is offline Offline
Newbie Poster

Re: Change DNS from DHCP to static in Ubuntu

 
0
  #6
Nov 19th, 2008
Thank you so much. I'll play around with that.
-----------------------------------
http://www.edwardcrosby.com
http://www.theuniquegeek.com
Reply With Quote Quick reply to this message  
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
  #7
Nov 20th, 2008
Cool,

I hope it works out for you

Best wishes,

Mike
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 Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC