Switch Between Dual/Single Monitor on (Ubuntu) Linux

xorg logo ubuntuRecently I wrote a howto on dual monitor configuration, which works great for my setup. However one thing that is not that great is switching between two modes: dual and single monitor. At work I have an external monitor that I use (which means I use two monitors – my laptop’s and external one), but whenever I am not at work I only need to use my laptop’s. Since all the xorg configuration resides in xorg.conf file, and this file is a regular static text file that is used by X (window system – gdm, kdm, etc.) when it starts, it is nontrivial to change this configuration while running X without some X tools. Unfortunately, Ubuntu is not that fancy (yet) and does not provide these tools by default, so here is a way to do it (sort of) manually.

What we can do is to create two xorg.conf files – "xorg.conf.single" and "xorg.conf.dual". In "xorg.conf.single" just comment out the following line from ServerLayout section:

# /etc/X11/xorg.conf (xorg X Window System server configuration file)
#
.....
Section "ServerLayout"
        Identifier      "Default Layout"
        Screen          0 "0 Screen"
        #Screen         1 "1 Screen" Above "0 Screen"   <-- comment out this line
        Option          "Xinerama" "on"
        Option          "Clone" "off"
.....
EndSection

Here is the listing of "xorg" files that I have:

user@host:/etc/X11$ ll xorg.conf*
-rw-r--r-- 1 root root 4457 2007-06-02 15:05 xorg.conf
-rw-r--r-- 1 root root 4456 2007-05-22 22:03 xorg.conf.dual
-rw-r--r-- 1 root root 4457 2007-05-22 22:04 xorg.conf.single

Now the idea is simple - before X (I use Gnome Desktop, but it can be any desktop environment) starts, we need to copy xorg file that we need (dual or single) to "xorg.conf", which will be picked and loaded by X.

In /home/user/ directory we have a .bashrc file that is loaded whenever the user logs in (if we use bash shell, which is a most popular shell anyway). Therefore we can leverage this file to define aliases that we would like to use once we login. Since alias can be anything we'd like, why not make a dual/single commands as aliases? Here is an example:

user@host:/etc/X11$ tail -5 /home/user/.bashrc
# restart gdm with dual monitor support
alias xdual='sudo cp /etc/X11/xorg.conf.dual /etc/X11/xorg.conf; sudo /etc/init.d/gdm restart'

# restart gdm with single monitor support
alias xsingle='sudo cp /etc/X11/xorg.conf.single /etc/X11/xorg.conf; sudo /etc/init.d/gdm restart'

Now when we need to switch between dual/single monitor, we can fire up shell (by pressing Ctrl+Alt+1, or gnome-terminal, or kterm, etc.) and type xsingle or xdual whichever we need.

16 comments

  1. Thanks

    It makes sense, I wonder why I did not think about doing it this way myself :)

  2. great! I was actually doing the same things but manually running commands.

    aliases ruzeeezzz! :)

  3. You can also write a shell script (or use an alias) to (un)comment out “Screen” line in xorg.conf using SED or AWK.

    This way you will only have one xorg.conf file

  4. Good call! I actually opted for shell scripts myself since I use my .bashrc on several machines. Now if only the X devs would get around to not requiring server restarts…

  5. i’ve been using my laptop at work with two monitors and and home just with one. what i did was very similar to what you did, in a sense. i had two x.org “master” files, and what you have as aliases there i used as two scripts, and they were linked into /etc/rc2.d/S##onemonitor and /etc/rc3.d/S##twomonitors. Then I added another menu item to grub menu, and it forced runlevel 3 (not used in debian, and i believe ubuntu too). so i could decide how many monitors i want to have right at the boot time

  6. This is what I have been thinking about doing manually. Thanks for posting this simplified method. However, before I set this up I am wondering about gnome settings. The two monitors I need to switch between are very different sizes. So I need to respace out my applets and icons and change the font sizes… maybe some other tweaks I could think of. Is there any simple way to also switch the knome layout? The only idea I had was have two different desktop users with differing layouts. However, that would seem to bring up a whole other host of security complications where I would have to make sure the two users were equivalent – in order to keep the desktop similarly functional. Any other ideas? Anybody else thinking similarly?

  7. whoops… I meant to say gnome. Should always proofread!

  8. I don’t know why, but this is not working properly for me. gdm will shut down but it doesn’t restart. And sometimes it becomes 2 instances. Strange. I tried doing it with ‘screen’ but that didn’t help.

  9. Or….
    you could just hit ctrl-alt-keypad minus or ctrl-alt-keypad plus if you did your xorg configuring correctly.

  10. Not sure what all the fuss is about – I use Gnewsense (kind of a stripped down Ubunto) with a Toshiba Satellite. It works fine running alone. When I connect my external monitor and boot as a desktop (with wireless mouse and keyboard), the ext monitor displays well & everything normal (it auto adjusts itself) and my laptop LCD is inactive (off). I wouldn’t want my laptop LCD to be on anyways. So it works perfectly with the default xorg.conf & no tweaking. I used to use Ubunto and the same applied as default.

  11. i want to have to different desktops with different icons say one for office and other for home .I also want that i should be able to switch between the two at a click.

  12. I am running Fluxbox as my desktop and having trouble trying to setup and easy way to switch between dual and single monitor setups. In Windows i can do this easily with a program called Ultramon. I have it setup so i use Ctrl-Alt-F1 and Ctrl-Alt-F2 to switch between single and dual. I tried to follow this tutorial but was unsuccessful;

  13. hi – ive had a rotten time just accepting bifold awning continued on my c700 with the intel chipset to plan at all. can you column your xorg.conf that works for your bifold screen. thx

  14. Unfortunately Linux sucks when it comes to dual-monitor configurations… Sad but true

  15. One can always try xrandr
    There are a couple of GUI\’s for it. arandr is one of them that saves your configs into scripts which one can run (with hotkeys, if one wishes). I made a script for myself (for ATI card with fglrx driver), which can be run with other drivers too, but one should find proper names for video outputs, by running xrandr in a x-terminal)

    #!/bin/bash
    #Usage:
    # \”autodisplay [mono|dual]\”
    #Mono for one monitor
    #Dual for two monitors
    #Without parameters it checks for external monitor and if found, sets up two monitors

    DPI=\”–dpi 96\”
    MODE=\”–mode 1152×864\”

    _mono () {
    xrandr –output CRT1 –off $DPI
    }

    _dual () {
    xrandr –output CRT1 $MODE –rate 75 –right-of DFP1 $DPI
    }

    if [ \"$1\" == \"mono\" ]; then
    _mono
    fi

    if [ \"$1\" == \"dual\" ]; then
    _dual
    fi

    if [ -z \"$1\" ] ; then
    if [ \"`xrandr|grep CRT|grep disco|wc -l`\" == 0 ];
    then _dual
    else _mono
    fi
    fi

tell me something...
  1. (required)
  2. (valid email - optional)
  3. Captcha
  4. (required)