Script to Change DNS on Linux servers to include new FQDN in all files

This is what I want to do, I have a script to check and add the new search DNS domain in /etc/resolv.conf, however I am trying to develop the script to do all of the below:

Check hostname, if using FQDN change to correct DNS name
check /etc/resolv.conf and remove old DNS search and nameservers, then put new nameservers in file.
check /etc/hosts, and remove old DNS domain name and put in new DNS domain name to server name.
check /etc/sysconfig/network, put full server name with new DNS appended to server short name.
check /etc/sysconfig/network-scripts/ifcfg-eth#, remove old DNS statements and put new in file.
service network restart

Thanks,
Peter
Peter MartinLinux AdminAsked:
Who is Participating?
 
David FavorConnect With a Mentor Linux/LXD/WordPress/Hosting SavantCommented:
Sounds like you may be attempting to replumb/redesign the cloud init system for containers.

Maybe give a bit more context about your specific application... like...

The purpose of all the above config file rewrites + also the target runtime environment - machine or LXD or Docker or some other VM.

Also mention your machine level OS + version, along with any container OS + version.

Keep in mind your OS + version will play a huge part in this, as Debian/Ubuntu has switched to netplan + likely many other Distros will follow, so keep this in mind, as any config changes you're talking about apply to netplan V1 (classic networking) + won't port to netplan V2.
_______

All this said, here's how I'd resolve your questions above.

1) check /etc/resolv.conf and remove old DNS search and nameservers, then put new nameservers in file.

Use the normal /etc/resolvconf/resolv.conf.d which already exists + will cause any changes to /etc/resolv.conf to be overwritten at various times, depending on many other system interactions.

2) check /etc/hosts, and remove old DNS domain name and put in new DNS domain name to server name.

Ugh... Don't do it... Use real DNS zone file configs, so hosts/domains resolve via global DNS.

3) check /etc/sysconfig/network, put full server name with new DNS appended to server short name.

Smells like RedHat...

The entire /etc/sysconfig system is ugly. I'd avoid mucking with it at all.

If you do muck around with it, each time the format changes in any minor way (RedHat tool changes), your modification system will break, so if you do this, you'll have to track all upstream bugs related to this facility + change your code, whenever config format patches publish.

I personally won't touch any complex file built by system level tools.

4) check /etc/sysconfig/network-scripts/ifcfg-eth#, remove old DNS statements and put new in file.

Ugh... Don't do it...

The /etc/resolvconf/resolv.conf.d conf system already handles this. Use this facility, if you must muck about with this.
_______

The way I avoid all this nonsense is run LXD containers + then completely disable resolvconf + populate containers with a custom dnsmasq config, so each container has a partitioned network setup.

This means /etc/network/interfaces + dnsmasq config file describes the entire networking config for each container, so debugging any problems takes seconds.
1
 
Peter MartinLinux AdminAuthor Commented:
David,

Thanks for your reply, and yes I see that the Network Interface setup on most servers has DNS nameservers, ect.  I was thinking about why this would be the case, but now understand this would be the way to go.  

Best Regards,
Peter Martin
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.