如何找出程序读取的配置文件

本文介绍了两种方法找出程序读取的配置文件。第一种是通过查看程序的手册页,特别是man手册的特定章节。第二种是利用strace命令跟踪进程的系统调用,找出打开的配置文件。在archlinux系统中,无需额外安装工具,但strace能揭示比手册更全面的信息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

原文地址:https://lujun9972.github.io/blog/2020/04/24/如何找出程序读取的配置文件/index.html

解决方案一:查看manual手册

涉及到的命令: man

本部分以archlinux系统为例

安装

无需安装

解决方案

man手册中有一个 FILES 章节专门用来提供应用相关文件的说明,其中通常都会包含应用读取的配置文件说明。

比如,我们来看看gvim的文件说明吧:

man gvim |sed -n '/^FILES/,/^[^ ]/p'
FILES
       /usr/share/vim/vim82/doc/*.txt
                      The  Vim documentation files.  Use ":help doc-file-list"
                      to get the complete list.

       /usr/share/vim/vim82/doc/tags
                      The tags file used for finding information in the  docu‐
                      mentation files.

       /usr/share/vim/vim82/syntax/syntax.vim
                      System wide syntax initializations.

       /usr/share/vim/vim82/syntax/*.vim
                      Syntax files for various languages.

       /etc/vimrc     System wide Vim initializations.

       ~/.vimrc       Your personal Vim initializations.

       /etc/gvimrc    System wide gvim initializations.

       ~/.gvimrc      Your personal gvim initializations.

       /usr/share/vim/vim82/optwin.vim
                      Script  used  for  the ":options" command, a nice way to
                      view and set options.

       /usr/share/vim/vim82/menu.vim
                      System wide menu initializations for gvim.

       /usr/share/vim/vim82/bugreport.vim
                      Script to generate a bug report.  See ":help bugs".

       /usr/share/vim/vim82/filetype.vim
                      Script to detect the type of a file by  its  name.   See
                      ":help 'filetype'".

       /usr/share/vim/vim82/scripts.vim
                      Script  to  detect  the  type of a file by its contents.
                      See ":help 'filetype'".

       /usr/share/vim/vim82/print/*.ps
                      Files used for PostScript printing.

       For recent info read the VIM home page:
       <URL:http://www.vim.org/>

SEE ALSO

解决方案二:使用strace命令

涉及到的命令: strace

本部分以archlinux系统为例

安装

在archlinux上,运行下面命令安装:

sudo pacman -S strace --noconfirm

解决方案

对于缺少man手册的程序又该怎么找出它的配置文件呢?

在Linux中我们常使用strace来跟踪进程执行时的系统调用和所接收的信号。因此我们可以通过跟踪进程检查和打开哪些文件来找出配置文件。

使用strace跟踪进程的方法很简单,只需要执行 strace 程序 即可。不过如此一来strace会把程序运行过程中的所有系统调用都输出来,结果会很乱。 不过我们这里只需要关注 statopenat 这两个系统调用即可,因此我们可以使用 -e trace=stat,openat 来指定跟踪的系统调用。

考虑到一般配置文件都是以 conf, cfg, inirc 结尾的文件,我们还可以再通过 grep 过滤以这些结尾的文件。 比如我们可以通过下面这个命令来查找 gvim 读取的配置参数:

strace -e trace=stat,openat gvim 2>&1 |grep -E 'conf"|cfg"|ini"|rc"'
stat("/etc/vimrc", {st_mode=S_IFREG|0644, st_size=912, ...}) = 0
openat(AT_FDCWD, "/etc/vimrc", O_RDONLY) = 4
stat("/home/lujun9972/.vimrc", 0x7ffc9198ceb0) = -1 ENOENT (没有那个文件或目录)
openat(AT_FDCWD, "/home/lujun9972/.vimrc", O_RDONLY) = -1 ENOENT (没有那个文件或目录)
openat(AT_FDCWD, "/home/lujun9972/_vimrc", O_RDONLY) = -1 ENOENT (没有那个文件或目录)
stat("/home/lujun9972/.vim/vimrc", 0x7ffc9198ceb0) = -1 ENOENT (没有那个文件或目录)
openat(AT_FDCWD, "/home/lujun9972/.vim/vimrc", O_RDONLY) = -1 ENOENT (没有那个文件或目录)
stat("/home/lujun9972/.exrc", 0x7ffc9198ceb0) = -1 ENOENT (没有那个文件或目录)
openat(AT_FDCWD, "/home/lujun9972/.exrc", O_RDONLY) = -1 ENOENT (没有那个文件或目录)

从中我们大概可以知道, gvim 会以此读取下面这些配置文件:

  • /etc/vimrc
  • $HOME/.vimrc
  • $HOME/_vimrc
  • $HOME/.vim/vimrc
  • $HOME/.exrc

跟上面的manual相比,我们可以发现strace实际上还会读取 $HOME/.exrc 的内容,但在 manual 中是没有体现的。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值