Linux命令-seinfo(SELinux 策略信息查询 —— 查看安全上下文与策略规则)
- 🔰 简介
- 安装位置
- 📖 语法
- ⚙️ 选项
- 💡 示例
- 示例 1:查看 SELinux 策略统计
- 示例 2:列出与 Web 服务相关的 SELinux 类型
- 示例 3:列出 SELinux 用户和角色
- 示例 4:列出所有布尔值及其当前状态
- 示例 5:列出 SELinux 类别和属性
- 示例 6:查看网络端口上下文
- 示例 7:完整查看所有策略信息
- ⚠️ 注意
- SELinux 相关工具集
- seinfo vs sesearch 使用场景
- 发行版差异
- SELinux 状态检查
- 📝 总结
- 📚 相关命令
快速参考:seinfo是 SELinux 策略分析工具集(setools-console)中的一员,用于从二进制 SELinux 策略文件中查询和提取类型(type)、角色(role)、用户(user)、布尔值(boolean)、类别(class)等安全策略信息。它是理解 SELinux 策略结构和问题排查的重要工具。需要安装setools-console包。
🔰 简介
SELinux(Security-Enhanced Linux)是 Linux 内核中的强制访问控制(MAC)系统。策略文件(policy.33或类似)以二进制格式存储,seinfo可以解析并展示这些策略的构成要素,帮助管理员了解当前系统中定义了哪些安全类型、哪些角色、哪些访问控制类别。
安装位置
# seinfo 命令路径whichseinfo# 输出:/usr/bin/seinfo# 查看所属软件包rpm-qf/usr/bin/seinfo2>/dev/null# 输出:setools-console-3.3.8-4.el7.x86_64dpkg-S/usr/bin/seinfo2>/dev/null# 输出:setools: /usr/bin/seinfo# 安装# CentOS/RHEL 7sudoyuminstallsetools-console# CentOS/RHEL 8+sudodnfinstallsetools-console# Debian/Ubuntusudoaptinstallsetools# SELinux 策略文件位置ls/etc/selinux/targeted/policy/# 输出:policy.33 (或其他版本号)# 路径模式:/etc/selinux/{策略名}/policy/policy.{版本号}📖 语法
seinfo [选项] [策略文件]如果不指定策略文件,默认使用当前活动的 SELinux 策略。
⚙️ 选项
| 选项 | 说明 |
|---|---|
-a, --all | 显示所有信息(类型、角色、用户、布尔值、类别等) |
-t, --type | 列出所有 SELinux 类型(最常用) |
-r, --role | 列出所有 SELinux 角色 |
-u, --user | 列出所有 SELinux 用户 |
-b, --bool | 列出所有 SELinux 布尔值及其状态 |
-c, --class | 列出所有对象类别(文件、进程、套接字等) |
--attribute | 列出类型属性 |
--sens | 列出敏感度级别 |
--cat | 列出类别(MLS/MCS 使用) |
-x, --expand | 展开显示(属性成员详细列表) |
--stats | 显示策略统计信息 |
-A, --avtab | 显示访问向量表(规则数较多) |
--polcap | 显示策略能力 |
--genfscon | 显示 genfscon 语句 |
--netifcon | 显示网络接口上下文 |
--nodecon | 显示节点上下文 |
--portcon | 显示端口上下文 |
💡 示例
示例 1:查看 SELinux 策略统计
# 查看策略整体统计信息seinfo--stats# 输出示例:# Statistics for policy file: /etc/selinux/targeted/policy/policy.33# Policy Version & Type: v.33 (binary, MLS)## Classes: 134 Permissions: 436# Sensitivities: 1 Categories: 1024# Types: 4875 Attributes: 252# Users: 9 Roles: 14# Booleans: 316 Cond. Expr.: 361# Allow: 113267 Neverallow: 0# Auditallow: 16 Dontaudit: 2017# Type_trans: 20039 Type_change: 74# Type_member: 0 Role allow: 36# Role_trans: 389 Range_trans: 682# Constraints: 106 Validatetrans: 0# Initial SIDs: 27 Fs_use: 34# Genfscon: 109 Portcon: 587# Netifcon: 0 Nodecon: 0# Permissives: 78 Polcap: 5# Ibpkeycon: 0 Ibendportcon: 0示例 2:列出与 Web 服务相关的 SELinux 类型
# 列出所有类型并过滤 http 相关项seinfo-t|grephttp# 输出示例:# httpd_t# httpd_sys_content_t# httpd_sys_script_t# httpd_sys_script_exec_t# httpd_sys_rw_content_t# httpd_sys_content_rw_t# httpd_user_content_t# httpd_user_script_t# httpd_user_script_exec_t# httpd_cache_t# httpd_tmp_t# httpd_log_t# httpd_config_t# httpd_exec_t# httpd_unit_file_t# ...# 统计 http 相关类型数量seinfo-t|grephttp|wc-l# 输出:47示例 3:列出 SELinux 用户和角色
# 列出所有 SELinux 用户seinfo-u# 输出:# Users: 9# guest_u# root# staff_u# sysadm_u# system_u# unconfined_u# user_u# xguest_u# *# 列出所有 SELinux 角色seinfo-r# 输出:# Roles: 14# auditadm_r# dbadm_r# guest_r# logadm_r# nx_server_r# object_r# secadm_r# staff_r# sysadm_r# system_r# unconfined_r# user_r# webadm_r# xguest_r# 查看特定角色的详细信息seinfo-ruser_r-x# 输出:# user_r# Dominated Roles:# user_r# Types:# virt_content_t# httpd_user_content_t# ...示例 4:列出所有布尔值及其当前状态
# 列出所有布尔值seinfo-b|head-20# 输出:# Boolean: 316# abrt_handle_event# abrt_upload_watch_anon_write# antivirus_can_scan_system# antivirus_use_jit# auditadm_exec_content# authlogin_nsswitch_use_ldap# authlogin_radius# authlogin_yubikey# awstats_purge_apache_log_files# boinc_execmem# ...# 查看 httpd 相关布尔值seinfo-b|grephttpd# 输出:# httpd_anon_write# httpd_builtin_scripting# httpd_can_check_spam# httpd_can_connect_ftp# httpd_can_connect_ldap# httpd_can_connect_mythtv# httpd_can_connect_zabbix# httpd_can_network_connect# httpd_can_network_connect_cobbler# httpd_can_network_connect_db# httpd_can_network_memcache# httpd_can_network_relay# httpd_can_sendmail# httpd_dbus_avahi# httpd_dbus_sssd# httpd_dontaudit_search_dirs# httpd_enable_cgi# httpd_enable_ftp_server# httpd_enable_homedirs# httpd_execmem# httpd_graceful_shutdown# httpd_manage_ipa# httpd_mod_auth_ntlm_winbind# httpd_mod_auth_pam# httpd_read_user_content# httpd_run_ipa# httpd_run_preupgrade# httpd_run_stickydir# httpd_serve_cobbler_files# httpd_setrlimit# httpd_ssi_exec# httpd_sys_script_anon_write# httpd_tmp_exec# httpd_tty_comm# httpd_unified# httpd_use_cifs# httpd_use_fusefs# httpd_use_gpg# httpd_use_nfs# httpd_use_opencryptoki# httpd_use_sasl# httpd_verify_dns示例 5:列出 SELinux 类别和属性
# 列出所有对象类别seinfo-c# 输出:# Classes: 134# appletalk_socket# association# atmpvc_socket# atm_svc_socket# ax25_socket# blk_file# bridge_socket# can_socket# cap_userns# cap2_userns# chr_file# context# db_blob# db_buffer# db_column# ...# 展开查看类型属性(查看哪些类型属于某个属性域)seinfo-adomain-x|head-30# 输出:# domain# abrt_dump_oops_t# abrt_handle_event_t# abrt_helper_t# abrt_retrace_worker_t# abrt_t# abrt_watch_log_t# abrt_upload_watch_t# accountsd_t# ...示例 6:查看网络端口上下文
# 查看所有端口安全上下文定义seinfo--portcon|head-20# 输出示例:# Portcon: 587# portcon tcp 0-511 system_u:object_r:reserved_port_t:s0# portcon tcp 80 system_u:object_r:http_port_t:s0# portcon tcp 443 system_u:object_r:http_port_t:s0# portcon tcp 22 system_u:object_r:ssh_port_t:s0# portcon tcp 25 system_u:object_r:smtp_port_t:s0# portcon tcp 3306 system_u:object_r:mysqld_port_t:s0# portcon tcp 5432 system_u:object_r:postgresql_port_t:s0# portcon udp 53 system_u:object_r:dns_port_t:s0# portcon udp 123 system_u:object_r:ntp_port_t:s0# ...# 查看与 http 相关的端口定义seinfo--portcon|grephttp# 输出:# portcon tcp 80 system_u:object_r:http_port_t:s0# portcon tcp 443 system_u:object_r:http_port_t:s0# portcon tcp 488 system_u:object_r:http_port_t:s0# ...示例 7:完整查看所有策略信息
# 查看所有策略信息(输出非常多)seinfo-a2>/dev/null|head-50# 输出(前50行):# ...# 包含类型、角色、用户、布尔值、类别的完整列表## 建议重定向到文件查看:seinfo-a>/tmp/selinux_policy_full.txt2>&1wc-l/tmp/selinux_policy_full.txt# 输出:15234 /tmp/selinux_policy_full.txt⚠️ 注意
SELinux 相关工具集
# setools-console 包含的常用工具rpm-qlsetools-console|grep/usr/bin/# 输出:# /usr/bin/seinfo # 策略信息查询# /usr/bin/sesearch # 策略规则搜索(查找 allow/auditallow 等规则)# /usr/bin/seinfo # 策略信息查询# /usr/bin/sediff # 策略比较(版本差异)# /usr/bin/sechecker # 策略安全检查# 这些工具的关系:# seinfo → 告诉你有多少类型、多少角色、多少类别# sesearch → 查找特定的 allow 规则(如 httpd_t 能访问哪些文件)# sediff → 比较两个策略版本之间的差异seinfo vs sesearch 使用场景
# seinfo:宏观了解策略结构seinfo-t|grephttpd# httpd 有哪些类型?seinfo-b|grephttpd# httpd 有哪些布尔值开关?# sesearch:微观查询具体规则sesearch-A-shttpd_t-thttpd_sys_content_t# 查询 httpd_t 类型对 httpd_sys_content_t 类型的访问规则sesearch-A-shttpd_t|grep"read"# 查询 httpd_t 类型的读权限发行版差异
# CentOS/RHEL 7# SELinux 默认 enforcing,策略版本 v.28 ~ v.33# setools-console 包名# CentOS/RHEL 8/9# SELinux 默认 enforcing,策略版本 v.33# setools-console 包名不变sudodnfinstallsetools-console# Fedora# 包管理相同,但 Selinux 策略更激进sudodnfinstallsetools-console# Debian/Ubuntu# SELinux 默认不安装(使用 AppArmor 替代)sudoaptinstallselinux-basics selinux-policy-defaultsudoaptinstallsetools# 注意:Ubuntu 中 seinfo 输出通常为空(SELinux 未启用)SELinux 状态检查
# 检查 SELinux 是否启用getenforce# 输出:Enforcing / Permissive / Disabledsestatus# 输出:# SELinux status: enabled# SELinuxfs mount: /sys/fs/selinux# SELinux root directory: /etc/selinux# Loaded policy name: targeted# Current mode: enforcing# Mode from config file: enforcing# Policy MLS status: enabled# Policy deny_unknown status: allowed# Max kernel policy version: 33# 如果 SELinux 为 Disabled,seinfo 无法读取策略sudoseinfo--stats# 输出:error: cannot open policy file: /etc/selinux/targeted/policy/policy.33📝 总结
seinfo是 SELinux 策略分析工具链中的"宏观查看器":
- 核心功能:解析二进制 SELinux 策略文件,展示类型、角色、用户、布尔值、类别等元数据
- 最常用选项:
-t(类型)、-b(布尔值)、--stats(统计摘要) - 与 sesearch 的关系:
seinfo告诉你"有什么",sesearch告诉你"怎么运作" - 前提条件:SELinux 必须处于 Enforcing 或 Permissive 模式,且安装了
setools-console - 实用价值:排查 SELinux 引起的服务异常时,先用
seinfo -t | grep 服务名了解相关安全类型
📚 相关命令
| 命令 | 说明 |
|---|---|
sesearch | 查询 SELinux 策略中的具体访问规则 |
semanage | SELinux 策略管理(端口/文件上下文/布尔值配置) |
restorecon | 恢复文件默认 SELinux 上下文 |
chcon | 修改文件 SELinux 上下文 |
getenforce | 查看 SELinux 当前模式 |
setenforce | 切换 SELinux 运行模式 |
setsebool | 设置 SELinux 布尔值 |
audit2why | 将 AVC 拒绝日志翻译为人类可读的解释 |
audit2allow | 根据拒绝日志生成允许规则 |