Access File Systems in Red Hat Enterprise Linux

Red Hat Enterprise LinuxBeginner
Practice Now

Introduction

In this lab, you will gain hands-on experience managing Linux file systems on a Red Hat Enterprise Linux (RHEL) system. You will learn to identify file systems and block devices, examine disk usage using df and du, and practice manually mounting and unmounting file systems.

Furthermore, this lab will guide you through locating files efficiently using commands like locate and find, enabling you to search for files based on various criteria such as name, ownership, permissions, size, time, and file type.

Identify File Systems and Block Devices

In this step, you will learn how to identify file systems and block devices on a Red Hat Enterprise Linux system. Understanding how storage is organized is fundamental for system administration. We will explore various commands to list and examine block devices and their associated file systems.

First, let's understand some core concepts:

  • Block Device: A block device is a file that provides low-level access to storage devices. Examples include hard drives, SSDs, and USB drives. In Linux, these are typically found in the /dev directory.
  • Partition: A partition is a logical division of a physical storage device. A single hard drive can have multiple partitions, each formatted with a different file system or used for different purposes.
  • File System: A file system is a method and data structure that an operating system uses to control how data is stored and retrieved. It organizes data into files and directories. Common Linux file systems include XFS and ext4.
  • Mount Point: A mount point is an empty directory in the file system hierarchy where a file system is attached or "mounted" to make its contents accessible.

Let's start by listing the block devices available on your system using the lsblk command. This command provides a tree-like overview of all block devices and their partitions.

lsblk

You should see output similar to this, showing devices like vda, vdb, etc., which represent virtual disk devices in your container environment:

NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINTS
vda    253:0    0   40G  0 disk
├─vda1 253:1    0    1M  0 part
├─vda2 253:2    0  100M  0 part /boot/efi
└─vda3 253:3    0 39.9G  0 part /
vdb    253:16   0   40G  0 disk

In the output:

  • NAME: The name of the block device (e.g., vda, vdb) or partition (e.g., vda1, vda2).
  • MAJ:MIN: Major and minor device numbers.
  • RM: Removable device (1 if removable, 0 if not).
  • SIZE: The size of the device or partition.
  • RO: Read-only (1 if read-only, 0 if not).
  • TYPE: Type of the device (e.g., disk, part for partition).
  • MOUNTPOINTS: Where the device or partition is currently mounted.

Next, let's examine the file systems and their usage with the df command. The df command reports file system disk space usage.

df

The output will show various file systems, their total size, used space, available space, and mount points:

Filesystem     1K-blocks    Used Available Use% Mounted on
devtmpfs            4096       0      4096   0% /dev
tmpfs            1822216       0   1822216   0% /dev/shm
tmpfs             728888     616    728272   1% /run
efivarfs             256       9       243   4% /sys/firmware/efi/efivars
/dev/vda3       41773036 3628732  38144304   9% /
/dev/vda2         102156    7198     94958   8% /boot/efi
tmpfs             364440       0    364440   0% /run/user/1000

To make the output more readable, especially for sizes, you can use the -h option for human-readable format (e.g., M for MiB, G for GiB).

df -h

You will see sizes in a more understandable format:

Filesystem      Size  Used Avail Use% Mounted on
devtmpfs        4.0M     0  4.0M   0% /dev
tmpfs           1.8G     0  1.8G   0% /dev/shm
tmpfs           712M  616K  712M   1% /run
efivarfs        256K  8.5K  243K   4% /sys/firmware/efi/efivars
/dev/vda3        40G  3.5G   37G   9% /
/dev/vda2       100M  7.1M   93M   8% /boot/efi
tmpfs           356M     0  356M   0% /run/user/1000

Finally, let's use the lsblk -fp command to list the full path of devices, their UUIDs (Universally Unique Identifiers), and file system types. UUIDs are stable identifiers that remain the same even if device names change, making them useful for consistent mounting.

lsblk -fp

The output will include UUIDs and file system types:

NAME        FSTYPE FSVER LABEL UUID                                 FSAVAIL FSUSE% MOUNTPOINTS
/dev/vda
├─/dev/vda1
├─/dev/vda2 vfat   FAT16       E52E-0564                              92.7M     7% /boot/efi
└─/dev/vda3 xfs          root  4c234c8b-4f67-4d65-abb5-06753b1ec236   36.4G     9% /
/dev/vdb

