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.

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

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.

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!

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):

#!/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

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

Thank you so much. I'll play around with that.

Cool,

I hope it works out for you :)

Best wishes,

Mike

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.