Where are bluetooth link keys stored in Ubuntu 14.10?

I am trying to set specific bluetooth link keys for a device in Ubuntu 14.10 but I am unable to find where they are stored.
The purpose is to have a bluetooth mouse working in both Ubuntu and Windows without having to re-pair it after switching operating systems.
All the answers I found on AskUbuntu state that the keys are stored in /var/lib/bluetooth/<mac_address>/linkkeys and this used to be the case at least until 13.10, but in 14.10 this file is missing from a clean desktop install and creating it manually has no effect, it is ignored by bluetoothd.

EDIT: It seems that the mouse doesn't issue a pairing request and the link keys aren't stored in this case. However, connecting the mouse in Windows and then rebooting in Ubuntu causes the bluetooth connection to fail (and the mouse needs to be removed and re-added to the bluetooth device list), so there's definitely some key exchange/authentication going on that prevents the mouse from being shared in both OS. The question remains, how can I force bluetoothd to save and use the saved link keys when connecting this mouse?

Hardware details:

  • Microsoft Bluetooth Notebook Mouse 5000
  • 413c:8161 Dell Computer Corp. Wireless 365 Bluetooth (Broadcom BCM2046B1)

The two logs below show that for a pairable device (mobile phone) the keys are stored in the linkkeys file; this is not the case for the mouse. I know that older bluetoothd versions used to save the link keys for mice as well, because there are lots of answers/tutorials here on askubuntu and ubuntuforums about copying linkkeys from Windows to Ubuntu for the specific purpose of sharing a mouse between the two OS.

