Program configuration data in Unix/Linux

守給你的承諾、 提交于 2021-02-18 22:38:35

问题


What is recommended way to keep a user configuration data in Unix/Linux? My programming language is C++. Configuration data will be kept in XML/text/binary format, I have no problem with handling such files. I want to know where can I keep them. For example, in the Windows OS configuration data may be kept in the Registry (old way) or in user application data directory. What about Linux? I need read/write access to configuration files.


回答1:


The concept of the registry is peculiar to Windows, and Microsoft once admitted to it being ill-conceived (see this, this, this, this (see #2), and this).

In Unix and Linux, configuration for system-wide programs is in /etc or maybe an application-specific subdirectory.

Per user configuration data are kept in the user's home directory in a hidden file—in text format—or an application-specific hidden directory in the user's home directory. The proper way to reference the home directory is through the environment variable HOME. Hidden files and directories are created by making . the first character of the name.

Examples for system-wide configuration is /etc/wgetrc and /etc/ssh/. Examples of per-user data are $HOME/.bashrc and $HOME/.mozilla/.




回答2:


The XDG Base Directory Specification specifies where configuration and other files should be stored in Linux and other X-based operating systems:

http://freedesktop.org/wiki/Specifications/basedir-spec

This is the modern way, and may eventually reduce the dotfile mess in the typical user's home directory.




回答3:


Dotfiles are the classic Unix solution. If you want to deal with reading/writing everything yourself, go for it.

However, most modern programs I use have used GConf for storing preferences. It makes a lot of things easier, both as a developer and as a user (and apparently as an administrator, but I have no experience there).




回答4:


That depends a little on your flavor of Linux but as a general rule most programs have the system default configuration somewhere in /etc with .config files in your home directory that can override the defaults in the /etc dir.


Great point .config should be .[Name of config file]



来源:https://stackoverflow.com/questions/2384933/program-configuration-data-in-unix-linux

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!