Notice the UUID column, which provides a unique identifier for each file system. This is crucial for reliably mounting file systems, especially in configuration files like /etc/fstab.

Examine File System Usage with df and du

In this step, you will delve deeper into examining file system usage using the df and du commands. While df provides an overview of disk space usage for mounted file systems, du (disk usage) is used to estimate file space usage for specific files or directories. Understanding the difference and when to use each command is crucial for effective disk space management.

Let's start by revisiting the df command with its human-readable option. This command is excellent for getting a quick summary of how much space is used and available on all mounted file systems.

df -h

The output will show the disk usage in an easy-to-read format (e.g., G for gigabytes, M for megabytes):

Filesystem      Size  Used Avail Use% Mounted on
devtmpfs        892M     0  892M   0% /dev
tmpfs           915M     0  915M   0% /dev/shm
tmpfs           915M   17M  899M   2% /run
tmpfs           915M     0  915M   0% /sys/fs/cgroup
/dev/vda4       8.0G  1.4G  6.7G  17% /
/dev/vda3      1014M  166M  849M  17% /boot
tmpfs           183M     0  183M   0% /run/user/1000

Now, let's explore the du command. Unlike df, du calculates the disk space used by files and directories within a specified path. This is particularly useful when you want to find out which directories or files are consuming the most space.

To see the disk usage of your current directory (~/project), use du without any arguments. This will list the size of every file and subdirectory within ~/project.

du

The output might be extensive, showing sizes in kilobytes by default:

4       ./.config/xfce4/xfconf/xfce-perchannel-xml
8       ./.config/xfce4/xfconf
12      ./.config/xfce4
16      ./.config
4       ./.local/share/nano
8       ./.local/share
12      ./.local
28      .

To make the output more readable, similar to df -h, you can use the -h option with du.

du -h

This will display the sizes in human-readable units:

4.0K    ./.config/xfce4/xfconf/xfce-perchannel-xml
8.0K    ./.config/xfce4/xfconf
12K     ./.config/xfce4
16K     ./.config
4.0K    ./.local/share/nano
8.0K    ./.local/share
12K     ./.local
28K     .

Often, you are interested in the total size of a directory rather than the size of each individual file and subdirectory. For this, you can use the -s (summary) option along with -h. Let's check the total size of your home directory (~).

du -sh ~

This command will output the total size of your home directory:

48K     /home/labex

Let's create some files to see how du reports their sizes. We will create a directory named test_data and then create a few files inside it.

First, create the directory:

mkdir ~/project/test_data

Now, navigate into the test_data directory:

cd ~/project/test_data

Next, create a few files with some content. We'll use the head command to generate files of specific sizes.

head -c 1K < /dev/urandom > file1.txt
head -c 5K < /dev/urandom > file2.txt
head -c 10K < /dev/urandom > file3.txt

Now, use du -h to see the sizes of these new files and the test_data directory.

du -h

You should see output similar to this:

1.0K    ./file1.txt
5.0K    ./file2.txt
10K     ./file3.txt
24K     .

The last line (24K .) shows the total size of the current directory (., which is ~/project/test_data).

Finally, let's go back to your ~/project directory and check the total size of test_data using du -sh.

cd ~/project
du -sh test_data

This will show the summarized size of the test_data directory:

24K     test_data

This demonstrates how du can be used to pinpoint disk space consumption within specific directories, helping you manage storage effectively.

Manually Mount and Unmount File Systems

In this step, you will learn how to manually mount and unmount file systems. Mounting a file system makes its contents accessible through a specific directory (mount point) in the file system hierarchy. Unmounting detaches the file system from its mount point, making its contents inaccessible until it is mounted again. This is a critical skill for managing removable media, temporary storage, or new disk partitions.

For this exercise, we will use one of the unmounted block devices available in your LabEx VM environment. From the previous step, you should have seen the /dev/vdb device that is not currently mounted. We will use /dev/vdb for this step.

First, let's confirm the available unmounted block devices using lsblk.

lsblk

You should see /dev/vdb listed without any mount points.

NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINTS
vda    253:0    0   40G  0 disk
├─vda1 253:1    0    1M  0 part
├─vda2 253:2    0  100M  0 part /boot/efi
└─vda3 253:3    0 39.9G  0 part /
vdb    253:16   0   40G  0 disk

Before you can mount a file system, you need a mount point, which is an empty directory. It's common practice to use /mnt for temporary mounts or create a subdirectory within it. Let's create a new directory named mydata inside your ~/project directory to serve as our mount point.

