Android Studio: /dev/kvm device permission denied
Asked Answered
S

30

688

When I try to run my Android app on an emulator I get this error:

/dev/kvm permission denied.

I checked the permissions and added the user I am currently logged in with to the kvm group. What is wrong?

Streetlight answered 18/5, 2016 at 13:16 Comment(1)
Followed this approach and it worked, sudo chown <username> /dev/kvm (This was on Ubuntu 18.04.4 LTS)Shiff
S
1057

As mentioned in the comments, starting with Ubuntu 18.04 and Linux Mint Tara you need to first sudo apt install qemu-kvm.

To check the ownership of /dev/kvm use

ls -al /dev/kvm

The user was root, the group kvm. To check which users are in the kvm group, use

grep kvm /etc/group

This returned

kvm:x:some_number:

on my system: as there is nothing rightwards of the final :, there are no users in the kvm group.

To add your user to the kvm group, you could use

sudo adduser $USER kvm

which adds the user to the group, and check once again with grep kvm /etc/group.

As mentioned by @marcolz, the command newgrp kvm should change the group membership live for you. If that did not work, @Knossos mentioned that you might want to log out and back in (or restart), for the permissions to take effect. Or do as @nmirceac mentioned and re-login in the same shell via su - $USER.

Suannesuarez answered 18/8, 2017 at 5:18 Comment(19)
After install qemu-kvm and add my user to the group kvm it did not work. But it did after restart computer (Ubuntu 18.04).Gateshead
@rupj does the trick however google has just released a patch for linux version that also fixes libstd++ issues.Permutation
What if the user was root and the group was root. Should we change its group?Losel
I had to everytime i turned on my pc sudo chown -R username:username /dev/kvm These steps saved my headache of everytime. Thansk @Suannesuarez This should be accepted as correct answer.Meredith
@slinstj Logging off and on again should be sufficient.Cornea
You can use this command to automatically add current user to kvm sudo adduser $USER kvmF
@MichaelFulton: in that case, sudo apt install qemu-kvm should add the kvm group (edit)Suannesuarez
@MichaelFulton I changed the user and group to my username. Then added my username to the group. Works.Halvaard
You might need to log out and back in AND restart Android Studio to see the change, for me, the 'refresh' button in the AVD manager didn't seem to refresh the permissionsGainless
I needed grant ownership of kvm to user too: sudo chown $USER /dev/kvm Intolerant
I don't know but this command worked for me to add user to the kvm usergroup: sudo usermod -a -G kvm seocliffOcclusive
@Gateshead to just logout and back in does the job too.Helenahelene
if you don't want to logout or restar, simply do sudo chown $USER /dev/kvm as described by @IvánRodríguezTorresKreitman
If you don't want to log out, use newgrp kvm as that will check your group membership like if you were logging in.Voiceful
For updating an existing user, it is probably best to use usermod -aG kvm $USER Also in my case (Ubuntu 18.04) I had to add the kvm group first.Grunt
don't chown it - most orthodox way is to refresh the groups - "su - $USER" as the user - and then check everything with the command idBuzz
@Gateshead A reboot/logout isn't strictly necessary. You can also do su - $USER, which refreshes the group assignment in that particular shell (i.e., you'll have to launch Android Studio from that shell).Allister
Works with me only need to reboot.Misguided
it's warned by Android studio itself you should logout and login to make changes take effect.Ewen
M
646

This is how I got it to work in Ubuntu 18.04

sudo apt install qemu-kvm

Add your user to kvm group using:

sudo adduser <Replace with username> kvm

If still showing permission denied:

sudo chown <Replace with username> /dev/kvm

Try it.

