Welcome back with another topic to explore what we have learned during the RHEL courses from alanfi. Our today’s topic is Files and Directories management and we are going to discuss it in the context of RHEL (specifically to its derivatives like CentOS 6,7, & 8).
Managing files and directories is a crucial part of the system administration of RHEL and CentOS. From understanding the hierarchical structure of the file system to mastering file permissions and ownership, effective management is essential for keeping systems running smoothly. In this article, I’ll share key concepts and practical insights I’ve learned during the course for handling files and directories within RHEL environments like CentOS 6, 7, and 8. I’ll try to keep it simple, crisp, and to the point yet try to cover all the important aspects. Are you ready? So, let’s get started!
The Linux File system in RHEL:
Hierarchy and Structure:
Like all Linux distributions, RHEL uses a hierarchical file system with the root directory (/) at the top. However, RHEL introduces specific defaults and standards suited for enterprise environments.
Here are the key directories you should keep in mind:
- /bin : This direcotry contains essential command binaries for the system’s basic operations. These are the core executables used by system administrators and users as well, such as ls, cp, mv, and cat.
- /etc: It holds the system-wide configuration files and shell scripts, and if remeber we got the configurations for Apache also in this directoires, ( did you remember?)
- /var: Stores variable data files that are expected to change frequently during system operation. Like /var/log
- /usr: Contains user programs and data that are not essential for the system’s basic functionality but are used by system administrators and users like /usr/bin, usr/lib
- /home: Provides a directory for user-specific files and personal configurations. Each user will have its own /home directory like /home/ali
- /run: Introduced in RHEL 7 as a temporary runtime data store for system information like PID.
If we talk about the hierarchy, the “/” is at the top which is called the root directory.
File Types:
- Regular Files: e.g system logs /var/log
- Direcotires: e.g /etc/ for configuraiton files
- Special Files: e.g binary files, config files
File Systems:
- Ext4: Most commonly used file system for Linux.
- XFS, Btrfs, and others: Specialized file systems offering unique features.
How does Linux File System work in RHEL?
Inode and MetaData:
An inode is a data structure used to store metadata about a file or directory on a Linux/Unix-based file system. It contains the file size, Owner, Group, Permissions, Timestamps( creation, modification, and access time), and Pointer to its physical data blocks on the disk. And it doesn’t contain the file name and its contents.
Mounting:
Mounting is the process of attaching an additional file system to a directory on the current file system, making it accessible to users and the operating system. When we mount a file system, it associates it with a mount point, which is actually a directory in the main file system.
The mount command is used to attach a file system to a directory:
mount [file_system] [mount_point]
For example, to mount a file system located at /dev/sda1 to the /mnt directory:
mount /dev/sda1 /mnt
File Permissions and Ownership:
RHEL follows the traditional UNIX-like permissions model: read, write, and execute for the owner ( of two types: user and Group), group, and others.
- We can use chmod command to modify file permissions: e.g chmod [permissions] [file_name]
- We can use chown command to change the ownership of a file, e.g chown [user]:[group] [file_name]
- Permissions are represented in symbolic (e.g., rwx) or numeric (e.g., 755) formats.
- r = 4, w = 2, x = 1.
- Adding these values (e.g., 7 = rwx) specifies different permissions for the user, group, and others.
Always follow the principle of least privilege when assigning file permissions. Use SELinux in enforcing mode to ensure additional layers of security.
Managing Files and Directories in RHEL:
Creating and Removing of Files/Directories:
The touch command creates files, mkdir creates directories, and rm removes files. To remove non-empty directories, use rm -r.
Navigating the File System:
cd allows you to navigate directories, and pwd shows your current working directory. The tab auto-completion feature available in modern RHEL versions simplifies file navigation.
Copying, Moving, and Renaming:
Use cp to copy files and directories and mv to move or rename them. For example, in RHEL, it’s common to use cp -r to recursively copy entire directories.
Viewing and Editing Files:
RHEL supports multiple tools to view and edit files:
- cat, more, less, and head for viewing files.
- vim, nano, or vi for editing files.
- The journalctl command, introduced in RHEL 7, is key to viewing system logs managed by systemd, replacing older logging systems from RHEL 6.
Finding Files:
Use find to search for files based on name, type, or permissions. The locate command can also be used after updating the file database with updatedb.
Creating Symbolic and Hard Links:
Symbolic links (ln -s) are useful when you want multiple references to a file across the file system. They’re especially handy for pointing to binaries or shared libraries that may reside in non-standard locations.
Best Practices in RHEL for File and Directory:
Organizing files:
RHEL systems rely on a clean, organized file system, especially for system-level administration. Always separate system files, user files, and application data into their respective directories (/etc/, /home/, /usr/, /var/).
Backup and Restore:
We can utilize rsync and tar tools to create backups. RHEL systems commonly integrate with enterprise-grade backup solutions. Ensure that you also take snapshots if using XFS and regularly back up the /etc directory, because it contains critical configuration files.
And, LVM snapshots can be used for backup in dynamic environments, especially in RHEL 7 and 8.
Key Takeaways:
- Understand RHEL’s File System History: While CentOS 6 uses traditional file systems and tools, CentOS 7 and 8 bring new features like XFS, systemd, and ACLs that improve scalability and security.
- Mastering Permissions and Ownership: Knowing how to manage file permissions and ownership is essential for securing a RHEL-based environment.
- Use Efficient File and Directory Management Tools: Commands like mkdir, rm, cp, mv, ln, and find are indispensable for managing files in an RHEL environment.
- Adopt Enterprise Best Practices: Regular backups, careful organization of files, and adhering to security protocols such as SELinux will ensure a stable, secure system.
- Take Advantage of LVM and XFS: Use these advanced tools to manage and scale your file systems flexibly in RHEL 7 and 8 environments.