mkdir ~/project/mydata

Now, we need to format the /dev/vdb device with a file system. We will use the XFS file system, which is the default for Red Hat Enterprise Linux. Be careful with this command, as it will erase all data on the specified device.

sudo mkfs.xfs /dev/vdb

You will see output indicating the creation of the XFS file system:

meta-data=/dev/vdb               isize=512    agcount=4, agsize=2621440 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=1, sparse=1, rmapbt=0
         =                       reflink=1    bigtime=1 inobtcount=1 nrext64=0
data     =                       bsize=4096   blocks=10485760, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0, ftype=1
log      =internal log           bsize=4096   blocks=16384, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0

Now that /dev/vdb has an XFS file system, you can mount it to your ~/project/mydata mount point. The mount command requires sudo privileges.

sudo mount /dev/vdb ~/project/mydata

To verify that the file system is successfully mounted, use the df -h command again. You should see /dev/vdb listed with /home/labex/project/mydata as its mount point.

df -h

Look for /dev/vdb in the output:

Filesystem      Size  Used Avail Use% Mounted on
...
/dev/vdb         40G  318M   40G   1% /home/labex/project/mydata

Now, you can create files and directories inside ~/project/mydata, and they will be stored on the /dev/vdb device. Let's create a test file:

sudo touch ~/project/mydata/testfile.txt

You can list the contents of ~/project/mydata to confirm the file's creation:

ls -l ~/project/mydata

You should see testfile.txt listed:

total 0
-rw-r--r--. 1 root root 0 Jun 16 11:09 testfile.txt

When you are finished using a mounted file system, it's important to unmount it to prevent data corruption, especially before removing a physical device. Use the umount command to unmount the file system.

sudo umount ~/project/mydata

If the unmount command fails with a "target is busy" error, it means some process is still accessing the mount point. This often happens if your current working directory is inside the mounted file system. To resolve this, change your current directory to a location outside the mount point, for example, your home directory (~).

cd ~

Then, try unmounting again:

sudo umount ~/project/mydata

After unmounting, verify that /dev/vdb is no longer mounted by checking df -h again.

df -h

You should no longer see /dev/vdb mounted on /home/labex/project/mydata.

Filesystem      Size  Used Avail Use% Mounted on
...
## /dev/vdb should not be listed here anymore

This completes the process of manually mounting and unmounting a file system.

Locate Files by Name with locate and find

In this step, you will learn how to locate files on your system using two powerful commands: locate and find. Both commands help you search for files, but they operate differently and are suited for different scenarios.

Using the locate command

The locate command is very fast because it searches a pre-built database of file names and paths. However, this means it might not find files that were created or deleted since the last database update. The database is typically updated daily by a cron job, but you can force an update.

First, let's ensure the mlocate package, which provides the locate command, is installed.

sudo dnf install -y mlocate

You will see output similar to this during installation:

Last metadata expiration check: 0:00:01 ago on Mon 15 May 2023 08:00:00 AM UTC.
Dependencies resolved.
================================================================================
 Package        Architecture  Version             Repository               Size
================================================================================
Installing:
 mlocate        x86_64        0.26-28.el9         rhel-9-for-x86_64-appstream-rpms 100 k

Transaction Summary
================================================================================
Install  1 Package

Total download size: 100 k
Installed size: 230 k
Downloading Packages:
mlocate-0.26-28.el9.x86_64.rpm     100 kB/s | 100 kB     00:01
--------------------------------------------------------------------------------
Total                                            100 kB/s | 100 kB     00:01
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
  Preparing        :                                                        1/1
  Installing       : mlocate-0.26-28.el9.x86_64                             1/1
  Running scriptlet: mlocate-0.26-28.el9.x86_64                             1/1
  Verifying        : mlocate-0.26-28.el9.x86_64                             1/1
Installed:
  mlocate-0.26-28.el9.x86_64

Complete!

After installation, you need to update the locate database. This command requires sudo privileges.

sudo updatedb

This command will run silently and may take a few moments depending on the size of your file system.

Now, let's search for a common system file, like passwd.

locate passwd

You will see a list of paths containing "passwd":

/etc/passwd
/etc/passwd-
/etc/pam.d/passwd
/usr/share/man/man1/passwd.1.gz
/usr/share/man/man5/passwd.5.gz
...output omitted...

To perform a case-insensitive search, use the -i option. Let's search for files containing "messages" without worrying about capitalization.

