0

I know that I can use my chosen DNS by adding the following line to /etc/dhcp/dhclient.conf:

$ supersede domain-name-servers 1.1.1.1, 1.0.0.1;

I can verify that the correct DNS is being used as follows:

$ nmcli dev show | grep DNS
IP4.DNS[1]:        1.1.1.1
IP4.DNS[2]:        1.0.0.1

But I'd like to automate the setup of my machine (Ubuntu 18.04), and therefore want to avoid editing /etc/dhcp/dhclient.conf directly. The "simple" solution would be to append to the file:

echo "supersede domain-name-servers 1.1.1.1, 1.0.0.1;" | sudo tee -a /etc/dhcp/dhclient.conf

But this has the disadvantage that the line will be duplicated if the setup script is run twice. Is there an established way for doing this?

  • 1
    use sed to delete any matching line, then add it. or use grep to check for the line, and don't add it if found. – Patrick Apr 28 '18 at 4:26
  • Thanks, @Patrick--please see my update. While I was looking for a standard interface, your suggestion does the job. If you'd like to add it as an answer, I'd be happy to accept it. Thanks for your time. – sudo make install Apr 29 '18 at 11:54
  • This WWW site does not do answers in questions (or "[SOLVED]" and the like). It does allow one to answer one's own question, in contrast. Please put your answer in an answer. – JdeBP Apr 30 '18 at 7:26
  • @JdeBP Sounds good! I moved the edit to a standalone answer. Thanks for clarifying the etiquette. – sudo make install May 3 '18 at 0:22
0

Per @Patrick's comment, you could do something like this:

LINE='supersede domain-name-servers 1.1.1.1, 1.0.0.1;'
FILE=/etc/dhcp/dhclient.conf

grep -qF "${LINE}" "${FILE}" || echo ${LINE} | sudo tee --append "${FILE}"

I was hoping/expecting that there would be a standard interface through dhclient or similar, but if it works, it works. :-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.