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.
- / 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.
/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.
- /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.
- /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.
- /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.
$ 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: 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.
- 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.
- 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.
- "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."