ubuntu

Ubuntu SELinux与防火墙如何配合

小樊
43
2025-09-29 12:36:51
栏目: 智能运维

Understanding SELinux and UFW in Ubuntu
Ubuntu systems typically use AppArmor as the default mandatory access control (MAC) tool, but SELinux (Security-Enhanced Linux) can be installed to provide more granular access control. SELinux enforces policies that restrict processes and users from accessing resources (files, ports, etc.) beyond their defined permissions. UFW (Uncomplicated Firewall) is the default frontend for managing firewall rules in Ubuntu, built on top of iptables. It simplifies the process of allowing/blocking traffic by translating user-friendly commands into low-level iptables rules. While UFW handles network-layer filtering, SELinux enforces host-based access control, making them complementary tools for a layered security approach.

Installing and Enabling SELinux on Ubuntu
By default, SELinux is not installed on Ubuntu. To enable it:

  1. Install required packages:
    sudo apt update && sudo apt install selinux-basics selinux-policy-default auditd
    
  2. Activate SELinux:
    sudo selinux-activate
    
  3. Set the enforcement mode (recommended: Permissive for testing, Enforcing for production):
    • Permissive mode logs violations without blocking actions (useful for debugging):
      sudo setenforce 0
      
    • Enforcing mode actively blocks unauthorized actions:
      sudo setenforce 1
      
  4. Make the mode persistent across reboots by editing /etc/selinux/config and setting SELINUX=enforcing.

Configuring UFW for Basic Firewall Rules
UFW simplifies firewall management with intuitive commands. Key steps include:

  1. Install UFW (if not already installed):
    sudo apt install ufw
    
  2. Enable UFW:
    sudo ufw enable
    
  3. Set default policies (deny all incoming traffic, allow all outgoing traffic):
    sudo ufw default deny incoming
    sudo ufw default allow outgoing
    
  4. Allow specific services/ports (e.g., SSH, HTTP, HTTPS):
    sudo ufw allow ssh     # Allows SSH (port 22)
    sudo ufw allow http    # Allows HTTP (port 80)
    sudo ufw allow https   # Allows HTTPS (port 443)
    
  5. Verify UFW status:
    sudo ufw status verbose
    

This displays active rules, default policies, and rule priorities.

Key Considerations for Compatibility
While SELinux and UFW can coexist, conflicts may arise if rules overlap or block each other. To ensure smooth operation:

Example Workflow: Allowing SSH with Both Tools
To allow SSH while leveraging both UFW and SELinux:

  1. Configure UFW to allow SSH:
    sudo ufw allow ssh
    
  2. Verify SELinux allows SSH access:
    • Check if the SSH service has the correct SELinux boolean enabled (e.g., ssh_sysadm_login for sysadmin logins):
      sudo getsebool ssh_sysadm_login
      
    • If disabled, enable it:
      sudo setsebool -P ssh_sysadm_login on
      
  3. Test the configuration:
    • Attempt to SSH into the system from an authorized IP.
    • Check UFW logs (sudo ufw status numbered) to confirm the rule is applied.
    • Check SELinux logs (sudo ausearch -m avc -ts recent) for any denied actions related to SSH.

Troubleshooting Common Issues

0
看了该问题的人还看了