linux系统的/bin,/sbin,/usr/bin,/usr/sbin

本文揭示了UNIX目录结构的真正原因,并解释了为何这些目录被创建及为何它们的分布如此混乱。从早期UNIX在PDP-7到PDP-11的迁移中,操作系统和用户数据的存储分配导致了/bin,/sbin,/usr/bin和/usr/sbin等目录的形成。随着硬盘容量的增加,目录结构发生了变化,但遗留下来的原因在现代Linux系统中已不再适用。
Finally something really interesting to talk about. If you've used UNIX or any of its derivatives, you've probably wondered why there's /bin, /sbin, /usr/bin, /usr/sbin in the file system. You may even have a rationalisation for the existence of each and every one of these directories. The thing is, though - all these rationalisations were thought up after these directories were created. As it turns out, the real reasoning is pretty damn straightforward.

I've never made a secret of the fact that I absolutely detest the UNIX directory structure. The names are non-descriptive and often entirely arbitrary, they require a book to properly understand, and everyone seems to have their own ideas about what goes where. And heck, does it show - even among Linux distributions there's no consistency about what goes where.

It's a total and utter nightmare that even defiled my beloved BeOS.

Solutions so far tend to be just layers upon layers upon layers to hide the mess of the directory structure. Mac OS X is especially egregious in this regard - open a terminal and check the directory listing at root - it's an even bigger mess than regular UNIX. Load up a finder window, and you're looking at an entirely different directory structure.

This is like buying a beautiful car, only to realise the engine is made of cake and rotting brocolli.

Whenever I complained about the UNIX directory structure in the past, I (and those who agreed with me) were always pummelled with explanations on why it makes sense, why it's best to spread stuff across /bin, /sbin, /usr/bin, /usr/sbin, and so on. The funny thing always was - these explanations were never particularly consistent between each other.

Late last week, I came across a link at HackerNews giving some intriguing insight into how /bin, /sbin, /usr/bin, and /usr/sbin came to be. Many of you will be surprised to learn that no, there is no divine plan behind all this separation.

Back on November 30 2010, David Collier wondered on the BusyBox mailing list why "kill is in /bin and killall in /usr/bin". He "[doesn't] have a grip on what might be the logic for that". Rob Landley replied, and offered an interesting insight into all this.

