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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

David FavorLinux/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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Linux

From novice to tech pro — start learning today.