Mongoose answered 23/6, 2018 at 17:23 Comment(16)
Thanks! This worked for me too. I didn't have to perform the last step because my permissions were set to: crw-rw---- 1 root kvm 10, 232 Jul 27 15:32 /dev/kvm (so once I added my user to the kvm group, it just worked).Lindemann
step 2 might require a rebootWelby
Do I need to reboot OS?Saphena
Worked on Ubuntu 18.04.1! @NikolayBaranenko is not necessaryHopson
this helped me in ubuntu 18.04Excreta
Worked for me on Mint 19.1 after rebootQuincyquindecagon
It works like charm, especially the last command you mentioned. I did not even need to restart IDE or OS.Prerecord
you should not change the ownership of /dev/kvm/ folder. instead you should add the user to kvm group as in this answer: https://mcmap.net/q/63584/-android-studio-dev-kvm-device-permission-deniedHalliburton
this worked for me on ubuntu 18.04 I had to do all the steps,I rebooted android studio after step 2Acetylate
Thanks. Worked for me on Ubuntu 18.04Nullification
@JakobEriksson It requires one to re-login. Reboots are reserved for Windows (and systemd updates) ;-)Zuckerman
If I close my device and try to restart it, it wont work unless I do sudo chown <Replace with username> /dev/kvm againPemphigus
It worked with Linux Mint 19 Tricia, XFCE, without a rebootAnticlimax
Yes just as @Halliburton has suggested, you shouldn't change the ownership of /dev/kvm, because doing that you break the Default ACL of your Linux system, which is not a good security practice. Follow this instruction instead: https://mcmap.net/q/63584/-android-studio-dev-kvm-device-permission-deniedMeyeroff
How would one adapt this solution outside of Ubuntu 18.04? I don't even have the qemu-kvm package on Elementary OS, which is based on Ubuntu. I think it's better not to install an additional package.Indole
It worked for me just need to restart the system after instructionsReadable
S
53

Try this, it worked for me:

  1. sudo apt install qemu-kvm

  2. sudo chown -R <username>:<username> /dev/kvm

Schrecklichkeit answered 21/5, 2018 at 17:8 Comment(5)
You might need to relogin for it to take effect.Bluecollar
This is questionable, since it treats a multiuser system as a single user system. No other user will be able to use KVM when following this approach. Is that a good idea? I doubt it...Swordtail
This solution is plain wrong as it modifies the default rights to /dev/kvm. It's not just that it will not persist between reboots / system updates. It also forcefully modifies system settings. Stay away.Jacinto
Don't do the chown - add your username to the proper groups - kvm or libvirt. See the documentation for [help.ubuntu.com/community/KVM/Installation](Ubuntu).Bricky
Why do you chown a device file recursively? Also, what's with the suffix? This surely yields an error.Marmoreal
A
37

This is because /dev/kvm is not accessible. To make is accessible from android studio run the below command

sudo chmod 777 -R /dev/kvm

It will ask for your password. After that restart Android Studio.

KVM is required to rum emulator. If you have not install it yet then install it

sudo apt install qemu-kvm
Aguirre answered 15/1, 2019 at 8:48 Comment(4)
This answer did the trick in my case after following the most upvoted answer! Thanks!Dyandyana
Do not do that. See my answer above.Jacinto
@andrzejwp: What answer? I don't see any answer from you. Given how adamant you are about your answer, can you link to it?Xl
Why do you recursively (-R) change the permissions on a single device file? Also, the execute permissions are superfluous. Finally, udev may adjust the permissions after each emulator start.Marmoreal
U
32

Have you also tried following, it should work:

sudo chown <username> /dev/kvm
sudo chmod o+x /dev/kvm
Unchancy answered 14/7, 2018 at 18:37 Comment(4)
chmod o+x is completely useless. The first command make it work - but see my other comments way down why this is a bad idea.Frig
This works but does not persist a reboot. I am on POP OS though!Brady
This solution is plaing wrong as it modifies the default rights to /dev/kvm. It's not just that it will not persist between reboots / system updates. It also forcefuly modifies system settings. Stay away.Jacinto
This is not persistent over reboots. The udev daemon might re-adjust the permissions during its runtime and given others the executable permission bit is simply superfluous.Marmoreal
R
30
sudo chown $USER /dev/kvm

Simply running that one command worked for me here in September 2019 running:

Description: Ubuntu 18.04.3

LTS Release: 18.04

Codename: bionic

Ruppert answered 14/9, 2019 at 16:55 Comment(4)
Do not do that. See my answer above.Jacinto
@Jacinto Where is your answer? Is it in a comment? If it is, it is probably collapsed and I can't see it. I've searched the page for your user tag, but can't find. I am happy to try your answer out -- because my answer isn't quite right (doesn't persist) -- but I cannot find it. Leave a link or create a new answer and I'll try it.Ruppert
@andrzejwp: please link to your answers, instead of saying "above". What if your answer ends up below the one you're commenting on?Xl
This is not persistent over reboots and the udev daemon may readjust the ownership of /dev/kvm during its runtime.Marmoreal
L
29

Step 1: (Install qemu-kvm)

sudo apt install qemu-kvm

Step 2: (Add your user to kvm group using)

sudo adduser username kvm

Step 3: (If still showing permission denied)

sudo chown username /dev/kvm

Final step:

ls -al /dev/kvm
Lamkin answered 14/10, 2020 at 18:10 Comment(1)
Duplicate of several answers.Marmoreal
M
28

Under Ubuntu, the permissions of /dev/kvm usually look like this:

$ ls -l /dev/kvm
crw-rw---- 1 root kvm 10, 232 May 24 09:54 /dev/kvm

The user that runs the Android emulator (i.e. your user) needs to get access to this device.

Thus, there are basically 2 ways how to get access:

  • Make sure that your user is part of the kvm group (requires a re-login of your user after the change)
  • Widen the permissions of that device such that your user has access (requires a change to the udev daemon configuration)

Add User to KVM Group

Check if your user is already part of the kvm group, e.g.:

$ id 
uid=1000(juser) gid=1000(juser) groups=1000(juser),10(wheel)

If it isn't then add it with e.g.:

$ sudo usermod --append --groups kvm juser

After that change you have to logout and login again to make the group change effective (check again with id).

Widen Permissions

Alternatively, you can just can widen the permissions of the /dev/kvm device.

Example:

echo 'KERNEL=="kvm", GROUP="kvm", MODE="0666", OPTIONS+="static_node=kvm"' \
    | sudo tee /etc/udev/rules.d/99-kvm4all.rules
sudo udevadm control --reload-rules
sudo udevadm trigger --name-match=kvm

FWIW, this is the default on other distributions such as Fedora and CentOS.

Check the effectiveness of the above commands with another ls. You should see output similar to:

$ ls -l /dev/kvm
crw-rw-rw-. 1 root kvm 10, 232 2020-05-16 09:19 /dev/kvm

Big advantage: You don't need to logout and login again for this change to be effective.

Non-Solutions

  • calling chmod and chown directly on /dev/kvm - 1) these changes aren't persistent over reboots and 2) since /dev/kvm permissions are controlled by the udev daemon, it can 'fix' its permissions at any time, e.g. after each emulator run
  • adding executable permissions to /dev/kvm - your emulator just requires read and write permissions
  • changing permissions recursively on /dev/kvm - I don't know what's up with that - looks like cargo cult
  • installing extra packages like qemu - you already have your emulator installed - you just need to get access to the /dev/kvm device
Marmoreal answered 24/5, 2020 at 10:41 Comment(2)
An amazing solution, not only it does not require relogin, but also explains why most other solutions have unneeded steps or are plain flawed. +1!Naiad
Thank you. It worked, and you also explained WHY and HOW it works.Kick
A
24

I am using ubuntu 18.04. I was facing the same problem. I run this piece of command in terminal and problem is resolved.

sudo chown $USER /dev/kvm

the above command is for all the user present in your system.

If you want to give access to only a specific user then run this command

sudo chown UserNameHere /dev/kvm
Alverta answered 21/10, 2019 at 3:0 Comment(2)
No. This is not something you should do. See my answer above.Jacinto
How is this answer different from @raddevus' answer which he posted one month earlier?Marmoreal
A
21

Here is a simple solution

open the terminal and run the following commands

sudo groupadd -r kvm

sudo gedit /lib/udev/rules.d/60-qemu-system-common.rules

Add the following line to the opened file and save it

KERNEL=="kvm", GROUP="kvm", MODE="0660"

Finally run:

sudo usermod -a -G kvm <your_username>

Reboot your PC and Done!

Ambo answered 7/11, 2019 at 8:37 Comment(3)
This is bad advice. Never directly edit udev configuration under /lib/udev/rules.d. Udev has an overlay mechanism where configuration placed in /etc/udev/rules.d has priority over one available under /lib/udev/rules.d.Marmoreal
I'm not a professional Linux user, It's just a solution that worked for me and I decide to make it succinct and easy to read for others like me. thank you for your clarification, Max.Ambo
@NasirJafarzadeh: so why not edit your answer to create that override at /etc/udev/rules.d instead of editing /lib/udev/rules.d? That's what Gerd suggests, by the way.Naiad
F
20

There's absolutely no need to install qemu-kvm (and all its dependencies) if you only want to run the Android Studio Emulator.

The only thing you have to do is to give your user (i.e. the one you are logged in with) the right to access the /dev/kvm-device.

This is done in three simple steps.

First:

Create the kvm-group

groupadd -r kvm

The option -r creates a system group, i.e. with a GID <= 999 (see /etc/login.defs => SYS_GID_MAX)

Second:

Change permissions on /dev/kvm. This could be done as part of the qemu-kvm-installation, because one of the dependencies is installing qemu-system-common (on current Ubuntu systems, package name may vary), which in turn installs the file /lib/udev/rules.d/60-qemu-system-common.rules containing the following:

KERNEL=="kvm", GROUP="kvm", MODE="0660"

So if you are just create a file /etc/udev/rules.d/60-qemu-permissions.rules containing the above line, you are done with the first step.

Third:

Add your username to the group by executing

usermod -a -G kvm <your_username> - the -a is important for adding your user to the kvm-group. Without that you will overwrite the group-settings for your user to only belonging to "kvm"...

That's it.

For the new udev rule and group setting to take effect it's easiest to reboot and login again.

You can also execute

udevadm control --reload-rules && udevadm trigger

for reloading the rules but you still have to logout and login again with regard to the new group.

Frig answered 1/11, 2019 at 8:32 Comment(1)
Btw, +1 for pointing out that installing qemu-kvm is completely unnecessary for Android Studio. This is the best answerNaiad
P
13

I countered the same problem and to solve this issue just type the following commands in terminal for Linux clients

   sudo apt-get install qemu-kvm

    // type your password

   sudo chmod 777 -R /dev/kvm

and after that try running simulator it'll work

Prehension answered 16/3, 2019 at 17:54 Comment(2)
God, no. See my comments above.Jacinto
Superfluous install of qemu-kvm, superfluous use of recursive option and superfluously sets executive permission. Also non persistent over reboots and even during runtime of udev.Marmoreal
M
9
sudo setfacl -m u:$USER:rwx /dev/kvm

Worked for me.

Manned answered 21/2, 2019 at 11:36 Comment(2)
Way better than all the chown examples here, but I would omit the x from the permissions.Frig
Change isn't not persistent over reboots.Marmoreal
Z
6

I am using linux debian, and i am facing the same way. In my AVD showing me a message "/dev/kvm permission denied" and i tried to find the solution, then what i do to solve it is, in terminal type this :

sudo chmod -R 777 /dev/kvm

it will grant an access for folder /dev/kvm,then check again on your AVD , the error message will disappear, hope it will help.

Zebapda answered 17/6, 2019 at 2:0 Comment(3)
This is completely wrong. Do not do that, ever. See my comments above.Jacinto
@andrzejwp: comments above, WHERE?!Xl
Recursive option is superfluous, execution permissions are superfluous. Change not persistent over reboots. Change not stable during udev runtime.Marmoreal
D
3

This Worked For Me on Linux (x18) ☑ Hope It Will Work For You Aswell

sudo chown hp /dev/kvm
Dup answered 18/6, 2019 at 10:9 Comment(2)
No it wont, and please do not do that.Jacinto
Change is not persistent over reboots.Marmoreal
C
3

