/usr/bin vs /usr/local/bin on Linux
Why are there so many places to put a binary in Linux? There are at least these five:
/bin/
/sbin/
/usr/bin/
/usr/local/bin/
/usr/local/sbin/
And on my office box, I do not have write permissions to some of these.
What type of binary goes into which of these bin
s?
The sbin
directories contains programs which are generally system administration only. Programs for regular users should never go in them.
A few programs are needed during startup, and end up in /bin/
or /sbin/
. These must be available before file systems are mounted. Things like mount
, and fsck
that are required to check and mount files systems must be there.
Most packaged programs end up in /usr/bin/
and /usr/sbin/
. These may be on a file system other than the root file system. In some cases they may be on a network mounted drive.
Local programs and scripts belong in /usr/local/bin/
and /usr/local/sbin/
. This identifies them as clearly non-standard, and possibly only available on site.
For further explanation try running the command man hier
which should provide a description of the recommended file system hierarchy for your distribution. You may also want to read about the File System Hierarchy on Wikipedia
-
/bin
(and/sbin
) were intended for programs that needed to be on a small/
partition before the larger/usr
, etc. partitions were mounted. These days, it mostly serves as a standard location for key programs like/bin/sh
, although the original intent may still be relevant for e.g. installations on small embedded devices. -
/sbin
, as distinct from/bin
, is for system management programs (not normally used by ordinary users) needed before/usr
is mounted. -
/usr/bin
is for distribution-managed normal user programs. -
There is a
/usr/sbin
with the same relationship to/usr/bin
as/sbin
has to/bin
. -
/usr/local/bin
is for normal user programs not managed by the distribution package manager, e.g. locally compiled packages. You should not install them into/usr/bin
because future distribution upgrades may modify or delete them without warning. -
/usr/local/sbin
, as you can probably guess at this point, is to/usr/local/bin
as/usr/sbin
to/usr/bin
.
In addition, there is also /opt
which is for monolithic non-distribution packages, although before they were properly integrated various distributions put Gnome and KDE there. Generally you should reserve it for large, poorly behaved third party packages such as Oracle.
I recommend taking a look at the file system hierarchy man page:
man hier
which is also available online, for instance: http://linux.die.net/man/7/hier.
Relevant portions have been copied below.
Depending on your system, it may say something different.
Name
hier – description of the file system hierarchy
Description
A typical Linux system has, among others, the following directories:
/bin
This directory contains executable programs which are needed
in single user mode and to bring the system 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/local/bin
Binaries for programs local to the site.
/usr/local/sbin
Locally installed programs for system administration.
/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.
The Filesystem Hierarchy Standard entry in Wikipedia helped me answer the same question when I had it, plus it has a very explanatory table.
Excerpt from that page1:
/bin Essential command binaries that need to be available in single user mode; for all users, e.g., cat, ls, cp. /usr/bin Non-essential command binaries (not needed in single user mode); for all users. /usr/local Tertiary hierarchy for local data, specific to this host. Typically has further subdirectories, e.g., bin, lib, share /usr/sbin Non-essential system binaries, e.g., daemons for various network-services. /sbin Essential system binaries, e.g., fsck, init, route.
1 Retrieved on June 19, 2019; permalink.
In the 1970s, UNIX had all official executables in /bin
and /usr/bin
was a location beneath the users home directories (e.g. /usr/dmr
) that was available for any user to store own binaries that might have been of interest for others as well.
The result of this open /usr/bin
was a junk yard of undocumented software and so Stephen Bourne wrote a cron script that checked for new binaries every night and removed all binaries that did not have a documentation or that have been updated without updating their documentation as well.
In the late 1970s, /usr/bin
was integrated into the OS base distribution and people started to use /usr/local/bin
for the purpose of the previous open /usr/bin
.
After a while, sysadmins used /usr/local/bin
to store non-local software that was imported from the network (e.g. the USENET) and as UNIX companies did not like to repeat the same mistake as with /usr/bin
again, there was a file system hierarchy conference around 1987 where all UNIX companies agreed to give up /usr/local/bin
and to use /opt/<vendor>/bin
instead.
Unfortunately, Linux distros did not follow this decision….