locate -i messages

You will see results like:

/usr/share/locale/zza/LC_MESSAGES
/usr/share/makedumpfile/eppic_scripts/ap_messages_3_10_to_4_8.c
/usr/share/vim/vim82/ftplugin/msmessages.vim
...output omitted...

You can also limit the number of results using the -n option. Let's find the first 5 occurrences of "passwd".

locate -n 5 passwd

This will show only the first 5 matches:

/etc/passwd
/etc/passwd-
/etc/pam.d/passwd
/usr/share/man/man1/passwd.1.gz
/usr/share/man/man5/passwd.5.gz

Using the find command

The find command searches the file system in real-time, which makes it slower than locate but ensures that it finds all files that match your criteria, including those created very recently. It also offers much more powerful search options.

The basic syntax for find is find [path] [expression]. If no path is specified, it searches the current directory.

Let's search for the sshd_config file starting from the root directory (/).

find / -name sshd_config

You should see the path to the configuration file:

/etc/ssh/sshd_config

When searching for partial names or using wildcards, it's important to quote the filename pattern to prevent the shell from expanding it prematurely. Let's find all files ending with .txt in your ~/project directory.

find ~/project -name '*.txt'

If you created file1.txt, file2.txt, and file3.txt in the previous step, you should see them listed:

/home/labex/project/test_data/file1.txt
/home/labex/project/test_data/file2.txt
/home/labex/project/test_data/file3.txt

To perform a case-insensitive search with find, use the -iname option. Let's search for files containing "README" (case-insensitive) in the /usr/share/doc directory.

find /usr/share/doc -iname '*readme*'

You will see many results, for example:

/usr/share/doc/libselinux/README
/usr/share/doc/libsepol/README
/usr/share/doc/libsemanage/README
...output omitted...

The find command is very versatile and can be combined with other criteria, which you will explore in the next steps.

Find Files by Ownership, Permissions, Size, and Time

In this step, you will learn how to use the powerful find command to locate files based on various criteria beyond just their name. This includes searching by ownership, permissions, size, and modification time. These advanced search capabilities are essential for system administration tasks like auditing, cleanup, and troubleshooting.

Find Files by Ownership

You can search for files owned by a specific user or group using the -user and -group options, respectively. You can specify the user/group name or their numeric ID.

Let's find all files in your home directory (~) that are owned by the labex user.

find ~ -user labex

This will list many files, including your configuration files:

/home/labex
/home/labex/.bash_logout
/home/labex/.bash_profile
/home/labex/.bashrc
/home/labex/.config
/home/labex/.config/xfce4
/home/labex/.config/xfce4/xfconf
/home/labex/.config/xfce4/xfconf/xfce-perchannel-xml
/home/labex/.local
/home/labex/.local/share
/home/labex/.local/share/nano
/home/labex/project
/home/labex/project/test_data
/home/labex/project/test_data/file1.txt
/home/labex/project/test_data/file2.txt
/home/labex/project/test_data/file3.txt
...output omitted...

Similarly, to find files owned by the labex group:

find ~ -group labex

The output will be similar, as labex is typically the primary group for the labex user.

You can also search by User ID (UID) or Group ID (GID). The labex user typically has a UID and GID of 1000.

find ~ -uid 1000
find ~ -gid 1000

Find Files by Permissions

The find command's -perm option allows you to search for files with specific permissions. Permissions can be specified in octal (e.g., 755) or symbolic (e.g., u=rwx,g=rx,o=rx) mode.

Let's create a test file in your ~/project directory with specific permissions.

touch ~/project/permission_test.txt
chmod 644 ~/project/permission_test.txt

Now, let's find files in ~/project that have exactly 644 permissions.

find ~/project -perm 644

You should see permission_test.txt listed:

/home/labex/project/permission_test.txt

You can also use a leading / or - with the octal permissions:

  • /: Matches if any of the specified permission bits are set.
  • -: Matches if all of the specified permission bits are set.

Let's find files in ~/project where others have at least read permission (o=r or 004).

find ~/project -perm -004

This will list permission_test.txt and other files that grant read access to others.

/home/labex/project/permission_test.txt
...output omitted...

Find Files by Size

The -size option allows you to search for files based on their size. You can specify the size with units (e.g., k for kilobytes, M for megabytes, G for gigabytes). You can also use + for "greater than" and - for "less than".

Let's find files in your ~/project/test_data directory that are exactly 1 kilobyte in size.

