ini/config 文件在 linux/unix 中的位置?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1024114/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 17:27:42  来源:igfitidea点击:

Location of ini/config files in linux/unix?

linuxunixconfiguration

提问by Newton Falls

Two questions, really:

两个问题,真的:

  1. Is there a standard/convention regarding the placement on configuration files?

    For system or quasi-system programs they seem to usually be somewhere in /etc. It seems less clear for plain application programs or programs with insufficient privileges for /etc.

  2. In processing program options is there a standard hierarchy of what takes precedence? E.g. does a command line option override an initialization file and/or an environment variable? Vice versa? Or is this entirely up to the developer?

  1. 是否有关于配置文件放置的标准/约定?

    对于系统或准系统程序,它们似乎通常位于/etc. 对于普通应用程序或没有足够权限的/etc.

  2. 在处理程序选项时,是否有优先的标准层次结构?例如,命令行选项是否会覆盖初始化文件和/或环境变量?反之亦然?或者这完全取决于开发人员?

采纳答案by cletus

  1. Generally system/global config is stored somewhere under /etc.
  2. User-specific config is stored in the user's home directory, often as a hidden file, sometimes as a hidden directory containing non-hidden files (and possibly more subdirectories).
  1. 通常系统/全局配置存储在 /etc 下的某处。
  2. 用户特定的配置存储在用户的主目录中,通常作为隐藏文件,有时作为包含非隐藏文件(可能还有更多子目录)的隐藏目录。

Generally speaking, command line options will override environment variables which will override user defaults which will override system defaults.

一般来说,命令行选项将覆盖环境变量,这些环境变量将覆盖将覆盖系统默认值的用户默认值。

回答by balpha

(1) No (unfortunately). Edit:The other answers are right, per-user configuration is usually stored in dot-files or dot-directories in the users home directory. Anything above user level often is a lot of guesswork.

(1) 没有(不幸的是)。编辑:其他答案是正确的,每个用户的配置通常存储在用户主目录中的点文件或点目录中。任何高于用户级别的内容通常都是大量猜测。

(2) System-wide ini file -> user ini file -> environment -> command line options (going from lowest to highest precedence)

(2) 全系统ini文件->用户ini文件->环境->命令行选项(从低到高优先级)

回答by Fredrik

  1. Typically in a dotfile (like .myprogramrc) in the user's home directory.
  2. It is of course up to the programmer but normally command line arguments override everything else. If environment variables are used it is usually as an alternative to the command line arguments or to specify where the configuration is located.
  1. 通常在用户主目录中的点文件(如 .myprogramrc)中。
  2. 这当然取决于程序员,但通常命令行参数会覆盖其他所有内容。如果使用环境变量,它通常作为命令行参数的替代或指定配置所在的位置。

回答by Tracker1

Newer Applications

较新的应用程序

