
matchpathcon Command in Linux
The matchpathcon command in Linux is a powerful tool used to query and verify the security context of files and directories based on SELinux (Security-Enhanced Linux) policies. This command is particularly useful for system administrators and security professionals who need to ensure that files and directories have the correct security labels.
Table of Contents
Here is a comprehensive guide to the options available with the matchpathcon command −
- Understanding matchpathcon Command
- Syntax of matchpathcon Command
- How to Use matchpathcon Command in Linux?
- Advanced Usage of matchpathcon Command
- Examples of matchpathcon Command in Linux
Understanding matchpathcon Command
Before diving into the matchpathcon command, it's important to have a basic understanding of SELinux. SELinux is a security module integrated into the Linux kernel that provides a mechanism for enforcing access control policies. It uses security contexts, which are labels assigned to files, processes, and other system objects, to control access based on predefined policies.
SELinux policies define the rules for how these security contexts interact, ensuring that only authorized actions are allowed. The matchpathcon command is used to query and verify these security contexts, making it an essential tool for managing SELinux policies.
Syntax of matchpathcon Command
The basic syntax of the matchpathcon command is as follows −
matchpathcon [options] pathname...
- options − Various options that modify the behavior of the command.
- pathname − The path(s) of the file(s) or directory(ies) to query.
How to Use matchpathcon Command in Linux?
The matchpathcon command is used to query the security context of files and directories based on SELinux policies. It compares the actual security context of the specified path(s) with the expected context defined in the SELinux policy.
sudo apt install selinux-utils

Querying the Security Context of a File
Let's start with a simple example of querying the security context of a file.
matchpathcon

Suppose you have a file named example.txt in your home directory. You can use the matchpathcon command to query its security context −
matchpathcon ~/example.txt

This command outputs the expected security context for example.txt based on the SELinux policy. The output might look something like this −
In this example, the security context system_u:object_r:user_home_t:s0 is assigned to the file example.txt.
Querying the Security Context of a Directory
You can also use the matchpathcon command to query the security context of a directory. For example, to query the security context of your home directory, you can use the following command −
matchpathcon ~/

In this example, the security context system_u:object_r:user_home_dir_t:s0 is assigned to the home directory.
Querying the Security Context of Multiple Paths
The matchpathcon command allows you to query the security context of multiple paths at once. You can specify multiple files and directories as arguments −
matchpathcon ~/example.txt ~/Documents ~/Downloads

In this example, the security contexts for example.txt, Documents, and Downloads are displayed.
Advanced Usage of matchpathcon Command
The matchpathcon command offers several advanced options that allow you to customize its behavior. Here are some of the most commonly used options −
- -V: Verbose mode. Displays additional information about the command's execution.
- -n − Do not follow symbolic links.
Using Verbose Mode
Verbose mode provides additional information about the command's execution, which can be useful for debugging. To enable verbose mode, use the -V option −
matchpathcon -V ~/example.txt

This command outputs the expected security context for example.txt along with additional information about the command's execution.
Not Following Symbolic Links
By default, the matchpathcon command follows symbolic links. If you want to query the security context of the symbolic link itself rather than the target, you can use the -n option −
matchpathcon -n ~/example_link

This command outputs the expected security context for the symbolic link example_link without following it to its target.
Examples of matchpathcon Command in Linux
To further illustrate the power and versatility of the matchpathcon command, let's explore some practical examples of how it can be used in real-world scenarios.
Verifying Security Contexts After a System Update
After a system update, it's important to verify that the security contexts of critical files and directories are still correct. You can use the matchpathcon command to perform this verification −
matchpathcon /etc/passwd /etc/shadow /var/log

In this example, the security contexts for /etc/passwd, /etc/shadow, and /var/log are displayed, allowing you to verify that they match the expected contexts.
Troubleshooting SELinux Denials
If you encounter SELinux denials, you can use the matchpathcon command to verify the security context of the affected files and directories. This can help you identify and resolve issues with SELinux policies.
For example, if you receive a denial related to a file in the /var/www/html directory, you can use the following command to query its security context −
matchpathcon /var/www/html/index.html

In this example, the security context system_u:object_r:httpd_sys_content_t:s0 is assigned to the file index.html. You can compare this with the expected context to identify any discrepancies.
Automating Security Context Verification
You can automate the process of verifying security contexts by creating a script that uses the matchpathcon command. This can be particularly useful for regular security audits.
Here's an example of a simple script that verifies the security contexts of critical files and directories −
#!/bin/ # List of paths to verify paths=( "/etc/passwd" "/etc/shadow" "/var/log" "/var/www/html" )
Verify security contexts
for path in "${paths[@]}"; do matchpathcon "$path" done
Save this script as verify_contexts.sh and make it executable −
chmod +x verify_contexts.sh

You can then run the script to verify the security contexts of the specified paths −
./verify_contexts.sh

Conclusion
The matchpathcon command in Linux is a powerful tool for querying and verifying the security context of files and directories based on SELinux policies. By understanding how to use this command and its various options, you can ensure that your system's security contexts are correctly configured and compliant with SELinux policies.
Whether you're verifying security contexts after a system update, troubleshooting SELinux denials, or automating security audits, the matchpathcon command provides the flexibility and control you need.
By mastering the matchpathcon command, you'll be well-equipped to handle any SELinux-related task in Linux, making your system more secure and reliable. For more detailed information, you can refer to the official Linux documentation.