Chapter 7 · Lesson 1 of 11
Linux for servers

Linux filesystem layout explained (/etc, /var, /usr, /home)

The Linux filesystem is one tree starting at /, and the Filesystem Hierarchy Standard says where config (/etc), variable data and logs (/var), programs (/usr) and user files (/home) belong.

By the end you will

Name the purpose of the main Linux directories and find where a service keeps its config, logs and data.

Time
15 min
Level
Beginner
Check
4 questions
You need
  • An Ubuntu or Debian server you can reach over SSH

You log in to a new server and need to change a setting, read an error, or find where an app stores its uploads. On Linux, the answer is almost always in one of a dozen directories, and each has a defined job. Learn the jobs once and you can guess where things live on any distribution.

How it works#

Linux has one filesystem tree that starts at /, called the root directory. There are no drive letters. A second disk, a USB stick or a network share is attached to a folder in the tree, a step called mounting (covered in disk usage and mounts).

The layout follows the Filesystem Hierarchy Standard (FHS), a document that says which kind of file belongs where. Debian and Ubuntu follow it closely. The idea is a split by purpose: programs in one place, configuration in another, changing data in a third. That split makes backups and permissions easier, because you can back up /etc and /var/lib without copying the whole system.

To see the top level on your server:

bash
ls /

Expect names such as bin, boot, dev, etc, home, lib, opt, proc, root, run, srv, sys, tmp, usr and var. The exact list differs a little between machines.

Configuration: /etc#

/etc holds system-wide configuration for the machine, mostly as plain text you can read and edit. Typical entries:

  • /etc/ssh/sshd_config for the SSH server.
  • /etc/nginx/ for nginx, if installed.
  • /etc/fstab for filesystems mounted at boot.
  • /etc/passwd, /etc/group and /etc/sudoers for accounts and privileges.
  • /etc/systemd/system/ for unit files you write yourself.
  • /etc/hosts for local name overrides.

Editing here needs root, and a typo can lock you out of SSH or stop a service from starting. Back up a file before you change it, as shown in read and edit files on Linux. Per-user config is different: it lives in hidden files such as ~/.bashrc and ~/.ssh/config inside the user’s home directory.

Variable data and logs: /var#

/var holds files that change while the system runs. Two subfolders matter most on a server.

/var/log contains text logs such as syslog, auth.log and per-service folders like /var/log/nginx/. Many services on systemd machines also log to the journal, which you read with journalctl. A runaway log in /var/log is a common cause of a full disk.

/var/lib contains the state of installed programs. PostgreSQL data is in /var/lib/postgresql, Docker images and volumes are under /var/lib/docker, and apt keeps its package lists in /var/lib/apt. Treat this directory as precious: it is the data you would be sad to lose.

Other parts of /var include /var/cache (data that can be rebuilt), /var/spool (queued jobs such as cron and mail) and /var/www, which some web server packages use as a default web root.

Programs and libraries: /usr, /bin, /lib#

/usr is where most installed software lives. /usr/bin holds commands, /usr/sbin holds admin commands, /usr/lib holds libraries, and /usr/share holds architecture-independent data such as docs and man pages. /usr/local is reserved for software you install by hand, so a package upgrade never overwrites it.

On current Ubuntu and Debian, /bin, /sbin and /lib are symlinks into /usr. Check with ls -ld /bin. You can ignore the old distinction.

/opt is for self-contained add-on software that ships as one folder, such as a vendor’s agent installed outside apt. It is a convention, not a rule, so you will only find something there if someone put it there.

Users and your own files: /home and /root#

Each normal user gets a directory under /home, such as /home/deploy. It holds that user’s files and hidden settings, and the user can write to it without sudo. ~ is shorthand for your own home directory.

/root is the home directory of the root user. It is separate from /home and is not the same as /. Do not confuse the three: / is the top of the tree, /root is root’s home, /home/alice is Alice’s home. See users, groups and sudo for how accounts map to these folders.

Temporary and served data: /tmp and /srv#

/tmp is scratch space for any user. It can be cleared at reboot or by age, so use it for things you can recreate, never for backups or the only copy of anything.

/srv is meant for data a machine serves out, such as files for a web or FTP server. Many people put site content there, for example /srv/www. The FHS defines it but packages rarely use it, so you decide how to use it and write it down.

Virtual directories: /proc, /dev, /sys, /run#

