Linux文件系統中/bin、/sbin、/usr/bin、/usr/sbin、/usr/local/bin、/usr/local/sbin文件夾的區別是什麼?


傳統上的常規做法是:

  • 系統級的組件放在/bin、/lib;

  • 根用戶才能訪問的放在/sbin;

  • 系統repository提供的應用程序放在/usr/bin、/usr/lib;

  • 用戶自己編譯的放在/usr/local/XXX。

現在有一些變化,在大約兩年前,大量Linux系統都將/bin、/lib弄成/usr/bin、/usr/lib的符號鏈接。

此外,不同系統還會有很多的細微區別,比如Redhat系喜歡把32位的庫放在/lib、/usr/lib,64位的庫放在/lib64、/usr/lib64,而Debian系喜歡把平台相關的那層名字放在/lib、/usr/lib的子目錄里,比如/usr/lib/x86_64-linux-gnu/。然後,各種配置文件的文件名、路徑也會有區別,比如ssh伺服器的配置文件可能叫/etc/ssh/sshd.conf,也可能叫/etc/ssh/sshd_config。。。

分成三塊的最早的淵源,據說是這樣的:

  1. Unix開發者的機器的硬碟不夠了,新加了一塊,掛在/usr上;

  2. 又TM不夠了,再加一塊,掛在/usr/local上;

  3. 不知怎麼,就變成規範了。。。。


Linux有專門的指導文件系統的標準,叫做:FHS (Filesystem Hierarchy Standard)

目前的FHS執行標準是 FHS 3.0 Filesystem Hierarchy Standard

Linux所依賴的文件系統,有兩個基本的概念:

  • shareable / unshareable (共享性)

  • static / variable (穩定性)

對於可分享(shareable)的文件系統,FHS 規定:

Shareable files can be stored on one host and used on serveral others.Typically, however, not all files in the filesystem hierarchy are shareable and so each system has local storage containing at least its ubshareavle files. It is convenient if all the files a system requires that are stored on a foreign host can be made available by mounting one or a few directories form the foreign host.

可分享文件能夠被存放在一台將被多個用戶同時訪問的主機上,通常情況下,當一個系統所需的所有文件都存放在一台外部主機上時,可以方便的掛載一個或很少幾個目錄來使這些文件可用。但是,並不是在文件系統架構中的所有文件都是可分享的,所以每個系統都至少需要一處能夠存放不可共享文件的本地空間。

對於靜態(static)文件系統,FHS 規定:

Static and variable files should be segregated because static files, unlike variable files, can be stored on read-only media and do not need to be backed up on the same schedule as variable files.

靜態文件需要與動態文件隔離,因為,不同於動態文件,靜態文件可以存放在只讀介質上(而動態文件不可以),並且不需要和動態文件一起備份。

FHS 中關於特定文件夾用途和所需包含內容的規定:

  • /bin: Essential user command binaries (for use by all users)
  • /sbin: System binaries
  • /usr: shareable static
    • /usr/bin: Most user commands
    • /usr/sbin: Non-essential standard system binaries
    • /usr/local: Local hierarchy

/bin

3.4.? /bin : Essential user command binaries (for use by all users)

包含基本的用戶命令,可被所有用戶使用

3.4.1 Purpose

/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.

/bin 包含能夠同時被用戶和系統管理員使用的命令(二進位程序),並且要能夠在不掛在其他任何文件系統的情況下可用。/bin 也可以包含腳本。

3.4.2. Requirements

There must be no subdirectories in /bin.

The following commands, or symbolic links to commands, are required in /bin:

/bin 中不能存在子文件夾,以下的命令是必須有的(或者提供一個符號鏈接,總之這些命令需要可用):

cat chgrp chmod chown cp date dd df dmesg echo false hostname kill ln login ls mkdir mknod more mount mv ps pwd rm rmdir sed sh stty su sync trye umount uname

3.4.3. Specific Options

The following programs, or symbolic links to programs, must be in /bin if the corresponding subsystem is installed:

如果系統中存在特定的子系統,那麼以下的命令必須存在:

csh ed tar cpio gzip gunzip zcat netstat ping

/sbin

3.16.? /sbin : System binaries

存放系統管理員以及其他需要root許可權來運行的工具。

3.16. /sbin : System binaries

3.16.1. Purpose

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.

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.

同類型的工具同時也可以保存在 /usr/bin/usr/local/sbin 。但是 /sbin 中保存的工具是在系統 啟動、復原、恢復和修復的過程中,作為 /bin 中所保存的工具的補充。

/usr 確保被掛載的情況下運行的管理員程序一般存放在 /usr/sbin 中,在本地安裝的管理員程序則應當被存放在 /usr/local/sbin 中。

3.16.2. Requirements

There must be no subdirectories in /sbin.

The following commands, or symbolic links to commands, are required in /sbin:

/sbin 文件夾下不允許存在子文件夾,以下命令是必須存在的:

shutdown

3.16.3. Specific Options

The following files, or symbolic links to files, must be in /sbin if the corresponding subsystem is installed:

和 /bin 相同,如果相應的子系統存在,那麼以下文件必須存在,或者存在一個符號鏈接指向對應的文件:

fastboot fasthalt fdisk fsck fsck.* getty halt ifconfig init mkfs mkfs.* mkswap reboot route swapon swapoff updata

/usr

Chapter? 4.? The /usr Hierarchy

/usr 是文件系統中第二大主要部分,其中存放了可分享的、只讀的數據(即可分享性,靜態性數據)。這說明 /usr 文件系統需要能夠在各種不同的兼容 FHS 系統的之間間共享,並且不可被修改。任何依賴於平台或者需要改寫的數據,都應該存放在其它位置。

