Where to set manual DNS with vpn boot script?

First off all, please forgive my ignorance.
I am a hobbyist self learned programmer. Patching scripts together and reading manuals is the way I learn to code.
I hope you understand

I have troubles setting a manual DNS. The vpn service I use has a custom vpn which i want to use.
I tried “sudo nano /etc/resolv.conf” but permission was denied. The read out is:

# Generated by Connection Manager
search home 
nameserver 208.67.220.220
nameserver 208.67.222.222
nameserver 8.8.8.8
nameserver 8.8.4.4

So i see that also the dns from the ovpn is ignored.
My preferred setup is a rpi running transmission behind a vpn. VPN start immeadlty after boot and kills transmission when the connection is lost. At this moment I have somewhat of a working situation.

I use this script in rc.local

cd /etc/openvpn
sudo openvpn --config /etc/openvpn/nl14.ovpn --auth-user-pass /etc/openvpn/login.txt --script-security 2 --up /etc/openvpn/torrentvpn.sh --down /etc/openvpn/torrentvpnstop.sh

and then this script for starting the vpn. I am afraid it might not be fully compatible with osmc. For the reason I am not able to set the DNS (tee command?)

#!/bin/sh

#Script to modify the transmission settings to bind to the VPN IP
#HTPCGuides.com

#enter the /etc/openvpn directory
cd /etc/openvpn

#stop the transmission-daemon
systemctl stop transmission

#Plug the VPN IP into transmission, may need to change $ifconfig_local to $ifconfig_remote
sed -i 's/.*"bind-address-ipv4":.*/    "bind-address-ipv4": \"'$ifconfig_local'\",/' /home/osmc/.config/transmission-daemon/settings.json

#add Open DNS servers, change to Google's 8.8.8.8 and 8.8.4.4 if you like
echo "nameserver 146.148.119.121" | tee -a /etc/resolv.conf
echo "nameserver 104.130.169.74" | tee -a /etc/resolv.conf

# add routing to retain remote access, use either the manual variables or automatic ones not both
MAINIP=192.168.2.190
GATEWAYIP=192.168.2.254
SUBNET=192.168.2.0/24

#automatic IP finding
#MAINIP=$(ip route | awk 'NR==3{print $9}')
#GATEWAYIP=$(ip route | awk 'NR==1{print $3}')
#SUBNET=$(ip route | awk 'NR==2{print $1}')

ip rule add from $MAINIP table 128
ip route add table 128 to $SUBNET dev wlan0
ip route add table 128 default via $GATEWAYIP

#start the transmission.daemon
systemctl restart transmission

Hope anyone can point me in the right direction.
Also setting DHCP to manual in the OSMC GUI didn’t work for setting a manual dns.

Update:

Ok, I am really confused now. Apparently the rc.local script is not running. However, the vpn is initiated at boot. When I do an ip check, the vpn ip is shown. Maybe I have set another run-at-boot method, but I can’t remember what.

what i did to set my manual was this

  1. created a script name it what ever you want

  2. add this to the script

    #!/bin/bash
    case “$script_type” in
    up)
    mv /etc/resolv.conf /etc/resolv.conf.orig
    echo “nameserver 198.18.0.1” > /etc/resolv.conf
    ;;
    down)
    cp -a /etc/resolv.conf.orig /etc/resolv.conf
    ;;
    esac

  3. change the 198.18.0.1 to what ever you want your dns to be (that ip is for IPVanish)

  4. go to your openvpn config files and add this at the end

  1. change the /home/vpn/updatedns.sh to where ever you saved the script and the name of it.
  2. run openvpn, for me i type openvpn --config /home/vpn/miami3.ovpn
  3. now you should be able to remote back in and do nano /etc/resolv.conf and your dns should be saved as what you set in the openvpn server .ovpn.
1 Like

Thanks for your input. I applied the script as suggested.
I ran into this problem, it is familiar, I ran into it before I remember now when trying to manually modify resolv.conf.

mv: cannot move '/etc/resolv.conf' to '/etc/resolv.conf.orig': Operation not permitted
/etc/openvpn/updatedns.sh: line 5: /etc/resolv.conf: Permission denied
Fri Feb  3 16:35:41 2017 WARNING: Failed running command (--up/--down): external program exited with error status: 1
Fri Feb  3 16:35:41 2017 Exiting due to fatal error

So I do not have permission to alter resolve.conf

sudo mv /etc/resolv.conf /etc/resolv.conf.orig

sudo to elevate your privelidges temporarly

when creating that script use chmod ugo+x

I have edited the script to:

#!/bin/bash
    case "$script_type" in
      up)
            sudo mv /etc/resolv.conf /etc/resolv.conf.orig
            echo "nameserver 146.148.119.121" > sudo /etc/resolv.conf
            ;;
      down)
            sudo cp -a /etc/resolv.conf.orig /etc/resolv.conf
            ;;
    esac
  1. and performed: sudo chmod ugo+x /etc/openvpn/updatedns.sh
  2. sudo openvpn --config /etc/openvpn/nl14.ovpn

unfortunately I still get the error

