Automatically switch to connected External Display on Boot with XRandR

I bought a notebook to be flexible, but when at home I use it with external monitor, keyboard and mouse to be comfortable. Now the problem was that I couldn't tell Ubuntu I want to only use the external monitor - if it's available.

Well, I've written a one-liner that fixes that problem. If the external display is attached, it will automatically switch over to it and display the internal display. Otherwise it boots normally (leaving the internal display enabled).

I put the script into the Xsession.d directory so it gets started for every user on boot: /etc/X11/Xsession.d/98vgaonly and made it executable.

The actual script line is this:
xrandr -q | grep 'VGA connected' && xrandr --output LVDS --off --output VGA --auto
It checks if the line 'VGA connected' was found in xrandr's query and then asks your X server to turn off the internal output and use the external one with automatic resolution detection.

So to automatically switch to a connected VGA during boot just execute:
echo 'xrandr -q | grep \'VGA connected\' && xrandr --output LVDS --off --output VGA --auto' | sudo tee /etc/X11/Xsession.d/98vgaonly && sudo chmod a+x /etc/X11/Xsession.d/98vgaonly

You may have to change the name of the outputs for your setup. xrandr -q will let you know what's available. It would probably be possible to somehow call this script automatically when plugging in a monitor, but I've not figured out how. xrandr only works when called from inside the running X session in my experience.

If you want to know more about XRandR, check this recent article.

Any comments are welcome!


  1. Thanks! You just saved me a few grey hairs! I've been trying to tinker around with a netbook, and this script is perfect for what I'm doing. Many thanks to you!

    -- JLangbridge

  2. You're quite welcome! That's exactly what I had written this for.

  3. it is good that it is possible. but how about on hdmi output? I have a g60-120us. i hope you coould post a script for it. i am a noob.

  4. Just replace "VGA" above in the script with what it says in the output of 'xrandr -q' e.g. "HDMI".

  5. Nice! I just installed a 1920x1080 external VGA to use with my little 13" and this really helps sort out some issues.

    Is there another place to save this script in order to run it before the GUI login system appears? It would be nice if the login screen ran at normal, non-weirdly-stretched resolution.

  6. @Hunter:
    Try to call the script from /etc/X11/xinit/xserverrc

    That should do the trick. But no warraty! ;)

  7. Hmm.. I think that might not work. Try putting it into your display managers init files, e.g.

  8. Thank you, I added this script to my startup and now I don't have to do anything to fix my monitors.

  9. You're welcome. Always great to hear I could help. ;)

  10. I don't need to detect the external monitor at boot. Instead in my work I have to move around a lot and my laptop has a small 12.1" monitor. So I modified a little the script and added a custom keyboard shortcut in GNOME to change monitors with a single key (F12 in my case).

    This is the script code:

    xrandr -q | grep 'VGA1 connected' && xrandr --output LVDS1 --off --output VGA1 --auto
    xrandr -q | grep 'VGA1 disconnected' && xrandr --output VGA1 --off --output LVDS1 --auto

    (note: my laptop detected the laptop display as LVSD1 and the external monitor as VGA1)

    It detects if the external monitor is connected, if so it turns off the laptop screen.

    The problem was that is I disconnected the external monitor, the laptop monitor stayed off and I couldn't see a thing. For that reason the second command detects if the external monitor is disconnected and turns on the laptop display.

    I'm using Ubuntu 10.04 and it works great.

  11. Mau, this works like a charm. I am using a Samsung Syncmaster 2443W and a rather old Samsung X20 laptop, running Ubuntu 10.04. Thanks a lot. Cheers, Martin

  12. Does this work for s-video ports? I have one of those as well that I use on tv's that don't have a VGA port.

  13. It works with any output supported by xrandr.


I appreciate comments. Feel free to write anything you wish. Selected comments and questions will be published.