Linux FHS 目錄樹

Most of the Linux distributions follow the Filesystem Hierarchy Standard (FHS) quite well.

FHS:Filesystem Hierarchy Standard 提取碼: hqcz

參考:https://www.cnblogs.com/kzloser/articles/2673771.html

 

Table of Contents

FHS(Filesystem Hierarchy Standard):

根目錄 (/) 的意義與內容:

/usr 的意義與內容:

/var 的意義與內容:

目錄樹(directory tree):


FHS(Filesystem Hierarchy Standard):

FHS依據文件系統使用的頻繁與否與是否允許使用者隨意更動, 而將目錄定義成爲四種交互作用的形態,用表格來說有點像底下這樣:

  可分享的(shareable) 不可分享的(unshareable)
不變的(static) /usr (軟件放置處) /etc (配置文件)
  /opt (第三方協力軟件) /boot (開機與核心檔)
可變動的(variable) /var/mail (使用者郵件信箱) /var/run (程序相關)
  /var/spool/news (新聞組) /var/lock (程序相關)
  • 可分享的:可以分享給其他系統掛載使用的目錄,所以包括執行文件與用戶的郵件等數據, 是能夠分享給網絡上其他主機掛載用的目錄;
  • 不可分享的:自己機器上面運作的裝置文件或者是與程序有關的socket文件等, 由於僅與自身機器有關,所以當然就不適合分享給其他主機了.
  • 不變的:有些數據是不會經常變動的,跟隨着distribution而不變動. 例如函式庫、文件說明文件、系統管理員所管理的主機服務配置文件等等;
  • 可變動的:經常改變的數據,例如登錄文件、一般用戶可自行收受的新聞組等.

 

事實上,FHS針對目錄樹架構僅定義出三層目錄底下應該放置什麼數據而已,分別是底下這三個目錄的定義:

  1. / (root, 根目錄):與開機系統有關;
  2. /usr (unix software resource):與軟件安裝/執行有關;
  3. /var (variable):與系統運作過程有關.

根目錄 (/) 的意義與內容:

概要:

  1. 所有的目錄都是由根目錄衍生出來的(根目錄是整個系統最重要的一個目錄)
  2. 與開機/還原/系統修復等動作有關. (由於系統開機時需要特定的開機軟件、核心文件、開機所需程序、 函式庫等等文件數據,若系統出現錯誤時,根目錄也必須要包含有能夠修復文件系統的程序纔行)
  3. FHS標準建議:根目錄(/)所在分割槽應該越小越好, 且應用程序所安裝的軟件最好不要與根目錄放在同一個分割槽內,保持根目錄越小越好.(因爲越大的分割槽妳會放入越多的數據,如此一來根目錄所在分割槽就可能會有較多發生錯誤的機會,如此不但效能較佳,根目錄所在的文件系統也較不容易發生問題.)

重要的目錄(簡單)

The most important directories and their contents:
/bin      Essential(極其重要的) user command binaries that need to be available also in single user mode(單用戶模式).
/sbin     Essential system binaries (e.g. init, insmod, ifup)
/lib       Libraries for the binaries in /bin and /sbin
/usr/bin      Non-essential user command binaries that are not needed in single user mode
/usr/sbin     Non-essential system binaries (e.g. daemons for network-services)
/usr/lib      Libraries for the binaries in /usr/bin and /usr/sbin
/etc       Host-specific system-wide configuration files
/dev       Device files
/home      User home directories (optional)
/proc      Virtual file system documenting kernel and process status as text files

 

根目錄(/)底下目錄FHS定義的說明:

目錄 應放置文件內容
/bin

系統有很多放置執行文件的目錄,但/bin比較特殊.因爲/bin放置的是在單人維護模式下還能夠被操作的指令. 在/bin底下的指令可以被root與一般賬號所使用,主要有:cat, chmod, chown, date, mv, mkdir, cp, bash等等常用的指令.

 

/bin contains commands that may be used by both the system administrator and by users, but which are
required when no other filesystems are mounted (e.g. in single user mode). It may also contain commands
which are used indirectly by scripts. 

/boot

這個目錄主要在放置開機會使用到的文件,包括Linux核心文件以及開機選單與開機所需配置文件等等. Linux kernel常用的檔名爲:vmlinuz,如果使用的是grub這個開機管理程序, 則還會存在/boot/grub/這個目錄喔!

 

This directory contains everything required for the boot process except configuration files not needed at
boot time and the map installer. Thus /boot stores data that is used before the kernel begins executing
user-mode programs. This may include saved master boot sectors and sector map files.
Programs necessary to arrange for the boot loader to be able to boot a file must be placed in /sbin.
Configuration files for boot loaders that are not required at boot time must be placed in /etc.

/dev

在Linux系統上,任何裝置與接口設備都是以文件的型態存在於這個目錄當中的. 你只要透過存取這個目錄底下的某個文件,就等於存取某個裝置囉~ 比要重要的文件有/dev/null, /dev/zero, /dev/tty, /dev/lp*, /dev/hd*, /dev/sd*等等

 

The /dev directory is the location of special or device files.

/etc 系統主要的配置文件幾乎都放置在這個目錄內,例如人員的賬號密碼文件、 各種服務的啓始檔等等.一般來說,這個目錄下的各文件屬性是可以讓一般使用者查閱的, 但是隻有root有權力修改.FHS建議不要放置可執行文件(binary)在這個目錄中喔.比較重要的文件有: /etc/inittab, /etc/init.d/, /etc/modprobe.conf, /etc/X11/, /etc/fstab, /etc/sysconfig/ 等等.另外,其下重要的目錄有:
  • /etc/init.d/:所有服務的預設啓動 script 都是放在這裏的,例如要啓動或者關閉 iptables 的話:『 /etc/init.d/iptables start』、『/etc/init.d/iptables stop』
  • /etc/xinetd.d/:這就是所謂的super daemon管理的各項服務的配置文件目錄.
  • /etc/X11/:與 X Window 有關的各種配置文件都在這裏,尤其是 xorg.conf 這個 X Server 的配置文件.

The /etc hierarchy contains configuration files. A "configuration file" is a local file used to control the
operation of a program; it must be static and cannot be an executable binary. 2
It is recommended that files be stored in subdirectories of /etc rather than directly in /etc.

/home

這是系統默認的用戶家目錄(home directory).在你新增一個一般使用者賬號時, 默認的用戶家目錄都會規範到這裏來.比較重要的是,家目錄有兩種代號喔:
~:代表目前這個用戶的家目錄,而 
~dmtsai :則代表 dmtsai 的家目錄!

 

/home is a fairly standard concept, but it is clearly a site-specific filesystem.  The setup will differ from
host to host. Therefore, no program should assume any specific location for a home directory, rather it
should query for it. 

/lib

系統的函式庫非常的多,而/lib放置的則是在開機時會用到的函式庫, 以及在/bin或/sbin底下的指令會呼叫的函式庫而已. 什麼是函式庫呢?妳可以將他想成是『外掛』,某些指令必須要有這些『外掛』才能夠順利完成程序的執行之意. 尤其重要的是/lib/modules/這個目錄, 因爲該目錄會放置核心相關的模塊(驅動程序)喔!

 

The /lib directory contains those shared library images needed to boot the system and run the commands
in the root filesystem, ie. by binaries in /bin and /sbin. 

/media

media是『媒體』的英文,顧名思義,這個/media底下放置的就是可移除的裝置啦! 包括軟盤、光盤、DVD等等裝置都暫時掛載於此.常見的檔名有:/media/floppy, /media/cdrom等等.

 

This directory contains subdirectories which are used as mount points for removable media such as floppy
disks, cdroms and zip disks.

/mnt

如果妳想要暫時掛載某些額外的裝置,一般建議妳可以放置到這個目錄中. 在古早時候,這個目錄的用途與/media相同啦!只是有了/media之後,這個目錄就用來暫時掛載用了.

 

This directory is provided so that the system administrator may temporarily(暫時的) mount a filesystem as needed.
The content of this directory is a local issue and should not affect the manner in which any program is run.

This directory must not be used by installation programs: a suitable temporary directory not in use by the
system must be used instead.

/opt

這個是給第三方協力軟件放置的目錄.什麼是第三方協力軟件啊? 舉例來說,KDE這個桌面管理系統是一個獨立的計劃,不過他可以安裝到Linux系統中,因此KDE的軟件就建議放置到此目錄下了. 另外,如果妳想要自行安裝額外的軟件(非原本的distribution提供的),那麼也能夠將你的軟件安裝到這裏來. 不過,以前的Linux系統中,我們還是習慣放置在/usr/local目錄下呢!

 

/opt is reserved for the installation of add-on application software packages.
A package to be installed in /opt must locate its static files in a separate /opt/<package> or /
opt/<provider> directory tree, where <package> is a name that describes the software package
and <provider> is the provider's LANANA registered name.

/root

系統管理員(root)的家目錄.之所以放在這裏,是因爲如果進入單人維護模式而僅掛載根目錄時, 該目錄就能夠擁有root的家目錄,所以我們會希望root的家目錄與根目錄放置在同一個分割槽中.

 

The root account's home directory may be determined by developer or local preference, but this is the
recommended default location.

/sbin

Linux有非常多指令是用來設定系統環境的,這些指令只有root才能夠利用來『設定』系統,其他用戶最多隻能用來『查詢』而已. 放在/sbin底下的爲開機過程中所需要的,裏面包括了開機、修復、還原系統所需要的指令. 至於某些服務器軟件程序,一般則放置到/usr/sbin/當中.至於本機自行安裝的軟件所產生的系統執行文件(system binary), 則放置到/usr/local/sbin/當中了.常見的指令包括:fdisk, fsck, ifconfig, init, mkfs等等.

 

Utilities used for system administration (and other root-only commands) are stored in /sbin, /usr/
sbin, and /usr/local/sbin. /sbin contains binaries essential for booting, restoring, recovering,
and/or repairing the system in addition to the binaries in /bin. 18 Programs executed after /usr is known
to be mounted (when there are no problems) are generally placed into /usr/sbin. Locally-installed
system administration programs should be placed into /usr/local/sbin. 19

/srv

srv可以視爲『service』的縮寫,是一些網絡服務啓動之後,這些服務所需要取用的數據目錄. 常見的服務例如WWW, FTP等等.舉例來說,WWW服務器需要的網頁數據就可以放置在/srv/www/裏面.

 

/srv contains site-specific data which is served by this system.

/tmp

這是讓一般使用者或者是正在執行的程序暫時放置文件的地方. 這個目錄是任何人都能夠存取的,所以你需要定期的清理一下.當然,重要數據不可放置在此目錄啊! 因爲FHS甚至建議在開機時,應該要將/tmp下的數據都刪除唷!

 

The /tmp directory must be made available for programs that require temporary files.
Programs must not assume that any files or directories in /tmp are preserved between invocations of the
program.

/run This directory contains system information data describing the system since it was booted. Files under this
directory must be cleared (removed or truncated as appropriate) at the beginning of the boot process.
The purposes of this directory were once served by /var/run. In general, programs may continue to
use /var/run to fulfill the requirements set out for /run for the purposes of backwards compatibility.
Programs which have migrated to use /run should cease their usage of /var/run, except as noted in
the section on /var/run.
Programs may have a subdirectory of /run; this is encouraged for programs that use more than one run-
time file. Users may also have a subdirectory of /run, although care must be taken to appropriately limit
access rights to prevent unauthorized use of /run itself and other subdirectories. 17

除上 FHS 中定義的目錄說明外, 底下是幾個在Linux當中非常重要的目錄:

目錄 應放置文件內容
/lost+found 這個目錄是使用標準的ext2/ext3文件系統格式纔會產生的一個目錄,目的在於當文件系統發生錯誤時, 將一些遺失的片段放置到這個目錄下.這個目錄通常會在分割槽的最頂層存在, 例如你加裝一顆硬盤於/disk中,那在這個系統下就會自動產生一個這樣的目錄『/disk/lost+found』
/proc 這個目錄本身是一個『虛擬文件系統(virtual filesystem)』喔!他放置的數據都是在內存當中, 例如系統核心、行程信息(process)、周邊裝置的狀態及網絡狀態等等.因爲這個目錄下的數據都是在內存當中, 所以本身不佔任何硬盤空間啊!比較重要的文件例如:/proc/cpuinfo, /proc/dma, /proc/interrupts, /proc/ioports, /proc/net/* 等等.
/sys 這個目錄其實跟/proc非常類似,也是一個虛擬的文件系統,主要也是記錄與核心相關的信息. 包括目前已加載的核心模塊與核心偵測到的硬件裝置信息等等.這個目錄同樣不佔硬盤容量喔!

/usr 的意義與內容:

概要:

  1. 依據FHS的基本定義,/usr裏面放置的數據屬於可分享的與不可變動的(shareable, static), 如果你知道如何透過網絡進行分割槽的掛載,那麼/usr確實可以分享給局域網絡內的其他主機來使用!
  2. usr(Unix Software Resource 即Unix操作系統軟件資源) FHS建議所有軟件開發者,應該將他們的數據合理的分別放置到這個目錄下的次目錄,而不要自行建立該軟件自己獨立的目錄.
  3. 所有系統默認的軟件(distribution發佈者提供的軟件)都會放置到/usr底下,因此這個目錄有點類似Windows 系統的『C:\Windows\ + C:\Program files\』這兩個目錄的綜合體,系統剛安裝完畢時,這個目錄會佔用最多的硬盤容量.
/usr is the second major section of the filesystem. /usr is shareable, read-only data. That means
that /usr should be shareable between various FHS-compliant hosts and must not be written to. Any
information that is host-specific or varies with time is stored elsewhere.
Large software packages must not use a direct subdirectory under the /usr hierarchy.

一般來說,/usr的次目錄建議有底下這些:

目錄 應放置文件內容
/usr/X11R6/ 爲X Window System重要數據所放置的目錄,之所以取名爲X11R6是因爲最後的X版本爲第11版,且該版的第6次釋出之意.
/usr/bin/

絕大部分的用戶可使用指令都放在這裏!請注意到他與/bin的不同之處.(是否與開機過程有關)

 

This is the primary directory of executable commands on the system.

/usr/include/

c/c++等程序語言的檔頭(header)與包含檔(include)放置處,當我們以tarball方式 (*.tar.gz 的方式安裝軟件)安裝某些數據時,會使用到裏頭的許多包含檔喔!

 

This is where all of the system's general-use include files for the C programming language should be placed.

/usr/lib/

包含各應用軟件的函式庫、目標文件(object file),以及不被一般使用者慣用的執行檔或腳本(script). 某些軟件會提供一些特殊的指令來進行服務器的設定,這些指令也不會經常被系統管理員操作, 那就會被擺放到這個目錄下啦.要注意的是,如果你使用的是X86_64的Linux系統, 那可能會有/usr/lib64/目錄產生喔!

 

/usr/lib includes object files and libraries.  On some systems, it may also include internal binaries
that are not intended to be executed directly by users or shell scripts. 
Applications may use a single subdirectory under /usr/lib. If an application uses a subdirectory, all
architecture-dependent data exclusively used by the application must be placed within that subdirectory. 

/usr/local/

系統管理員在本機自行安裝自己下載的軟件(非distribution默認提供者),建議安裝到此目錄, 這樣會比較便於管理.舉例來說,你的distribution提供的軟件較舊,你想安裝較新的軟件但又不想移除舊版, 此時你可以將新版軟件安裝於/usr/local/目錄下,可與原先的舊版軟件有分別啦! 你可以自行到/usr/local去看看,該目錄下也是具有bin, etc, include, lib...的次目錄喔!

 

The /usr/local hierarchy is for use by the system administrator when installing software locally. It
needs to be safe from being overwritten when the system software is updated. It may be used for programs
and data that are shareable amongst a group of hosts, but not found in /usr.
Locally installed software must be placed within /usr/local rather than /usr unless it is being
installed to replace or upgrade software in /usr. 

/usr/sbin/

非系統正常運作所需要的系統指令.最常見的就是某些網絡服務器軟件的服務指令(daemon)囉!

 

This directory contains any non-essential binaries used exclusively by the system administrator. System administration programs that are required for system repair, system recovery, mounting /usr, or other
essential functions must be placed in /sbin instead. 

/usr/share/ 放置共享文件的地方,在這個目錄下放置的數據幾乎是不分硬件架構均可讀取的數據, 因爲幾乎都是文本文件嘛!在此目錄下常見的還有這些次目錄:
  • /usr/share/man:聯機幫助文件
  • /usr/share/doc:軟件雜項的文件說明
  • /usr/share/zoneinfo:與時區有關的時區文件

The /usr/share hierarchy is for all read-only architecture independent data files.

 

/usr/src/

一般原始碼建議放置到這裏,src有source的意思.至於核心原始碼則建議放置到/usr/src/linux/目錄下.

 

Source code may be placed in this subdirectory, only for reference purposes.

 

/var 的意義與內容:

概要:

/var目錄主要針對常態性變動的文件,包括緩存(cache)、登錄檔(log file)以及某些軟件運作所產生的文件, 包括程序文件(lock file, run file),或者例如MySQL數據庫的文件等等. 所以/var在系統運作後纔會漸漸佔用硬盤容量的目錄

/var contains variable data files. This includes spool directories and files, administrative and logging
data, and transient and temporary files.

常見的次目錄有:

目錄 應放置文件內容
/var/cache/

應用程序本身運作過程中會產生的一些暫存檔;

 

/var/cache is intended for cached data from applications. Such data is locally generated as a result of
time-consuming I/O or calculation. The application must be able to regenerate or restore the data. Unlike
/var/spool, the cached files can be deleted without data loss. The data must remain valid between
invocations of the application and rebooting the system.
Files located under /var/cache may be expired in an application specific manner, by the system
administrator, or both. The application must always be able to recover from manual deletion of these files
(generally because of a disk space shortage). No other requirements are made on the data format of the
cache directories.

/var/lib/

程序本身執行的過程中,需要使用到的數據文件放置的目錄.在此目錄下各自的軟件應該要有各自的目錄. 舉例來說,MySQL的數據庫放置到/var/lib/mysql/而rpm的數據庫則放到/var/lib/rpm去!

 

This hierarchy holds state information pertaining to an application or the system. State information is data
that programs modify while they run, and that pertains to one specific host. Users must never need to
modify files in /var/lib to configure a package's operation, and the specific file hierarchy used to store
the data must not be exposed to regular users.

State information is generally used to preserve the condition of an application (or a group of inter-
related applications) between invocations and between different instances of the same application. State
information should generally remain valid after a reboot, should not be logging output, and should not
be spooled data.
An application (or a group of inter-related applications) must use a subdirectory of /var/lib for its data.
There is one required subdirectory, /var/lib/misc, which is intended for state files that don't need a subdirectory; the other subdirectories should only be present if the application in question is included
in the distribution. 
/var/lib/<name> is the location that must be used for all distribution packaging support. Different
distributions may use different names, of course.

/var/lock/

某些裝置或者是文件資源一次只能被一個應用程序所使用,如果同時有兩個程序使用該裝置時, 就可能產生一些錯誤的狀況,因此就得要將該裝置上鎖(lock),以確保該裝置只會給單一軟件所使用. 舉例來說,刻錄機正在刻錄一塊光盤,你想一下,會不會有兩個人同時在使用一個刻錄機燒片? 如果兩個人同時刻錄,那片子寫入的是誰的數據?所以當第一個人在刻錄時該刻錄機就會被上鎖, 第二個人就得要該裝置被解除鎖定(就是前一個人用完了)才能夠繼續使用囉.

 

Lock files should be stored within the /var/lock directory structure.
Lock files for devices and other resources shared by multiple applications, such as the serial device lock
files that were originally found in either /usr/spool/locks or /usr/spool/uucp, must now be
stored in /var/lock. The naming convention which must be used is "LCK.." followed by the base name
of the device. For example, to lock /dev/ttyS0 the file "LCK..ttyS0" would be created. 6
The format used for the contents of such lock files must be the HDB UUCP lock file format. The HDB
format is to store the process identifier (PID) as a ten byte ASCII decimal number, with a trailing newline.
For example, if process 1230 holds a lock file, it would contain the eleven characters: space, space, space,
space, space, space, one, two, three, zero, and newline.

/var/log/

重要到不行!這是登錄文件放置的目錄!裏面比較重要的文件如/var/log/messages, /var/log/wtmp(記錄登入者的信息)等.

 

This directory contains miscellaneous(各種各樣的) log files. Most logs must be written to this directory or an appropriate subdirectory.

/var/mail/

放置個人電子郵件信箱的目錄,不過這個目錄也被放置到/var/spool/mail/目錄中! 通常這兩個目錄是互爲鏈接文件啦!

 

The mail spool must be accessible through /var/mail and the mail spool files must take the form
<username>. 
User mailbox files in this location must be stored in the standard UNIX mailbox format.
Rationale
The logical location for this directory was changed from /var/spool/mail in order to bring
FHS in-line with nearly every UNIX distribution. This change is important for inter-operability
since a single /var/mail is often shared between multiple hosts and multiple UNIX distribution
(despite NFS locking issues).
It is important to note that there is no requirement to physically move the mail spool to this
location. However, programs and header files must be changed to use /var/mail.

/var/run/

某些程序或者是服務啓動後,會將他們的PID放置在這個目錄下喔! 至於PID的意義我們會在後續章節提到的.

 

This directory was once intended for system information data describing the system since it was booted.
These functions have been moved to /run; this directory exists to ensure compatibility with systems and
software using an older version of this specification.

/var/spool/

這個目錄通常放置一些隊列數據,所謂的『隊列』就是排隊等待其他程序使用的數據啦! 這些數據被使用後通常都會被刪除.舉例來說,系統收到新信會放置到/var/spool/mail/中, 但使用者收下該信件後該封信原則上就會被刪除.信件如果暫時寄不出去會被放到/var/spool/mqueue/中, 等到被送出後就被刪除.如果是工作排程數據(crontab),就會被放置到/var/spool/cron/目錄中!

 

/var/spool contains data which is awaiting some kind of later processing. Data in /var/spool
represents work to be done in the future (by a program, user, or administrator); often data is deleted after
it has been processed. 

目錄樹(directory tree):


在Linux底下,所有的文件與目錄都是由根目錄開始的!然後再一個一個的分支下來,有點像是樹枝狀.因此,我們也稱這種目錄配置方式爲:『目錄樹(directory tree)』它主要的特性有:

  • 目錄樹的啓始點爲根目錄 (/, root);
  • 每一個目錄不止能使用本地端的 partition 的文件系統,也可以使用網絡上的 filesystem 。舉例來說, 可以利用 Network File System (NFS) 服務器掛載某特定目錄等。
  • 每一個文件在此目錄樹中的文件名(包含完整路徑)都是獨一無二的。

如果我們將整個目錄樹以圖標的方法來顯示,並且將較爲重要的文件數據列出來的話,那麼目錄樹架構有點像這樣:

圖釋一:

圖釋二:

注意:上表解釋有點問題而且是繁體字,可不看

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章