Skip to content

Instantly share code, notes, and snippets.

@madkoding
Last active April 8, 2024 14:07
Show Gist options
  • Save madkoding/f3cfd3742546d5c99131fd19ca267fd4 to your computer and use it in GitHub Desktop.
Save madkoding/f3cfd3742546d5c99131fd19ca267fd4 to your computer and use it in GitHub Desktop.
Pairing bluetooth devices in dual boot with Linux Ubuntu and Windows 10/11

Pairing Bluetooth Devices in Dual Boot with Linux Ubuntu and Windows 10/11

Introduction

This guide provides updated instructions for pairing Bluetooth devices (such as keyboards or mice) in a dual-boot environment with Linux Ubuntu and Windows 10/11, incorporating community feedback and suggestions.

Instructions

1. Pair in Linux First

  • Pair your Bluetooth device in Linux. This is crucial to ensure the LinkKey remains consistent.
  • Note: Do not re-pair the device in Linux after completing the pairing in Windows.

2. Pair in Windows

  • Pair the Bluetooth device in Windows. Note the MAC address of the device for later steps.

3. Install chntpw in Linux

  • Install the chntpw package to read Windows registry keys:
    sudo apt-get install chntpw

4. Access Windows System Drive in Linux

  • Mount your Windows system drive and navigate to the System32 config folder:
    cd /[MountedDrive]/Windows/System32/config

5. Use chntpw to Access Registry

  • Execute the following command in the config folder:
    chntpw -e SYSTEM

6. Navigate to Bluetooth Registry Keys

  • In the chntpw console, navigate to the Bluetooth registry keys:
    cd \ControlSet001\Services\BTHPORT\Parameters\Keys

7. Find and Copy the Pairing Key

  • Use ls to list unique IDs and find your device's MAC address.
  • Retrieve the pairing key (hex code) associated with your device.

8. Edit Linux Bluetooth File

  • Edit the corresponding file in your Linux drive:
    sudo nano /var/lib/bluetooth/[Unique ID]/[Mac Address]/info
  • Replace the Key value in the [LinkKey] section with the pairing key from Windows.
  • If the [LinkKey] section is missing, add it manually.

9. Restart Bluetooth Service in Linux

  • Save the changes and restart the Bluetooth service:
    sudo service bluetooth restart

Additional Methods and Tips

  • Simplification with reged: Use reged to export Bluetooth pairing keys directly into a file for easier identification and copying.
  • Bluetooth LE Devices: For Bluetooth LE devices, the data storage might differ. Users should research specific steps for these devices.
  • Windows 11 Compatibility: This method is also compatible with Windows 11.
  • Multiple Bluetooth Receivers: If you have multiple Bluetooth receivers, ensure you identify and use the correct pairing key.
  • Changing Bluetooth MAC Address in Linux: If necessary, you can change the Bluetooth MAC address in Linux using the following commands:
    sudo hciconfig hci0 down
    sudo bluemoon -A
    sudo hciconfig hci0 up
    sudo systemctl restart bluetooth.service
  • Adding Missing [LinkKey] Section: If the [LinkKey] section is missing in the info file, you should add it manually.

Acknowledgements

Special thanks to the community members who provided valuable insights and suggestions, including nnnnicholas, kna0085, lguangyu, KeyofBlueS, bjoern-vh, Nielius, IgorRodriguez, princeofguilty, and others.

@madkoding
Copy link
Author

madkoding commented Jul 13, 2021

@bjoern-vh meaby the devices are in another control set. search for ControlSet002 or CurrentControlSet.

If not works, try this in wintendo:

  • Run regedit
  • Navigate to HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\BTHPORT\Parameters\Devices
  • Delete each subkey here (there's one for each Bluetooth device)
  • Reboot - you'll need to re-pair any bluetooth devices

then try again

@bjoern-vh
Copy link

Sorry it's taken me so long to get back to you.

I have reinstalled both systems and still had the same error message when running "hex".

After some research on the internet, I came across the following post: https://unix.stackexchange.com/a/413831

I think I'm using Bluetooth LE (Low Energy) and the data is stored differently. Now everything works in any case :-)

@vatsdimri
Copy link

Thanks. It worked.

@S-trace
Copy link

S-trace commented Jan 20, 2022

This also works for Xbox Wireless Controller and headsets. Thank you for your guide.
Usually, MAC address increments by one after each pairing on multi-pairing mouses and keyboards, and stays intact for single-pairing devices.

You may need to move /var/lib/bluetooth/AD:AP:TE:RM:AC:00/cache/DE:VI:CE:MA:C0:00 file from old device mac to new device mac if you get following error:

src/device.c:read_device_records() Unable to load key file from /var/lib/bluetooth/AD:AP:TE:RM:AC:00/cache/DE:VI:CE:MA:C0:00: (No such file or directory)

@cfontes
Copy link

cfontes commented Jan 28, 2022

Works like a charm! Thanks for this!

@dwickycandralesmana
Copy link

Thanks, it worked!

@Nielius
Copy link

Nielius commented Mar 27, 2022

I'm using Ubuntu 21.10, and I had to add the [LinkKey] section (it wasn't there before): I added

