Chuyển đến nội dung

Bài 5 · Getting Started

The Linux Filesystem Hierarchy for Administrators

Learn where things live on a Linux server: /etc for configuration, /var for changing data, /usr for programs, /proc and /sys for the kernel, and why it matters for backups.

Linux has one directory tree starting at /. Every disk, partition and network share is attached somewhere in that tree. The Filesystem Hierarchy Standard (FHS) describes what each top-level directory is for, so administrators can find configuration, logs and data on any distribution.

Knowing the layout answers practical questions: where do I edit the web server's settings, what must I back up, and what is safe to delete?

The top-level directories

Directory Purpose
/etc System-wide configuration files (text)
/var Variable data: logs, caches, mail, databases, spools
/usr Installed programs, libraries, documentation
/bin, /sbin, /lib On most modern systems symlinks into /usr/bin, /usr/sbin, /usr/lib
/home Users' personal files
/root The root user's home directory
/tmp Temporary files, cleared at reboot or by a timer
/boot Kernel, initramfs and bootloader files
/dev Device files such as /dev/sda
/proc Virtual files exposing processes and kernel data
/sys Virtual files exposing devices and drivers
/run Runtime state since boot (PID files, sockets), kept in memory
/mnt, /media Places to mount disks temporarily
/opt Self-contained third-party software
/srv Data served by this system, such as websites

Config, data and programs are separate

A core idea: programs live in /usr, their configuration in /etc, and the data they produce in /var or /srv. That is why you can reinstall a package without losing settings, and why backups focus on /etc, /home, /var data and your application directories.

flowchart TD
    R["/"] --> E["/etc config"]
    R --> V["/var data + logs"]
    R --> U["/usr programs"]
    R --> H["/home users"]
    R --> P["/proc and /sys kernel views"]

Where an administrator spends time.

Looking at /etc

Terminal
ls /etc
ls /etc/ssh
cat /etc/hostname
cat /etc/fstab        # filesystems to mount at boot
cat /etc/hosts        # local name lookups

Many services use a main file plus a drop-in directory such as /etc/ssh/sshd_config.d/ or /etc/nginx/conf.d/. Put your changes in a drop-in so package upgrades do not overwrite them.

Looking at /var

Terminal
ls /var/log           # log files
ls /var/lib           # service state, e.g. databases
ls /var/cache         # regenerable caches
ls /var/spool         # queued jobs and mail
du -sh /var/*         # which part is big?

When a disk fills up, /var/log and /var/lib are the first suspects (Quotas and disk usage).

The virtual filesystems

/proc and /sys are not on disk. Reading them asks the kernel a question.

Terminal
cat /proc/cpuinfo | head
cat /proc/meminfo | head
cat /proc/loadavg
ls /proc/1/            # information about process 1
cat /sys/class/net/eth0/address

Because they are views, ls -l shows size 0 for many files. Never copy /proc into a backup.

Mount points and the single tree

Attaching a filesystem at a directory hides what was there before and shows the new contents instead. You will mount disks at places like /var or /data (Mounting and fstab).

Terminal
findmnt                  # tree of mounts
df -h                    # space per mounted filesystem
findmnt /var

Find which file or package owns something

Terminal
which sshd                       # where the program is found
type -a python3
dpkg -S /usr/sbin/sshd           # Debian family
rpm -qf /usr/sbin/sshd           # Red Hat family

A stand-in: classify paths

This model maps paths to the kind of data they hold, to decide what a backup should include. It is a teaching table, not a real tool.

Python Stand-in: what is this path for?

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

A stand-in: reading fstab

/etc/fstab has six whitespace-separated fields per line. This model parses a made-up sample.

Python Stand-in: parse fstab lines

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Common mistakes

  • Editing a file under /usr that a package will overwrite on update; configure under /etc instead.
  • Backing up /proc, /sys or /dev.
  • Filling /tmp or /var on a small root filesystem.
  • Forgetting that mounting over a non-empty directory hides its old files.

Check your understanding

Quick check · 4 questions

of 4 answered You scored / 4 · perfect!

  1. 1. Where do system-wide configuration files normally live?

    /etc holds text configuration for the system and its services.
  2. 2. Which directory holds logs that grow over time?

    Logs are variable data and live under /var/log.
  3. 3. What is special about /proc and /sys?

    Reading them queries the running kernel; they should not be backed up.
  4. 4. Why put custom service settings in a drop-in directory under /etc?

    Drop-ins survive upgrades of the packaged main file.

Summary

  • One tree from /; disks attach at mount points.
  • /etc config, /var changing data and logs, /usr programs, /home users.
  • /proc and /sys are virtual; /tmp and caches are disposable.
  • Know the layout to troubleshoot disk usage and plan backups.

Next: Users and the passwd Files

Theo dõi tiến độ của bạn

Tạo tài khoản miễn phí để đánh dấu bài đã học và tiếp tục từ chỗ bạn dừng lại trên mọi thiết bị.