How can I check dhcpd.conf against syntax error without running dhcpd?
Asked Answered
M

2

25

I need to make sure there are no syntax errors on dhcpd.conf. If there are errors, I want to get what they are.

I can check for syntax errors with this command:

dhcpd -cf /path/to/dhcpd.conf

but that prints a lot of information in addition to the error I got. Another thing is that I don't want to run dhcpd, even there is no syntax error. I only want to check for syntax errors and see what they are.


Unfortunately, running dhcpd -tf /path/to/dhcpd.conf also didn't solve my problem.

Marguritemargy answered 14/12, 2012 at 12:28 Comment(1)
WARNING: if you run the above command as root, you'll overwrite your existing dhcpd.conf! It writes the trace to that path!Dyspepsia
B
44

The syntax you are looking for is

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

The -t option will do a config check:

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

You do not need to use -cf if you are using the default config file path.

/usr/sbin/dhcpd -t

The one you tried with -tf /path/to/... is quite different and relates to tracing.

Baudoin answered 4/1, 2013 at 16:0 Comment(1)
yes, this command not runs dhcpd, but still print lots of things in addition to error. I want to print only what error it isMarguritemargy
C
1

One thing not on the manual page, and not covered here yet, 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 non 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 check if changes made to the configuration are valid before trying to restart the server with the new version.

Unfortunately I don't think there is any option to just display the errors. It would be possible to use a text parsing tool (awk, python etc) to remove the header lines (for the version I have, everything up to a line beginning with "For info"), and trailer lines (for the version I have, everything after a line saying "Configuration file errors encountered -- exiting") which would leave just the syntax error and location

Czechoslovakia answered 5/11, 2018 at 13:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.