Just one slight improvement on Jerrin's answer on fixing this error with Ubuntu 18.04 by utilizing $USER variable available in the bash terminal. So you could use the following commands two commands:

sudo apt install qemu-kvm

Add the current user to the kvm group

sudo adduser $USER kvm

Also if you are still having issues, one other problem for me was the way in which I installed Ubuntu. I made the mistake of checking the box during installation for installing 3rd party software which did not play nice with my nvidia graphics card for development. So I reinstalled Ubuntu with this third party software unchecked.

enter image description here

Then after installation, open up Software & Updates and go to the Additional Drivers tab. Select the most up to date proprietary drivers that have also been tested and apply changes. Should restart the machine for the changes to take affect.

enter image description here

Catchpenny answered 26/8, 2019 at 6:55 Comment(0)
C
3

What finally fixed it for me on Ubuntu 18.04 was:

sudo apt install qemu-kvm
sudo adduser $USER kvm
sudo chown $USER /dev/kvm
Compartmentalize answered 21/4, 2020 at 14:1 Comment(1)
Duplicate of Jerin A Mathews'.Marmoreal
V
1
  1. I tried sudo setfacl -m u:UserName:rwx /dev/kvm . and it works .

  2. In the android studio you need to change : tools> avd manager >(chose the pen to edit your device and change 'graphics' from automatic to software ) to avoid emulator drawable error

Varied answered 1/10, 2020 at 14:30 Comment(0)
U
1

Here is what I did:

user@user:~$ whoami

antonio

sudo apt install qemu-kvm

sudo adduser antonio kvm

sudo chown antonio /dev/kvm

last but not least

On Android studio select File -> Restart IDE

to apply the changes

Then create the emulator

Unbacked answered 28/9, 2022 at 12:45 Comment(0)
Z
0

I was in a similar situation with the same error of permissions on /dev/kvm I had done the necessary installations but not added the user to the kvm group All I had to do was

sudo adduser <Replace with username> kvm

and ofcourse DON'T forget to restart your Ubuntu instance.

Zito answered 23/7, 2018 at 9:40 Comment(2)
Isn't logout/login enough?Hijack
@Hijack After adding a user to a group there's no need to reboot (even after 3.5 years ;-) )Frig
H
0

/dev/kvm permission denied android studio

[Motherboard]Set VT(Virtualization Technology) in BIOS and install Virtual Machine

enter image description here

Hoenack answered 23/11, 2022 at 6:9 Comment(0)
S
0
sudo usermod -aG kvm $USER
newgrp kvm

-aG option is to add Current $USER to group kvm

newgrp kvm is to switch current user group to kvm group. so the user will have the kvm related permission.

Sistrunk answered 7/10, 2023 at 3:6 Comment(1)
Thank you for your interest in contributing to the Stack Overflow community. This question already has quite a few answers—including one that has been extensively validated by the community. Are you certain your approach hasn’t been given previously? If so, it would be useful to explain how your approach is different, under what circumstances your approach might be preferred, and/or why you think the previous answers aren’t sufficient. Can you kindly edit your answer to offer an explanation?Arcboutant
A
-1

I got this error after updating my ubuntu to 18.04.1. I just download new system image for emulator or you can say that download new emulator and it is worked for me.

Advertent answered 10/9, 2018 at 5:30 Comment(1)
This won't change anything regarding the original problem with /dev/kvm-permissions.Frig
C
-1

Open Terminal and log as admin

sudo su

Go to the dev folder

cd /dev/

Change the kvm mode

chmod 777 -R kvm
Candleberry answered 6/4, 2019 at 6:27 Comment(4)
I think you mean cd /dev instead of sudo.Fellmonger
yeah, cd /dev .Candleberry
This works. But have to do this every time computer restarts.Hardnosed
Why do you call chmod with -R? Recursively changing the permissions on a single device file /dev/kvm doesn't look too useful. Also, execute permissions are superfluous on that device. Finally, udev may readjust the /dev/kvm permissions after each run. Thus, this method isn't very effective.Marmoreal
H
-1

Type in terminal:

