Manually configuring DNS server - raspbian

2

I have written a simple script to allow a user to manually config their IP address and DNS server. The DNS servers are changed by creating a new /etc/resolv.conf file with the lines the user has entered, for example the file could end up looking like:

nameserver 12.34.56.78
nameserver 12.34.56.79

however after a reboot these changes seem not to work and using the DNS fails.

I can still ping IP address but trying to ping websites fail.

Below is the complete script, let know what you think the issue could be.

#!/bin/bash

# wipes any corrent set up
> /etc/network/interfaces

echo "Automatic DHCP or Manual?,(D/M) followed by [ENTER]:"
read network

if [ $network == "D" ]; then
    echo "auto lo" >> /etc/network/interfaces

    echo "iface lo inet loopback" >> /etc/network/interfaces
    echo "iface eth0 inet dhcp" >> /etc/network/interfaces
    echo "iface default inet dhcp" >> /etc/network/interfaces
    echo "Network set up!"
    exit 0
fi

if [ $network == "M" ]; then
    echo "Enter IP address (e.g 192.168.0.7), followed by [ENTER]:"
    read address
    echo "Enter Netmask (e.g 255.255.255.0, followed by [ENTER]:"
    read mask
    echo "Enter router IP (e.g 192.168.0.1), followed by [ENTER]:"
    read router
    echo "Enter first DNS server (e.g 8.8.8.8), followed by [ENTER]:"
    read dns1
    echo "Enter second DNS server (e.g 8.8.8.8), followed by [ENTER]:"
    read dns2

    echo "auto lo" >> /etc/network/interfaces
    echo "iface lo inet loopback" >> /etc/network/interfaces

    echo "iface eth0 inet static" >> /etc/network/interfaces
    echo "  address $address" >> /etc/network/interfaces    
    echo "  netmask $mask" >> /etc/network/interfaces
    echo "  gateway $router" >> /etc/network/interfaces

    echo "iface default inet dhcp" >> /etc/network/interfaces

    > /etc/resolv.conf
    echo "nameserver $dns1" >> /etc/resolv.conf
    echo "nameserver $dns2" >> /etc/resolv.conf

    echo "Network set up!"
    exit 0

fi

echo "ERROR: you do not enter D or M";
exit 0

The script was based on the information for manual configuration found here http://wiki.debian.org/NetworkConfiguration

When Automatic DHCP is used the /etc/resolv.conf contains:

domain zyxel.com
search zyxel.com
nameserver 192.168.1.1

Zac Powell

Posted 2013-06-19T19:32:01.677

Reputation: 477

Do you have network mangler, errh, manager installed? – tink – 2013-06-19T19:57:07.293

@tink users will have very limited access to the system so a network manager would not be best – Zac Powell – 2013-06-19T20:18:48.370

I appreciate that, which is why I asked :) It just frequently interferes with attempts to manually configure things. – tink – 2013-06-19T20:23:40.190

Ok so you think the script 'should' work, but something is possible interfering with the manual set up? The raspbian used is pretty standard so would likely include a network manager I am just not using it – Zac Powell – 2013-06-19T20:26:35.833

Note that http://raspberrypi.stackexchange.com and http://unix.stackexchange.com may help you find a more specific answer

– Tobias Kienzler – 2013-06-20T12:15:13.490

Thanks @TobiasKienzler it now seems If I enter the nameserver as the IP for the router and use it, it will work. But using other nameservers like say Googles it does not work – Zac Powell – 2013-06-20T12:22:00.343

Answers

0

The default resolvconf package makes /etc/resolv.conf into a symlink. If you remove the symlink and create a new resolv.conf it will stick after a reboot. You are very close with the > /etc/resolv.conf line, but apparently that doesn't overwrite the symlink. I would suggest first deleting the old symlink with rm /etc/resolv.conf (or better, back it up with mv /etc/resolv.conf /etc/resolv.conf.bak) just before that line.

ssmy

Posted 2013-06-19T19:32:01.677

Reputation: 1 250

No luck still doesn't work as before IP addresses work so I am connected but DNS server is not being used – Zac Powell – 2013-06-20T12:07:58.630