Here is the bluetoothd log for the mouse discovery and connection setup:

    bluetoothd[15615]: plugins/mgmtops.c:mgmt_start_discovery() index 0
    bluetoothd[15615]: Discovery session 0x7feb52b14130 with :1.137 activated
    bluetoothd[15615]: src/adapter.c:session_ref() 0x7feb52b14130: ref=1
    bluetoothd[15615]: plugins/mgmtops.c:mgmt_event() cond 1
    bluetoothd[15615]: plugins/mgmtops.c:mgmt_event() Received 10 bytes from management socket
    bluetoothd[15615]: plugins/mgmtops.c:mgmt_cmd_complete() 
    bluetoothd[15615]: plugins/mgmtops.c:start_discovery_complete() hci0 type 1 status 0
    bluetoothd[15615]: plugins/mgmtops.c:mgmt_event() cond 1
    bluetoothd[15615]: plugins/mgmtops.c:mgmt_event() Received 8 bytes from management socket
    bluetoothd[15615]: plugins/mgmtops.c:mgmt_discovering() Controller 0 type 1 discovering 1
    bluetoothd[15615]: plugins/mgmtops.c:mgmt_event() cond 1
    bluetoothd[15615]: plugins/mgmtops.c:mgmt_event() Received 25 bytes from management socket
    bluetoothd[15615]: plugins/mgmtops.c:mgmt_device_found() hci0 addr DE:AD:BE:EF:00:00, rssi -43 flags 0x0003 eir_len 5
    bluetoothd[15615]: plugins/mgmtops.c:mgmt_confirm_name() hci0 bdaddr DE:AD:BE:EF:00:00 name_known 0
    bluetoothd[15615]: plugins/mgmtops.c:mgmt_event() cond 1
    bluetoothd[15615]: plugins/mgmtops.c:mgmt_event() Received 16 bytes from management socket
    bluetoothd[15615]: plugins/mgmtops.c:mgmt_cmd_complete() 
    bluetoothd[15615]: Unknown command complete for opcode 37
    bluetoothd[15615]: plugins/mgmtops.c:mgmt_event() cond 1
    bluetoothd[15615]: plugins/mgmtops.c:mgmt_event() Received 61 bytes from management socket
    bluetoothd[15615]: plugins/mgmtops.c:mgmt_device_found() hci0 addr DE:AD:BE:EF:00:00, rssi -43 flags 0x0000 eir_len 41
    bluetoothd[15615]: plugins/mgmtops.c:mgmt_event() cond 1
    bluetoothd[15615]: plugins/mgmtops.c:mgmt_event() Received 8 bytes from management socket
    bluetoothd[15615]: plugins/mgmtops.c:mgmt_discovering() Controller 0 type 1 discovering 0
    bluetoothd[15615]: src/adapter.c:adapter_set_discovering() hci0 restarting discovery, disc_sessions 1
    bluetoothd[15615]: plugins/mgmtops.c:mgmt_start_discovery() index 0
    bluetoothd[15615]: plugins/mgmtops.c:mgmt_event() cond 1
    bluetoothd[15615]: plugins/mgmtops.c:mgmt_event() Received 10 bytes from management socket
    bluetoothd[15615]: plugins/mgmtops.c:mgmt_cmd_complete() 
    bluetoothd[15615]: plugins/mgmtops.c:start_discovery_complete() hci0 type 1 status 0
    bluetoothd[15615]: plugins/mgmtops.c:mgmt_event() cond 1
    bluetoothd[15615]: plugins/mgmtops.c:mgmt_event() Received 8 bytes from management socket
    bluetoothd[15615]: plugins/mgmtops.c:mgmt_discovering() Controller 0 type 1 discovering 1
    bluetoothd[15615]: plugins/mgmtops.c:mgmt_event() cond 1
    bluetoothd[15615]: plugins/mgmtops.c:mgmt_event() Received 25 bytes from management socket
    bluetoothd[15615]: plugins/mgmtops.c:mgmt_device_found() hci0 addr DE:AD:BE:EF:00:00, rssi -43 flags 0x0003 eir_len 5
    bluetoothd[15615]: src/adapter.c:session_unref() 0x7feb52b14130: ref=0
    bluetoothd[15615]: src/adapter.c:session_remove() Discovery session 0x7feb52b14130 with :1.137 deactivated
    bluetoothd[15615]: src/adapter.c:session_remove() Stopping discovery
    bluetoothd[15615]: plugins/mgmtops.c:mgmt_stop_discovery() index 0
    bluetoothd[15615]: Stopping discovery
    bluetoothd[15615]: src/adapter.c:create_device() DE:AD:BE:EF:00:00
    bluetoothd[15615]: src/adapter.c:adapter_create_device() DE:AD:BE:EF:00:00
    bluetoothd[15615]: src/device.c:device_create() Creating device /org/bluez/15615/hci0/dev_DE:AD:BE:EF:00:00
    bluetoothd[15615]: src/device.c:btd_device_ref() 0x7feb52b15680: ref=1
    bluetoothd[15615]: src/device.c:device_set_temporary() temporary 1
    bluetoothd[15615]: src/device.c:btd_device_ref() 0x7feb52b15680: ref=2
    bluetoothd[15615]: plugins/mgmtops.c:mgmt_event() cond 1
    bluetoothd[15615]: plugins/mgmtops.c:mgmt_event() Received 10 bytes from management socket
    bluetoothd[15615]: plugins/mgmtops.c:mgmt_cmd_complete() 
    bluetoothd[15615]: plugins/mgmtops.c:mgmt_cmd_complete() stop_discovery complete
    bluetoothd[15615]: plugins/mgmtops.c:mgmt_event() cond 1
    bluetoothd[15615]: plugins/mgmtops.c:mgmt_event() Received 8 bytes from management socket
    bluetoothd[15615]: plugins/mgmtops.c:mgmt_discovering() Controller 0 type 1 discovering 0
    bluetoothd[15615]: plugins/mgmtops.c:mgmt_event() cond 1
    bluetoothd[15615]: plugins/mgmtops.c:mgmt_event() Received 65 bytes from management socket
    bluetoothd[15615]: plugins/mgmtops.c:mgmt_device_connected() hci0 device DE:AD:BE:EF:00:00 connected eir_len 46
    bluetoothd[15615]: src/adapter.c:adapter_get_device() DE:AD:BE:EF:00:00
    bluetoothd[15615]: src/device.c:device_probe_drivers() Probing drivers for DE:AD:BE:EF:00:00
    bluetoothd[15615]: input/manager.c:hid_device_probe() path /org/bluez/15615/hci0/dev_DE:AD:BE:EF:00:00
    bluetoothd[15615]: src/device.c:btd_device_ref() 0x7feb52b15680: ref=3
    bluetoothd[15615]: input/device.c:input_device_new() Registered interface org.bluez.Input on path /org/bluez/15615/hci0/dev_DE:AD:BE:EF:00:00
    bluetoothd[15615]: src/device.c:device_set_temporary() temporary 0
    bluetoothd[15615]: src/device.c:btd_device_unref() 0x7feb52b15680: ref=2

