How to check syntax errors in dhcpd.conf beforfore restart or reload ?

  Linux

Before restarting or reload the service , we can check syntax errors with this command:

dhcpd -cf /path/to/dhcpd.conf

This prints a lot of information besides the error we may have.

dhcpd -t -cf /path/to/dhcpd.conf

The -t option will do a configuration check:

If the -t flag is specified, the server will simply test the configuration file for correct syntax, but not attempt to perform any network operations. This can be used to test the new configuration file automatically before installing it.

One thing that isn’t in the man page, and that hasn’t been covered here, is that the ‘/usr/sbin/dhcpd -t’ command uses the return value to indicate whether the configuration is correct or not.
If there are no errors, it will return zero. if there are syntax errors, it will return zero (1 for the test I did)

So you can use something like:

/usr/sbin/dhcpd -t
if [ $? -ne 0 ]; then
echo "Configuration has errors, aborting"
fi
/bin/systemctl restart isc-dhcp-server

To verify that the configuration changes made are valid before trying to restart the server with the new version.

Unfortunately there is no option to just display errors.