Home

About Us

Advertisement

Contact Us

PRIVACY POLICY

  • Facebook
  • X
  • Instagram
  • Pinterest
  • WhatsApp
  • RSS Feed
  • TikTok
ALT

Maalavs Blog

Your Trusted Voice Across the World.

  • NEWS
  • Linux Tech Blogs
  • Windows Tech Blogs
  • VIRTUALIZATION Blogs
Search

šŸ” Linux Security Hardening: SELinux & Semanage šŸ–„ļø

Author: Madhan Gopalakrishnan | Published on : 13-02-2025

SELinux (Security-Enhanced Linux) is a security architecture that enforces mandatory access controls (MAC) on Linux systems. It adds an extra layer of security by defining and enforcing policies that restrict the actions of users and applications. The semanage command is a crucial tool in managing SELinux policies, making it easier to configure and maintain security contexts. This guide provides an in-depth and beginner-friendly explanation of SELinux and the semanage tool. šŸš€


šŸ›”ļø Understanding SELinux

šŸ›  FeaturešŸ” Description
Enforcement ModeDefines how SELinux policies are applied (Enforcing, Permissive, or Disabled).
Security ContextsLabels processes and files with a security context to control access.
BooleansToggle SELinux policy features dynamically without modifying policies.
Targeted PolicyThe default SELinux policy that enforces restrictions on specific processes.

SELinux Modes:

šŸ” ModešŸ›  Description
EnforcingSELinux actively enforces security policies and blocks violations.
PermissiveSELinux logs policy violations but does not enforce them. Useful for debugging.
DisabledSELinux is completely turned off. This is not recommended for security.

šŸ” Checking and Setting SELinux Mode

  • To check the current SELinux mode: sestatus
  • To temporarily set SELinux to permissive mode: sudo setenforce 0
  • To permanently change SELinux mode, edit /etc/selinux/config: sudo nano /etc/selinux/config Change the line: SELINUX=enforcing # Change to permissive or disabled if needed Save and reboot for changes to take effect.

šŸ›  Managing SELinux Policies with Semanage

semanage is used to modify SELinux policies, manage file contexts, port labeling, booleans, and user mappings.

šŸ” Common semanage Commands

šŸ›  CommandšŸ” Description
semanage fcontextManage file security contexts.
semanage portModify SELinux rules for network ports.
semanage booleanEnable or disable SELinux booleans.
semanage loginMap Linux users to SELinux roles.
semanage permissiveMark domains as permissive for debugging.

šŸ” Changing File Security Contexts

  1. List file contexts: semanage fcontext -l
  2. Add a new file context: semanage fcontext -a -t httpd_sys_content_t '/myweb(/.*)?'
  3. Apply changes using restorecon: restorecon -Rv /myweb

šŸ” Managing Network Ports with SELinux

  1. List current SELinux port mappings: semanage port -l
  2. Allow a service to use a custom port (e.g., Apache on port 8080): semanage port -a -t http_port_t -p tcp 8080
  3. Modify an existing port label: semanage port -m -t http_port_t -p tcp 9090

šŸ” Enabling and Disabling SELinux Booleans

  1. List all SELinux booleans: getsebool -a
  2. Enable or disable a boolean (e.g., allow Apache to connect to a database): setsebool -P httpd_can_network_connect_db on

šŸ” Assigning SELinux Roles to Users

  1. List current user mappings: semanage login -l
  2. Map a Linux user to an SELinux role: semanage login -a -s user_u myuser

šŸ” Auditing SELinux Logs with Ausearch

The ausearch tool is used to analyze SELinux logs and troubleshoot security issues effectively.

šŸ” Using ausearch for SELinux Events

  1. Search for SELinux AVC (Access Vector Cache) denials: ausearch -m avc
  2. Filter logs based on a specific process (e.g., Apache): ausearch -c httpd
  3. Search logs for a specific time range: ausearch -m avc -ts yesterday
  4. View logs for a particular user: ausearch -m USER_LOGIN -ua 1000

Using ausearch effectively helps administrators identify policy violations and security issues that need to be addressed to ensure a hardened Linux environment.


šŸ”„ Best Practices for SELinux Security Hardening

  1. Always keep SELinux in enforcing mode unless troubleshooting.
  2. Regularly audit SELinux logs for security violations: ausearch -m avc
  3. Use restorecon to fix mislabelled files instead of disabling SELinux.
  4. Enable relevant SELinux booleans based on system requirements.
  5. Keep SELinux policies updated to prevent vulnerabilities.
  6. Use semanage to properly configure ports, file contexts, and user mappings.
  7. Implement log monitoring and alerting systems for SELinux-related issues.

šŸš€ Conclusion

SELinux provides an advanced security mechanism for Linux systems by enforcing strict access controls. The semanage command simplifies policy management, making it easier to configure security settings. The ausearch tool is invaluable for analyzing logs and detecting policy violations. By understanding and properly configuring SELinux, administrators can significantly enhance system security and reduce risks from unauthorized access.

By implementing these practices, you can achieve a robust and secure Linux system. šŸ”’šŸ§ Happy Securing! šŸš€

Featured Articles

  • Insights into Royal Family Dynamics

    Insights into Royal Family Dynamics

    February 26, 2025
  • Over 50 Deaths Linked to ‘Unknown Disease’ in Congo

    Over 50 Deaths Linked to ‘Unknown Disease’ in Congo

    February 26, 2025
  • North Korea Accused of Major Cyber Heist

    North Korea Accused of Major Cyber Heist

    February 26, 2025
  • Manchester United Announces Job Cuts Amid Financial Restructuring

    Manchester United Announces Job Cuts Amid Financial Restructuring

    February 26, 2025
  • Shannon Sharpe Criticizes Zion Williamson’s Fitness: A Deep Dive into the Controversy

    Shannon Sharpe Criticizes Zion Williamson’s Fitness: A Deep Dive into the Controversy

    February 26, 2025

Search

Author Details

Madhan Gopalakrishnan

I am a passionate “tech blogger” with a knack for breaking down complex topics into simple insights or exploring the latest trends in AI With 5 years of experience in IT Infra implementation and maintenance, I love to share knowledge through in-depth articles and practical tips. When not writing, you can find my hobby “traveling to offbeat destinations”.

  • X
  • Instagram
  • TikTok
  • Facebook

Follow Us on

  • Facebook
  • X
  • Instagram
  • VK
  • Pinterest
  • Last.fm
  • TikTok
  • Telegram
  • WhatsApp
  • RSS Feed

Categories

  • Article (15)
  • NEWS (73)

Archives

  • February 2025 (88)

Tags

About Us

Maalavs Magazine

It is your hub for the latest news across all domains, from technology and business to travel and innovation. We bring insightful updates, expert opinions, and engaging content to keep you informed and ahead. Whether you’re a tech enthusiast, a business professional, or a curious reader, we’ve got something for you. Stay updated with Maalavs Magazine!

Latest Articles

  • Insights into Royal Family Dynamics

    Insights into Royal Family Dynamics

    February 26, 2025
  • Over 50 Deaths Linked to ‘Unknown Disease’ in Congo

    Over 50 Deaths Linked to ‘Unknown Disease’ in Congo

    February 26, 2025
  • North Korea Accused of Major Cyber Heist

    North Korea Accused of Major Cyber Heist

    February 26, 2025

Categories

  • Article (15)
  • NEWS (73)
  • Instagram
  • Facebook
  • LinkedIn
  • X
  • VK
  • TikTok

Proudly Powered by Maalavs | Maalavs Magazine

Scroll to Top