Automatically reconfigure X11 displays when a monitor is plugged in or unplugged.
A udev rule watches for DRM hotplug events (monitor connected/disconnected)
and runs a script that re-applies the X11 layout with xrandr: every
connected output is enabled (--auto), every disconnected output is turned
off. Handy for laptops and docked machines where the display arrangement
changes frequently.
monitor-hotplug.sh— finds the running X server (DISPLAY/XAUTHORITY), queriesxrandr --verbosefor output states, and enables/disables outputs accordingly. Logs to syslog (logger -t monitor-hotplug.sh).99-monitor-hotplug.rules— udev rule that triggers the script onACTION=="change", SUBSYSTEM=="drm", ENV{HOTPLUG}=="1".
-
Install the script and make it executable:
sudo install -m 755 monitor-hotplug.sh /usr/local/bin/monitor-hotplug.sh
-
Install the udev rule:
sudo cp 99-monitor-hotplug.rules /etc/udev/rules.d/99-monitor-hotplug.rules
-
Reload udev rules:
sudo udevadm control --reload-rules
Plug in or unplug a monitor — the layout updates after a short delay (the
script sleeps 3s to let the EDID settle). Check journalctl -t monitor-hotplug.sh
to see what the script did.
- X11 (the script discovers the X server process to read its auth file; it does not work on pure Wayland sessions)
xrandr