Some directories are not on disk at all. The kernel builds them in memory.

  • /proc exposes processes and kernel state. cat /proc/meminfo shows memory, and /proc/<pid>/ describes one process.
  • /dev holds device files. Disks appear as /dev/sda or /dev/nvme0n1, and special files include /dev/null (discards writes) and /dev/urandom (random bytes).
  • /sys exposes hardware and driver information.
  • /run holds runtime data such as PID files and sockets. It is cleared on every boot.

These files take no disk space, so ignore them when you wonder where space went.

Check it worked#

Run these on your server and confirm you can place each result:

bash
ls -ld /bin /etc /home /root
ls /var/log | head
ls /var/lib | head
head -n 3 /proc/meminfo
df -h /

ls -ld /bin should show a symlink to usr/bin on a current Ubuntu. /root should be owned by root with mode drwx------, so a normal user gets “Permission denied” if they try to list it.

Edge cases and gotchas#

  • Docker data is under /var/lib/docker. Images, containers and named volumes all live there, and it can grow large. Read Docker volumes and persistent data before you move or clean it.
  • Separate partitions change the picture. /var, /home or /tmp can each be their own disk. A full /var can break logging and package installs while / still shows free space. Run df -h to see which one is full.
  • Files in /tmp can vanish. Do not store uploads, build output you need, or database dumps there.
  • Package files and your files should not mix. Put hand-installed binaries in /usr/local/bin or /opt, not /usr/bin, so apt upgrades do not clash with them.
  • Hidden files start with a dot. ls hides .bashrc, .ssh and similar. Use ls -a or ls -la to see them.
  • /root and / are different places. Running rm -rf /root and rm -rf / are very different mistakes, and both are destructive. Read the path twice.
  • Mounted folders hide what was under them. If you mount a disk on /srv while files already exist there, those files are hidden until you unmount it.

Troubleshooting#

SymptomLikely causeFix
ls: cannot open directory '/root': Permission deniedYou are not root and /root is privateUse sudo ls /root, or work in your own /home/<user>
No such file or directory for /etc/nginx/nginx.confThe package is not installed, or config is elsewhereRun dpkg -l nginx and nginx -V to check install and config path
No space left on device while df -h / shows free space/var or another mount is fullRun df -h and clear the full mount, see the full disk lesson
bash: /usr/local/bin/app: No such file or directory for a file that existsWrong architecture or missing interpreterRun file /usr/local/bin/app and install the matching build
cd: /srv/www: No such file or directory/srv/www is a convention, not created by defaultCreate it with sudo mkdir -p /srv/www
Files in /tmp disappeared after reboot/tmp is cleared on bootMove durable files to /var/lib/<app> or /home/<user>
/var/log fills the diskA service logs without rotationCheck sizes with sudo du -sh /var/log/* | sort -h and fix rotation

Quick reference#

bash
ls /                      # top-level directories
ls -ld /bin               # is /bin a symlink to /usr/bin?
ls /etc                   # system configuration
ls /var/log               # text logs
ls /var/lib               # service data and state
ls /usr/local/bin         # software you installed by hand
head -n 3 /proc/meminfo   # kernel view of memory
df -h                     # which mount is full

For a worked example of keeping a site’s files in one place, see self-host a static site with Docker.

Next#

Now that you know where things live, learn to move through them without breaking anything in work with files safely.

Check what you learned4 questions
1Where should you look first for a program's system-wide configuration file?
2Which directory is the best place for a database's data files on a typical Debian or Ubuntu package install?
3What happens to files in /tmp?
4What is /proc?

Frequently asked questions

What is the root directory in Linux?

The root directory is /, the top of the single filesystem tree. Every file and mounted disk appears somewhere below it. It is not the same as /root, which is the home directory of the [root user](/learn/glossary/#root-user).

Where are config files stored in Linux?

System-wide config lives in /etc, usually one file or folder per program, such as /etc/ssh/sshd_config or /etc/nginx/. Per-user config lives in hidden files in the user's home directory.

Where are log files in Linux?

Traditional text logs are under /var/log. On systemd machines the journal holds most service logs, which you read with journalctl.

What is the difference between /bin and /usr/bin?

Historically /bin held essential commands and /usr/bin held the rest. On current Ubuntu and Debian, /bin is a symlink to /usr/bin, so they are the same place.

Sources

View MD Edit
Share
Helpful?
Progress is saved in this browser only.

Keep going: guides and tutorials

View MD Edit
Share
Helpful?
Run this on your own server

Levelrail deploys apps from git with health-checked releases and one-click rollback.

docker pull ghcr.io/glincker/levelrail:latest Install Levelrail From GLINR Studios