Follow XDG Base Directory Specificationusually ~/.config/yourapp/*can be INF, JSON, YML or whatever format floats your boat, and whatever files... yourappshould match your executable name, or be namespaced with your organization/company/username/handle to ~/.config/yourorg/yourapp/*

按照XDG基本目录规范通常~/.config/yourapp/*可以INF,JSON,阳明海运或任何格式你的船浮筒,以及任何文件...yourapp应该符合你的可执行文件的名称,或与您的组织/公司/用户名/手柄命名空间,~/.config/yourorg/yourapp/*

Older Applications

较旧的应用程序

Per-user configuration, usually right in your home directory...

每用户配置,通常就在您的主目录中...

  • ~/.yourappfile for a single file
  • ~/.yourapp/for multiple files + data usually in ~/.yourapp/config
  • ~/.yourapp单个文件的文件
  • ~/.yourapp/对于通常在~/.yourapp/config 中的多个文件 + 数据

Global configurations are generally in /etc/appnamefile or /etc/appname/directory.

全局配置一般在/etc/appname文件或/etc/appname/目录中。

Global App data: /var/lib/yourapp/

全球应用数据: /var/lib/yourapp/

Cache data: /var/cache/

缓存数据: /var/cache/

Log data: /var/log/yourapp/

日志数据: /var/log/yourapp/



Some additional info from tutorialhelpdesk.com

来自tutorialhelpdesk.com 的一些附加信息

The directory structure of Linux/other Unix-like systems and directory details.

Linux/其他类Unix系统的目录结构和目录详细信息。

In Windows, almost all programs install their files (all files) in the directory named: 'Program Files' Such is not the case in Linux. The directory system categorizes all installed files. All configuration files are in /etc, all binary files are in /binor /usr/binor /usr/local/bin. Here is the entire directory structure along with what they contain:

在 Windows 中,几乎所有程序都将它们的文件(所有文件)安装在名为:'Program Files' 的目录中。Linux 中并非如此。目录系统对所有安装的文件进行分类。所有的配置文件都在/etc,所有的二进制文件都在/bin/usr/bin/usr/local/bin。这是整个目录结构及其包含的内容:

/- Root directory that forms the base of the file system. All files and directories are logically contained inside the root directory regardless of their physical locations.

/- 构成文件系统基础的根目录。所有文件和目录在逻辑上都包含在根目录中,无论它们的物理位置如何。

/bin- Contains the executable programs that are part of the Linux operating system. Many Linux commands, such as cat, cp, ls, more, and tar, are locate in /bin

/bin- 包含作为 Linux 操作系统一部分的可执行程序。许多 Linux 命令,例如 cat、cp、ls、more 和 tar,都位于/bin

/boot- Contains the Linux kernel and other files needed by LILO and GRUB boot managers.

/boot- 包含 LILO 和 GRUB 引导管理器所需的 Linux 内核和其他文件。

/dev- Contains all device files. Linux treats each device as a special file. All such files are located in /dev.

/dev- 包含所有设备文件。Linux 将每个设备视为一个特殊文件。所有这些文件都位于/dev.

/etc- Contains most system configuration files and the initialisation scripts in /etc/rc.dsubdirectory.

/etc- 在/etc/rc.d子目录中包含大多数系统配置文件和初始化脚本。

/home- Home directory is the parent to the home directories of users.

/home- 主目录是用户主目录的父目录。

/lib- Contains library files, including loadable driver modules needed to boot the system.

/lib- 包含库文件,包括启动系统所需的可加载驱动程序模块。

/lost+found- Directory for lost files. Every disk partition has a lost+found directory.

/lost+found- 丢失文件的目录。每个磁盘分区都有一个 lost+found 目录。

/media- Directory for mounting files systems on removable media like CD-ROM drives, floppy disks, and Zip drives.

/media- 用于在可移动媒体(如 CD-ROM 驱动器、软盘和 Zip 驱动器)上安装文件系统的目录。

/mnt- A directory for temporarily mounted filesystems.

/mnt- 临时安装的文件系统的目录。

/opt- Optional software packages copy/install files here.

/opt- 可选软件包在此处复制/安装文件。

/proc- A special directory in a virtual filesystem. It contains the information about various aspects of a Linux system.

/proc- 虚拟文件系统中的特殊目录。它包含有关 Linux 系统各个方面的信息。

/root- Home directory of the root user.

/root- root 用户的主目录。

/sbin- Contains administrative binary files. Commands such as mount, shutdown, umount, reside here.

/sbin- 包含管理二进制文件。mount、shutdown、umount 等命令驻留在此处。

/srv- Contains data for services (HTTP, FTP, etc.) offered by the system.

/srv- 包含系统提供的服务(HTTP、FTP 等)的数据。

/sys- A special directory that contains information about the devices, as seen by the Linux kernel.

/sys- 包含有关设备信息的特殊目录,如 Linux 内核所见。

/tmp- Temporary directory which can be used as a scratch directory (storage for temporary files). The contents of this directory are cleared each time the system boots.

/tmp- 临时目录,可用作临时目录(存储临时文件)。每次系统启动时都会清除此目录的内容。

/usr- Contains subdirectories for many programs such as the X Window System.

/usr- 包含许多程序的子目录,例如 X Window System。

/usr/bin- Contains executable files for many Linux commands. It is not part of the core Linux operating system.

/usr/bin- 包含许多 Linux 命令的可执行文件。它不是核心 Linux 操作系统的一部分。

/usr/include- Contains header files for C and C++ programming languages

/usr/include- 包含 C 和 C++ 编程语言的头文件

/usr/lib- Contains libraries for C and C++ programming languages.

/usr/lib- 包含 C 和 C++ 编程语言的库。

/usr/local- Contains local files. It has a similar directories as /usr contains.

/usr/local- 包含本地文件。它具有与 /usr 包含的类似目录。

/usr/sbin- Contains administrative commands.

/usr/sbin- 包含管理命令。

/usr/share- Contains files that are shared, like, default configuration files, images, documentation, etc.

/usr/share- 包含共享的文件,例如默认配置文件、图像、文档等。

/usr/src- Contains the source code for the Linux kernel.

/usr/src- 包含 Linux 内核的源代码。

/var- Contains various system files such as log, mail directories, print spool, etc. which tend to change in numbers and size over time.

/var- 包含各种系统文件,例如日志、邮件目录、打印假脱机等,这些文件的数量和大小会随着时间的推移而发生变化。

/var/cache- Storage area for cached data for applications.

/var/cache- 应用程序缓存数据的存储区域。

/var/lib- Contains information relating to the current state of applications. Programs modify this when they run.

/var/lib- 包含与应用程序当前状态相关的信息。程序在运行时修改它。

/var/lock- Contains lock files which are checked by applications so that a resource can be used by one application only.

/var/lock- 包含由应用程序检查的锁定文件,以便资源只能由一个应用程序使用。

/var/log- Contains log files for different applications.

/var/log- 包含不同应用程序的日志文件。

/var/mail- Contains users' emails.

/var/mail- 包含用户的电子邮件。

/var/opt- Contains variable data for packages stored in /opt directory.

/var/opt- 包含存储在 /opt 目录中的包的变量数据。

/var/run- Contains data describing the system since it was booted.

/var/run- 包含自启动以来描述系统的数据。

/var/spool- Contains data that is waiting for some kind of processing.

/var/spool- 包含等待某种处理的数据。

/var/tmp- Contains temporary files preserved between system reboots.

/var/tmp- 包含在系统重新启动之间保留的临时文件。

回答by Fritz G. Mehner

Please do also consult the Filesystem Hierarchy Standard.

也请参考Filesystem Hierarchy Standard

回答by bluebrother

For user configuration I've noticed a tendency towards moving away from individual ~/.myprogramrcto a structure below ~/.config. For example, Qt 4 uses ~/.config/<vendor>/<programname>with the default settings of QSettings. The major desktop environments KDE and Gnome use a file structure below a specific folder too (not sure if KDE 4 uses ~/.config, XFCE does use ~/.config).

对于用户配置,我注意到一种趋势是从个人~/.myprogramrc转移到下面的结构~/.config。例如,Qt 4 使用~/.config/<vendor>/<programname>默认设置QSettings. 主要的桌面环境 KDE 和 Gnome 也使用特定文件夹下的文件结构(不确定 KDE 4 是否使用~/.config,XFCE 确实使用~/.config)。

回答by Juliano

You should adhere your application to the XDG Base Directory Specification. Most answers here are either obsolete or wrong.

您应该将您的应用程序遵守XDG 基本目录规范。这里的大多数答案要么已经过时,要么是错误的。

Your application should store and load data and configuration files to/from the directories pointed by the following environment variables:

您的应用程序应该在以下环境变量指向的目录中存储和加载数据和配置文件:

  • $XDG_DATA_HOME(default: "$HOME/.local/share"): user-specific data files.
  • $XDG_CONFIG_HOME(default: "$HOME/.config"): user-specific configuration files.
  • $XDG_DATA_DIRS(default: "/usr/local/share/:/usr/share/"): precedence-ordered set of system data directories.
  • $XDG_CONFIG_DIRS(default: "/etc/xdg"): precedence-ordered set of system configuration directories.
  • $XDG_CACHE_HOME(default: "$HOME/.cache"): user-specific non-essential data files.
  • $XDG_DATA_HOME(默认:):"$HOME/.local/share"用户特定的数据文件。
  • $XDG_CONFIG_HOME(默认:):"$HOME/.config"用户特定的配置文件。
  • $XDG_DATA_DIRS(默认:):"/usr/local/share/:/usr/share/"系统数据目录的优先顺序集。
  • $XDG_CONFIG_DIRS(默认:):"/etc/xdg"系统配置目录的优先顺序集。
  • $XDG_CACHE_HOME(默认:):"$HOME/.cache"用户特定的非必要数据文件。

You should first determine if the file in question is:

您应该首先确定有问题的文件是否是:

  1. A configuration file ($XDG_CONFIG_HOME:$XDG_CONFIG_DIRS);
  2. A data file ($XDG_DATA_HOME:$XDG_DATA_DIRS); or
  3. A non-essential (cache) file ($XDG_CACHE_HOME).
  1. 一个配置文件 ( $XDG_CONFIG_HOME:$XDG_CONFIG_DIRS);
  2. 一个数据文件 ( $XDG_DATA_HOME:$XDG_DATA_DIRS); 或者
  3. 非必要(缓存)文件 ( $XDG_CACHE_HOME)。

It is recommended that your application put its files in a subdirectory of the above directories. Usually, something like $XDG_DATA_DIRS/<application>/filenameor $XDG_DATA_DIRS/<vendor>/<application>/filename.

建议您的应用程序将其文件放在上述目录的子目录中。通常,类似于$XDG_DATA_DIRS/<application>/filename$XDG_DATA_DIRS/<vendor>/<application>/filename

When loading, you first try to load the file from the user-specific directories ($XDG_*_HOME) and, if failed, from system directories ($XDG_*_DIRS). When saving, save to user-specific directories only (since the user probably won't have write access to system directories).

加载时,您首先尝试从用户特定目录 ( $XDG_*_HOME)加载文件,如果失败,则从系统目录 ( $XDG_*_DIRS)加载。保存时,只保存到用户特定的目录(因为用户可能没有对系统目录的写访问权限)。

For other, more user-oriented directories, refer to the XDG User Directories Specification. It defines directories for the Desktop, downloads, documents, videos, etc.

对于其他更面向用户的目录,请参阅XDG 用户目录规范。它定义了桌面、下载、文档、视频等的目录。