Interview

10 Linux Filesystem Interview Questions and Answers

Prepare for your next technical interview with our comprehensive guide on the Linux filesystem, featuring common questions and detailed answers.

The Linux filesystem is a critical component of the Linux operating system, providing a structured way to store and organize data. Known for its robustness, flexibility, and security features, the Linux filesystem is widely used in various environments, from personal computers to enterprise servers. Its hierarchical structure and support for multiple file types make it a versatile choice for managing complex data storage needs.

This article offers a curated selection of interview questions focused on the Linux filesystem. By reviewing these questions and their detailed answers, you will gain a deeper understanding of key concepts and practical skills, enhancing your readiness for technical interviews and professional challenges.

Linux Filesystem Interview Questions and Answers

1. Describe the difference between hard links and soft (symbolic) links. Provide an example command for creating each.

In the Linux filesystem, a hard link is a directory entry that associates a name with a file on a filesystem. Hard links point directly to the inode of a file, meaning multiple filenames can reference the same physical data on the disk. If the original file is deleted, the data remains accessible through the hard link.

A soft (symbolic) link is a special type of file that contains a reference to another file or directory in the form of a path. Symbolic links can span across different filesystems and can link to directories. If the original file is deleted, the symbolic link becomes a dangling link, pointing to a non-existent file.

Example commands:

Creating a hard link:

ln original_file hard_link

Creating a symbolic link:

ln -s original_file symbolic_link

2. How would you check disk usage of a specific directory and all its subdirectories?

To check the disk usage of a specific directory and all its subdirectories in Linux, you can use the du (disk usage) command. The du command estimates file space usage and provides a summary of the disk usage of each file and directory.

The basic syntax for checking the disk usage of a specific directory is:

du -sh /path/to/directory

Here, the -s option provides a summary of the total disk usage, and the -h option makes the output human-readable by showing sizes in KB, MB, or GB.

If you want to see the disk usage of the directory along with all its subdirectories, you can use:

du -h /path/to/directory

This command will display the disk usage of each subdirectory within the specified directory.

3. What is the purpose of the fstab file, and how would you add a new filesystem to it?

The fstab file, located at /etc/fstab, stands for “File System Table.” It is a configuration file that contains information about all the available disk partitions and storage devices and how they should be integrated into the filesystem structure of the operating system. Each line in the fstab file represents a filesystem and contains six fields:

  • The device or partition (e.g., /dev/sda1)
  • The mount point (e.g., /mnt/data)
  • The filesystem type (e.g., ext4, ntfs)
  • Mount options (e.g., defaults, ro)
  • Dump options (used by the dump utility, usually set to 0 or 1)
  • Filesystem check order (used by fsck, usually set to 0, 1, or 2)

To add a new filesystem to the fstab file, you would follow these steps:

  • Identify the device name (e.g., /dev/sdb1).
  • Create a mount point (e.g., mkdir /mnt/newdisk).
  • Edit the /etc/fstab file and add a new line with the appropriate fields.

Example entry to add a new filesystem:

/dev/sdb1    /mnt/newdisk    ext4    defaults    0    2

This entry tells the system to mount the device /dev/sdb1 to the directory /mnt/newdisk using the ext4 filesystem with default mount options. The 0 indicates that the filesystem should not be dumped, and the 2 indicates that it should be checked second by fsck after the root filesystem.

4. Write a command to find all files larger than 100MB in the /var directory.

To find all files larger than 100MB in the /var directory, you can use the find command in Linux. The find command is a powerful utility that allows you to search for files and directories based on various criteria.

find /var -type f -size +100M

Explanation:

  • /var: Specifies the directory to search in.
  • -type f: Ensures that only files are considered (not directories).
  • -size +100M: Finds files larger than 100MB.

5. Write a script to monitor free space on all mounted filesystems and send an alert if any fall below 10% free space.

To monitor free space on all mounted filesystems and send an alert if any fall below 10% free space, you can use a shell script. The script will utilize the df command to check disk usage and mail or echo for sending alerts.

#!/bin/bash

# Threshold for free space
THRESHOLD=10

# Get the list of filesystems and their free space percentage
df -H | grep -vE '^Filesystem|tmpfs|cdrom' | awk '{ print $5 " " $1 }' | while read output;
do
  # Extract the percentage and filesystem name
  usep=$(echo $output | awk '{ print $1}' | cut -d'%' -f1)
  partition=$(echo $output | awk '{ print $2 }')

  # Check if the free space is below the threshold
  if [ $usep -ge $((100 - THRESHOLD)) ]; then
    echo "Alert: Almost out of disk space on $partition ($usep% used)"
    # Uncomment the following line to send an email alert
    # echo "Alert: Almost out of disk space on $partition ($usep% used)" | mail -s "Disk Space Alert" [email protected]
  fi
done

6. Write a command to list all open files and the processes that opened them.

To list all open files and the processes that opened them in a Linux filesystem, you can use the lsof command. lsof stands for “list open files” and is a powerful utility that provides detailed information about files that are opened by various processes.

Example:

lsof

This command will display a list of all open files along with the associated processes. The output includes columns such as the command name, process ID (PID), user, file descriptor, type, device, size, and the file name.

If you want to filter the results to show open files for a specific user, you can use the -u option followed by the username:

lsof -u username

To list open files for a specific process ID, use the -p option followed by the PID:

lsof -p PID

7. Write a script to back up the /home directory to another drive, ensuring that only changed files are copied.

To back up the /home directory to another drive while ensuring that only changed files are copied, you can use the rsync command. rsync is a powerful tool for efficiently transferring and synchronizing files between different directories or drives. It only copies the files that have changed, which makes it ideal for backup purposes.

Here is a simple script to achieve this:

#!/bin/bash

# Define source and destination directories
SOURCE="/home/"
DESTINATION="/mnt/backup_drive/home_backup/"

# Use rsync to copy only changed files
rsync -av --delete $SOURCE $DESTINATION

In this script:

  • -a stands for “archive” mode, which preserves permissions, timestamps, symbolic links, and other attributes.
  • -v stands for “verbose” mode, which provides detailed output of the process.
  • --delete ensures that files deleted from the source are also deleted from the destination, keeping the backup in sync.

8. Explain how filesystem permissions and ownership work. Provide an example of changing permissions and ownership of a file.

In Linux, filesystem permissions and ownership are fundamental concepts that control access to files and directories. Each file and directory has an associated owner and group, and permissions are set to define what actions the owner, group members, and others can perform.

Permissions are represented by a combination of read (r), write (w), and execute (x) rights. These permissions are grouped into three categories:

  • User (u): The owner of the file.
  • Group (g): Users who are members of the file’s group.
  • Others (o): All other users.

Permissions can be viewed using the ls -l command, which displays a string of characters representing the permissions. For example, -rwxr-xr-- indicates that the owner has read, write, and execute permissions, the group has read and execute permissions, and others have only read permissions.

Ownership of a file includes:

  • User (owner): The user who owns the file.
  • Group: The group associated with the file.

To change permissions, the chmod command is used. To change ownership, the chown command is used.

Example:

# Change permissions to read, write, and execute for the owner, and read and execute for the group and others
chmod 755 example.txt

# Change the owner to 'user' and the group to 'group'
chown user:group example.txt

9. Explain how to set up and manage filesystem quotas for users.

Filesystem quotas are used to limit the amount of disk space and the number of inodes (files) that a user or group can use. This helps in managing disk usage and ensuring that no single user can consume all the available disk space, which could affect other users and system performance.

To set up and manage filesystem quotas for users in a Linux environment, follow these steps:

  • Enable Quotas on the Filesystem:
    Edit the /etc/fstab file to add the usrquota and/or grpquota options to the filesystem where you want to enable quotas. For example:
    /dev/sda1 /home ext4 defaults,usrquota,grpquota 0 2
  • Remount the Filesystem:
    Remount the filesystem to apply the changes:
    mount -o remount /home
  • Create Quota Database Files:
    Create the quota database files using the quotacheck command:
    quotacheck -cum /home
  • Enable Quotas:
    Enable quotas using the quotaon command:
    quotaon /home
  • Set Quotas for Users:
    Use the edquota command to set quotas for individual users. This will open a text editor where you can specify the soft and hard limits for blocks and inodes:
    edquota username
  • Check Quota Usage:
    Use the quota command to check the quota usage for a user:
    quota username

10. Discuss the tools and methods used to check and repair filesystems.

In Linux, checking and repairing filesystems is a task to ensure data integrity and system stability. Several tools and methods are available for this purpose, each suited to different types of filesystems.

fsck (File System Consistency Check): This is a general-purpose tool that can be used to check and repair various types of filesystems. It acts as a front-end for specific filesystem checkers like e2fsck for ext2/ext3/ext4 filesystems, xfs_repair for XFS filesystems, and others.

e2fsck: This tool is specifically designed for ext2, ext3, and ext4 filesystems. It checks the filesystem for errors and attempts to repair them. It can be run manually or automatically during system boot.

xfs_repair: This tool is used for XFS filesystems. Unlike e2fsck, xfs_repair does not run automatically at boot time. It must be run manually and is known for its efficiency in handling large filesystems.

Steps to Check and Repair Filesystems:

  • Unmount the filesystem: Before running any filesystem check or repair tool, it is important to unmount the filesystem to prevent data corruption.
  • Run the appropriate tool: Use fsck, e2fsck, or xfs_repair depending on the filesystem type.
  • Review the output: Carefully review the tool’s output to understand the issues found and the actions taken.

Example usage:

# Unmount the filesystem
umount /dev/sdX1

# Check and repair ext4 filesystem
e2fsck -p /dev/sdX1

# Check and repair XFS filesystem
xfs_repair /dev/sdX1
Previous

15 Cisco ASA Interview Questions and Answers

Back to Interview
Next

10 Netcool Interview Questions and Answers