[LinkKey]
Key=7E22FCFB21A3C0918B765A3E05B31234
Type=4
PINLength=0

@danielw-mt
Copy link

Thank you! This is great. Works on Fedora 36

@Julka3561
Copy link

Thank you very much!!! You saved my day!

@amxxrsuhail
Copy link

instead of step 10 and 11, u can just the open files in ubuntu and search /var/lib/bluetooth find the info txt file and edit the key. Then do the step 12

@madkoding
Copy link
Author

instead of step 10 and 11, u can just the open files in ubuntu and search /var/lib/bluetooth find the info txt file and edit the key. Then do the step 12

Ok, I'll check that to add to gist. Thanks

Also sometimes win11 don't shows the pairing keys, gonna add that lines too

@OlliKantola
Copy link

Great guide! Worked perfectly and now my headphones are pairing automatically. Tested on two different computers, first one is running WIN10 + Ubuntu 18.04 and second one is running WIN10 + Ubuntu 20.04.

@zampierilucas
Copy link

Is there a way to use this method with the official xbox dongle?

@KeyofBlueS
Copy link

Hi 😄

If you do:

sudo reged -x "<WINDOWS_MOUNTPOINT>/Windows/System32/config/SYSTEM" "HKEY_LOCAL_MACHINE\SYSTEM" "\ControlSet001\Services\BTHPORT\Parameters\Keys" "/tmp/bt_keys.reg"