Here is the bluetoothd log for the pairing of a mobile phone (user is asked to validate a code shown on both devices):

    bluetoothd[15615]: plugins/mgmtops.c:mgmt_start_discovery() index 0
    bluetoothd[15615]: Discovery session 0x7feb52b14ca0 with :1.138 activated
    bluetoothd[15615]: src/adapter.c:session_ref() 0x7feb52b14ca0: ref=1
    bluetoothd[15615]: plugins/mgmtops.c:mgmt_event() cond 1
    bluetoothd[15615]: plugins/mgmtops.c:mgmt_event() Received 10 bytes from management socket
    bluetoothd[15615]: plugins/mgmtops.c:mgmt_cmd_complete() 
    bluetoothd[15615]: plugins/mgmtops.c:start_discovery_complete() hci0 type 1 status 0
    bluetoothd[15615]: plugins/mgmtops.c:mgmt_event() cond 1
    bluetoothd[15615]: plugins/mgmtops.c:mgmt_event() Received 8 bytes from management socket
    bluetoothd[15615]: plugins/mgmtops.c:mgmt_discovering() Controller 0 type 1 discovering 1
    bluetoothd[15615]: plugins/mgmtops.c:mgmt_event() cond 1
    bluetoothd[15615]: plugins/mgmtops.c:mgmt_event() Received 64 bytes from management socket
    bluetoothd[15615]: plugins/mgmtops.c:mgmt_device_found() hci0 addr DE:AD:BE:EF:01:01, rssi -52 flags 0x0000 eir_len 44
    bluetoothd[15615]: src/adapter.c:session_unref() 0x7feb52b14ca0: ref=0
    bluetoothd[15615]: src/adapter.c:session_remove() Discovery session 0x7feb52b14ca0 with :1.138 deactivated
    bluetoothd[15615]: src/adapter.c:session_remove() Stopping discovery
    bluetoothd[15615]: plugins/mgmtops.c:mgmt_stop_discovery() index 0
    bluetoothd[15615]: Stopping discovery
    bluetoothd[15615]: src/adapter.c:adapter_create_device() DE:AD:BE:EF:01:01
    bluetoothd[15615]: src/device.c:device_create() Creating device /org/bluez/15615/hci0/dev_DE:AD:BE:EF:01:01
    bluetoothd[15615]: src/device.c:btd_device_ref() 0x7feb52b17cd0: ref=1
    bluetoothd[15615]: src/device.c:device_set_temporary() temporary 1
    bluetoothd[15615]: plugins/mgmtops.c:mgmt_create_bonding() hci0 bdaddr DE:AD:BE:EF:01:01 io_cap 0x01
    bluetoothd[15615]: src/device.c:bonding_request_new() Requesting bonding for DE:AD:BE:EF:01:01
    bluetoothd[15615]: src/device.c:bonding_request_new() Temporary agent registered for DE:AD:BE:EF:01:01 at :1.138:/org/bluez/agent/wizard
    bluetoothd[15615]: plugins/mgmtops.c:mgmt_event() cond 1
    bluetoothd[15615]: plugins/mgmtops.c:mgmt_event() Received 10 bytes from management socket
    bluetoothd[15615]: plugins/mgmtops.c:mgmt_cmd_complete() 
    bluetoothd[15615]: plugins/mgmtops.c:mgmt_cmd_complete() stop_discovery complete
    bluetoothd[15615]: plugins/mgmtops.c:mgmt_event() cond 1
    bluetoothd[15615]: plugins/mgmtops.c:mgmt_event() Received 8 bytes from management socket
    bluetoothd[15615]: plugins/mgmtops.c:mgmt_discovering() Controller 0 type 1 discovering 0
    bluetoothd[15615]: plugins/mgmtops.c:mgmt_event() cond 1
    bluetoothd[15615]: plugins/mgmtops.c:mgmt_event() Received 37 bytes from management socket
    bluetoothd[15615]: plugins/mgmtops.c:mgmt_device_connected() hci0 device DE:AD:BE:EF:01:01 connected eir_len 18
    bluetoothd[15615]: src/adapter.c:adapter_get_device() DE:AD:BE:EF:01:01
    bluetoothd[15615]: plugins/mgmtops.c:mgmt_event() cond 1
    bluetoothd[15615]: plugins/mgmtops.c:mgmt_event() Received 18 bytes from management socket
    bluetoothd[15615]: plugins/mgmtops.c:mgmt_user_confirm_request() hci0 DE:AD:BE:EF:01:01 confirm_hint 0
    bluetoothd[15615]: src/adapter.c:adapter_get_device() DE:AD:BE:EF:01:01
    bluetoothd[15615]: src/device.c:new_auth() Requesting agent authentication for DE:AD:BE:EF:01:01
    bluetoothd[15615]: src/agent.c:agent_request_confirmation() Calling Agent.RequestConfirmation: name=:1.138, path=/org/bluez/agent/wizard, passkey=436733
    bluetoothd[15615]: plugins/mgmtops.c:mgmt_confirm_reply() index 0 addr DE:AD:BE:EF:01:01 success 1
    bluetoothd[15615]: plugins/mgmtops.c:mgmt_event() cond 1
    bluetoothd[15615]: plugins/mgmtops.c:mgmt_event() Received 16 bytes from management socket
    bluetoothd[15615]: plugins/mgmtops.c:mgmt_cmd_complete() 
    bluetoothd[15615]: plugins/mgmtops.c:mgmt_cmd_complete() user_confirm_reply complete
    bluetoothd[15615]: plugins/mgmtops.c:mgmt_event() cond 1
    bluetoothd[15615]: plugins/mgmtops.c:mgmt_event() Received 32 bytes from management socket
    bluetoothd[15615]: plugins/mgmtops.c:mgmt_new_link_key() Controller 0 new key of type 5 pin_len 0
    bluetoothd[15615]: src/adapter.c:adapter_get_device() DE:AD:BE:EF:01:01
    bluetoothd[15615]: src/event.c:btd_event_link_key_notify() storing link key of type 0x05
    bluetoothd[15615]: src/device.c:device_set_bonded() bonded 1
    bluetoothd[15615]: src/device.c:device_set_temporary() temporary 0
    bluetoothd[15615]: src/adapter.c:adapter_get_device() DE:AD:BE:EF:01:01
    bluetoothd[15615]: src/device.c:device_bonding_complete() bonding 0x7feb52b2d270 status 0x00
    bluetoothd[15615]: src/device.c:device_bonding_complete() Proceeding with service discovery
    bluetoothd[15615]: src/device.c:btd_device_ref() 0x7feb52b17cd0: ref=2
    bluetoothd[15615]: src/agent.c:agent_release() Releasing agent :1.138, /org/bluez/agent/wizard
    bluetoothd[15615]: plugins/mgmtops.c:mgmt_event() cond 1
    bluetoothd[15615]: plugins/mgmtops.c:mgmt_event() Received 16 bytes from management socket
    bluetoothd[15615]: plugins/mgmtops.c:mgmt_cmd_complete() 
    bluetoothd[15615]: plugins/mgmtops.c:pair_device_complete() hci0 DE:AD:BE:EF:01:01 pairing complete status 0
    bluetoothd[15615]: src/adapter.c:adapter_get_device() DE:AD:BE:EF:01:01
    bluetoothd[15615]: src/device.c:device_bonding_complete() bonding (nil) status 0x00

