Connecting to dbus over tcp

后端 未结 3 1062
故里飘歌
故里飘歌 2020-12-07 18:05

I wrote a simple python program to play and pause banshee music player. While its working on my own machine, I have trouble doing it to a remote computer, connected to the s

3条回答
  •  余生分开走
    2020-12-07 18:33

    I recently needed to set this up, and discovered that the trick is: order matters for the elements in session.conf. You should make sure the TCP element occurs first. Bizarre, I know, but true, at least for my case. (I see exactly the same black screen behavior if I reverse the order and put the UNIX socket element first.)

    Also, prepending the TCP tag is necessary, but not sufficient. To make remote D-Bus connections via TCP work, you need to do three things:

    1. Add a tag above the UNIX one, similar to this:

      tcp:host=localhost,bind=*,port=55556,family=ipv4
      unix:tmpdir=/tmp
      
    2. Add a line (right below the tags is fine) that says:

      ANONYMOUS
      
    3. Add another line below these that says:

      
      

    The tag should be added in addition to any other tags that may be contained in your session.conf. In summary, your session.conf should contain a snippet that looks like this:

    tcp:host=localhost,bind=*,port=55556,family=ipv4
    unix:tmpdir=/tmp
    
    ANONYMOUS
    
    

    After doing these three things, you should be able to connect to the session bus remotely. Here's how it looks when specifying a remote connection in D-Feet:

    D-Feet screen capture

    Note that, if you want to connect to the system bus, too, you need to make similar changes to /etc/dbus-1/system.conf, but specify a different TCP port, for example 55557. (Oddly enough, the element order appears not to matter in this case.)

    The only weird behavior I've noticed in this configuration is that running Desktop apps with sudo (e.g., sudo gvim) tends to generate errors or fail outright saying "No D-BUS daemon running". But this is something I need to do so rarely that it hardly matters.

    If you want to send to a remote machine using dbus-send, you need to set DBUS_SESSION_BUS_ADDRESS accordingly, e.g., to something like:

    export DBUS_SESSION_BUS_ADDRESS=tcp:host=localhost,bind=*,port=55556,family=ipv4
    

    This works even if the bus you want to send to is actually the system bus of the remote machine, as long as the setting matches the TCP tag in /etc/dbus-1/system.conf on the target. (Thanks to Martin Vidner for this tip. Until I stumbled across his answer to this question, I didn't believe dbus-send supported remote operation.)

    UPDATE: If you're using systemd (and want to access the system bus), you might also need to add a line saying ListenStream=55557 to /lib/systemd/system/dbus.socket, like so:

    [Socket]
    ListenStream=/var/run/dbus/system_bus_socket
    ListenStream=55557  # <-- Add this line
    

    UPDATE2: Thanks to @altagir for pointing out that recent versions of D-Bus will enable AppArmor mediation on systems where it's available, so you may also need to add to session.conf/system.conf for these instructions to work.

提交回复
热议问题