Page 1 of 1

Changing DNS nameservers by shell script

Posted: 13 Aug 2022, 21:11
by hope
I am using Porteus v5.0 in always fresh mode. Today I tried to change DNS nameservers by shell script. I did this :

Code: Select all

sudo -s<<NW
echo "nameserver 208.67.222.222" >>/etc/resolv.conf
echo nameserver 208.67.220.220" >>/etc/resolv.conf
nmcli networking off
nmcli networking on
NW
I checked whether DSN nameserver changed or not by :

Code: Select all

sudo nmcli
command as @Blaze's suggested.
The thing I noticed that, when I restart the networking service, DNS nameserver file resetted, in other words changes deleted.
But, when I did @Blaze's suggested ( in the link https://askubuntu.com/questions/2321/wh ... the-dns-ip by using GUI of network manager and changed DNS name servers) then, after "off & on" of "networking" service worked and changed DNS settings.

Therefore, I could not find a pure shell script solution to change DNS nameservers yet :unknown: . If anybody know, please share with us :sos:

Changing DNS nameservers by shell script

Posted: 17 Aug 2022, 22:04
by itrukrakso
Hi,
Even if the topic has already been discussed in chat, here would be my suggestion.
Why do you want to edit DNS name servers by using a shell script?
The entries of resolv.conf are generated by NetworkManager at startup.

Code: Select all

root@azer:head -n 1 /etc/resolv.conf
# Generated by NetworkManager

root@azer:nmcli conn show
NAME                UUID                                  TYPE      DEVICE
xyz              ...................................      wifi      wlan0
insert nameserver

Code: Select all

root@azer:nmcli conn modify "xyz" ipv4.dns  "208.67.222.222 208.67.220.220"
root@azer:nmcli networking off && nmcli networking on

copy the corresponding file /etc/NetworkManager/system-connections/xyz.nmconnection into the filesystem at boot time.
maybe it helps ?

Changing DNS nameservers by shell script

Posted: 21 Aug 2022, 17:18
by hope
Thanks @itrukrakso, of course it helps. I will try it and write the solution here :)
I try to do it by script because I am using Porteus from USB in always fresh mode ;)