Comprehensive Guide to Linux File System Management Print

  • 0

Managing the file system is a critical aspect of system administration in a Linux environment. This guide covers essential commands for monitoring disk usage, checking and repairing file systems, and managing disk partitions.

Disk Usage and File System Information

Viewing Disk Usage with df

The df (disk free) command is used to display the amount of disk space available on file systems. The -h option makes the output human-readable, showing sizes in KB, MB, or GB.

df -h
 
Example output:
Filesystem Size Used Avail Use% Mounted on
/dev/sda1 50G 15G 33G 32% /
tmpfs 7.8G 0 7.8G 0% /dev/shm
/dev/sda2 100G 75G 25G 75% /home
 
Checking Disk Inodes with `df -i`
 
The `df -i` command shows the inode usage of file systems. Inodes are data structures that store information about files.
 
df -i
 
Example output:
Filesystem Inodes IUsed IFree IUse% Mounted on
/dev/sda1 3276800 20000 3256800 1% /
tmpfs 2000000 10 1999990 1% /dev/shm
/dev/sda2 6553600 500000 6053600 8% /home
 

Checking and Repairing File Systems

 
Checking and Repairing File Systems with `fsck`
 
The `fsck` (file system consistency check) command is used to check and optionally repair one or more Linux file systems. The `-y` option automatically answers 'yes' to all prompts, making it useful for automated scripts.
 
Checking a Specific Partition
 
To check and repair `/dev/md2`:
fsck -y /dev/md2
 
To check and repair `/dev/md3`:
fsck -y /dev/md3
 
To check and repair `/dev/sda3`:
fsck -y /dev/sda3
 
To check and repair `/dev/sda4`:
fsck -y /dev/sda4
 
Example Output
 
Example output of running `fsck` on a partition:
fsck from util-linux 2.31.1
e2fsck 1.44.1 (24-Mar-2018)
/dev/sda3 contains a file system with errors, check forced.
Pass 1: Checking inodes, blocks, and sizes
Pass 2: Checking directory structure
Pass 3: Checking directory connectivity
Pass 4: Checking reference counts
Pass 5: Checking group summary information
/dev/sda3: 325/2621440 files (0.0% non-contiguous), 32768/10485760 blocks
 

Checking and Repairing XFS File Systems

The XFS file system is a high-performance 64-bit journaling file system created by Silicon Graphics, Inc. It is particularly suited for large files and large file systems. Checking and repairing XFS file systems is done using the xfs_repair command.

Checking an XFS File System with xfs_repair

The xfs_repair command is used to check and repair XFS file systems. The -n option performs a dry run without making any changes, which is useful for diagnostic purposes.

Checking a Specific Partition

To check the XFS file system on /dev/sdb3 without making changes:

xfs_repair -n /dev/sdb3

Checking a RAID Device

To check the XFS file system on a RAID device such as /dev/md126 without making changes:

xfs_repair -n /dev/md126

Example Output

Example output of running xfs_repair in no-modify mode:

Phase 1 - find and verify superblock...
Phase 2 - using internal log
- scan filesystem freespace and inode maps...
- found root inode chunk
Phase 3 - for each AG...
- scan and clear agi unlinked lists...
- process known inodes and perform inode discovery...
- agno = 0
- agno = 1
- agno = 2
- agno = 3
Phase 4 - check for duplicate blocks...
- setting up duplicate extent list...
- check for inodes claiming duplicate blocks...
Phase 5 - rebuild AG headers and trees...
- reset superblock...
Phase 6 - check inode connectivity...
- resetting contents of realtime bitmap and summary inodes
- traversing filesystem ...
- traversal finished ...
- moving disconnected inodes to lost+found ...
Phase 7 - verify and correct link counts...

No modify flag was set, skipping filesystem flush and exiting.

By running xfs_repair with the -n option, you can safely check the integrity of your XFS file systems and identify any issues without risking changes to the data. This step is crucial before performing any actual repairs, ensuring you have a clear understanding of the file system's health.

Monitoring Disk Usage in Real-Time
 
Using `du` to Estimate File Space Usage
 
The `du` (disk usage) command estimates the file space usage. The `-h` option makes the output human-readable, and the `--max-depth=1` option limits the display to the current directory level.
 
du -h --max-depth=1 /var
 
Example output:
4.0K /var/spool
2.3M /var/cache
1.1G /var/log
2.0G /var
 
Managing Disk Partitions
 
Listing All Partitions with `lsblk`
 
The `lsblk` (list block devices) command lists information about all available block devices.
 
lsblk
 
Example output:
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 120G 0 disk
├─sda1 8:1 0 50G 0 part /
├─sda2 8:2 0 100G 0 part /home
└─sda3 8:3 0 20G 0 part /data
 
Creating a New Partition with `fdisk`
 
The `fdisk` command is a powerful disk partitioning tool. Here is how you create a new partition:
 
1. Start `fdisk`:
 
fdisk /dev/sda
 
2. Command to create a new partition:
 
Command (m for help): n
 
3. Choose partition type (primary or extended):
 
Select (default p): p
 
4. Specify the partition number:
 
Partition number (1-4, default 3): 4


5. Define the starting sector:

 
First sector (2048-20971519, default 2048): (press Enter for default)


6. Define the last sector or size:

 
Last sector, +sectors or +size{K,M,G,T,P} (2048-20971519, default 20971519): +10G
 
7. Write the changes and exit:
 
Command (m for help): w

Setting File Permissions and Ownership

Changing File Ownership with chown

