Skip to Content

Day 2: Mastering Linux File User and Disk Management: Essential Commands and Techniques for System Administrators

In the Linux system, file operations and user management are two very important concepts, which are crucial for the normal operation and maintenance of the system. Below I will introduce some basic operations on these two topics.

Linux File Operations

Linux File System for New Learners: An In-Depth Guide

In-Depth File Type Understanding

Linux file types are not just classifications but also determine how files interact with the system. Regular files are straightforward data containers, while directories, symbolically represented as folders, organize these files. Symbolic links, often used for shortcuts, are references to other files or directories. Block and character devices represent hardware, with block devices being random-access I/O devices, and character devices being sequential-access I/O devices. Sockets enable inter-process communication, and pipes are used for sequential data transfer between processes.

A Closer Look at File Permissions

File permissions in Linux are a fundamental security feature, allowing for fine-grained control over who can access or modify files. The permissions are additive and can be combined to allow various levels of access. For example, read permission allows a user to view the file’s contents, write permission enables changes to the file, and execute permission lets users run the file as a program. Directories have slightly different implications for permissions, with execute permission allowing users to list or access the directory’s contents.

Ownership and Groups Explained

Ownership in Linux is pivotal for resource sharing and permission settings. Each file is associated with a user (owner) and a group. The user is the file’s creator or the entity that last modified the file. Groups are collections of users who share a common set of access permissions. Understanding how to change ownership and group associations with chown and chgrp commands is crucial for managing access to files and directories.

The Significance of Home Directories

Home directories serve as the personal space for each user to store and organize personal files, configurations, and data. They are denoted by the ~ symbol and are essential for maintaining user privacy and organization within the system.

Decoding File Paths

File paths in Linux are the means by which files and directories are located within the file system hierarchy. Absolute paths provide the full route from the root directory, ensuring unambiguous navigation to any file or directory. Relative paths, on the other hand, are based on the current working directory, offering a more flexible approach to file navigation.

Unveiling Hidden Files

Hidden files in Linux, indicated by a dot prefix, are not displayed by default to avoid clutter and potential mishandling of critical system files. These files often contain configuration settings or are used for system housekeeping tasks.

Embracing Case Sensitivity

Linux’s case sensitivity is a key aspect that distinguishes it from other operating systems. This feature means that ‘File’, ‘file’, and ‘FILE’ are considered three separate entities, which is particularly important when working with file names, commands, or parameters.

Beyond File Extensions

While file extensions can hint at the file’s content or format, they are not a requirement in Linux. However, they are a helpful convention for users and applications to quickly identify the file’s intended use or the application associated with it.

Distinguishing Between Text and Binary Files

Text files are human-readable and can be opened and edited with any text editor. Binary files, however, are not meant to be read or edited directly by users, as they contain data in a format specific to an application or the operating system.

Inodes: The Backbone of File Metadata

Inodes are a unique feature of the Linux file system, storing metadata about files independently from the file data itself. This metadata includes the file’s size, ownership, permissions, and the location of the file’s data blocks on the disk.

Understanding Filesystems

Filesystems in Linux are the methods by which files and directories are stored and organized. Different filesystems offer various features, such as journaling, encryption, and performance optimizations, tailored to specific use cases.

Mounting: Accessing Storage

Mounting is the process of making a filesystem accessible within the directory hierarchy. This is essential for accessing data on hard drives, SSDs, or external storage devices.

Package Managers: Simplified Software Management

Package managers streamline the process of software installation, updating, and management. They handle dependencies, version control, and package repositories, making it easier for users to maintain their system’s software.

Archiving: Efficient Data Management

Archiving is a way to organize, compress, and reduce the size of files or directories, making it easier to manage storage and transfer data.

Editing Text Files: The Basics

Text editors in Linux are essential tools for creating and modifying files. They range from simple editors like nano to more complex ones like vi or emacs, which offer advanced features like syntax highlighting and scripting.

Finding Files: A Systematic Approach
The find command is a powerful tool for locating files based on various criteria, such as name, type, or modification time, making it easier to navigate through large file systems.

Viewing File Contents: A Practical Guide
Viewing the contents of files is a common task. Commands like cat, less, and more provide different ways to view text files, with less and more offering navigation features.

File Operations: Copying, Moving, and Deleting
File operations are fundamental to file management. The cp command is used for copying files, mv for moving or renaming files, and rm for deleting files.

Symbolic Links: Navigating with Ease
Symbolic links provide a flexible way to reference other files or directories, simplifying file navigation and organization.

Monitoring Disk Space

Keeping an eye on disk space is crucial for system performance and maintenance. The df and du commands provide insights into disk usage and help administrators manage storage effectively.

By understanding these concepts, new learners can navigate the Linux file system with confidence, manage files efficiently, and perform essential system administration tasks.

  1. Viewing File Contents:
    • cat filename: Displays the entire content of the file.
    • less filename: Displays the file content in pages, allowing forward and backward navigation.
    • more filename: Similar to less, but with fewer features.
  2. Creating Files:
    • touch filename: Creates an empty file.
    • nano filename: Creates or edits a file using the nano editor.
  3. Copying Files:
    • cp source destination: Copies the source file to the destination location.
  4. Moving/Renaming Files:
    • mv oldname newname: Moves a file or renames it.
  5. Deleting Files:
    • rm filename: Deletes the file.
  6. Finding Files:
    • find / -name filename: Searches for the file in the root directory and its subdirectories.
  7. File Permissions:
    • chmod: Changes file permissions.
    • chown: Changes the file owner.
  8. File Links:
    • ln -s /path/to/original /path/to/link: Creates a symbolic link.
  9. Filesystem Management:
    • df: Checks disk space usage.
    • du: Checks the disk usage of files or directories.

Linux User Management

Linux user management is an integral part of system administration, focusing on the security, organization, and efficiency of the operating system. User accounts in Linux are unique, identified by usernames and passwords, with each user assigned a User Identifier (UID) and a Group Identifier (GID). Home directories offer a personal space for users to store their files, typically located under /home/username.

The /etc/passwd file is a cornerstone of user management, containing essential account information such as usernames, UIDs, GIDs, and home directory paths. Security is bolstered by the /etc/shadow file, which stores encrypted passwords and enforces password aging policies. Group management is facilitated through the /etc/group file, listing group names and their member users.

Command-line tools like useradd, usermod, userdel, groupadd, and groupdel are pivotal for administering users and groups. Password management is simplified with the passwd and chage commands, allowing users to update their passwords and manage password expiration. The sudo command extends functionality, permitting certain users to execute commands with elevated privileges.

File permissions are a fundamental aspect of security, with chmod and chown controlling access to files and directories. Advanced access control is available through Access Control Lists (ACLs). Pluggable Authentication Modules (PAM) provide flexibility in authentication mechanisms, and the auditd daemon enhances security by monitoring and logging user activities.

SSH access is a standard for secure remote user access, with key-based authentication offering a secure login method. Users can tailor their shell environment through configuration files like .bashrc or .bash_profile. Resource utilization is controlled through ulimit and the /etc/security/limits.conf file, which set limits on system resources available to users.

The /etc/security/limits.conf file also plays a role in resource management by defining limits on what a user can do in terms of system resources. Tools such as who, w, and last offer insights into user sessions and login history, aiding in system monitoring. Disk quotas help prevent resource hogging by capping the disk space and file count a user can consume.

Multi-factor authentication (MFA) is another layer of security that can be implemented on Linux systems, requiring users to provide additional verification beyond just a password. This approach significantly strengthens the system’s defense against unauthorized access.

In summary, Linux user management encompasses a wide range of tasks and tools designed to protect the system, ensure user privacy, and optimize performance. Administrators must stay adept in these practices to maintain a secure and well-functioning Linux environment.

  1. Viewing User List:
    • cat /etc/passwd: Views the list of all users in the system.
  2. Adding Users:
    • useradd username: Adds a new user.
  3. Deleting Users:
    • userdel username: Deletes a user.
  4. Changing User Passwords:
    • passwd username: Changes the password for a specified user.
  5. User Group Management:
    • groupadd groupname: Creates a new group.
    • groupdel groupname: Deletes a group.
  6. Associating Users and Groups:
    • usermod -aG groupname username: Adds a user to a group.
    • gpasswd -d username groupname: Removes a user from a group.
  7. Viewing Groups a User Belongs To:
    • groups username: Views all groups a user belongs to.
  8. Switching Users:
    • su – username: Switches to a specified user.
  9. Permissions and Ownership:
    • File and directory permissions are typically managed with the chmod and chown commands.
  10. sudo:
    • sudo command: Executes a command with superuser (root) privileges.

Important Linux Files

  1. /etc/passwd:
    • Stores user account information, such as username and user’s home directory.
  2. /etc/shadow:
    • Stores the encrypted version of user passwords, securely protecting password information.
  3. /etc/group:
    • Defines groups in the system, including the group name and a list of users in the group.
  4. /etc/sudoers:
    • Defines which users and groups can use the sudo command and which commands they can execute.
  5. /etc/fstab:
    • Filesystem table, defining the filesystems to be mounted at system startup.
  6. /var/log/:
    • Stores system log files, recording system activities and error information.
  7. /var/run/:
    • Stores temporary files during system runtime, such as process ID files.
  8. /var/spool/:
    • Stores queue data for various services, such as mail and print queues.
  9. /etc/profile:
    • Provides global shell environment settings for all logged-in users.
  10. ~/.bashrc:
    • The user’s bash shell configuration file, used to customize the user’s environment.
  11. /etc/hosts:
    • Stores mappings of hostnames to IP addresses for local resolution.

In the Linux system, files related to users are mainly stored in the /etc directory, containing key data such as user accounts, passwords, and group information. Here are some important files related to users:

  1. /etc/passwd:
    • Contains basic information about all users in the system. Each line records a user account, including the username and some attributes, such as the user’s UID (User ID), GID (Group ID), home directory, login shell, etc. The password field is usually an x, indicating that the password is in the /etc/shadow file.
  2. /etc/shadow:
    • Stores the encrypted passwords of users. For security reasons, the actual password is stored in this file, not in /etc/passwd. Only the root user can read this file.
  3. /etc/group:
    • Contains information about all the groups defined in the system. Each line defines a group, including the group name, GID, and a list of usernames in the group.
  4. /etc/gshadow:
    • Similar to /etc/shadow, this file stores encrypted group information, including the group’s administrator username and a list of group members.
  5. ~/.bashrc:
    • This is the user’s bash shell configuration file, containing configuration information for the bash shell when the user logs in, such as aliases, environment variables, etc.
  6. ~/.bash_profile:
    • When the user logs in, the bash shell reads this file to set the environment. It can contain commands that execute once or a source (sourcing) call to .bashrc.
  7. ~/.profile:
    • This file is read when the user logs in, used to set the user’s global environment, such as environment variables, shell prompts, etc.
  8. /etc/sudoers:
    • This file defines which users and groups can use the sudo command and which commands they can execute. It is usually edited through the visudo command to ensure correct syntax and prevent permission issues.
  9. /etc/skel/:
    • This is a directory containing the default files and directories for new user accounts. When a new user is created, these files and directories are copied to the new user’s home directory.
  10. /etc/login.defs:
    • Contains default settings for users and groups, such as password policies, account expiration information, etc.
  11. /var/mail/username:
    • The user’s mail storage file. When the user receives mail, it is stored in this file.
  12. /var/log/wtmp:
    • Records all user login and logout events.
  13. /var/log/btmp:
    • Records failed login attempts.

Understanding the function of these files is crucial for managing user accounts and groups in the Linux system. System administrators typically use specialized commands such as useradd, usermod, userdel, groupadd, and groupmod to manage the data in these files.

Linux disk management

In the Linux system, disk management is an important task, involving viewing disk space, managing filesystems, checking disk health, etc.

Here are some commonly used commands related to disks:

  1. df – Displays the disk space usage of the filesystem.
    df -h
    • The h flag displays information in a human-readable format (e.g., MB, GB).
  2. du – Estimates the disk usage of files or directories.
    du -sh directory
    • The s flag summarizes the size of each specified directory, and h displays it in a human-readable format.
  3. lsblk – Lists all available block devices (physical disks, partitions, etc.).
  4. fdisk – Disk partition table operations.
    sudo fdisk /dev/sda

    Used to view or modify the partition table of a disk.

  5. parted – Another partition management tool.
    sudo parted /dev/sda
  6. mkfs – Creates a new filesystem on a disk partition.
    sudo mkfs -t ext4 /dev/sda1
    • The t flag specifies the filesystem type (e.g., ext4, xfs, etc.).
  7. mount – Mounts a filesystem.
    sudo mount /dev/sda1 /mnt

    Mounts the /dev/sda1 partition to the /mnt directory.

  8. umount – Unmounts a filesystem.
    sudo /umount /mnt
  9. fsck – Checks and repairs the filesystem.
    sudo fsck /dev/sda1

    Typically run automatically at system startup.

  10. dd – A data conversion and copying tool, also used to create disk images.
    dd if=/dev/sda of=sda.img

    The if specifies the input file, and of specifies the output file.

  11. resize2fs – Adjusts the size of ext2, ext3, or ext4 filesystems.
    sudo resize2fs /dev/sda
  12. tune2fs – Adjusts mount options for ext2/ext3/ext4 filesystems.
    sudo tune2fs -l /dev/sda1

    The l flag lists detailed information about the filesystem.

  13. dmesg – Displays messages from the kernel ring buffer, including disk-related warnings or errors.
    dmesg | grep sd*

    The grep filters information related to disks.

  14. lsof – Lists currently opened files in the system, which can be used to check which process is using a specific file or directory.
    lsof /path/to/file
  15. blkid – Shows information about block devices, such as UUIDs and labels.
    sudo blkid

These commands are fundamental to disk management, covering aspects from viewing disk usage to partitioning, formatting, and mounting filesystems. When using these commands, select the appropriate parameters and options based on the actual situation and requirements.

Mendy W

Wednesday 17th of July 2024

That is a pretty detailed post. It covers all important things about Linux.