The issue was that when Ken Thompson and Dennis Ritchie upgraded from a PDP-7 (on which they created UNIX in 1969) to a PDP-11 in 1971, they were confronted with not one 1.5MB hard drive, but two. Amazingly, they now had an insane amount of megabytes (3 of 'm). The first disk contained the operating system, while the second one contained all the user stuff. This second disk, with all the user stuff, was mounted at /usr (/home was invented later).

Server
Data Center image via Shutterstock

At some point, the operating system grew too big for the first disk, and had to spill over to the second disk. As a result, Thompson and Ritchie replicated the system directory structure (/bin, /sbin, /lib, /tmp, and so on) on this second disk under /usr. When they got a third disk, they moved all the user stuff from /usr to the third disk, mounted under /home.

This forced them to come up with a number of rules, such as that a command like mount couldn't be installed in /usr/bin, since mount was needed to mount the second disk (/usr) in the first place.

"The /bin vs /usr/bin split (and all the others) is an artifact of this, a 1970's implementation detail that got carried forward for decades by bureaucrats who never question _why_ they're doing things," Landley explains, "It stopped making any sense before Linux was ever invented, for multiple reasons."

He then continues to details these reasons. First, Linux already has a temporary system that takes care of the 'this file is needed before that one'-problem. Second, shared libraries solved the issues caused by static linking (which was the norm back when UNIX was created). Third, hard drive space hit the 100MB mark back in 1990, so small disks are no longer an issue.

"Standards bureaucracies like the Linux Foundation (which consumed the Free Standards Group in its ever-growing accretion disk years ago) happily document and add to this sort of complexity without ever trying to understand why it was there in the first place," he adds, "'Ken and Dennis leaked their OS into the equivalent of home because an RK05 disk pack on the PDP-11 was too small' goes whoosh over their heads."

In a way, this feels like vindication. All those silly rationalisations people are bandying about - they're all made up after the fact, for reasons that haven't made sense in at least 30 years - and heck, that never made any sense in the Linux world at all.

Arguing that the UNIX directory structure is a horrible, horrible mess that defiles an otherwise elegant system is like trying to convince a floor tile to flip over. People are so used to their knee-jerk responses about how it all supposedly makes sense, they often refuse to even think about redesigning it for the modern age. Since the geek is a proud and stubborn creature, there's little to no chance of this ever changing in my lifetime.

"I'm pretty sure the busybox install just puts binaries wherever other versions of those binaries have historically gone. There's no actual REASON for any of it anymore. Personally, I symlink /bin /sbin and /lib to their /usr equivalents on systems I put together," Landley, currently working on embedded Linux, concludes, "Embedded guys try to understand and simplify..."

<think>好的,我现在要回答用户关于PATH环境变量组成以及各目录含义的问题。首先,我需要回忆一下自己了解的Linux目录结构知识。记得PATH是系统用来查找可执行文件的路径列表,用户输入命令时,系统会按顺序在这些目录中查找对应的程序。 接下来,用户提到了echo $PATH显示的具体目录,比如/usr/local/sbin、/usr/local/bin等,需要解释这些路径的作用。根据之前学过的内容,/usr/bin和/bin系统预装的可执行文件,而/usr/local/bin通常是用户自己安装的软件,这样可以避免系统升级时被覆盖。同样,sbin目录下的命令通常是系统管理相关的,需要管理员权限。 然后,我需要确认每个目录的具体用途。例如,/sbin和/usr/sbin存放的是系统管理命令,如ifconfig、shutdown等,普通用户可能用不到,但管理员常用。而/usr/local/bin和/usr/local/sbin则是用户自行安装的软件位置,优先级可能更高,这样当系统中有多个同名程序时,会优先执行用户自己安装的版本。 还要考虑PATH的顺序问题。用户提到的引用中提到,/usr/local/bin优先于/usr/bin,这是因为PATH中的目录是从左到右依次查找的。例如,如果两个目录中有同名程序,排在前面的目录中的程序会被执行。这点需要重点说明。 另外,引用中提到了Jenkins的环境变量示例,显示PATH包含/usr/bin、/bin等,这可能是因为Jenkins在特定环境下使用了不同的配置。这说明PATH的配置可能因用户或应用的不同而有所变化。 最后,要确保回答结构清晰,分点解释每个目录的作用,并解释PATH的顺序重要性。同时,生成相关问题时要围绕PATH环境变量的组成、顺序影响、目录区别以及实际应用中的配置等方面展开。</think>### PATH环境变量组成与目录作用分析 #### 1. PATH环境变量功能 PATH是操作系统用于搜索可执行程序的**路径集合**,当用户在终端输入命令时,系统会按照PATH中定义的顺序依次查找对应的可执行文件[^1][^5]。若未找到则提示`command not found`。 #### 2. 常见目录含义及作用 通过`echo $PATH`显示的典型路径如下(以`/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin`为例): - **`/usr/local/sbin`** 存放**系统管理员安装的系统级工具**,如Nginx、MySQL服务管理命令。普通用户无权限直接使用[^3][^5]。 - **`/usr/local/bin`** 用户或第三方软件**自定义安装的普通命令**存放位置(如Python的`pip`安装工具)。优先级通常高于系统自带命令,避免被系统升级覆盖[^5]。 - **`/usr/sbin` 与 `/sbin`** 包含**系统关键管理命令**(如`shutdown`、`ifconfig`),需`root`权限执行。`/sbin`存放基础系统工具,`/usr/sbin`存放非必要系统工具[^3]。 - **`/usr/bin` 与 `/bin`** 存放**系统预装的核心命令**(如`ls`、`gcc`)。`/bin`包含紧急修复工具(单用户模式可用),`/usr/bin`包含用户级应用程序[^5]。 #### 3. 路径顺序的重要性 PATH的**从左到右顺序决定命令优先级**。例如: ```bash # 若PATH为 /usr/local/bin:/usr/bin,则优先使用/usr/local/bin下的程序 $ which python /usr/local/bin/python # 用户自定义安装的Python ``` 若两个目录包含同名程序,系统会选择第一个匹配的路径[^4]。 #### 4. 典型应用场景 - **自定义软件覆盖系统命令**:将自定义安装路径(如`/opt/app/bin`)添加到PATH头部。 - **安全隔离**:限制PATH范围可防止恶意程序劫持常用命令(如将`.`当前目录从PATH中移除)[^2][^5]。 ```bash # Jenkins环境变量示例(缺少/usr/local路径) $ echo $PATH /usr/bin:/bin:/usr/sbin:/sbin # 仅使用系统默认路径 ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值