943,632 Members | Top Members by Rank

Ad:
Nov 18th, 2008
0

Change DNS from DHCP to static in Ubuntu

Expand Post »
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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
EdTheUniqueGeek is offline Offline
20 posts
since Nov 2008
Nov 18th, 2008
0

Re: Change DNS from DHCP to static in Ubuntu

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
Reputation Points: 102
Solved Threads: 47
Posting Whiz
eggi is offline Offline
399 posts
since Oct 2007
Nov 18th, 2008
0

Re: Change DNS from DHCP to static in Ubuntu

Click to Expand / Collapse  Quote originally posted by eggi ...
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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
EdTheUniqueGeek is offline Offline
20 posts
since Nov 2008
Nov 18th, 2008
0

Re: Change DNS from DHCP to static in Ubuntu

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!
Reputation Points: 44
Solved Threads: 19
Junior Poster
Stylish is offline Offline
148 posts
since May 2007
Nov 18th, 2008
0

Re: Change DNS from DHCP to static in Ubuntu

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

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

Quote ...
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
Reputation Points: 102
Solved Threads: 47
Posting Whiz
eggi is offline Offline
399 posts
since Oct 2007
Nov 19th, 2008
0

Re: Change DNS from DHCP to static in Ubuntu

Thank you so much. I'll play around with that.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
EdTheUniqueGeek is offline Offline
20 posts
since Nov 2008
Nov 20th, 2008
0

Re: Change DNS from DHCP to static in Ubuntu

Cool,

I hope it works out for you

Best wishes,

Mike
Reputation Points: 102
Solved Threads: 47
Posting Whiz
eggi is offline Offline
399 posts
since Oct 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Shell Scripting Forum Timeline: newbie question - sorting a vertical list into a csv format
Next Thread in Shell Scripting Forum Timeline: executing a script remotely





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC