Virt-manager and libvirt is core tools used for virtualization in Linux ecosystem. As a end user I am using these tools to create and run virtual machines. I am running this tools as normal user without using privileged user like root
But every time I try to run these tool the sudo
password is asked me. This become a nightmare after some time. Are there any solution to run these tools without putting password and changing any permission in virtualization side. Yes.
Virt-manager和libvirt是Linux生态系统中用于虚拟化的核心工具。 作为最终用户,我正在使用这些工具来创建和运行虚拟机。 我以普通用户身份运行此工具,而没有使用root
用户这样的特权用户,但是每次我尝试运行这些工具时,都要求输入sudo
密码。 一段时间后,这成为一场噩梦。 是否有任何解决方案可以运行这些工具而无需在虚拟化方面输入密码和更改任何权限。 是。
PolicyKit or simply Polkit is a component used to controlling system wide privileges in Unix and Linux operating systems. Fedora Linux distribution heavily uses Polkit. We will use Polkit to authenticate our self and start virt-manager without password.
PolicyKit或简称Polkit是用于控制Unix和Linux操作系统中的系统范围特权的组件。 Fedora Linux发行版大量使用Polkit。 我们将使用Polkit来验证自己的身份,并无需密码即可启动virt-manager。
To run virtualization services and software we need a group which have right to access related system resources. Most of the operating systems create this group as libvirt
. If not create the group with the following command. But keep in mind this needs root privileges.
要运行虚拟化服务和软件,我们需要一个有权访问相关系统资源的小组。 大多数操作系统将此组创建为libvirt
。 如果没有,请使用以下命令创建组。 但是请记住,这需要root特权。
$ groupadd libvirt
Now we need to put our normal or current user to the virtualization group. As stated previous step the group name is libvirt
but if it is different please change accordingly. In this command we added secondary group named libvirt
to the user john
现在,我们需要将普通用户或当前用户加入虚拟化小组。 如上一步所述,组名称为libvirt
但如果不同,请进行相应更改。 在此命令中,我们向用户john添加了名为libvirt
辅助组。
$ sudo usermod -a -G libvirt john
We will create polkit rule by using libvirt
group. Create a file like
我们将使用libvirt
组创建polkit规则。 创建一个类似的文件
/etc/polkit-1/rules.d/80-libvirt.rules
And put following content to the file. This rule will give libvirt
groups users access to the virtualization capabilities without password.
并将以下内容放入文件中。 此规则将使libvirt
组用户无需密码即可访问虚拟化功能。
polkit.addRule(function(action, subject) {
if (action.id == "org.libvirt.unix.manage" && subject.local && subject.active && subject.isInGroup("libvirt")) {
return polkit.Result.YES;
}
});