To automatically change the display layout of the Xserver when I dock my laptop I need not only detect that this event actually happened but also which monitors are connected to be able to distinguish between multiple docking station setups, i.e. there are multiple docking stations with different connected monitors. How is this possible on Linux (preferable in POSIX shell code)?

1 Answer 1


If you are actually looking for a way to set up your displays automatically when (un)docking I suggest that you are looking at autorandr before starting something alike on your own (unlike me ;).

To detect if a certain display output is connected to a monitor or not there exist good answers in this question. The most intriguing seems to be to look at /sys/class/drm/card0-*-*/status which reads connected or disconnected.

This does not solve the problem stated in this very question though but a very similar approach can be taken since there exist /sys/class/drm/card0-*-*/edid with the (cached) EDID aka DDC information of the respective monitors. If you are somewhat lucky this data does even contain the serial number of the monitor and thus can distinguish even between setups of identical monitor types (you can check with edid-decode /sys/class/drm/.../edid).

So for the purpose of distinguishing sets of attached monitors I am using the following shell function that is heavily based on a function with a similar functionality in autorandr.

# hash_sysfs_edid() simply concatenates the md5 hashes of all connected 
# monitors and hashes them again so that the output is always 32 characters long.
hash_sysfs_edid () {
    for DEVICE in /sys/class/drm/card*-*; do
        [ -e "${DEVICE}/status" ] && grep -q "^connected$" "${DEVICE}/status" || continue
      edid_hash="${edid_hash}"$(md5sum "${DEVICE}/edid" | awk '{print $1}')
  echo $(echo "$edid_hash" | md5sum | awk '{print $1}')

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .