這兩天在搗鼓CentOS 7,因為之前是在Debian用KVM,於是這次照着之前的做法來一步步安裝KVM,然而最后打算用virt-manager來管理的時候,發現被拒之門外了。
Unable to connect to libvirt.
authentication failed: no agent is available to authenticate.
呃,真夠磨人的。看着像是權限問題。嘗試了下,發現root級別的qemu:///system 是出錯的,不過用戶級別的qemu:///session 沒有問題。因為默認是以非root用戶執行virt-manager的,應該是這個原因導致權限的問題。
用戶級別的權限受到很多限制,要全面發揮KVM的功能還是需要root級別。不過,現在卻被擋在了門外。我先是檢查了各種包有沒有安裝,結果是該裝的都裝了。
又看了下libvirtd服務的狀態,一樣:
$ service libvirtd status Jan 06 02:55:16 - libvirtd[1195]: libvirt version: 1.2.17, package: 13.el7_2.2 (CentOS BuildSystem <http://bugs.centos.org>, 2015-12-09-13:08:22, worker1.bsys.centos.org) Jan 06 02:55:16 - libvirtd[1195]: authentication failed: no agent is available to authenticate Jan 06 02:55:16 - libvirtd[1195]: End of file while reading data: Input/output error
然后又Google了下,網上很多CentOS教程第一步就是說禁用SELinux,不過CentOS官方Wiki沒有提這點。而且我沒有打算把虛擬機放到默認路徑之外,應該不是SELinux的問題。
最后在Red Hat的郵件列表搜到一篇,提到有關“no agent is available to authenticate”錯誤以及解決方法 ( https://www.redhat.com/archives/libvir-list/2015-June/msg00583.html )。照着這封郵件第二個鏈接 ( https://fedorapeople.org/groups/docs/cookbook/#access-to-libvirt-without-root-privileges ) 中的方法做了后,最終解決了我的非root用戶運行virt-manager時的問題。
下面為概括說明
1、為libvirt添加一條polkit策略
vi /etc/polkit-1/localauthority/50-local.d/50-org.libvirt-group-access.pkla
內容為:
[libvirt group Management Access] Identity=unix-group:libvirt Action=org.libvirt.unix.manage ResultAny=yes ResultInactive=yes ResultActive=yes
2、創建libvirt組,將需要用於管理的非root用戶添加到libvirt組。
groupadd libvirt
usermod -a -G libvirt user_name
至此,問題解決。
參考:
https://www.redhat.com/archives/libvir-list/2015-June/msg00583.html
https://fedorapeople.org/groups/docs/cookbook/#access-to-libvirt-without-root-privileges
轉自:https://blog.xiaoben.li/p/497