Guest's picture

Hi everybody,


I just started with this, but I would like to know if the following is possible:

I wish to run a few different virtual machines, using the great Turnkey distro. So far, so good, just installed VM Virtual Box, added a template, cloned and reset the mac address. Now I have a simple website (php) that asks me for the server name, IP address, Gateway, Primary DNS and Secondary DNS. After filling out the fields, it creates a script ( for me to download and place on the server. The purpose of this script will likely not be a surprise: set the given values for the server.

Is there any way to set the DNS servers from the Command Line ONLY? I have searched over 20 different websites and each and everyone of them tell me to edit the /etc/resolv.conf file. Since I want to minimize the user interaction, this is not the desired way. If I wish to add / change the Gateway, I can do a "ip route add default" / "route add default gw".

Can I run a similar command to set the Primary DNS and / or Secondary DNS?

Thanks in advance,

Jasmin Amarea

Jeremy Davis's picture

Sed allows you to use regular expressions to alter the contents of text files, which can be really handy when you're trying to do what you want to do. However TBH I'm not totally up on how to use sed so it'd be the blind leading the blind (everytime I want to do that I find an example online that is close and tweak it to do what I want - I still don't really get RegEx...).

Another option if you don't mind overwriting (i.e. deleting) the existing contents is to use ">" and just echo in the DNS server you want. E.g.:

echo "nameserver" > /etc/resolv.conf

Another way would be to have a generic resolv.conf stored somewhere and use your script to copy it over the top of the existing resolv.conf

Jeremy Davis's picture

But not one that I'm aware of.

Good luck

Login as the root, enter:
# vi /etc/resolv.conf
$ sudo vi /etc/resolv.conf
Modify or enter nameserver as follows:

Save and close the file. To test DNS configuration type any one of the following command:
$ host
$ dig
$ ping
$ nslookup


also check Linux chown commnad tutorial here

Add new comment