對於 /usr ,其中必須包含以下5個鏈接,可以是實際的子文件夾,也可以是一個符號鏈接指向實際的位置:

bin lib local sbin share

同時,如果存在相應的子系統,那麼以下鏈接也應該存在:

games include libexec lib& src

為了保持向前兼容,有些具有 X window 的平台會有如下的鏈接:

/usr/spool -&> /var/spool

/usr/tmp -&> /var/tmp

/usr/spool/locks -&> /var/lock

/usr 之下,對應子文件夾存放的內容與 / 之下同名子文件夾中的內容應該是相同的,唯一的區別就是這裡的內容都是不可改寫的。

需要特殊說明的是 /usr/local ,local 文件夾是給系統管理員用來存放本地文件的,同樣,由於其處於 /usr 之下,所以其中的內容也應該是靜態的。

Fin.


如果你用過BSD,autoconf,就很容易組合出這幾個目錄的原始邏輯是什麼了。

autoconf很大程度上代表了傳統的unix應用程序設計邏輯,autoconf有一個prefix的概念,指定這個工具(被編譯的應用程序)的根在什麼地方,基於這個根:

1. 一般可執行程序放在bin中

2. 系統可執行程序放在sbin中

3. 全局配置文件放在etc中

4. 庫放在lib中

……

當你編譯這些程序的時候,一般是先:

./configure --prefix=/usr/local

然後

make
make install

這樣對應的文件就被安裝到對應的目錄中了。

所以,/usr, /usr/local,都是prefix。

然後我們看看這些prefix是怎麼選擇的。如果你編譯過FreeBSD一類的系統,你會發現,這些系統的系統庫,基礎工具和內核是放到一套代碼樹中的,編譯這個代碼,內核和核心庫,工具是一同完成的,這些都被認為是操作系統的一部分。這些核心文件,就以根目錄作為prefix。所以,/是所有操作系統核心程序的prefix。

在這個核心之外增加新的程序,構成一個發行版,這個發行版增加的程序就用/usr作為prefix。

你把發行版安裝好了,安裝發行版之外應用程序,那些程序通常用/opt, /srv作為prefix。

但如果你自己從源代碼開始編譯一個應用程序,這些程序是專門向你這個Site編譯的,這種情況下,默認的prefix是/usr/local。

但這些僅僅是個習慣,我們無法保證每個人都用一樣習慣使用這些目錄。Filesystem Hierarchy Standard嘗試把這個定義為一個標準(CGL也有自己的標準,現在兩者是否已經合併我就懶得查了),但現在來說,沒有什麼標準是被所有發行版都贊成的。當然,更約束不了你。

我個人特別喜歡把自己編譯的程序直接放在$HOME/local中。


搬運工。

/bin This directory contains executable programs which are needed in single user mode and to bring the sys‐
tem up or repair it.
/sbin Like /bin, this directory holds commands needed to boot the system, but which are usually not executed
by normal users.
/usr/bin
This is the primary directory for executable programs. Most programs executed by normal users which
are not needed for booting or for repairing the system and which are not installed locally should be
placed in this directory.
/usr/sbin
This directory contains program binaries for system administration which are not essential for the boot
process, for mounting /usr, or for system repair.
/usr/local/bin
Binaries for programs local to the site.
/usr/local/sbin
Locally installed programs for system administration.


man hier 查看


//別人寫的,覺得寫的不錯

所有用戶皆可用的系統程序放在/bin

超級用戶才能使用的系統程序放在/sbin

所有用戶都可用的應用程序放在/usr/bin

超級用戶才能使用的應用程序放在/usr/sbin

所有用戶都可用的與本地機器無關的程序存放在/usr/local/bin

超級用戶才能使用的與本地機器無關的程序存放在/usr/local/sbin


這其實是很混亂、很原始的設計方式。正確的方式是用戶態每個應用互相隔離,就像iOS的.ipa和OSX的.app格式一樣。你看,Xcode這麼大型的軟體,帶有一堆小工具,做成.app不是一樣能用嘛


此圖依據FHS3.0最新標準製成,有需要的點個贊另存為吧!


講道理,你記住了怎麼用,不代表別人這麼用,總有些軟體比較搞笑………


/bin /sbin 是在系統引導的時候用到的。

/usr/bin /usr/sbin 是正常使用時用到的。

/usr/local/bin /usr/local/sbin 是用戶自己編譯安裝的。

各種sbin放的都是只有root用戶才可以用的。


貌似69年丹尼斯里齊開發的unix跑在一台擁有很小的存儲的物理主機上,(那個年代可能也就幾兆)。後來不夠用了,然後在掛載了一塊硬碟,掛載的目錄文件路徑就是/usr,然后里面也放著/目錄下的所有文件路徑,最後演變到如今就是:

/bin下存放一些普通的基本命令,如ls,chmod(普通用戶就行);

/sbin下的命令屬於基本的系統命令,如shutdown,reboot(管理員才能有許可權)

/usr/bin下一般存放在後期安裝的一些軟體的運行腳本c++、g++、gcc、 gzip

/usr/sbin下放置一些用戶安裝的系統管理的必備命令例如:httpd、sendmail、swap、tcpd、tcpdump

/usr/local/bin /usr/local/sbin 是用戶自己編譯安裝的。


推薦閱讀:

怎麼搭建學習Linux內核的運行、調試環境?
在現代的Unix操作系統上執行sudo rm -rf /會發生什麼?
有什麼理由選擇收費的RHEL而不用免費的Centos?
為什麼那麼多「國產」操作系統?
linux kernel有設計和開發文檔嗎?

TAG:Linux | Linux系統管理 | Ubuntu |