find ~/project/test_data -size 1k

You should see file1.txt:

/home/labex/project/test_data/file1.txt

Now, find files larger than 5 kilobytes.

find ~/project/test_data -size +5k

This should list file3.txt:

/home/labex/project/test_data/file3.txt

And files smaller than 10 kilobytes.

find ~/project/test_data -size -10k

This should list file1.txt and file2.txt:

/home/labex/project/test_data/file1.txt
/home/labex/project/test_data/file2.txt

Find Files by Modification Time

You can search for files based on their modification time using options like -mmin (modified minutes ago) or -mtime (modified days ago).

Let's find files in your ~/project directory that were modified within the last 60 minutes.

find ~/project -mmin -60

This will likely include permission_test.txt and the files in test_data if you created them recently:

/home/labex/project
/home/labex/project/permission_test.txt
/home/labex/project/test_data
/home/labex/project/test_data/file1.txt
/home/labex/project/test_data/file2.txt
/home/labex/project/test_data/file3.txt

To find files modified more than 1 day ago (24 hours), you can use +1 with -mtime.

find ~/project -mtime +1

This command might not return any files if all your ~/project files were created or modified recently.

These options can be combined to create very specific search queries, allowing you to efficiently manage files on your system.

Search for Files Based on File Type

In this final step, you will learn how to use the find command to search for files based on their type. This is particularly useful when you need to locate all directories, regular files, symbolic links, or device files within a specific path.

The find command uses the -type option followed by a single character to specify the file type. Here are some common file types you can search for:

  • f: Regular file
  • d: Directory
  • l: Symbolic link (symlink)
  • b: Block device
  • c: Character device
  • p: Named pipe (FIFO)
  • s: Socket

Let's start by searching for all directories within your ~/project directory.

find ~/project -type d

You should see output similar to this, listing all directories and subdirectories:

/home/labex/project
/home/labex/project/test_data

Next, let's search for all regular files within your ~/project directory.

find ~/project -type f

This will list files like file1.txt, file2.txt, and file3.txt that you created earlier:

/home/labex/project/test_data/file1.txt
/home/labex/project/test_data/file2.txt
/home/labex/project/test_data/file3.txt

Now, let's create a symbolic link to demonstrate searching for symlinks. We'll create a symlink to file1.txt in your ~/project directory.

ln -s ~/project/test_data/file1.txt ~/project/link_to_file1.txt

Verify the symlink was created using ls -l:

ls -l ~/project/link_to_file1.txt

You should see output indicating it's a symbolic link:

lrwxrwxrwx. 1 labex labex 32 May 15 08:00 /home/labex/project/link_to_file1.txt -> /home/labex/project/test_data/file1.txt

Now, search for all symbolic links within your ~/project directory.

find ~/project -type l

You should see your newly created symlink:

/home/labex/project/link_to_file1.txt

Finally, let's search for block devices. Block devices are typically found in the /dev directory.

find /dev -type b

This will list block devices like vda, vda1, vda2, etc.:

/dev/vda1
/dev/vda2
/dev/vda3
/dev/vda
/dev/vdb

You can combine the -type option with other find options you learned in previous steps. For example, to find all directories in /etc that are owned by the root user:

find /etc -type d -user root

This will produce a long list of directories:

/etc
/etc/selinux
/etc/selinux/targeted
/etc/selinux/targeted/active
/etc/selinux/targeted/active/modules
...output omitted...

This concludes the lab on accessing Linux file systems and locating files. You have learned how to identify devices, examine disk usage, manually mount and unmount file systems, and use locate and find with various criteria.

Summary

In this lab, we gained practical experience in managing Linux file systems on a Red Hat Enterprise Linux system. We began by learning to identify file systems and block devices using commands like lsblk, understanding core concepts such as block devices, partitions, file systems, and mount points. Subsequently, we explored how to examine file system usage with df and du, distinguishing between their functionalities for reporting disk space. The lab also covered the essential skill of manually mounting and unmounting file systems, demonstrating how to make file system contents accessible and then detach them.

Furthermore, we delved into locating files efficiently using various criteria. We learned to find files by name with locate and find, understanding the differences and appropriate use cases for each. The lab extended this by teaching how to search for files based on ownership, permissions, size, and time attributes, providing powerful tools for system administration and troubleshooting. Finally, we practiced searching for files based on their specific file types, completing a comprehensive overview of file system management and file location techniques.