SELinux Python Bindings (Shim)

0.3.0 · active · verified Sun Apr 12

The `selinux` library for Python provides a shim for interacting with the system's SELinux module. It allows Python applications to query SELinux status, manage security contexts, and perform other SELinux-related operations. It primarily acts as a wrapper around the system's native `_selinux` module, which is typically provided by the `libselinux-python` package on Linux distributions. The current version is 0.3.0, with an active release cadence driven by contributions.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to import the `selinux` module and check if SELinux is enabled on the system, along with its current enforcement status and policy version. This code will gracefully handle systems where SELinux is not active.

import selinux

if selinux.is_selinux_enabled():
    print(f"SELinux is enabled: {selinux.security_getenforce() == 1}")
    print(f"Current SELinux mode: {'Enforcing' if selinux.security_getenforce() == 1 else 'Permissive'}")
    print(f"SELinux policy version: {selinux.security_getpolicyvers()}")
else:
    print("SELinux is not enabled on this system or the module is not fully functional.")

view raw JSON →