Linux Directory Structure: /etc, /var, /usr, /home Guide

Master Linux filesystem hierarchy with this comprehensive guide to key directories. Learn /etc, /var, /usr, and /home structure for effective system administration.

Linux Directory Structure: Understanding /etc, /var, /usr, and /home

Introduction

The Linux directory structure follows the Filesystem Hierarchy Standard (FHS), which defines the purpose and contents of various directories. Understanding the role of key directories like /etc, /var, /usr, and /home is crucial for effective system administration, troubleshooting, and general Linux usage. This comprehensive guide explores these directories in detail, providing practical examples and commands to help you navigate and manage Linux systems effectively.

Overview of Linux Directory Structure

The Linux filesystem is organized in a hierarchical tree structure starting from the root directory (/). Each directory serves a specific purpose and contains files and subdirectories that follow standardized conventions.

Key Characteristics

- Hierarchical Structure: All directories branch from the root (/) - Case Sensitive: Directory and file names are case-sensitive - Standardized: Follows FHS guidelines for consistency across distributions - Permission-based: Each directory has specific access controls - Mount Points: Directories can serve as mount points for filesystems

The /etc Directory

Purpose and Function

The /etc directory contains system-wide configuration files and shell scripts used during boot process. The name "etc" historically stands for "et cetera" but is now interpreted as "Editable Text Configuration" or "Extended Tool Chest."

Key Characteristics

- Contains system configuration files - Files are typically plain text and human-readable - Requires root privileges for modification - Changes affect the entire system - Critical for system functionality

Important Subdirectories and Files

| Path | Description | Purpose | |------|-------------|---------| | /etc/passwd | User account information | Stores user account details | | /etc/shadow | Encrypted password information | Contains password hashes | | /etc/group | Group account information | Defines user groups | | /etc/hosts | Static hostname-to-IP mappings | Local DNS resolution | | /etc/fstab | Filesystem mount table | Defines filesystem mounts | | /etc/crontab | System-wide cron jobs | Scheduled tasks | | /etc/sudoers | Sudo configuration | Privilege escalation rules | | /etc/ssh/ | SSH configuration directory | SSH server and client settings | | /etc/network/ | Network configuration | Network interface settings | | /etc/systemd/ | Systemd configuration | Service management |

Common Commands for /etc

`bash

View user accounts

cat /etc/passwd

Check system hostname configuration

cat /etc/hostname

View network interfaces (on some distributions)

cat /etc/network/interfaces

List all configuration files in /etc

ls -la /etc/

Search for specific configuration files

find /etc -name "*.conf" -type f

View SSH server configuration

sudo cat /etc/ssh/sshd_config

Check cron jobs

cat /etc/crontab

View DNS resolver configuration

cat /etc/resolv.conf `

Configuration File Examples

#### /etc/passwd Structure ` username:x:UID:GID:GECOS:home_directory:shell root:x:0:0:root:/root:/bin/bash user1:x:1001:1001:User One:/home/user1:/bin/bash `

#### /etc/hosts Example ` 127.0.0.1 localhost 127.0.1.1 hostname.domain hostname 192.168.1.10 server1.local server1 `

#### /etc/fstab Example `

filesystem mount-point type options dump pass

/dev/sda1 / ext4 defaults 0 1 /dev/sda2 /home ext4 defaults 0 2 tmpfs /tmp tmpfs defaults 0 0 `

Security Considerations

- Many files contain sensitive information - Backup before making changes - Use proper file permissions - Test configuration changes carefully - Monitor for unauthorized modifications

The /var Directory

Purpose and Function

The /var directory contains variable data files that change during system operation. This includes logs, databases, cache files, and other data that grows or changes over time.

Key Characteristics

- Contains variable and changing data - Files grow during system operation - Includes system logs and temporary files - Often requires regular maintenance - May need separate partition for large systems

Important Subdirectories

| Path | Description | Typical Contents | |------|-------------|------------------| | /var/log/ | System log files | Application and system logs | | /var/cache/ | Application cache data | Package cache, web cache | | /var/lib/ | Application state information | Database files, application data | | /var/spool/ | Spool directories | Print queues, mail queues | | /var/tmp/ | Temporary files (persistent) | Temporary files preserved between reboots | | /var/run/ | Runtime variable data | Process IDs, socket files | | /var/lock/ | Lock files | Process lock files | | /var/mail/ | User mailboxes | Local mail storage | | /var/www/ | Web server content | Web server document root | | /var/backups/ | System backups | Automated backup files |

Common Commands for /var

`bash

View system logs

tail -f /var/log/syslog tail -f /var/log/messages

Check disk usage in /var

du -sh /var/*

Monitor log files in real-time

journalctl -f

View authentication logs

tail /var/log/auth.log

Check web server logs

tail /var/log/apache2/access.log tail /var/log/nginx/access.log

Clean package cache

sudo apt clean # Clears /var/cache/apt/

View mail queue

mailq

Check process IDs

ls /var/run/*.pid

Monitor system log files

multitail /var/log/syslog /var/log/auth.log `

Log File Management

#### Common Log Files

| Log File | Purpose | Typical Location | |----------|---------|------------------| | syslog | General system messages | /var/log/syslog | | auth.log | Authentication attempts | /var/log/auth.log | | kern.log | Kernel messages | /var/log/kern.log | | boot.log | Boot process messages | /var/log/boot.log | | cron.log | Cron job execution | /var/log/cron.log | | mail.log | Mail server logs | /var/log/mail.log |

#### Log Rotation Configuration

`bash

View logrotate configuration

cat /etc/logrotate.conf

Check specific log rotation rules

ls /etc/logrotate.d/

Manually rotate logs

sudo logrotate /etc/logrotate.conf `

Maintenance Tasks

`bash

Clean old log files

sudo find /var/log -name "*.log" -mtime +30 -delete

Check for large files in /var

sudo find /var -size +100M -exec ls -lh {} \;

Monitor disk space usage

watch df -h /var

Clean temporary files

sudo rm -rf /var/tmp/* `

The /usr Directory

Purpose and Function

The /usr directory contains user utilities and applications. It's often called the "Unix System Resources" directory and contains the majority of user utilities and applications installed on the system.

Key Characteristics

- Contains user programs and utilities - Usually read-only during normal operation - Can be shared across multiple systems - Contains documentation and source code - Large directory with many subdirectories

Important Subdirectories

| Path | Description | Contents | |------|-------------|----------| | /usr/bin/ | User command binaries | Standard user commands | | /usr/sbin/ | System administration binaries | System administration tools | | /usr/lib/ | Shared libraries | Library files for programs | | /usr/lib64/ | 64-bit shared libraries | 64-bit library files | | /usr/share/ | Architecture-independent data | Documentation, icons, themes | | /usr/include/ | Header files | C/C++ header files | | /usr/src/ | Source code | Kernel and application source | | /usr/local/ | Local hierarchy | Locally compiled software | | /usr/games/ | Games and educational programs | Game binaries | | /usr/libexec/ | Internal binaries | Programs called by other programs |

Detailed Subdirectory Analysis

#### /usr/bin/ - User Binaries Contains executable programs available to all users:

`bash

List common user programs

ls /usr/bin/ | head -20

Find specific programs

which python3 which gcc which vim

Count total programs

ls /usr/bin/ | wc -l `

#### /usr/sbin/ - System Administration Binaries Contains system administration tools:

`bash

List system administration tools

ls /usr/sbin/ | grep -E "(service|mount|network)"

Check if command is in sbin

which useradd which crontab `

#### /usr/share/ - Shared Data Contains architecture-independent files:

`bash

View documentation

ls /usr/share/doc/

Check manual pages

ls /usr/share/man/

View application data

ls /usr/share/applications/

Check icons and themes

ls /usr/share/icons/ ls /usr/share/themes/ `

#### /usr/local/ - Local Hierarchy Used for locally compiled software:

`bash

Structure mirrors main system

ls /usr/local/

Typical output: bin/ sbin/ lib/ share/ etc/ var/

Check locally installed programs

ls /usr/local/bin/

View local libraries

ls /usr/local/lib/ `

Common Commands for /usr

`bash

Find all executable files

find /usr/bin /usr/sbin -type f -executable

Check program locations

whereis python whereis gcc

View shared library dependencies

ldd /usr/bin/ls

Search for specific files

locate filename find /usr -name "filename"

Check disk usage

du -sh /usr/*

List installed packages (Debian/Ubuntu)

dpkg -l | grep package_name

View program manual

man program_name

Check program version

program_name --version `

Package Management Integration

The /usr directory is closely integrated with package management systems:

#### Debian/Ubuntu (APT) `bash

Install package

sudo apt install package_name

Remove package

sudo apt remove package_name

List files installed by package

dpkg -L package_name

Find which package owns a file

dpkg -S /usr/bin/program_name `

#### Red Hat/CentOS (YUM/DNF) `bash

Install package

sudo yum install package_name sudo dnf install package_name

List package files

rpm -ql package_name

Find package owning file

rpm -qf /usr/bin/program_name `

The /home Directory

Purpose and Function

The /home directory contains user home directories. Each user (except root) typically has a subdirectory under /home that serves as their personal workspace and storage area.

Key Characteristics

- Contains user personal directories - Each user owns their home directory - Stores personal files and configurations - User-specific application data - Often mounted on separate partition

Directory Structure

` /home/ ├── user1/ │ ├── Desktop/ │ ├── Documents/ │ ├── Downloads/ │ ├── Music/ │ ├── Pictures/ │ ├── Videos/ │ ├── .bashrc │ ├── .profile │ └── .config/ ├── user2/ └── user3/ `

User Home Directory Contents

| Item | Type | Description | |------|------|-------------| | Desktop/ | Directory | Desktop files and shortcuts | | Documents/ | Directory | User documents | | Downloads/ | Directory | Downloaded files | | Pictures/ | Directory | Image files | | Videos/ | Directory | Video files | | Music/ | Directory | Audio files | | .bashrc | File | Bash shell configuration | | .profile | File | Shell profile settings | | .config/ | Directory | Application configurations | | .ssh/ | Directory | SSH keys and configuration | | .local/ | Directory | User-local application data |

Hidden Files and Directories

User home directories contain many hidden files (starting with dot):

`bash

Show all files including hidden

ls -la ~/

Common hidden files

ls -la ~/.* | head -10

View shell configuration

cat ~/.bashrc cat ~/.profile

Check SSH configuration

ls -la ~/.ssh/ `

Common Hidden Files

| File/Directory | Purpose | Description | |----------------|---------|-------------| | .bashrc | Shell configuration | Bash shell settings | | .profile | Login shell profile | Environment variables | | .bash_history | Command history | Previously executed commands | | .ssh/ | SSH configuration | SSH keys and settings | | .config/ | Application config | User application settings | | .local/ | Local user data | User-specific application data | | .cache/ | Application cache | Temporary application data | | .vimrc | Vim configuration | Vim editor settings | | .gitconfig | Git configuration | Git version control settings |

Common Commands for /home

`bash

Navigate to home directory

cd ~ cd $HOME cd /home/$USER

Check home directory size

du -sh ~

List all users' home directories

ls -la /home/

View current user information

id whoami echo $HOME

Check disk quota (if enabled)

quota -u username

Backup home directory

tar -czf home_backup.tar.gz ~

Find large files in home

find ~ -size +100M -type f

Search for specific files

find ~ -name "*.pdf" find ~ -name "*.conf"

Check directory permissions

ls -ld ~ `

User Environment Configuration

#### Shell Configuration Files

`bash

Edit bash configuration

nano ~/.bashrc

Add custom aliases

echo "alias ll='ls -la'" >> ~/.bashrc

Set environment variables

echo "export EDITOR=vim" >> ~/.bashrc

Reload configuration

source ~/.bashrc `

#### Example .bashrc Content

`bash

Custom aliases

alias ll='ls -alF' alias la='ls -A' alias l='ls -CF' alias grep='grep --color=auto'

Environment variables

export EDITOR=vim export PATH=$PATH:~/bin

Custom functions

function mkcd() { mkdir -p "$1" && cd "$1" }

Command prompt customization

PS1='\u@\h:\w\$ ' `

Home Directory Management

#### Creating User Home Directories

`bash

Add new user with home directory

sudo useradd -m -s /bin/bash username

Create home directory for existing user

sudo mkhomedir_helper username

Set home directory permissions

sudo chown -R username:username /home/username sudo chmod 755 /home/username `

#### Home Directory Backup Strategies

`bash

Simple tar backup

tar -czf /backup/home_$(date +%Y%m%d).tar.gz /home/

Rsync backup

rsync -av /home/ /backup/home/

Exclude certain directories

tar --exclude='.cache' --exclude='Downloads' -czf backup.tar.gz ~ `

Directory Comparison and Relationships

Functional Comparison

| Directory | Primary Function | Data Type | Modification Frequency | |-----------|------------------|-----------|----------------------| | /etc | System configuration | Static configuration | Low | | /var | Variable data | Dynamic data | High | | /usr | User programs | Static programs | Low | | /home | User data | Personal files | High |

Permission Patterns

| Directory | Typical Owner | Typical Permissions | Access Pattern | |-----------|---------------|-------------------|----------------| | /etc | root:root | 644 (files), 755 (dirs) | Root write, all read | | /var | Various | Various | Application-specific | | /usr | root:root | 644 (files), 755 (dirs) | Root write, all read | | /home | user:user | 755 (home), 644 (files) | User-specific |

Storage Considerations

#### Separate Partition Recommendations

`bash

Check current mount points

df -h

View partition layout

lsblk

Common partition schemes:

/ - Root partition (10-20GB minimum)

/home - User data (varies by usage)

/var - Variable data (5-10GB minimum)

/usr - User programs (5-15GB)

`

Interdependencies

- /etc configurations affect all other directories - /usr programs write logs to /var - /home directories use programs from /usr - /var contains user-specific data (mail, cron jobs)

Advanced Topics and Best Practices

Security Considerations

#### File Permissions

`bash

Check permissions

ls -la /etc/passwd ls -la /etc/shadow

Set proper permissions

sudo chmod 644 /etc/passwd sudo chmod 640 /etc/shadow

Monitor permission changes

sudo find /etc -type f -perm /o+w `

#### Access Control

`bash

Use sudo for system files

sudo nano /etc/hosts

Restrict access to sensitive directories

sudo chmod 700 /home/username/.ssh

Monitor file access

sudo auditctl -w /etc/passwd -p wa `

Monitoring and Maintenance

#### Disk Usage Monitoring

`bash

Monitor directory sizes

watch "du -sh /etc /var /usr /home"

Find large files

find /var -size +100M -exec ls -lh {} \;

Check inode usage

df -i `

#### Log Management

`bash

Rotate logs manually

sudo logrotate -f /etc/logrotate.conf

Clean old logs

sudo journalctl --vacuum-time=30d

Monitor log growth

watch "ls -lh /var/log/*.log" `

Backup Strategies

#### System Configuration Backup

`bash

Backup /etc directory

sudo tar -czf /backup/etc_$(date +%Y%m%d).tar.gz /etc

Version control for configurations

cd /etc sudo git init sudo git add . sudo git commit -m "Initial configuration backup" `

#### User Data Backup

`bash

Backup all home directories

sudo tar -czf /backup/home_$(date +%Y%m%d).tar.gz /home

Incremental backup with rsync

rsync -av --delete /home/ /backup/home/ `

Troubleshooting Common Issues

#### Configuration Problems

`bash

Backup before changes

sudo cp /etc/file /etc/file.backup

Test configuration syntax

sudo nginx -t # For nginx sudo apache2ctl configtest # For Apache

Restore from backup

sudo cp /etc/file.backup /etc/file `

#### Disk Space Issues

`bash

Find large directories

sudo du -sh /* | sort -h

Clean package cache

sudo apt clean sudo yum clean all

Remove old logs

sudo find /var/log -name "*.log" -mtime +30 -delete `

#### Permission Issues

`bash

Fix home directory permissions

sudo chown -R username:username /home/username

Reset /etc permissions

sudo find /etc -type f -exec chmod 644 {} \; sudo find /etc -type d -exec chmod 755 {} \; `

Conclusion

Understanding the /etc, /var, /usr, and /home directories is fundamental to Linux system administration and daily usage. Each directory serves a specific purpose in the filesystem hierarchy:

- /etc provides centralized system configuration - /var handles dynamic and changing data - /usr contains user programs and utilities - /home stores user personal data and configurations

Mastering these directories enables effective system management, troubleshooting, and optimization. Regular maintenance, proper backup strategies, and security considerations ensure system stability and data protection. The commands and examples provided in this guide serve as practical tools for exploring and managing these critical filesystem components.

By understanding the relationships between these directories and their roles in the Linux ecosystem, administrators and users can make informed decisions about system configuration, storage allocation, and maintenance procedures. This knowledge forms the foundation for advanced Linux system administration and helps in developing efficient workflows for various Linux distributions and environments.

Tags

  • Linux
  • directory-structure
  • fhs
  • filesystem
  • system-administration

Related Articles

Popular Technical Articles & Tutorials

Explore our comprehensive collection of technical articles, programming tutorials, and IT guides written by industry experts:

Browse all 8+ technical articles | Read our IT blog

Linux Directory Structure: /etc, /var, /usr, /home Guide