sudo apt install qemu-kvm -y
sudo chown $USER /dev/kvm
Hardwick answered 18/7, 2019 at 23:17 Comment(0)
B
-1

Although KVM is a module built into the Linux kernel itself, it doesn't mean that all the necessary packages are included in your Ubuntu/Linux install by default. You'll need a few to get started, and they can be installed with this command in the terminal:

& sudo apt install qemu-kvm libvirt-clients libvirt-daemon-system bridge-utils virt-manager    

Configure the network bridge

In order for your virtual machines to access your network interface and be assigned their own IP addresses, we need to configure bridged networking on our system.

First, run the following Linux command in order to find out what name your network interface has been assigned. Knowing this will allow us to do additional configuration later.

$ ip a     

Determine name of network interface

In my case, the network interface is called enp2s0. Yours will likely be very similarly named.

In order to tell Ubuntu that we want our connection to be bridged, we'll need to edit the network interfaces configuration file. Doing this won't negatively impact your connection at all. It'll just allow that connection to be shared with the VMs.

Use code (Visual Studio Code) or your favorite text editor to open the following file:

$ code /etc/network/interfaces     

When you first open this file, it may be empty or contain just a couple of lines. Your bridge interface is called br0, so add the following line for the interface to come up by default:

auto br0    

Below this line, add the following line for your current network interface (the one who's named you determined earlier).

iface enp2s0 inet manual

Next, you can add the bridge information. These lines tell Ubuntu that your bridge will use DHCP for automatic IP address assignment, and your bridge will manage your current interface.

iface br0 inet dhcp
      bridge_ports enp2s0

This is how your file should look once all the changes have been applied (if you also have a couple of lines that were already there, it's fine to have them too):

Network interfaces configuation file

Save your changes and exit the file.

Add your user to the groups

In order to manage your virtual machine(s) without root privileges, your user will need to belong to two user groups. Run the following commands to add your user to the appropriate groups (replacing user1 with the name of your user):

$ sudo adduser user1 libvirt
$ sudo adduser user1 libvirt-qemu
$ sudo adduser user1 kvm

When you're done, you should restart your system to ensure that all of the changes done to your user and network configuration have a chance to take effect.

Banting answered 29/4, 2020 at 10:32 Comment(2)
Downvote: This thread isn't about getting KVM to work. It's just about omitting a "permission denied" message while starting the Emulator packaged with Andoid Studio. No need for libvirt and no need for configuring interfaces...Frig
While I'm very new to Android development, I bet it does most of these steps for you. It does ask you to install many of the same packages, though. (Expecting every Android developer to properly set up a bridge is a bit in left field, even limited to Linux Android developers.)Chloris
G
-2

In order to make a virtual device in Linux - I have to follow this three command and it helps me to avoid trouble for building avd devices - the process are -

sudo apt install qemu-kvm
sudo adduser $USER kvm
sudo chown $USER /dev/kvm 

so, now you are good to go, restart android studio and start building application with emulator.

Glitter answered 13/5, 2020 at 5:47 Comment(1)
Duplicate of James Hooper's answer.Marmoreal
B
-3

Provide appropriate permissions with this command

sudo chmod 777 -R /dev/kvm
Bring answered 14/5, 2020 at 19:14 Comment(2)
See also my comments to a similar answer: #37301311Marmoreal
It worked for meBasseterre
W
-5

Running the below command in Ubuntu 18.04 worked for me sudo chown -R /dev/kvm

Weeny answered 12/6, 2018 at 22:17 Comment(1)
First: Never change ownership of system files --- Second: As /dev/kvm is a file -R has no effect --- Third: chown needs a new owner argument --- Fourth: After rebooting you have to do this again and again.Frig
T
-10

If you open your ide with sudo. You are not going to have this problem.

Telling answered 14/9, 2018 at 15:7 Comment(2)
why on earth I need sudo permissions to develop an android app?Malm
Emphasizing again: one must never do this. Also look into gksu and gksudo for running GUI apps as root.Unchartered

© 2022 - 2024 — McMap. All rights reserved.