203.2 Maintaining a Linux filesystem (weight: 3)

Tech Tutorial: 203.2 Maintaining a Linux Filesystem #

Introduction #

In this tutorial, we will explore how to maintain a Linux filesystem effectively, a critical skill for any system administrator or IT professional. Proper filesystem maintenance ensures data integrity, optimal performance, and system reliability. We’ll cover key utilities and commands as outlined in the exam objectives for maintaining filesystems.

Key Knowledge Areas: #

  • Monitor free space and inodes
  • Repair simple filesystem problems
  • Clearing filesystem caches

Utilities: #

  • df
  • du
  • fsck
  • touch
  • mkdir
  • sync
  • echo 3 > /proc/sys/vm/drop_caches

Step-by-Step Guide #

Monitoring Free Space and Inodes #

1. Using df #

The df command displays the amount of disk space used and available on filesystems.

Example:

# Display all filesystems disk space usage
df -h

# Display inode usage
df -i

Output Example:

Filesystem      Size  Used Avail Use% Mounted on
/dev/sda1       100G   30G   65G  30% /

2. Using du #

The du command shows the disk usage of files and directories.

Example:

# Show the disk usage of the current directory
du -sh

# Show the disk usage of a specific directory
du -sh /var/log

Output Example:

1.5G    /var/log

Repairing Simple Filesystem Problems #

3. Using fsck #

The fsck command checks and repairs filesystems. Caution: Ensure the filesystem is unmounted during the check if it’s not the root filesystem.

Example:

# Check and repair filesystem on /dev/sda1
sudo umount /dev/sda1
sudo fsck -y /dev/sda1

Output Example:

fsck from util-linux 2.34
e2fsck 1.45.6 (20-Mar-2020)
/dev/sda1: clean, 112/12800 files, 2342/51200 blocks

Clearing Filesystem Caches #

4. Using echo 3 > /proc/sys/vm/drop_caches #

To free up the cache without interrupting any services or operations, Linux allows you to drop caches.

Example:

# Clear pagecache, dentries, and inodes
sudo sync; sudo echo 3 > /proc/sys/vm/drop_caches

Output Example:

This command does not produce an output but clears the caches.

Additional Commands #

5. touch #

Used to create a new empty file or update the timestamp of an existing file.

Example:

# Create a new empty file
touch newfile.txt

# Update the timestamp of an existing file
touch existingfile.txt

6. mkdir #

Create new directories.

Example:

# Create a single directory
mkdir newdir

# Create multiple directories
mkdir dir1 dir2 dir3

7. sync #

Ensures all modified data is written to disk. This is useful before shutting down or unmounting a filesystem.

Example:

# Sync all pending writes to disk
sync

Conclusion #

Maintaining a Linux filesystem is a vital task for system administrators. By understanding and using the tools and commands such as df, du, fsck, touch, mkdir, and sync, you can ensure the health and efficiency of your Linux filesystems. Regular checks, monitoring, and maintenance routines can prevent data loss and improve system performance. Always remember the importance of backups before performing operations that might affect data integrity.