Linux 目录结构：/lib 分析
目录
![](/data/attachment/album/201804/26/211418zpv68t7cgn6pff7m.jpg)


我们在之前的文章中已经分析了其他重要系统目录，比如 `/bin`、`/boot`、`/dev`、 `/etc` 等。可以根据自己的兴趣进入下列链接了解更多信息。本文中，让我们来看看 `/lib` 目录都有些什么。


* [目录结构分析：/bin 文件夹](https://www.linuxnix.com/linux-directory-structure-explained-bin-folder/)
* [目录结构分析：/boot 文件夹](https://www.linuxnix.com/linux-directory-structure-explained-boot-folder/)
* [目录结构分析：/dev 文件夹](https://www.linuxnix.com/linux-directory-structure-explained-dev-folder/)
* [目录结构分析：/etc 文件夹](https://www.linuxnix.com/linux-directory-structure-explainedetc-folder/)
* [目录结构分析：/lost+found 文件夹](https://www.linuxnix.com/lostfound-directory-linuxunix/)
* [目录结构分析：/home 文件夹](https://www.linuxnix.com/linux-directory-structure-home-root-folders/)


### Linux 中，/lib 文件夹是什么？


`/lib` 文件夹是 **库文件目录** ，包含了所有对系统有用的库文件。简单来说，它是应用程序、命令或进程正确执行所需要的文件。在 `/bin` 或 `/sbin` 目录中的命令的动态库文件正是在此目录中。内核模块同样也在这里。


以 `pwd` 命令执行为例。执行它需要调用一些库文件。让我们来探索一下 `pwd` 命令执行时都发生了什么。我们需要使用 [strace 命令](https://www.linuxnix.com/10-strace-command-examples-linuxunix/) 找出调用的库文件。


示例：



```
root@linuxnix:~# strace -e open pwd
open("/etc/ld.so.cache", O_RDONLY|O_CLOEXEC) = 3
open("/lib/x86_64-linux-gnu/libc.so.6", O_RDONLY|O_CLOEXEC) = 3
open("/usr/lib/locale/locale-archive", O_RDONLY|O_CLOEXEC) = 3
/root
+++ exited with 0 +++
root@linuxnix:~# 

```

如果你注意到的话，会发现我们使用的 `pwd` 命令的执行需要调用两个库文件。


### Linux 中 /lib 文件夹内部信息


正如之前所说，这个文件夹包含了目标文件和一些库文件，如果能了解这个文件夹的一些重要子文件，想必是极好的。下面列举的内容是基于我自己的系统，对于你的来说，可能会有所不同。



```
root@linuxnix:/lib# find . -maxdepth 1  -type d
./firmware
./modprobe.d
./xtables
./apparmor
./terminfo
./plymouth
./init
./lsb
./recovery-mode
./resolvconf
./crda
./modules
./hdparm
./udev
./ufw
./ifupdown
./systemd
./modules-load.d

```

`/lib/firmware` - 这个文件夹包含了一些硬件、<ruby> 固件 <rt>  Firmware </rt></ruby>代码。



> 
> **硬件和固件之间有什么不同？**
> 
> 
> 为了使硬件正常运行，很多设备软件由两部分软件组成。加载到实际硬件的代码部分就是固件，用于在固件和内核之间通讯的软件被称为驱动程序。这样一来，内核就可以直接与硬件通讯，并确保硬件完成内核指派的工作。
> 
> 
> 


`/lib/modprobe.d` - modprobe 命令的配置目录。


`/lib/modules` - 所有的可加载内核模块都存储在这个目录下。如果你有多个内核，你会在这个目录下看到代表美国内核的目录。


`/lib/hdparm` - 包含 SATA/IDE 硬盘正确运行的参数。


`/lib/udev` - 用户空间 /dev 是 Linux 内核设备管理器。这个文件夹包含了所有的 udev 相关的文件和文件夹，例如 `rules.d` 包含了 udev 规范文件。


### /lib 的姊妹文件夹：/lib32 和 /lib64


这两个文件夹包含了特殊结构的库文件。它们几乎和 `/lib` 文件夹一样，除了架构级别的差异。


### Linux 其他的库文件


`/usr/lib` - 所有软件的库都安装在这里。但是不包含系统默认库文件和内核库文件。


`/usr/local/lib` - 放置额外的系统文件。这些库能够用于各种应用。


`/var/lib` - 存储动态数据的库和文件，例如 rpm/dpkg 数据和游戏记录。




---


via: <https://www.linuxnix.com/linux-directory-structure-lib-explained/>


作者：[Surendra Anne](https://www.linuxnix.com/author/surendra/) 译者：[CYLeft](https://github.com/CYLeft) 校对：[wxy](https://github.com/wxy)


本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译，[Linux中国](https://linux.cn/) 荣誉推出


OK
We already explained other important system folders like /bin, /boot, /dev, /etc etc folders in our previous posts. Please check below links for more information about other stuff which you are interested. In this post, we will see what is /lib folder all about.

**Linux Directory Structure explained: /bin folder**

**Linux Directory Structure explained: /boot folder**

**Linux Directory Structure explained: /dev folder**

**Linux Directory Structure explained: /etc folder**

**Linux Directory Structure explained: /lost+found folder**

**Linux Directory Structure explained: /home folder**


## What is /lib folder in Linux?

The lib folder is a **library files directory** which contains all helpful library files used by the system. In simple terms, these are helpful files which are used by an application or a command or a process for their proper execution. The commands in /bin or /sbin dynamic library files are located just in this directory. The kernel modules are also located here.

Taken an example of executing pwd command. It requires some library files to execute properly. Let us prove what is happening with pwd command when executing. We will use [the strace command](https://www.linuxnix.com/10-strace-command-examples-linuxunix/) to figure out which library files are used.

Example:

root@linuxnix:~# strace -e open pwdopen("/etc/ld.so.cache", O_RDONLY|O_CLOEXEC) = 3open("/lib/x86_64-linux-gnu/libc.so.6", O_RDONLY|O_CLOEXEC) = 3open("/usr/lib/locale/locale-archive", O_RDONLY|O_CLOEXEC) = 3/root+++ exited with 0 +++root@linuxnix:~#

If you observe, We just used open kernel call for pwd command. The pwd command to execute properly it will require two lib files.

Contents of /lib folder in Linux

As said earlier this folder contains object files and libraries, it’s good to know some important subfolders with this directory. And below content are for my system and you may see some variants in your system.

root@linuxnix:/lib# find . -maxdepth 1 -type d./firmware./modprobe.d./xtables./apparmor./terminfo./plymouth./init./lsb./recovery-mode./resolvconf./crda./modules./hdparm./udev./ufw./ifupdown./systemd./modules-load.d

**/lib/firmware** – This is a folder which contains hardware firmware code.

### What is the difference between firmware and drivers?

Many devices software consists of two software piece to make that hardware properly. The piece of code that is loaded into actual hardware is firmware and the software which communicate between this firmware and kernel is called drivers. This way the kernel directly communicate with hardware and make sure hardware is doing the work assigned to it.

**/lib/modprobe.d** – Configuration directory for modprobe command

**/lib/modules** – All loadable kernel modules are stored in this directory. If you have more kernels you will see folders within this directory each represents a kernel.

**/lib/hdparm** – Contains SATA/IDE parameters for disks to run properly.

**/lib/udev** – Userspace /dev is a device manager for Linux Kernel. This folder contains all udev related files/folders like rules.d folder which contain udev specific rules.

## The /lib folder sister folders: /lib32 and /lib64

These folders contain their specific architecture library files. These folders are almost identical to /lib folder expects architecture level differences.

## Other library folders in Linux

**/usr/lib** – All software libraries are installed here. This does not contain system default or kernel libraries.

**/usr/local/lib** – To place extra system library files here. These library files can be used by different applications.

**/var/lib** – Holds dynamic data libraries/files like the rpm/dpkg database and game scores.

#### Latest posts by Surendra Anne ([see all](https://www.linuxnix.com/author/surendra/))

-
[Docker: How to copy files to/from docker container](https://www.linuxnix.com/docker-how-to-copy-files-to-from-docker-container/)- June 30, 2020 -
[Anisble: ERROR! unexpected parameter type in action:](https://www.linuxnix.com/anisble-error-unexpected-parameter-type-in-action-fix/)- June 29, 2020Fix -
[FREE: JOIN OUR DEVOPS TELEGRAM GROUPS](https://www.linuxnix.com/free-join-our-devops-telegram-group/)- August 2, 2019 -
[Review: Whizlabs Practice Tests for AWS Certified Solutions Architect Professional (CSAP)](https://www.linuxnix.com/review-whizlabs-practice-tests-for-aws-certified-solutions-architect-professional-csap/)- August 27, 2018 -
[How to use ohai/chef-shell to get node attributes](https://www.linuxnix.com/chef-get-node-attributes-or-values/)- July 19, 2018