Important system directories
On this page
A guide you are following says: “The settings for this program are in /etc. If it fails to start, check the logs in /var/log.” You can reach any directory with one cd, and lesson B05.1 showed you the top of the tree:
bin home mnt sbin.usr-is-merged usrbin.usr-is-merged lib opt snap varboot lib64 proc srvcdrom lib.usr-is-merged root swap.imgdev lost+found run sysetc media sbin tmpTwenty-six names, and none of them says what it holds. The guide expects you to know where settings and logs live, and you do not know yet.
By the end of this lesson, you can say where to look for a setting, a log, a program, a user’s file, a temporary file, a device, or information about a process or the hardware. You can also tell which directories are on the disk and which the kernel creates in memory.
Before you start
Section titled “Before you start”- Listing and moving around (B05.3):
ls -a,ls -l, andls -d. - Root, home, and the current directory (B05.1):
/,/home, and/root.
This lesson does not use ~/lab. All output comes from the course VM, because other Linux systems show different contents here. Every command only reads.
Eight directories and what they hold
Section titled “Eight directories and what they hold”Lesson B05.1 explained /home and /root, the root account’s home directory. With /home, eight directories answer the questions in the opening:
| Directory | In short | What it holds |
|---|---|---|
/etc |
Settings | Configuration files for this computer: settings that apply to the whole system |
/home |
Users’ files | One home directory for each regular user, such as /home/learner |
/tmp |
Temporary files | Files that programs and users create for a short time |
/var |
Logs, changing files | Files that grow and change while the system runs, such as the logs in /var/log |
/usr |
Programs | The installed programs and the files that come with them |
/dev |
Device files | Files that stand for devices, such as disks and terminals |
/proc |
Processes, kernel | Information about the running processes and the kernel |
/sys |
Hardware, kernel | Information about the kernel and the devices it manages |
The purposes follow the manual page hier(7) and the Filesystem Hierarchy Standard (Further reading).
Settings: /etc
Section titled “Settings: /etc”List the first 20 names in /etc:
ls /etc | head -n 20adduser.confalsaalternativesanacrontabapg.confapmapparmorapparmor.dapportaptavahibash.bashrcbash_completionbindresvport.blacklistbinfmt.dbluetoothbrlapi.keybrlttybrltty.confca-certificatesThe | passes the output of ls to head, as in lesson B04.3. Several names end in .conf, short for configuration. You met bash.bashrc, which Bash reads for every terminal window, in lesson B03.3, and /etc/skel in lesson B05.3.
How many names are there in total? wc -l counts the lines it receives:
ls /etc | wc -l233Many programs keep their system-wide settings here. Your own settings stay in hidden files in your home directory, such as ~/.bashrc.
Programs: /usr
Section titled “Programs: /usr”ls /usrbin games include lib lib64 libexec local sbin share srcLesson B04.4 found head in /usr/bin, and /usr/bin is the main directory for the programs that users run. Look at the file and count the names in the directory:
ls -l /usr/bin/headls /usr/bin | wc -l-rwxr-xr-x 1 root root 43528 Aug 25 17:09 /usr/bin/head1478head is an ordinary file, owned by root, and /usr/bin holds 1478 names. The other directories hold what those programs need: lib holds libraries, code that many programs share, and share holds data such as the documentation in /usr/share/doc (lesson B04.3). The files in /usr change when software is installed or updated (module B12). Note the spelling: usr, not user.
Logs and changing files: /var
Section titled “Logs and changing files: /var”ls /varbackups crash local log metrics run spoolcache lib lock mail opt snap tmp/var holds files that change while the system runs. log is the directory from the opening: programs write their logs there, files where they record what they did and what went wrong. Other directories, such as cache and spool, hold data that programs keep for later.
Temporary files: /tmp
Section titled “Temporary files: /tmp”ls -a /tmp....font-unix.ICE-unix.iprt-localipc-DRMIpcServersnap-private-tmpsystemd-private-0d53e9a41d34414db8e5562b12d1a1d6-colord.service-jjZ0oysystemd-private-0d53e9a41d34414db8e5562b12d1a1d6-ModemManager.service-Y7hNhCsystemd-private-0d53e9a41d34414db8e5562b12d1a1d6-polkit.service-B7CtF6….X0-lock.X1024-lock.X1025-lock.X11-unix.X1-lock.XIM-unix/tmp holds temporary files that programs and users create. Programs that run in the background, such as polkit, have private directories here. The names that start with systemd-private- contain random letters and digits, so yours look different, and the list changes with the programs that run.
Unlike most directories outside your home directory, /tmp lets every account create files in it. Its permissions, drwxrwxrwt, say so; module B11 teaches you to read them.
Do not keep anything in /tmp that you want to keep. Ubuntu empties it each time the system starts, and also removes files that nobody has used or changed for 30 days. The rule is one line in the file /usr/lib/tmpfiles.d/tmp.conf, which the manual page tmpfiles.d(5) explains.
Devices: /dev
Section titled “Devices: /dev”ls /dev | head -n 20autofsblockbsgbtrfs-controlbuscdromcharconsolecorecpucpu_dma_latencycpu_wakeup_latencycusediskdma_heapdriecryptfsfb0fdfullEach name in /dev stands for a device. In lesson B03.2, tty printed /dev/pts/0 for your terminal window and /dev/tty3 for a text console. cdrom is the VM’s virtual CD drive. A program that uses one of these files talks to the device through the kernel.
Running processes: /proc
Section titled “Running processes: /proc”Here is the start of /proc. Guess first: what kind of names will you see?
ls /proc | head -n 30Run it, then compare with your guess
11011121297131307131113201335…1846Numbers. They are PIDs, the numbers the kernel gives to running processes, which ps showed in lesson B03.2. Each running process has a directory in /proc, named after its PID. Your numbers will differ.
Other files in /proc describe the whole system, such as its memory:
head -n 3 /proc/meminfoMemTotal: 4005444 kBMemFree: 1702716 kBMemAvailable: 2865052 kBMemTotal is a little less than the 4096 MB you gave the VM in lesson B02.3. No program wrote this file: the kernel produces its text when a program reads it, so your numbers will differ.
Hardware: /sys
Section titled “Hardware: /sys”ls /sysblock class devices fs kernel powerbus dev firmware hypervisor module/sys is also produced by the kernel. It describes the devices the kernel knows about, sorted in different ways: block for disks, bus for the connections between devices, class for kinds of devices.
Which directories are on the disk
Section titled “Which directories are on the disk”In lesson B02.3, the installer formatted a partition as ext4 and used it for /. Are all eight directories on that partition?
Start with ls -ld (lesson B05.3). Guess first: what size does it report for /proc, a directory full of entries?
ls -ld /proc /sys /dev /tmp /runRun it, then compare with your guess
drwxr-xr-x 19 root root 4180 Nov 5 06:56 /devdr-xr-xr-x 234 root root 0 Nov 5 06:56 /procdrwxr-xr-x 34 root root 880 Nov 5 06:56 /rundr-xr-xr-x 13 root root 0 Nov 5 06:57 /sysdrwxrwxrwt 18 root root 4096 Nov 5 06:57 /tmpThe size, in the fifth column, is 0 for /proc and /sys. Their contents are not stored anywhere: the kernel produces them when a program asks.
A size of 0 is a clue, but /dev and /run show other sizes. The command findmnt gives the full answer: for a path, it prints the file system that holds it. The line below runs findmnt once for each of ten directories; you do not need to type it.
for d in / /home /etc /usr /var /tmp /dev /proc /sys /run; do findmnt -n -o TARGET,FSTYPE --target "$d"; done/ ext4/ ext4/ ext4/ ext4/ ext4/ ext4/dev devtmpfs/proc proc/sys sysfs/run tmpfsEach line belongs to one directory, in the order of the command. The first column is a mount point: the directory where a file system is attached to the tree. The second is the type of that file system.
- The first six lines say
/ ext4:/,/home,/etc,/usr,/var, and/tmpare all on the ext4 file system on the VM’s disk. Their files stay when you turn the VM off, except in/tmp, which Ubuntu empties when the system starts again. /dev,/proc,/sys, and/runare file systems of their own. Their types,devtmpfs,proc,sysfs, andtmpfs, are file systems that the kernel provides in memory, not on the disk, each time the system starts.
So on the course VM, /tmp is on the disk. Other Linux systems may keep it in memory; findmnt tells you which.
/bin and /usr/bin are the same directory
Section titled “/bin and /usr/bin are the same directory”One more detail in ls / needs an explanation. Look at the long listing:
ls -l /total 4008028lrwxrwxrwx 1 root root 7 Apr 22 2024 bin -> usr/bindrwxr-xr-x 2 root root 4096 Feb 26 2024 bin.usr-is-merged…lrwxrwxrwx 1 root root 7 Apr 22 2024 lib -> usr/liblrwxrwxrwx 1 root root 9 Apr 22 2024 lib64 -> usr/lib64…lrwxrwxrwx 1 root root 8 Apr 22 2024 sbin -> usr/sbin…These four lines start with l, not d, and end in an arrow. bin is not a directory but a symbolic link, or link: a name that points to another path. Lesson B06.4 in module B06 explains links. readlink prints the path that a link points to:
readlink /bin /sbin /lib /lib64usr/binusr/sbinusr/libusr/lib64The paths are relative, read from /, where the links are. So on this system, /bin and /usr/bin are one directory with two names, and /bin/head is the same file as /usr/bin/head. This is why type head said /usr/bin/head in lesson B04.4: the programs are stored in /usr, and the names in / only point there. This layout is called merged /usr.
The three directories ending in .usr-is-merged are left from the change to this layout; this lesson does not cover them.
The whole picture
Section titled “The whole picture”In words: below /, the region with a solid border holds the directories stored on the disk, in the ext4 file system: /etc, /home, /tmp, /var, and /usr, with /bin as a link to usr/bin (the same for /sbin, /lib, and /lib64). The region with a dashed border holds the directories that the kernel makes in memory: /dev, /proc, and /sys. Each box carries the short purpose from the first table.
The diagram leaves out the other entries of /, most importantly /run, which is also made in memory. The table names them.
Name in / |
What it holds |
|---|---|
/root |
The root account’s home directory (lesson B05.1) |
/boot |
The files needed to start the system |
/run |
Information about the system since it was started, kept in memory |
/media |
Mount points for removable media, such as a USB stick |
/mnt |
A mount point for a file system that is attached for a short time |
/opt |
Add-on software packages |
/srv |
Data that this system serves to others, for example as a web server |
/lost+found |
Pieces of files recovered after a disk error or a crash |
/snap, /cdrom, and swap.img are not described in hier(7); they come from Ubuntu’s software and installer, and later modules cover them.
Security Angle
Section titled “Security Angle”Two facts from this lesson combine into a known attack: every account can create files in /tmp, and a link can point to any path. Suppose a program that runs as root always writes to the same name in /tmp. Another account creates a link with that name first, pointing to a file in /etc. The program follows the link and overwrites that settings file.
MITRE’s list of software weaknesses describes this as CWE-59: Improper Link Resolution Before File Access: a program opens a file by its name without checking that the name leads to the file it intends.
The Linux kernel has a defense against this case, the setting fs.protected_symlinks. When it is 1, the kernel follows a link in a directory such as /tmp, which every account can write to, only if the link belongs to the account that follows it or to the owner of the directory. Ubuntu 24.04 turns it on by default, and you can read its value in /proc, in the file /proc/sys/fs/protected_symlinks. The weakness is still in the program, and the setting covers only links: a program that creates its temporary files safely, for example under names nobody can guess, does not depend on it.
Try it yourself
Section titled “Try it yourself”Goal: Decide where five files are, whether each is on the disk, and check that they exist.
- Without running anything, say which directory to look in for each:
- the program
wc - the Bash settings that apply to every user
- your own home directory
- the device file for the VM’s CD drive
- the information about the process with PID 1
- the program
- For each answer, say whether it is stored on the disk or made by the kernel in memory.
- Check that all five paths exist, with one
ls -dcommand.
Expected: ls -d prints the five paths and no No such file or directory message.
Hint
type from lesson B04.4 shows where a program is. Device names are in /dev.
Solution
| File | Path | Stored |
|---|---|---|
The program wc |
/usr/bin/wc |
On the disk |
| Bash settings for every user | /etc/bash.bashrc |
On the disk |
| Your home directory | /home/learner |
On the disk |
| The CD drive’s device file | /dev/cdrom |
In memory |
| Information about PID 1 | /proc/1 |
In memory |
ls -d /usr/bin/wc /etc/bash.bashrc /home/learner /dev/cdrom /proc/1ls -d prints the five paths and no error; -d stops ls from listing what is inside /home/learner and /proc/1.
/etcholds settings,/varlogs and other changing files,/usrthe programs,/homethe users’ files, and/tmptemporary files that any account can create and that Ubuntu removes when the system starts./devholds device files,/procinformation about processes and the kernel, and/sysabout the hardware.- On the course VM,
findmntshows/dev,/proc,/sys, and/runas file systems that the kernel provides in memory; the others are on the disk. /bin,/sbin,/lib, and/lib64are links into/usr, so/bin/headand/usr/bin/headare the same file.
Next, Practice: Navigating the Filesystem puts lessons B05.1 to B05.4 to work in one task.
Further reading
Section titled “Further reading”man 7 hieron your system, or the Ubuntu 24.04 hier(7) page online: one entry for each directory in this lesson.- Filesystem Hierarchy Standard 3.0: The Root Filesystem, with sections on /dev, /etc, and /tmp; The /usr Hierarchy; and The /var Hierarchy.
man 5 procon your system, or the Ubuntu 24.04 proc(5) page online: the proc file system, and the/proc/<pid>directories.- Linux kernel documentation: sysfs - The filesystem for exporting kernel objects.
man 5 tmpfiles.don your system, or the Ubuntu 24.04 tmpfiles.d(5) page online: theDline type in/usr/lib/tmpfiles.d/tmp.conf, which empties/tmpat each start.- Linux kernel documentation: Documentation for /proc/sys/fs/, the section on
protected_symlinks. man findmnton your system, or the Ubuntu 24.04 findmnt(8) page online, including the option--target.