检查 Linux / Unix 配置文件是否存在语法错误
在Linux 和 UNIX 系统中,服务是使用位于目录树中的各种文本文件进行配置的/etc/。/usr/local/etc/典型的服务器系统可能有几十个配置文件。您可以在不启动服务器的情况下检查配置文件中的语法错误并验证所有设置。在某些情况下,可以检查特定数据/配置选项和目录的完整性。文本文件更易于远程管理。您可以使用 ssh 和文本编辑器。如果配置有错误,服务器可能无法启动。这可能会导致灾难。这篇文章解释了如何快速找出流行服务器的语法错误并测试配置文件中的语法错误。
教程详细信息 | |
---|---|
难度等级 | 简单的 |
Root 权限 | 是的 |
要求 | Linux 或 Unix 终端 |
类别 | 系统管理 |
操作系统兼容性 | BSD • Linux • macOS • Unix |
预计阅读时间 | 10 分钟 |
停止!在重新启动 Unix 和 Linux 服务之前,请测试 Linux / Unix 服务器配置文件是否存在语法错误
Linux 或 Unix 服务器的问题在于,最小的拼写错误(例如配置文件中缺少分号或空格)都可能造成最大的损失。
- 以下命令行选项不会运行守护进程或停止运行服务器服务;它只会测试配置文件然后退出。
- 它将检查配置的语法是否正确,然后尝试打开配置中引用的文件。
- 服务器将解析配置文件并检查其语法错误,以及所有包含配置文件的文件。
- 在大多数情况下,您还可以指定服务器应使用哪个配置文件来代替默认配置文件。
- 验证配置文件并修复所有错误后,您可以继续重新加载或重新启动服务器。
关于重新加载服务器的说明
在 Linux 下,语法如下,具体取决于您的Linux 发行版和初始化系统(例如 systemd):
reload 选项会重新加载配置文件,而不会中断待处理的操作。例如,以下命令将在配置文件更改后重新加载 Apache Web 服务器:
或但是,大多数 Linux 和类 Unix 守护程序有时会使用 SIGHUP 作为信号来重新启动自身,最常见的原因是重新读取已更改的配置文件。 cat 命令和kill 命令
的语法如下:
或
让我们看看如何测试各种 Linux 和 Unix 服务的语法。
# /sbin/service SERVICE-NAME [reload|restart]
# /etc/init.d/SERVICE-NAME [reload|restart]
# systemctl reload SERVICE-NAME-HERE
# /sbin/service httpd reload
# systemctl reload httpd
# kill -HUP $(cat /var/run/SERVICE.pid)
# kill -HUP `cat /var/run/SERVICE.pid`
1.OpenSSH 服务器
您可以使用以下语法来测试 OpenSSH 配置文件,类型:
# /usr/sbin/sshd -t && echo $?
示例配置错误会话:
# usr/sbin/sshd -t
示例输出:
/etc/ssh/sshd_config line 26: Bad yes/without-password/forced-commands-only/no argument: Naa
要打印第26 行,请输入:
# sed -n '26p' /etc/ssh/sshd_config
示例输出:
PermitRootLogin Naa
使用文本编辑器(如 vi)编辑该文件,输入:
# vi +26 /etc/ssh/sshd_config
最后更新语法,输入:
PermitRootLogin No
保存并关闭文件。再次测试:
# /usr/sbin/sshd -t
OpenSSH 扩展测试模式
使用 -T 选项检查配置文件的有效性,将有效配置输出到 stdout(屏幕)然后退出:
# /usr/sbin/sshd -T
另请参阅:Openssh 服务器安全最佳实践以获取更多信息。
2. Apache Web 服务器
语法如下,仅对配置文件运行语法测试:
# /usr/sbin/apache2 -t
错误报告示例:
apache2: Syntax error on line 50 of /etc/apache2/apache2.conf: ServerRoot must be a valid directory
在 RHEL 和朋友上,输入:
# /usr/sbin/httpd -t
示例输出:
Syntax OK
您还可以使用 apachectl 命令(传递 configtest 或 -t 选项)。它将运行配置文件语法测试。它解析配置文件并报告 Syntax Ok 或有关特定语法错误的详细信息:
# apachectl configtest
或
# apachectl -t
重新加载 Apache 服务器,输入:
# apachectl -k graceful
3. Nginx Web 服务器
要运行 nginx 配置文件的语法测试,请输入:
示例输出:
# /usr/local/nginx/sbin/nginx -t
# /sbin/nginx -t -c /etc/nginx/nginx.conf
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
在哪里,
- -c /path/to/file:指定 Nginx 应该使用哪个配置文件来代替默认配置文件。
- -t:仅测试配置文件。
另请参阅:如何将 nginx 设置为反向代理服务器以及20 条 nginx 最佳安全实践以获取更多信息。
4. Lighttpd Web 服务器
要测试配置文件并退出,请输入:
# /usr/local/sbin/lighttpd -t -f /usr/local/etc/lighttpd/example.com/lighttpd.conf
示例输出:
Syntax OK
在哪里,
- -f filename:使用配置文件的文件名。
- -t:测试配置文件。
5. BIND(命名)DNS服务器
使用named-checkconf命令检查语法,但不检查语义。该文件及其包含的所有文件都会被解析并检查语法错误。
# named-checkconf /etc/named.conf
您还可以检查绑定区域文件,输入:
# named-checkzone example.com /var/named/zone.example.com
6. Squid 代理服务器
要解析和测试配置文件,请输入:
示例输出:
# /usr/sbin/squid -k check
# /usr/sbin/squid -k parse
2012/03/30 07:44:35| Processing Configuration File: /etc/squid/squid.conf (depth 0) 2012/03/30 07:44:35| Initializing https proxy context
7. MySQL(mysqld)数据库服务器
输入以下命令:
# mysqld --verbose --help
以上命令会产生过多的输出。我建议将输出重定向到 /dev/null,并在屏幕上仅显示错误/警告
# /usr/libexec/mysqld --verbose --help 1>/dev/null
示例输出:
120330 7:52:43 [Warning] '--log_slow_queries' is deprecated and will be removed in a future release. Please use ''--slow_query_log'/'--slow_query_log_file'' instead.
您可以指定一个新的配置文件例如/root/test-my.cnf
# mysqld --defaults-file=/root/test-my.cnf --verbose --help 1>/dev/null
8. Postfix 邮件服务器(MTA)
使用以下语法。要警告错误的目录/文件所有权或权限,并创建缺失的目录,请输入:
# postfix check
或
# postfix -vvv
示例输出:
postfix: dict_register: mail_dict 1 postfix: dict_update: config_directory = /etc/postfix postfix: dict_update: queue_directory = /var/spool/postfix postfix: dict_update: command_directory = /usr/sbin postfix: dict_update: daemon_directory = /usr/libexec/postfix postfix: dict_update: data_directory = /var/lib/postfix postfix: dict_update: mail_owner = postfix postfix: dict_update: inet_interfaces = localhost postfix: dict_update: inet_protocols = all postfix: dict_update: mydestination = $myhostname, localhost.$mydomain, localhost postfix: dict_update: unknown_local_recipient_reject_code = 550 postfix: fatal: /etc/postfix/main.cf, line 385: missing '=' after attribute name: "sss"
您可以在 maillog 日志文件中看到错误,输入:
# tail -f /var/log/maillog
示例输出:
And it'll run mysqld (or drizzled), parse tMar 30 08:01:34 mx421 postfix[2284]: dict_update: command_directory = /usr/sbin Mar 30 08:01:34 mx421 postfix[2284]: dict_update: daemon_directory = /usr/libexec/postfix Mar 30 08:01:34 mx421 postfix[2284]: dict_update: data_directory = /var/lib/postfix Mar 30 08:01:34 mx421 postfix[2284]: dict_update: mail_owner = postfix Mar 30 08:01:34 mx421 postfix[2284]: dict_update: inet_interfaces = localhost Mar 30 08:01:34 mx421 postfix[2284]: dict_update: inet_protocols = all Mar 30 08:01:34 mx421 postfix[2284]: dict_update: mydestination = $myhostname, localhost.$mydomain, localhost Mar 30 08:01:34 mx421 postfix[2284]: dict_update: unknown_local_recipient_reject_code = 550 Mar 30 08:01:34 mx421 postfix[2284]: fatal: /etc/postfix/main.cf, line 385: missing '=' after attribute name: "sss" Mar 30 08:01:42 mx421 postfix[2285]: fatal: /etc/postfix/main.cf, line 385: missing '=' after attribute name: "sss"he config, report any problems, print help, and exit without initializing storage engines or trying to
9. Samba(SMB/CIFS)文件服务器
键入以下命令:
# testparm -v
10. tcpd
可以设置 tcpd 程序来监控 telnet、finger、ftp、exec、rsh、rlogin、tftp、talk、comsat 和其他与可执行文件一一映射的服务的传入请求。tcpdchk 命令检查您的 tcp 包装器配置并报告它能找到的所有潜在和实际问题:
其中,
# tcpdchk
# tcpdchk -a
# tcpdchk -d
# tcpdchk -i /path/to/inetd.conf
# tcpdchk -v
- -a:报告允许访问而无需明确 ALLOW 关键字的访问控制规则。
- -d :检查当前目录中的 hosts.allow 和 hosts.deny 文件,而不是默认目录下的 hosts.allow 和 hosts.deny 文件。
- -i inet_conf:当 tcpdchk 无法找到您的 inetd.conf 网络配置文件,或者您怀疑该程序使用了错误的配置文件时,请指定此选项。
- -v:显示每个访问控制规则的内容。守护程序列表、客户端列表、shell 命令和选项都以漂亮的打印格式显示;这让您更容易发现您想要的内容和程序理解的内容之间的任何差异。
11. dhcpd 服务器
互联网系统联盟 DHCP 服务器 dhcpd 实现了动态主机配置协议 (DHCP) 和互联网引导协议 (BOOTP)。DHCP 允许 TCP/IP 网络上的主机请求和分配 IP 地址,并发现有关它们所连接的网络的信息。BOOTP 提供类似的功能,但有一定的限制。要测试语法,请传递以下选项:
# dhcpd -t
或
# dhcpd -t -cf /path/to/dhcpd.testing.conf
或
# dhcpd -T
或
# dhcpd -T -lf /path/to/dhcpd.lease.file
在哪里,
- -t:指定 -t 标志后,服务器将仅测试配置文件的语法是否正确,但不会尝试执行任何网络操作。这可用于在安装新配置文件之前自动测试它。
- -T:此标志可以用来以类似的方式测试租约数据库文件。
- -cf /path/to/dhcpd.testing.conf:使用名为 /path/to/dhcpd.testing.conf 的备用配置文件。
- -lf /path/to/dhcpd.lease.file:名为 -lf /path/to/dhcpd.lease.file 的备用租约文件
12. vsftpd FTP 服务器
vsftpd 是“非常安全的文件传输协议”守护程序。使用以下命令确保配置文件没有语法错误:
# vsftpd
或
# vsftpd -olisten=NO /path/to/vsftpd.testing.conf
该示例将 vsftpd 的内置默认“listen”选项覆盖为 NO,然后从 /path/to/vsftpd.testing.conf 加载设置。
13. Nagios
Nagios is a popular open source computer system monitor, network monitoring and infrastructure monitoring software application. Use the following syntax to run a sanity check on nagios.cfg as follows:
# nagios -v /path/to/testing/nagios.cfg
Where,
- -v : Verify your configuration.
14. Openntpd Server
The ntpd daemon synchronizes the local clock to one or more remote NTP servers or local timedelta sensors. ntpd can also act as an NTP server itself, redistributing the local time. Use the following syntax to run a sanity check on ntpd.conf:
# ntpd -n
# ntpd -f /usr/local/etc/ntpd.conf -n
# ntpd -d -f /usr/local/etc/ntpd.conf -n
Where,
- -n : Only check the configuration file for validity.
- -f /usr/local/etc/ntpd.conf : Use /usr/local/etc/ntpd.conf file as the configuration file, instead of the default /etc/ntpd.conf.
- -d : Do not daemonize and ntpd will run in the foreground and log to stderr.
15. Xorg – The X11 Server
Linux and Unix like operating systems uses X11 to provide users with a powerful graphical user interface. X11 is a freely available version of the X Window System that is implemented in Xorg. The default xorg.conf is located in /etc/X11 directory. You can build an initial configuration file by simply running the following command:
# Xorg -configure
To test the existing configuration to verify that Xorg can work with the graphics hardware on the target system, enter:
# Xorg -config /path/to/xorg.conf.new -retro
Please note that the new Xorg is largely self-configuring and doesn’t need one. But, if you use proprietary (say Nvidia) or other drives you need to test Xorg syntax using the above described method.
16. syslogd / rsyslogd
syslogd is Unix / Linux system logging server. rsyslogd is reliable and extended syslogd for modern Linux distros. Rsyslogd is derived from the sysklogd package which in turn is derived from the stock BSD sources. To check for syntax error, type:
# syslogd -f /etc/rsyslog.testing.conf -d
OR
# rsyslogd -c4 -f /etc/rsyslog.testing.conf -N 1
Sample outputs:
rsyslogd: version 4.6.4, config validation run (level 1), master config /etc/rsyslog.conf rsyslogd: invalid or yet-unknown config file command - have you forgotten to load a module? [try http://www.rsyslog.com/e/3003 ] rsyslogd: the last error occured in /etc/rsyslog.conf, line 11:"$FilesOnwer root" rsyslogd: CONFIG ERROR: could not interpret master config file '/etc/rsyslog.testing.conf'. [try http://www.rsyslog.com/e/2124 ]
A sanity check without any errors:
rsyslogd: version 4.6.4, config validation run (level 1), master config /etc/rsyslog.testing.conf rsyslogd: End of config validation run. Bye.
Where,
- -c4 : Selects the desired backward compatibility mode (in this example it is # 4).
- -f /etc/rsyslog.testing.conf : Specify an alternative configuration file instead of /etc/rsyslog.conf, which is the default.
- -d : Debug mode (only use with syslogd)
- -N 1 : Do a config file check. Do NOT run in regular mode, just check configuration file correctness. This option is meant to verify a config file. The level argument modifies behaviour. Currently, 0 is the same as not specifying the -N option at all (so this makes limited sense) and 1 actually activates the code. Later, higher levels will mean more verbosity (this is a forward-compatibility option).
17. CUPS Printing System
CUPS is the standards-based, open source printing system developed by Apple, for Mac OS X and other UNIX/Linux-like operating systems. cupsd is the scheduler for CUPS. It implements a printing system based upon the Internet Printing Protocol, version 2.1. To test config files for error type:
# cupsd -f -c /path/to/cupsd.testing.conf -t
Sample outputs:
Unknown directive Loggslevel on line 6. /etc/cups/cupsd.conf is OK
A sanity check without any errors:
/etc/cups/cupsd.conf is OK
Where,
- -f : Run cupsd in the foreground; the default is to run in the background as a “daemon”.
- -c /path/to/cupsd.testing.conf : Uses the /path/to/cupsd.testing.conf configuration file.
- -t :
Test the configuration file for syntax errors.
18. slapd Stand-alone LDAP Daemon
OpenLDAP Software is a free, open source implementation of the Lightweight Directory Access Protocol (LDAP) developed by the OpenLDAP Project. To test whether the configuration file is correct or not, type:
# slapd -Tt
OR
# slapd -Tt -f /path/to/slapd-config.conf
Where,
- -Tt : Test for config file errors.
- -f /path/to/slapd-config.conf : Specifies the slapd configuration file. The default is /etc/openldap/slapd.conf.
19. varnishd - HTTP Accelerator Daemon
To test varnishd vlc syntax, enter:
# varnishd -C -f /path/to/wordpress.vlc
Where,
- -C : Print VCL code compiled to C language and exit. Specify the VCL file to compile with the -f option.
- -F /path/to/wordpress.vlc : Use the specified VCL configuration file instead of the builtin default.
20. exim MTA
Pass the -bV option to exim. If there are any errors in the configuration file, Exim outputs error messages. Otherwise it outputs the version number and build date, the DBM library that is being used, and information about which drivers and other optional code modules are included in the binary.
# exim -bV
Some simple routing tests can be done by using the address testing option. For example,
# exim -bt <local username>
Admin users can test the malware scanning configuration (in Exim >= 4.73) with the -bmalware option:
# exim -bmalware <filename>
Another source of information is running Exim with debugging turned on, by specifying the -d option. If a message is stuck on Exim's spool, you can force a delivery with debugging turned on by a command of the form
# exim -d -M <exim-message-id>
21. monit
Pass the -t option:
# monit -t
Sample outputs:
Control file syntax OK
22. dnscrypt-proxy
Pass the -check and -config /path/to/config_file.toml option:
# dnscrypt-proxy -check
# dnscrypt-proxy -check -config /etc/dnscrypt-proxy/dnscrypt-proxy.toml
Here is what I see:
[2023-06-13 20:06:26] [NOTICE] dnscrypt-proxy 2.1.4 [2023-06-13 20:06:27] [NOTICE] Source [public-resolvers] loaded [2023-06-13 20:06:30] [NOTICE] Source [relays] loaded [2023-06-13 20:06:30] [NOTICE] Configuration successfully checked
See How to install dnscrypt proxy with adblocker on Linux for more info.
23. sudo command
Please ensure to inspect the sudo file for configuration and other errors using the following steps. Pass the -c to the visudo command:
$ sudo visudo -c
Outputs:
/etc/sudoers: parsed OK /etc/sudoers.d/README: parsed OK /etc/sudoers.d/adminuserpermissions: bad permissions, should be mode 0440
Use the chmod command to fix permission issues as follows:
$ sudo chmod -v 0440 /etc/sudoers.d/adminuserpermissions
24. Bonus tips
Some additional tips:
Bash / KSH Shell Scripts
It is possible to check a bash script syntax without executing it:
$ bash -n ./myscript
Sample outputs:
./myscript<: line 16: syntax error near unexpected token `fi' ./myscript<: line 16: `fi'
OR
$ ksh -n /path/to/backup.ksh
See how to debug a shell script under Linux or UNIX for more information.
FreeBSD/OpenBSD/macOS pf Firewall
The pfctl command used to control the pf firewall and NAT devices under *BSD operating system including Apple OS X. The syntax is as follows to test for errors:
# pfctl -nf /etc/pf.conf
Where,
- -n : Do not load rules from /path/to/pf.testing.conf, just parse them.
- -f /path/to/pf.testing.conf : Load the firewall rules contained in a file called /path/to/pf.testing.conf.
Command Summary
Daemon | Command(s) |
---|---|
OpenSSH | /usr/sbin/sshd -t && echo $? /usr/sbin/sshd -T |
Apache | /usr/sbin/apache2 -t apachectl configtest |
nginx | nginx -t /sbin/nginx -t -c /etc/nginx/nginx.conf |
lighttpd | lighttpd -t -f /etc/lighttpd/lighttpd.conf |
绑定(命名服务器配置) | named-checkconf /etc/named.conf |
绑定(区域语法) | 命名检查区域 example.com /var/named/zone.example.com |
Squid 代理 | /usr/sbin/squid -k 检查 /usr/sbin/squid -k 解析 |
MySQL 服务器 | mysqld --verbose --help /usr/libexec/mysqld --verbose --help 1>/dev/null |
Postfix MTA | postfix 检查 postfix -vvv |
Samba SMB/CIFS | 测试参数-v |
tcpd | tcpdchk tcpdchk -v |
dhcpd(DHCP / BOOTP)服务器 | dhcpd -t -cf /路径/到/dhcpd.testing.conf |
vsftpd 服务器 | vsftpd -olisten=NO /path/to/vsftpd.testing.conf |
纳吉奥斯 | nagios -v /path/to/testing/nagios.cfg |
Openntpd NTPD 服务器 | ntpd -d -f /usr/local/etc/ntpd.conf -n |
Xorg(X11 服务器) | Xorg -config /path/to/xorg.conf.new -retro |
syslogd/rsyslogd | rsyslogd -c4 -f /etc/rsyslog.testing.conf -N 1 |
CUPS 打印系统 | cupsd -f -c /路径/到/cupsd.testing.conf -t |
slapd(OpenLDAP) | slapd-Tt |
清漆 | varnishd -C -f /路径/到/wordpress.vlc |
进出口贸易管理局 | 进出口 |
Bash/Ksh 脚本 | bash -n ./myscript ksh -n /path/to/script.ksh |
BSD pf 防火墙 | pfctl -nf /etc/pf.conf |
proftpd | proftpd -t -c /路径/到/proftpd.testing.conf |
Perl 脚本 | perl -c /路径/到/script.pl perl -wc /路径/到/script.pl |
监控 | 监控 |
dnscrypt-代理 | dnscrypt-proxy -check \ -config /etc/dnscrypt-proxy/dnscrypt-proxy.toml |
须藤 | sudo visudo -c |
参见
使用 man 命令或 help 命令阅读文档。例如:
$ man sshd
$ server_name --help
$ nginx -?
$ nginx -h | grep test
结论
我希望这些技巧对你有用。你有一个最喜欢的命令来检查 Unix / Linux / *BSD 服务器配置文件中是否存在语法错误和警告?让我们在评论中听听你的看法。