详解polkitd(polkitd进程)

polkitd是一个在Linux系统中广泛使用的授权管理守护进程,它的作用是轻松控制用户进程对系统资源进行访问和操作。

一、polkitd用户

polkitd以系统用户的身份运行,通常是以root或polkitd用户身份运行。在CentOS 7中,polkitd用户是由系统会话服务处理的,而在CentOS 8中,polkitd用户则默认由polkitd包提供。

polkitd用户通常是被设计用来运行polkitd守护进程的用户,它拥有对系统资源的高权限访问权,因此在保护系统安全方面具有重要作用。当用户请求对系统资源进行访问操作时,polkitd会检查该用户所属组是否具有合适的权限,从而决定是否授权。

二、polkitd进程占用CPU高

有时,用户可能会发现polkitd进程会消耗很多CPU资源,这通常是由于polkitd正在进行授权检查过程。为了避免这种情况,可以采取以下措施:

1、禁用polkitd的debug模式,这通常可以通过在/etc/polkit-1/polkitd.conf配置文件中修改debug选项来实现。警告:禁用debug模式可能导致polkitd运行异常,因此必须小心谨慎。

[debug]
# Enable/disable debugging
#debug=no
debug=yes #改为no

2、尽量减少需要进行授权检查的操作。这可以通过审查程序的代码、减少程序的调用次数以及在可能的情况下缓存已通过授权检查的结果等方式来实现。

三、polkitd相关配置

polkitd的配置文件通常位于/etc/polkit-1/目录下。重要的配置文件包括:

1、polkitd.conf:polkitd的主配置文件,包括运行模式、授权验证策略、debug选项等。

[General]
# This is a comment
# A comment can appear anywhere on a line
# Comments start with a hash symbol.

# Here, we define a new constant:
# We use this constant later to check if a client is a member
# of the wheel group, and we grant the client the admin
# authentication level if it is.
const=wheel_check,check_group(wheel)

# Uncomment this to enforce the same authentication requirements for
# every action
#DefaultAuthentication=auth_admin

# In addition to authentication, authorization is required to
# execute privileged actions. In situations where authorization
# precedes authentication, you can change the control flow by
# setting this variable.
#PrioritizeAuthorization=0

# Define the authentication agent(s) that will be used for obtaining
# passwords or challenges.
#DefaultAuthenticationAgent=pkexec

# Define the authorization agent(s) that will be used for obtaining
# authorization decisions.
#DefaultAuthorizationAgent=org.freedesktop.policykit.exec

# The following controls which users can access the system via Polkit
# Please note that enabling this is not a good idea due to security
# reasons.
# AllowRoot=no

2、pkla:policy kit local authority的缩写,一个XML格式的本地授权文件,用于允许或拒绝用户执行特定的命令或操作。

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE pklocalauthority PUBLIC "-//freedesktop//DTD pklocalauthority//EN" "http://www.freedesktop.org/software/polkit/pklocalauthority.dtd">

<pklocalauthority>
  <!-- Let unprivileged users reboot and shut down the system. -->
  <action id="org.freedesktop.consolekit.system.restart-allow-more">
    <description>Restart allowed for everyone and multiple logind instances</description>
    <message>Authentication is required to restart the system</message>
    <defaults>
      <allow_any>no</allow_any>
      <allow_inactive>no</allow_inactive>
      <allow_active>auth_admin_keep</allow_active>
    </defaults>
    <annotate key="org.freedesktop.consolekit.system.restart-multiple-sessions" type="BOOL" value="true">Whether to allow restarting when multiple sessions are active</annotate>
  </action>
</pklocalauthority>

3、policykit-default-privs:用于为所有系统提供通用行为的本地授权文件。

org.freedesktop.hal.detach-encrypted.partitions
org.freedesktop.hal.power-management.hibernate
org.freedesktop.hal.power-management.reboot
org.freedesktop.hal.power-management.shutdown
org.freedesktop.hal.power-management.suspend
org.freedesktop.hal.power-management.suspend-hybrid

Published by

风君子

独自遨游何稽首 揭天掀地慰生平