Linux › Section G · System configuration › Topic G.2

The structure of /etc

Almost everything that makes one Linux machine behave differently from another is a text file under /etc. Knowing how that directory is organised, and which edits are safe, lets you change a system without breaking it.

What /etc is for

/etc holds system-wide, host-specific configuration, nearly all of it plain text. Compare the neighbours: /usr/lib and /usr/share hold defaults shipped by packages, /var holds changing data such as logs and spools, and a user's home directory holds personal settings in dot files. The rule of thumb is that /etc is where an administrator's decisions live. Because the files are text, you can read them, compare them with diff, back them up with a copy and keep them under version control. Most daemons read their files only at start-up or when told to reload, so an edit rarely takes effect until you reload or restart the service.

Files you meet first

Some files appear on nearly every system: /etc/hostname (the machine's name), /etc/hosts (a local name-to-address table), /etc/fstab (filesystems to mount), /etc/passwd, /etc/group and /etc/shadow (accounts; see the next topic), /etc/os-release (which distribution and version), /etc/shells (valid login shells), /etc/nsswitch.conf (where to look up names and users), /etc/resolv.conf (DNS servers), /etc/environment and /etc/profile (login settings), and /etc/machine-id (a unique identifier for the installation). Several have their own manual pages in section 5, so man 5 fstab or man 5 passwd explains the exact format.

The .d directory pattern

A directory ending in .d, such as /etc/cron.d, /etc/sudoers.d, /etc/rsyslog.d, /etc/logrotate.d or /etc/ssh/sshd_config.d, holds many small fragments instead of one large file. Software reads the fragments in lexical order, so a name like 10-base.conf loads before 90-local.conf. The benefit is that a package can drop in its own fragment and you can drop in yours without either of you editing the same file, and removing a fragment undoes exactly that change. Naming matters: sudo ignores files in sudoers.d whose names contain a dot or end in a tilde. Always check the application's manual to learn which names it reads.

Vendor defaults versus local overrides

Packages place their defaults under /usr/lib, and local configuration under /etc takes precedence. systemd shows this clearly: a unit file in /usr/lib/systemd/system belongs to the package and may be replaced on upgrade, while the same name in /etc/systemd/system wins. To change a packaged unit, run systemctl edit nginx.service, which creates a drop-in at /etc/systemd/system/nginx.service.d/override.conf, rather than editing the vendor file. The journal's own settings follow the same layering under journald.conf.d. Older conventions persist for service options: /etc/default on Debian-family systems and /etc/sysconfig on Fedora and RHEL families.

Identity: hostnamectl and localectl

Instead of editing files by hand, systemd provides tools. hostnamectl status shows the static, pretty and transient hostnames; hostnamectl hostname web01 sets all three and stores the static one in /etc/hostname (set-hostname, the verb used before systemd 249, still works as an obsolete alias). The older hostname web01 command changes only the running name, so it is lost at reboot. Use short names of lowercase letters, digits and hyphens. localectl status shows the system locale and keyboard layout; localectl set-locale LANG=en_US.UTF-8 changes the locale, localectl list-locales lists the options, and localectl set-keymap sets the console keymap. The settings are kept in files such as /etc/locale.conf and /etc/vconsole.conf, though Debian-family systems may use /etc/default/locale as well.

Worked example: where does the change go?

Decide where each change belongs. 1. Run a backup script every night: add a file in /etc/cron.d/ or a crontab, not an edit to a package's cron entry. 2. Let user ana restart one service without a password: create /etc/sudoers.d/ana using sudo visudo -f /etc/sudoers.d/ana, which checks syntax on save. 3. Rename the machine: hostnamectl hostname, which updates /etc/hostname. 4. Add a restart policy to a packaged service: systemctl edit, which writes a drop-in under /etc/systemd/system. 5. Check each answer: every change is a new small file or a tool call, and no vendor file under /usr/lib was touched, so a package upgrade cannot undo your work.

Symlinks and generated files

Not every file in /etc is meant to be edited. /etc/localtime is a symbolic link into /usr/share/zoneinfo, and timedatectl set-timezone replaces it. /etc/os-release is commonly a link to /usr/lib/os-release. /etc/resolv.conf is often a link to a file maintained by systemd-resolved or NetworkManager, and the owning service regenerates it, so a hand edit can vanish after a network change. /etc/alternatives holds links managed by update-alternatives. Run ls -l on a file first: an arrow in the output means that you are looking at a pointer, and the real configuration lives elsewhere.

Worked example: a safe edit of sshd_config

Suppose you must change the SSH server's settings, where a typing error could lock you out. 1. Make a backup: sudo cp -a /etc/ssh/sshd_config /etc/ssh/sshd_config.bak. 2. Open a second terminal logged in as a safety net. 3. Edit the file or, where the main file includes it, add a fragment in /etc/ssh/sshd_config.d/. 4. Validate: sudo sshd -t prints nothing when the syntax is valid. 5. Reload: sudo systemctl reload ssh (the unit is called sshd on Fedora and RHEL). 6. From a third terminal, start a new login to confirm it works before closing the safety net. If step 6 fails, restore the backup from the surviving session. The same pattern, backup then validate then reload then test, suits most services.

Permissions and package upgrades

Most of /etc is world-readable, but secrets are restricted: /etc/shadow is readable only by root (and on Debian-family systems the shadow group), /etc/sudoers is mode 0440, and SSH private host keys are closed to ordinary users. When a package ships a new default for a file you have changed, it does not silently overwrite your edit. Debian's dpkg asks, or leaves the new version beside yours with a .dpkg-dist suffix; rpm leaves .rpmnew or saves your old file as .rpmsave. Compare with diff, merge by hand, and delete the leftover. Tools such as etckeeper commit /etc to a version-control repository automatically, giving you history and an easy rollback.

Common mistakes to avoid

First, editing a vendor file under /usr/lib, which the next package upgrade may replace. Second, editing /etc/resolv.conf or another generated link directly. Third, changing a file but forgetting to reload the service, then believing the change failed. Fourth, saving a fragment with a name the program ignores, such as a dotted sudoers.d file. Fifth, editing the live sudoers file with a normal editor instead of visudo, where one syntax error can remove sudo access. Sixth, changing the hostname with the plain hostname command and expecting it to persist. Seventh, closing your only root session before testing a new SSH configuration.

Key takeaways

/etc holds host-specific text configuration; vendor defaults live under /usr/lib and /usr/share. Directories ending in .d take small fragments read in lexical order, and local files in /etc override vendor ones. Use hostnamectl and localectl for identity and locale, systemctl edit for unit overrides, and visudo for sudo rules. Check whether a file is a symlink before editing. Back up, validate, reload, then test from a second session. Expect .dpkg-dist, .rpmnew and .rpmsave files after upgrades, and consider etckeeper for history.

Flashcards

Flashcard

/etc

System-wide, host-specific configuration, mostly text

Administrator decisions live here.

Test your understanding

Question 1 of 5

You need to add a restart policy to nginx.service, a unit supplied by a package. Where should your change live?

Question 2 of 5

Which command changes a machine's name so that it persists after a reboot?

Question 3 of 5

Before reloading SSH after editing sshd_config, which step checks the syntax without applying it?

Question 4 of 5

Why can a hand edit of /etc/resolv.conf disappear after a network change?

Question 5 of 5

A package upgrade leaves sshd_config.dpkg-dist next to your edited sshd_config. What is the correct action?

← Previous topicNext topic →