For the most part, Ubuntu works pretty much “out of the box” — run the installation program, boot into Ubuntu, and you're up and running. From what I can understand, computers with multiple video cards and monitors will also work without any tweaking required. Therefore, chances are you won't need to read this article — Ubuntu/X will have done all of the work for you.
One piece that isn't so easy, however, is getting a laptop to work with multiple monitors, since there is only one video card, but two displays (the laptop's LCD screen, and the external monitor). X doesn't seem to be able to configure that out of the box. Even worse, since it's a laptop, chances are that there will be times that you will have two monitors, and other times when you don't, and it's just the laptop's built-in screen.
If you're trying to get Ubuntu working on a laptop, and want to be able to get an external monitor working — but only when you have it plugged in — then this article is for you.
TODO: See if this link helps.
This page requires you to edit the xorg.conf file. If you're not sure about doing that, see the X Window Configuration page.
If all you needed to do was get multiple monitors working, it would be much easier to do so. Unfortunately, if you're using a laptop, you're more than likely going to have two configurations to worry about: one when the external monitor is plugged in, and one when it isn't, and you just have the laptop's built-in screen. This means that you're going to have to prepare two xorg.conf files, for both situations. This is unfortunate, because it means that any other changes you want to make to your configuration — for example, if you want to configure your mouse — will have to be made in both files.
You will also need to create a startup script, which will figure out which configuration file to load when Ubuntu starts. (As we'll see later, X always loads the file named /etc/X11/xorg.conf, so what we'll be doing is creating two files, with different names, and copying the appropriate file to that location at startup, overwriting the file that was there before.)
Before you Begin
If you've just installed Ubuntu, you will have noticed that it's working on your laptop's monitor, but not on the external monitor. (If you press the appropriate buttons on the laptop, you might be able to switch between the two, but it will always be one or the other.) The first thing you should do is take a backup of your existing xorg.conf file, and name it xorg.justlaptop.prod1. This will become the configuration file you'll be using when you boot up with just the laptop's screen. You shouldn't have to make any further changes to it; X did all the work for you.
From this point on, the rest of this article will be describing the changes you need to make to xorg.conf to get it working with both of your monitors, so make sure that you have your external monitor plugged into the laptop.
Editing the File
If you haven't already, you should read the X Window Configuration article, on how to edit the xorg.conf file — especially the sections on making a backup copy!
There are various pieces of xorg.conf having to do with display. Here are the pieces you will need to edit in yours — each of which will be described in more detail below:
- One Device for every video card — or virtual video card — that the system has
- One Monitor for every physical monitor the system has
- One Screen for every Monitor
- A ServerLayout section
- There are also some ServerFlags you need.
The first thing you'll have to do is set up a Device section for your video card(s). This was the tricky part, for a laptop, because you actually have to create two device sections, one for each head on the video card.
Generically, a Device section for a video card should look like this:
Section "Device" Identifier "NAME YOU'RE GIVING THE CARD" Driver "DRIVER" BusID "BUS LOCATION" Option "OPTION" "OPTION VALUE" Screen NUMBER OF THE MONITOR, STARTING AT 0 EndSection
If you install Ubuntu on a laptop, you will probably just have one Device created for your video card, even though it has two “heads.” Also, it won't have all of the options you need, for running multiple monitors. So you'll have to fill it out, and create a second section.
Here's what it looks like on my laptop, which is a Dell Latitude D610:
Section "Device" Identifier "Intel Graphics Controller" Driver "i810" BusID "PCI:0:2:0" Option "VBERestore" "true" Option "DRI" "true" Option "SWCursor" "true" Option "MonitorLayout" "CRT,LFP" Option "CloneRefresh" "85" Screen 0 EndSection Section "Device" Identifier "Intel Graphics Controller 2" Driver "i810" BusID "PCI:0:2:0" Option "Display" "CRT" Option "MonitorLayout" "CRT,LFP" Screen 1 EndSection
The first one is the head for the video card which will run the laptop's LCD screen, and the second is for the external monitor. The key points for this section are:
- The Identifier must be unique for each “head”
- You'll need to make sure you have the right driver for your card; whichever one was filled in for you was probably the right one, so use it when you create your second Device section.
- You'll notice, in my example, that the BusID is actually the same for both entries
Most of the other entries I got from examples on the internet; I don't understand it all.
Now that you have your two Device sections, you need two Monitor sections. Again, the xorg.conf file that was created for you probably only has one of these sections, so you'll have to create the second one from scratch. Luckily, the Monitor section is normally pretty easy to fill in. It looks like this:
Section "Monitor" Identifier "UNIQUE NAME" Option "OPTION" "VALUE" EndSection
On my computer, it looks like this:
Section "Monitor" Identifier "Generic Monitor" Option "DPMS" EndSection Section "Monitor" Identifier "External Monitor" Option "DPMS" EndSection
Again, the first section is for the laptop's LCD screen, and the second for the external CRT.
Some notes on this section:
- There are actually a lot of options you can specify for a monitor, for things like refresh rate and size and a billion other things. I don't normally specify any of them, because X can figure most of that out on its own. (For example, if you get the refresh rate wrong, your monitor can flicker badly, which hurts your eyes; but if you don't specify anything for the refresh rate, X seems to be able to figure out the best refresh rate to use, for your monitor.)
- I don't know what the DPMS option is, but it works, and the examples all have it, so I included it…
For each “Monitor” you've defined, you also need to define a “Screen,” using the Screen section. The Monitor section describes the hardware, the Screen section outlines how you want the display to work.
The section looks like this:
Section "Screen" Identifier "UNIQUE NAME" Device "NAME OF VIDEO CARD DEVICE" Monitor "NAME OF MONITOR" DefaultDepth DEFAULT DEPTH NUMBER SubSection "Display" Depth DEPTH NUMBER Modes "SCREEN RESOLUTION", "LOWER SCREEN RESOLUTION", "ETC." EndSubSection EndSection
Just as with the previous two sections, your initial xorg.conf will only have one of these sections, so you'll have to create the second one from scratch. On my machine, it looks like this:
Section "Screen" Identifier "Default Screen" Device "Intel Graphics Controller" Monitor "Generic Monitor" DefaultDepth 24 SubSection "Display" Depth 1 Modes "1024x768" EndSubSection SubSection "Display" Depth 4 Modes "1024x768" EndSubSection SubSection "Display" Depth 8 Modes "1024x768" EndSubSection SubSection "Display" Depth 15 Modes "1024x768" EndSubSection SubSection "Display" Depth 16 Modes "1024x768" EndSubSection SubSection "Display" Depth 24 Modes "1024x768" EndSubSection EndSection Section "Screen" Identifier "External Screen" Device "Intel Graphics Controller 2" Monitor "External Monitor" DefaultDepth 24 SubSection "Display" Depth 1 Modes "1280x1024" "1024x768" EndSubSection SubSection "Display" Depth 4 Modes "1280x1024" "1024x768" EndSubSection SubSection "Display" Depth 8 Modes "1280x1024" "1024x768" EndSubSection SubSection "Display" Depth 15 Modes "1280x1024" "1024x768" EndSubSection SubSection "Display" Depth 16 Modes "1280x1024" "1024x768" EndSubSection SubSection "Display" Depth 24 Modes "1280x1024" "1024x768" EndSubSection EndSection
As with the two previous sections, I've put the laptop screen first, and the external one second.
Some notes on this section:
- Note that the Device and Monitor settings are referring back to the names you defined earlier.
- You can include as many Display subsections as you want. For the files generated automatically by tools, I always get 6 entries, as above, that are all identical. A lot of examples I've seen on the net simply have one, with the number 24.
- On a related note, the DefaultDepth setting must be the same as one of the Depth identifiers. Every example I've ever seen has been set to 24.
The Server Layout ties it all together. The xorg.conf man page says that you can have multiple ServerLayout sections, and gives some rules as to which one will be used. However, we'll just have one in each xorg.conf file that we use.
This section looks like this:
Section "ServerLayout" Identifier "UNIQUE IDENTIFIER" Screen "SCREEN NAME" Option "OPTION" "VALUE" InputDevice "INPUT DEVICE IDENTIFIER" EndSection
For example, on my laptop, for my two-screen configuration, it looks like this:
Section "ServerLayout" Identifier "Two Screen Layout" Screen 0 "Default Screen" 0 0 Screen 1 "External Screen" RightOf "Default Screen" Option "Xinerama" "On" InputDevice "Generic Keyboard" InputDevice "Configured Mouse" InputDevice "stylus" "SendCoreEvents" InputDevice "cursor" "SendCoreEvents" InputDevice "eraser" "SendCoreEvents" InputDevice "Synaptics Touchpad" EndSection
There are two key pieces to note about this section:
- I have turned on the Xinerama option. This is one of the ways X has of working with multiple monitors, and probably the easiest.
- For each Screen, I had to prefix it with a number, starting with 0, to identify it.
- Also, for the first screen, I included a location, where to put the screen; in this case, at the coordinates "0 and 0".
- For the second screen, I indicated that it is to be to the right of the first screen.
Finally, I had to set a “server flag,” to enable the Xinerama functionality:
Section "ServerFlags" Option "Xinerama" "true" EndSection
Testing the Changes
Once you've finished editing the file, simply hit Ctrl+Alt+Backspace to restart X. (Make sure you shut everything down, first.) If it doesn't work, simply restore your xorg.conf file back to a version that you know works — for example, the xorg.justlaptop.prod file that you created earlier — and then start again. (See the X Window Configuration article for more help on doing that.)
Feel free to make as many backup copies as you need, while you're doing your editing. You can always delete them later, when you're up and running.
Finishing Your Editing
You now have at least two copies of your configuration file: xorg.justlaptop.prod, which works with just the laptop's display, and a working xorg.conf that powers both of your monitors. Your final step is to make another copy of your configuration file — the current one, that works with both of your monitors — and call it xorg.twomonitors.prod.
The Startup Script
Your final step is to create a script which will figure out whether or not the monitor is plugged in; depending on that answer, it will decide which configuration file should be used, and copy that file to /etc/X11/xorg.conf. The file should be put in the /etc/init.d directory; I named mine xorg.conf_switcher.sh. Here is what it contains:
# determine whether an external display is attached if /usr/sbin/ddcprobe | grep "monitorname"; then echo "External Display attached." # set external display as exclusive primary cp /etc/X11/xorg.twoscreens.prod /etc/X11/xorg.conf else echo "Using built-in LCD." # set build-in LCD as exclusive primary display cp /etc/X11/xorg.justlaptop.prod /etc/X11/xorg.conf fi
Once this is done, you need to change the permissions on the file, to make it “executable:”
$ sudo chmod +x /etc/init.d/xorg.conf_switcher.sh
And, finally, you have to tell Ubuntu to load this script automatically, on startup:
sudo update-rc.d /etc/init.d/xorg.conf_switcher.sh default
Now, every time you restart Ubuntu, this script will decide whether there is an external monitor attached, and load the appropriate configuration file.
This script doesn't quite work, yet. It's always one step behind. For example, if I boot up with a monitor, and then shut down, disconnect the monitor, and boot up again, X will think there is still a monitor. If I shut down again, and boot up again without a monitor, it will work fine. Similarly, when I reattach a monitor, the first time I boot up, the script will think there is no monitor, and then the second time it will work again.
Further Information — TODO
In addition to the information on this page, I was sent a very helpful tip from John Senora, on how to ensure that the startup script works successfully. It is my intention to follow his advice, and post the results here — but I haven't had a chance to play with Ubuntu in an environment where I have multiple monitors.
So, in the meantime, I'm going to post his comment here verbatim, and when I get a chance to play with it some more, I'll edit the page to incorporate his comments properly.
Thanks for the scripting info! My monitor switching is going great now!
If you are still having to restart twice, you might be able to alleviate the problem, at least I was able to. I did the following:
I noted where x11-common is, mine was S70x11-common. (S for startup, 70 - as x11 is the 70th thing to startup- at least in theory, I do not have 70 things before it)
I then linked to the script to run before this, by creating a symbolic link:
ln -s /etc/init.d/xorg.conf.switch.sh /etc/rcS.d/S69xorg.conf.switch.sh
It has started up in the proper mode every time since I did that.
I hope that helps, at least if you still needed it. Else - sorry for the spam
I would also recommend, if you are willing, to inform people to get the xresprobe package, so they can use the ddcprobe used in your script.
Thanks again for your wiki, it saved me quite a bit of time!
Many thanks to John for this comment, and apologies for letting it sit so long, before mentioning it. (It was my intention to do it quickly, but after the months went by, I realized that I'd already missed the boat on “quickly.”)
As much as I'm loving Ubuntu, it's ridiculous that I have to have a page like this, with instructions for getting multiple monitors to work. Why on earth can't the software figure this out for me? Linux nerds may feel that I'm just a Windows wuss, but honestly, this is so easy to do in Windows, and so frustratingly difficult to do in Ubuntu. I don't know if it's the fault of Ubuntu, the good folks behind X, the Linux kernel, or something else altogether, but this is nasty to have to do (and try to explain, on a page like this).
Compare all of the steps mentioned above with what I have to do in Windows:
First, I right-click on the desktop, to get the following dialog box:
Yes, you heard me, I right-click — with the mouse. In fact, in Windows, I won't even have to touch the keyboard through this entire exercise.
In that dialog box, I choose the secondary monitor, make sure it's active, and click the Extend my Windows desktop onto this monitor checkbox. I then make sure I have the right resolution for each monitor, and I can even, on that screen, drag the monitor to where I want it. (In the example above, you'll notice that the CRT is to the right of the laptop's LCD, and is also slightly higher, because that's how it looks in the physical world. When I move my mouse from screen to screen, it is much more natural looking, because Windows has set up the display properly.) Once I click OK, I'm done. I'm done.
If I boot up my laptop, and the monitor is not attached, Windows is smart enough to figure this out — without me having to do anything — and it gives me just the laptop's LCD for my display. It doesn't get confused, and think that the CRT is still there. When I boot up with the external monitor attached, again, Windows is able to figure this out on its own, and it loads up the settings that I used for that monitor.
So why can't Ubuntu work this way?