Ubuntu – How to use the same Bluetooth keyboard and mouse with both macOS and Ubuntu

bluetoothdual-bootmacmagic-mousewireless-keyboards

I have an iMac with both macOS and Ubuntu on it. I want to be able to use my Bluetooth mouse and keyboard with both operating systems without re-pairing every time I switch OS.

Has anyone had this problem and figured out a solution?

Best Answer

The answers to this question give you the general idea, though a bit vague on the macOS side, so I'll spell out the procedure I used.

OS versions as follows. The file names and locations appear to be subject to change according to versions, so this is what worked for me.

  • MacOS High Sierra 10.13.1
  • Ubuntu 17.04

I am assuming that you have a filesystem (USB key or whatever) that is at least read-write in MacOS and readable in Ubuntu.

The general idea is that when you pair a device to an OS, a unique link key is generated, which is necessary for authentication next time the pairing is active. You can transfer these link keys from MacOS pairings to Ubuntu pairings such that the pairings work in both OSes.

  1. Boot Ubuntu
  2. Pair device(s) as usual
  3. Reboot into MacOS
  4. Pair device(s) as usual
  5. In a terminal window type: sudo defaults read com.apple.bluetoothd.plist LinkKeys. This will give you output something like:
{
    "a0-99-9b-16-43-d2" =     {
        "00-1f-20-47-e5-22" = <4d6b002f 37584c09 ee219365 b78ba03e>;
        "04-0c-ce-3d-15-4d" = <fe998c62 4bb29a7c 40b2e670 10db71ed>;
    };
}

Here a0-99-9b-16-43-d2 is the MAC address of the bluetooth adapter on my system.

Following that are the MAC addresses of devices that I have paired (00-1f-20-47-e5-22 and 04-0c-ce-3d-15-4d and the link keys associated with those pairings (4d6b002f 37584c09 ee219365 b78ba03e and fe998c62 4bb29a7c 40b2e670 10db71ed respectively).

  1. Redirect this output to a file visible to both MacOS and Ubuntu:
sudo defaults read com.apple.bluetoothd.plist LinkKeys > /Volumes/4T/linkkeys.txt
  1. Switch off all devices that you have paired
  2. Reboot into Ubuntu. Do not turn on your bluetooth devices yet. Hopefully you have a wired keyboard and mouse to use at this point
  3. Open a terminal window
  4. Stop the bluetooth service:
sudo service bluetooth stop
  1. Look in the directory /var/lib/bluetooth. You should see a subdirectory entry for the bluetooth adapter MAC address, formatted like A0:99:9B:16:43:D2. Inside that you should see subdirectories for each device MAC address, like 00:1F:20:47:E5:22 and 04:0C:CE:3D:15:4D. Inside those directories you will find files named info. Edit these files with your favourite editor:
sudo gedit /var/lib/bluetooth/A0:99:9B:16:43:D2/00:1F:20:47:E5:22/info
sudo gedit /var/lib/bluetooth/A0:99:9B:16:43:D2/04:0C:CE:3D:15:4D/info
  1. In these files you'll find a Key entry. This key needs to be made the same as what you found in MacOS, but with some important formatting differences:
    • spaces removed
    • hex digits in uppercase
    • byte-swapped 128-bit integer (i.e. split into 2 hex digit chunks and reversed)

You can achieve this as follows:

$ echo 4d6b002f 37584c09 ee219365 b78ba03e | sed 's/ //g;s/../\U&\n/g' | tac | tr -d '\n' ; echo
3EA08BB7659321EE094C58372F006B4D
$ echo fe998c62 4bb29a7c 40b2e670 10db71ed | sed 's/ //g;s/../\U&\n/g' | tac | tr -d '\n' ; echo
ED71DB1070E6B2407C9AB24B628C99FE
$ 

Edit these reformatted keys into the Key entry of the appropriate info files. Most likely sudo will be required.

  1. Re-start the bluetooth service:
sudo service bluetooth start
  1. Now turn on your bluetooth devices. They should be recognised and the pairings active/valid in both MacOS and Ubuntu.

Update: The pairings seem to have persisted through upgrades on both MacOS and Ubuntu. I'm now running 10.13.6 and 18.04 respectively and have not yet had to redo this procedure.

Related Question