Fri Feb  3 17:15:57 2017 /etc/openvpn/updatedns.sh tun0 1500 1590 10.8.8.238 10.8.8.237 init
sudo: mv: command not found
/etc/openvpn/updatedns.sh: line 5: /etc/resolv.conf: Permission denied
Fri Feb  3 17:15:57 2017 WARNING: Failed running command (--up/--down): external program exited with error status: 1
Fri Feb  3 17:15:57 2017 Exiting due to fatal error

If you guys would properly use the </> preformatted text feature (and not trying to “quote” the text) for you scripts in the post editor, your code would print to this thread in an appropriate and usable fashion. Just a hint…

1 Like

i did everything under root so sudo wasn’t needed for me

thanks for the heads up

you are right.
However, for some reason, the editor doesn’t read my lines properly…

it had something to do with the enumerating.

Edit the post. Completely remove the script text. Paste a fresh copy of the text again, r-click select all of the script text, press the </> button.

1 Like

Yeah I don’t know why i get that when using the updatedns.sh script. update-resolve-conf does not give conflict.

  1. I tried something else. I added these lines for my preferred dns server to the ovpn file:

dhcp-option DNS 146.148.119.121
dhcp-option DNS 104.130.169.74

2.When I initiate the vpn, I get this:

Fri Feb  3 17:33:09 2017 /etc/openvpn/update-resolv-conf tun0 1500 1590 10.8.8.238 10.8.8.237 init
dhcp-option DNS 146.148.119.121
dhcp-option DNS 104.130.169.74
dhcp-option DNS 78.46.223.24
dhcp-option DNS 162.242.211.137
/etc/resolvconf/update.d/libc: Warning: /etc/resolv.conf is not a symbolic link to /etc/resolvconf/run/resolv.conf

3.still this is wat returns when I perform: cat /etc/resolv.conf

# Generated by Connection Manager
search home 
nameserver 208.67.220.220
nameserver 208.67.222.222
nameserver 8.8.8.8
nameserver 8.8.4.4

should be

sudo echo “nameserver 146.148.119.121” > /etc/resolv.conf

1 Like

Looking at update-resolve-conf I noticed you have to first CP and then MV at the end of the script. Thus, I managed to get not errors using this script:

#!/bin/bash
    case "$script_type" in
      up)
            cp /etc/resolv.conf /etc/resolv.conf.default
            echo "nameserver 146.148.119.121" > sudo /etc/resolv.conf
            ;;
      down)
            mv -a /etc/resolv.conf.default /etc/resolv.conf
            ;;
    esac

If I use sudo in front instead. I get the permission error for /etc/resolv.conf again:

sudo echo “nameserver 146.148.119.121” > /etc/resolv.conf

Although, I am still not confident the correct DNS is being used.

cat /etc/resolv.conf

still shows the opendns and google dns instead of the vpn dns or manual dns.

i apologise for my ignorance. I can understand this might be basic stuff for some of you guys

UPDATE:

Now that I re-evaluate my steps. I believe the problem is that the manual/vpn DNS server is not written to /etc/resolv.conf.

Setting a manual dns by putting this line in the ovpn config file seems to work ok:

dhcp-option DNS 146.148.119.121

Then this dns is pushed by updat-resolve-conf automatically. But in the end not written to /etc/resolv.conf. How can i know which DNS is used?

My guess why it works with > sudo /etc/resolv.conf is that you now have a file named sudo in your home dir?

This should absolutly work:

sudo bash -c ‘echo “nameserver 146.148.119.121” > /etc/resolv.conf’

at least it does on my machine =)

1 Like

I greatly appreciate your contribution!

I do get this error

Fri Feb  3 19:10:29 2017 /etc/openvpn/updatedns.sh tun0 1500 1590 10.8.8.238 10.8.8.237 init
sudo: bash: command not found

Also just putting it straight into the terminal gives:

osmc@osmc:/etc/openvpn$ sudo bash -c 'echo "nameserver 146.148.119.121" > /etc/resolv.conf'
bash: /etc/resolv.conf: Permission denied

Maybe I am better off doing a fresh OSMC install on my rpi2.

last try:

sudo /bin/bash -c ‘echo “nameserver 146.148.119.121” > /etc/resolv.conf’

Dont understand why you cant start bash

1 Like

PATH is probably mangled.

Just do

echo ‘nameserver 146.148.119.121’ | sudo tee /etc/resolv.conf

Be aware this is not the proper way to do this and ConnMan will overwrite it shortly afterwards. Correct solution is to configure OpenVPN via ConnMan.

1 Like

I keep getting

osmc@osmc:~$ sudo /bin/bash -c 'echo "nameserver 146.148.119.121" > /etc/resolv.conf'
/bin/bash: /etc/resolv.conf: Permission denied

are these permission issues the normal behaviour of OSMC?

Ones again, thank you for your time to help me out!

Hi thank you for you input!

echo “nameserver 146.148.119.121” | sudo tee /etc/resolv.conf

and

sudo echo “nameserver 209.222.18.222” | sudo tee /etc/resolv.conf

both return

tee: /etc/resolv.conf: Permission denied
nameserver 146.148.119.121

If ConnMan will overwrite everything again in the same session, then this might not be a sustainable solution in the end. I would like to read up how to configure OpenVPN via Connman.