DEVOPS, SRE AND CLOUD · PART 1 · LINUX, FROM THE INSIDE · FILES · 10 OF 187

Linux Filesystem Hierarchy: /etc, /var, /proc, /sys

Where everything lives on a Linux server and why: one tree from /, config in /etc, logs and data in /var, programs in /usr, temporary files in /tmp, and the virtual /proc and /sys that are not on disk at all (/proc/self listed 54 entries in our run). Includes what grows and fills disks, where a service keeps its files, and how to find your way on any unfamiliar server.

  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 8
  9. 9
  10. 10
  11. 11
  12. 12
  13. 13
  14. 14
  15. 15
  16. 16
  17. 17
  18. 18
  19. 19
  20. 20
  21. 21
  22. 22
  23. 23
  24. 24
  25. 25
  26. 26
  27. 27
  28. 28
  29. 29
  30. 30
  31. 31
  32. 32
  33. 33
  34. 34
  35. 35
  36. 36
  37. 37
  38. 38
  39. 39
  40. 40
  41. 41
  42. 42
  43. 43
  44. 44
  45. 45
1 / 12

Press play to hear the narration, or step through at your own pace.

Key takeaways

  • /etc config
  • /var logs and data
  • /usr programs
  • /tmp and /run temporary
  • /proc and /sys live kernel views

Notes

One tree

Linux has one tree, starting at slash, the root. There are no drive letters. A second disk is mounted into the tree at a folder such as slash data. Even devices appear as files under slash dev, like the first disk or the null device that throws data away. The layout follows a standard, the Filesystem Hierarchy Standard, so the same folders mean the same thing on Ubuntu, Red Hat and Alpine.

No drive letters
  • / is the root of one tree
  • other disks are mounted INTO it: /data, /mnt/backup
  • devices appear as files: /dev/sda, /dev/null
  • the layout is the Filesystem Hierarchy Standard (FHS)

/etc

Slash etc holds configuration, almost all of it plain text. The hostname, local name lookups in hosts, DNS servers in resolv conf, disks to mount in fstab, the user list, SSH's server config, nginx's config, and your own systemd unit files. On our Ubuntu machine the whole folder was just eight point three megabytes. Because it is text, it can be versioned and managed by tools like Ansible, which part nine covers.

In /etc
/etc/hostname      the name
/etc/hosts         local DNS
/etc/resolv.conf   DNS servers
/etc/fstab         disks to mount
/etc/passwd        users
/etc/ssh/sshd_config
/etc/nginx/nginx.conf
/etc/systemd/system/  units
our /etc: 8.3 MB

/var

Slash var holds data that changes while the system runs. Logs in var log. Application state in var lib, including databases, Docker's images and package lists. Caches in var cache, and queues in var spool. This is the folder that fills disks. When a server reports a full disk, var log and var lib slash docker are the first places to look, and log rotation, episode nineteen, is the usual cure.

In /var
  • /var/log logs: syslog, nginx, auth.log
  • /var/lib app state: databases, docker images, package lists
  • /var/cache caches
  • /var/spool queues: mail, cron jobs
  • the #1 place disks fill up

/usr and friends

Programs live in slash usr: commands in usr bin, libraries in usr lib, shared data and documentation in usr share. Things you build or install yourself go in usr local, and big third-party applications often go in slash opt. On modern Ubuntu, the old top-level bin and lib folders are just links into usr. Slash boot holds the kernel and initramfs from the last episode. Treat all of these as read-only; the package manager owns them.

Programs
  • /usr/bin programs (ls, curl)
  • /usr/lib libraries
  • /usr/share docs, data
  • /usr/local things you built or installed by hand
  • /opt third-party apps
  • /bin, /lib now links into /usr on Ubuntu
  • /boot kernel, initramfs

Home, tmp, run

Users' files live in slash home, one folder each, and the root user has slash root. Slash tmp is scratch space anyone can write to; its permissions show a t, the sticky bit, which stops people deleting each other's files, and it is often cleared on reboot. Slash run holds runtime files like process ID files and sockets. It lives in memory and disappears on reboot, so never keep anything important there.

Users and scratch space
  • /home/<user> each user's files
  • /root root's home
  • /tmp scratch, anyone can write, often cleared on reboot (sticky bit: drwxrwxrwt)
  • /run runtime files: PID files, sockets; in memory, gone on reboot

