| | |
Change DNS from DHCP to static in Ubuntu
![]() |
•
•
Join Date: Nov 2008
Posts: 14
Reputation:
Solved Threads: 0
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.
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.
•
•
Join Date: Oct 2007
Posts: 399
Reputation:
Solved Threads: 47
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
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!
------------------------------------------------------------------------
The greatest viral marketing idea of all time, get your copy of this Free Report now!
•
•
Join Date: Nov 2008
Posts: 14
Reputation:
Solved Threads: 0
•
•
•
•
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
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.
•
•
Join Date: Oct 2007
Posts: 399
Reputation:
Solved Threads: 47
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):
That would change this resolv.conf
to this
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
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)
#!/bin/bash sed '1,/^nameserver.*$/ {/^nameserver.*$/a\ nameserver 10.10.10.10 }' resolv.conf >resolv.tmp mv resolv.tmp resolv.conf sed '/^options.*/d' resolv.conf >>resolv.tmp echo "options retrans:1 retry:1" >> resolv.tmp 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
•
•
•
•
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
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!
------------------------------------------------------------------------
The greatest viral marketing idea of all time, get your copy of this Free Report now!
•
•
Join Date: Nov 2008
Posts: 14
Reputation:
Solved Threads: 0
Thank you so much. I'll play around with that.
•
•
Join Date: Oct 2007
Posts: 399
Reputation:
Solved Threads: 47
Cool,
I hope it works out for you
Best wishes,
Mike
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!
------------------------------------------------------------------------
The greatest viral marketing idea of all time, get your copy of this Free Report now!
![]() |
Other Threads in the Shell Scripting Forum
- Previous Thread: newbie question - sorting a vertical list into a csv format
- Next Thread: SSH that requires a password via a bash script
| Thread Tools | Search this Thread |