Solution 1:

After a while, I finally solved the issue I was facing. I still haven't found an answer to the initial question (I couldn't determine where the link keys are stored for this particular bluez/adapter/peripheral combination) but I did find out that bluez will read the linkkeys file, if present, and will use the keys in the file.

So even though the link keys might not be written to the file after pairing, it is possible to override those keys by manually adding them to the linkkeys file.

It is important that the key type is set correctly, otherwise the key will be ignored.

For reference, the key type that worked in my case is shown here: https://askubuntu.com/a/246791/352576

Solution 2:

Ubuntu 13.10 uses bluez 4.98, 14.04 and 14.10 uses 4.101

Both versions were compiled with the same option (--localstatedir=/var)

I'm using 14.04 and the files are there and if I remove or add a device the /var/lib/bluetooth/(myadapteraddress)/linkkeys file is updated accordingly.

I made a test on a 14.10 system, I added a BT device and the /var/lib/bluetooth/(adapteraddress)/linkkeys file was created

Maybe you are using a newer version of bluez.

Schema for new versions of bluez (5 onwards i think) :

/var/lib/bluetooth/<adapter address>/<remote device address>/info

In the documentation of settings storage for the new bluez version, I found this:

http://git.kernel.org/cgit/bluetooth/bluez.git/tree/doc/settings-storage.txt

...

Storage directory structure

There is one directory per adapter, named by its Bluetooth address, which
contains:
 - a settings file for the local adapter
 - an attributes file containing attributes of supported LE services
 - a cache directory containing:
    - one file per device, named by remote device address, which contains device name
 - one directory per remote device, named by remote device address, which contains:
    - an info file
- an attributes file containing attributes of remote LE services
- a ccc file containing persistent Client Characteristic Configuration
  (CCC) descriptor information for GATT characteristics

So the directory structure is:

/var/lib/bluetooth/<adapter address>/

    ./settings
    ./attributes
    ./cache/
        ./<remote device address>
        ./<remote device address>
        ...
    ./<remote device address>/
        ./info
        ./attributes
        ./ccc
    ./<remote device address>/
        ./info
        ./attributes
    ...

...

Info file format

...

[LinkKey] group contains:

  Key           String      Key in hexadecimal format

  Type          Integer     Type of link key

  PINLength     Integer     Length of PIN

...

In any case seems that bluez should storage the files in /var/lib/bluetooth

If you use this command

bluetoothd --version

you get 4.101?