/proc

Now the special ones. Slash proc is not on any disk. The kernel invents its contents on the fly, as a live window into the system. Load average, memory, CPU details. Each process has a folder named after its ID, with its status, its command line, and fd, the list of its open files. On our machine the current process's folder had fifty four entries. Tools like top and ps simply read slash proc.

Try these
$ cat /proc/loadavg
$ cat /proc/meminfo
$ cat /proc/cpuinfo
$ ls /proc/self | wc -l
54
$ cat /proc/<pid>/status
$ ls -l /proc/<pid>/fd
# open files of a process

/sys

Slash sys is its sister: devices, drivers and kernel objects as a tree. Slash sys fs cgroup holds the resource limits that containers rely on, which part six opens up. And slash proc sys holds kernel settings you can change while the system runs, which the sysctl command reads and writes, as you will see in episode nineteen. Reading these is safe. Writing them changes the running kernel, so do it deliberately.

/sys and tuning
  • /sys: devices, drivers, cgroups as a tree
  • /sys/fs/cgroup: resource limits (containers use it)
  • /proc/sys: kernel settings you can change live (sysctl reads and writes it)
  • ours: block bus class dev devices firmware fs kernel

Find your way

Put together, a service spreads over the tree in a predictable way. For nginx: config in etc nginx, logs in var log nginx, default web files in usr share, the program in usr sbin, its systemd unit under lib systemd, and its process ID file in slash run. If you are not sure, ask the package manager to list every file a package installed. That one command saves a lot of guessing on a strange server.

nginx, for example
  • config /etc/nginx/ logs /var/log/nginx/ data /usr/share/nginx/html
  • binary /usr/sbin/nginx
  • unit /lib/systemd/system/ nginx.service
  • PID /run/nginx.pid
  • ask the package: dpkg -L nginx

Common mistakes

Mistakes. Keeping important data in slash tmp or slash run, which vanish. Editing files under usr, which the next package update silently overwrites; put overrides in etc. Logs that grow forever in var log. A single small root partition, so one runaway log takes down the whole machine. And trying to clean up slash proc, which is not on disk at all.

Avoid these
  • keeping data in /tmp or /run
  • editing files under /usr (updates overwrite them)
  • logs that grow forever in /var/log
  • one small root partition for everything
  • "deleting" files in /proc

Say it like this

In the interview: Linux has one tree from slash. Configuration lives in etc, changing data and logs in var, programs and libraries in usr. Slash proc and slash sys are virtual, live views of the kernel: memory, load, per-process details and tunable settings. For a misbehaving service I check its config in etc, its logs in var log, its unit file, and how full var is.

In the interview
  • "Config is in /etc, logs and state in /var/log and /var/lib, programs in /usr. /proc and /sys
  • are live kernel views: memory, load, per-process info and tunables. For a service I check
  • /etc/<svc>, /var/log/<svc> and its systemd unit."

Interview questions

Q1What is stored in /etc, /var and /usr?

/etc holds configuration (mostly text), /var holds changing data such as logs, databases, caches and queues, and /usr holds programs, libraries and shared read-only data.

Q2What is /proc?

A virtual filesystem the kernel generates on the fly: system information (loadavg, meminfo, cpuinfo) and a folder per process with its status, command line and open file descriptors.

Q3What is the difference between /proc and /sys?

/proc focuses on processes and system statistics (plus /proc/sys tunables); /sys exposes devices, drivers and kernel objects such as cgroups as a tree.

Q4Where would you look when a disk fills up?

Usually /var: /var/log for logs and /var/lib for application data such as Docker images or databases; du -sh /var/* narrows it down.

Q5What is the sticky bit on /tmp?

A permission (the t in drwxrwxrwt) that lets anyone create files but only the owner delete or rename them.

Q6Why not edit files under /usr?

They belong to packages and are overwritten on updates; put overrides and config in /etc.

Q7How do you find all files installed by a package?

dpkg -L <package> on Debian/Ubuntu, rpm -ql <package> on RHEL-based systems.

Q8What is /run?

A memory-backed folder for runtime files such as PID files and sockets; it is emptied on reboot.