The chown command is used to change the ownership of files and directories. The basic syntax is chown [owner]:[group] [file].

Change Ownership of a Specific File

To change the ownership of a specific file:

chown user:group /path/to/file

Change Ownership Recursively

To change the ownership of a directory and all its contents recursively:

chown -R user:group /path/to/directory

Example:

chown -R user:user /var/www/html

Setting File Permissions with chmod

The chmod command is used to change the file access permissions. Permissions can be set using symbolic or numeric modes.

Symbolic Mode

The symbolic mode uses symbols to represent permissions. For example, to give the owner write permission and remove write permission for others:

chmod u+w,o-w /path/to/file

Numeric Mode

The numeric mode uses a three-digit octal number to represent permissions. For example, to set read, write, and execute permissions for the owner, and read and execute permissions for the group and others:

chmod 755 /path/to/file

Example of Setting Permissions

To set read, write, and execute permissions for the owner, and read and execute permissions for the group and others:

chmod 755 /path/to/file

To recursively set the same permissions for a directory and all its contents:

chmod -R 755 /path/to/directory

Setting File Permissions with `chmod`

The `chmod` command is used to change file and directory permissions in Unix and Linux systems. Permissions are specified using either symbolic notation or numeric (octal) notation.

Common Permission Settings

Numeric Mode Explanation

Permissions are represented by three sets of digits, each ranging from 0 to 7. These sets correspond to the user (owner), group, and others.

- **7**: Read, write, and execute (4+2+1)
- **6**: Read and write (4+2)
- **5**: Read and execute (4+1)
- **4**: Read only (4)
- **3**: Write and execute (2+1)
- **2**: Write only (2)
- **1**: Execute only (1)
- **0**: No permissions (0)

`chmod 755`

- **7** (Owner): Read, write, and execute
- **5** (Group): Read and execute
- **5** (Others): Read and execute

This setting is typically used for directories and executable files where the owner needs full access, and others need only read and execute permissions.

chmod 755 /path/to/file_or_directory

Example:
chmod 755 /usr/local/bin/myscript.sh

This allows the owner to read, write, and execute the script, while the group and others can only read and execute it.

`chmod 644`

- **6** (Owner): Read and write
- **4** (Group): Read only
- **4** (Others): Read only

This setting is commonly used for regular files where the owner needs to modify the file, but others should only read it.

chmod 644 /path/to/file

Example:
chmod 644 /var/www/html/index.html

This allows the owner to read and write the HTML file, while the group and others can only read it.

Examples of Other Common Permissions

`chmod 700`

- **7** (Owner): Read, write, and execute
- **0** (Group): No permissions
- **0** (Others): No permissions

This setting is used to ensure that only the owner has any access to the file or directory.

chmod 700 /path/to/private_script.sh

`chmod 600`

- **6** (Owner): Read and write
- **0** (Group): No permissions
- **0** (Others): No permissions

This setting is used for sensitive files that only the owner should be able to read and write.

chmod 600 /path/to/sensitive_file.txt

`chmod 755` (for directories)

- **7** (Owner): Read, write, and execute
- **5** (Group): Read and execute
- **5** (Others): Read and execute

This setting is often used for directories to ensure the owner can fully manage the directory while others can navigate and list the contents.
 
Setting File Permissions with `chmod`: Symbolic Notation

In addition to using numeric (octal) notation, you can also set file permissions using symbolic notation with the `chmod` command. Symbolic notation involves specifying changes to the permissions rather than setting them directly.

Understanding Symbolic Notation


Symbolic notation uses letters and symbols to represent different permissions and operations:

- **u**: User (owner)
- **g**: Group
- **o**: Others
- **a**: All (user, group, and others)
- **+**: Add permission
- **-**: Remove permission
- **=**: Set exact permission

Examples of Symbolic Notation

`chmod u+w`

Adds write permission for the user (owner):
chmod u+w /path/to/file
Example:
chmod u+w /var/www/html/index.html
This allows the owner to write to the HTML file.

`chmod o-w`

Removes write permission for others:
chmod o-w /path/to/file
Example:
chmod o-w /var/www/html/index.html
This prevents others from writing to the HTML file.

`chmod -R`

The `-R` option applies changes recursively to all files and directories within the specified directory:
chmod -R u+w /path/to/directory
Example:
chmod -R u+w /var/www/html
This allows the owner to write to all files and directories within `/var/www/html`.

Combining Symbolic Permissions

You can combine multiple symbolic changes in a single command:

`chmod u+w,o-r`

Adds write permission for the user and removes read permission for others:
chmod u+w,o-r /path/to/file
Example:
chmod u+w,o-r /var/www/html/index.html
This allows the owner to write to the HTML file while preventing others from reading it.

`chmod a+x`

Adds execute permission for all users (user, group, and others):
chmod a+x /path/to/file
Example:
chmod a+x /usr/local/bin/myscript.sh
This makes the script executable by anyone.

Using symbolic notation with the `chmod` command provides a flexible and intuitive way to modify file permissions. By understanding and applying these symbolic permissions, you can fine-tune access control for files and directories on your Linux system, ensuring appropriate security and functionality.
 

Conclusion

Managing file systems and disk partitions is crucial for maintaining a healthy and efficient Linux system. This guide has covered essential commands and their usage, including disk usage monitoring, file system checking and repairing, and disk partition management. By mastering these commands, system administrators can ensure optimal performance and reliability of their systems.

Further Reading

For more information on related topics, check out the following articles:

Advanced Guide to Linux File System Management


Was this answer helpful?

« Back