reged (it's part of chntpw) will export the bluetooth pairing keys in a file that you can simply cat:

cat "/tmp/bt_keys.reg"

and that will give:

Windows Registry Editor Version 5.00

[HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Services\BTHPORT\Parameters\Keys]

[HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Services\BTHPORT\Parameters\Keys\00f48d9e41aa]
"34885dd82480"=hex:BE,7F,B1,99,23,29,D5,B2,6A,E2,F6,96,2E,FD,16,8A

00f48d9e41aa is the bluetooth controller MAC (what you call Unique id)
34885dd82480 is the bluetooth device MAC
BE,7F,B1,99,23,29,D5,B2,6A,E2,F6,96,2E,FD,16,8A is the pairing key

With this method you can skip most of the steps here. Just copy the key and paste it (without commas obviously) in the relative info file, in this case is:
/var/lib/bluetooth/00:F4:8D:9E:41:AA/34:88:5D:D8:24:80/info

Moreover, with having that exported reg file, you can automate the whole process with a simple script.
Take a look here https://github.com/KeyofBlueS/bt-keys-sync

Regards 👍

@IgorRodriguez
Copy link

@KeyofBlueS , your comment was really useful. I followed the original instructions unsuccessfully as I have 2 different bluetooth receivers (only one in use) and for some reason I was getting an incorrect pairing key. With the registry file itself I was able to identify both pairing keys and use the right one in the Linux config. Thank you very much!

@KeyofBlueS
Copy link

@IgorRodriguez It’s a pleasure

@robot92000
Copy link

@Nielius I tried this on Linux Mint 21.1 (Equivalent to Ubuntu 22.04) and this did not work. My Link Key came from Windows 11. I don't have an option for the [LinkKey] entry on my 'info' file either. Any work arounds?

@princeofguilty
Copy link

princeofguilty commented Apr 15, 2023

an easy solution for this, change bluetooth mac address on linux

sudo hciconfig hci0 down
sudo bluemoon -A
sudo hciconfig hci0 up
sudo systemctl restart bluetooth.service

now you have a new identity on linux. pair with your device, note you have to do this on every restart.
now when you return to windows you won't need to pair again.

@yegormk
Copy link

yegormk commented May 14, 2023

@Nielius I tried this on Linux Mint 21.1 (Equivalent to Ubuntu 22.04) and this did not work. My Link Key came from Windows 11. I don't have an option for the [LinkKey] entry on my 'info' file either. Any work arounds?

I have Windows 11 - Ubuntu 22.04 Dual Boot System.
This instructions didn`t work for me for the first few times. But I reset my Bluetooth device, repeated the steps and it worked.
Also try to modify key for your bluetooth device using this steps:

  • Enter sudo mode using this command: sudo su -
  • Go to /var/lib/bluetooth/ using this command: cd /var/lib/bluetooth/
  • Chose your bluetooth module using unique id: cd [Unique ID]
  • Look through paired bluetooth devices(using "ls" command) and chose one that you need using this command: cd [Mac Address]
  • Enter "vim info" and hit enter
  • Modify your bluetooth device key using vim(take one from Windows as explained up above and switch with one that already here)
  • Save and close Vim
  • Not exiting sudo mode enter this command: sudo service bluetooth restart

Screenshot with most of commands attached below.
image

@edwinclement08
Copy link

Thanks so much guys, this helped a lot

@alllexx88
Copy link

@bjoern-vh
I experienced something similar with 2 different Logitech keyboards:

Node has 0 subkeys and 11 values
  size     type              value name             [value if type DWORD]
    16  3 REG_BINARY         <LTK>
     4  4 REG_DWORD          <KeyLength>               16 [0x10]
     8  b REG_QWORD          <ERand>
     4  4 REG_DWORD          <EDIV>                 51860 [0xca94]
    16  3 REG_BINARY         <IRK>
     8  b REG_QWORD          <Address>
     4  4 REG_DWORD          <AddressType>              1 [0x1]
    16  3 REG_BINARY         <CSRK>
     4  4 REG_DWORD          <OutboundSignCounter>      0 [0x0]
     4  4 REG_DWORD          <MasterIRKStatus>          1 [0x1]
     4  4 REG_DWORD          <AuthReq>                 45 [0x2d]

I don't understand the problem and appreciate any support :-)

I did find a solution. Looks like each time you pair a keyboard like that, there are two differences when compared to @madkoding instructions:

  1. Each time the keyboard gets paired, it gets a new MAC address (don't know whether it's a simple increment or a limited pool of addresses)
  2. You need to migrate at least two keys (you have three: LTK, IRK and CSRK, but mine doesn't have the latter)

What I did was:

  1. Pair your device in Linux, reboot to Windows
  2. Pair it in Windows, reboot to Linux
  3. Find the device entry using chntpw like you already did and save the keys in HEX format: hex LTK, hex IRK and hex CSRK (and remove the separating spaces between the bytes)
  4. Change saved Linux bluetooth device MAC address to match Windows one (in my case the difference was in the final hex digit): just rename /var/lib/bluetooth/<bt adapter mac>/<device mac>, in the quote above it should be changed to D8:37:79:92:51:BE.
  5. Change the keys in /var/lib/bluetooth/<bt adapter mac>/<new device mac>/info. Just compare Windows registry keys abbreviation to keys names from info, for me it was:
IRK:
[IdentityResolvingKey]
Key=...

LTK:
[PeripheralLongTermKey]
Key=...

And, it should then work after a reboot, or sudo service bluetooth restart. In my case, on Kubuntu 23.04, the system was glitching and crashing a lot after the bluetooth service restart, not sure what happened, but a reboot cured it.

@Tuhinpaul5
Copy link

Tuhinpaul5 commented Dec 10, 2023

I have a Fastrack FZ100 Bluetooth earbuds and dual booting to ubuntu completely messed up my Bluetooth for both windows and ubuntu. The Bluetooth takes longer to find the earbuds on windows and when I press connect it doesn't connect sometimes and sometimes it does. Even after connecting it keeps connecting and disconnecting. This only started happening after dualbooting. (Sometimes it says connected but the earbud still is in pairing mode)

@madkoding
Copy link
Author

Updated instructions... i'm no longer testing it, but i'll still receive the recommendations from other users to update this gist

@pnka
Copy link

pnka commented Dec 24, 2023

I successfully copied Windows 11 key to Debian 12. Thanks for the guide.

@b33ev
Copy link

b33ev commented Jan 14, 2024

I'm on Windows 11 and Computer\HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Services\BTHPORT\Parameters\Keys is empty. But I have a paired mouse in W11. Any idea where the key is stored?

@b33ev
Copy link

b33ev commented Jan 14, 2024

I'm on Windows 11 and Computer\HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Services\BTHPORT\Parameters\Keys is empty. But I have a paired mouse in W11. Any idea where the key is stored?

This is the solution: https://superuser.com/questions/1677881/bluetooth-pairing-on-dual-boot-of-windows-11-linux-or-some-os-else

@ForestEco
Copy link

I just successfully paired the keyboard this morning, then a sleep mode occurred when I returned from work and the keyboard wont pair with Ubuntu anymore, although I double checked the linking key harmonization is correct with Windows. Any idea why this might have happened?

@madkoding
Copy link
Author

@ForestEco Sleep mode usually have trouble with some bluetooth-wifi. Check your bluetooth model. If your notebook not fully support, check for experimental drivers or replace the wifi-bluetooth module with some other compatible like Intel AX

@bipox
Copy link

bipox commented Feb 28, 2024

It worked, thank you!
In case someone runs into the same issues as me:

Note the MAC address of the device for later steps.

I couldn't find a tutorial for Windows 11.
You can find the mac address under:
device manager -> bluetooth -> right click the desired device -> properties -> details -> in the list scroll to "bluetooth device address"

Use chntpw to Access Registry

If chntpw SYSTEM is supposed to open a new console then it didn't happen for me. But the command shared by @KeyofBlueS works (remove the commas!)

@chiragdhunna
Copy link

Am I the only one for whom this doesn't work.
PS : Followed all the steps.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment