Setting up dual monitors system-wide with XRandR on Debian Lenny

by Sander Marechal

I have been playing with my monitor setup again on my Dell D530 laptop. The internal monitor of the laptop is 1400x1050 pixels, but I usually use a 19" external monitor set to 1280x1024. With Etch this worked flawlessly. When the external monitor was hooked up and the lid on my laptop was closed, then my desktop would be 1280x1024. When I booted with the lid open and no monitor attached, the desktop would be 1400x1050. When I upgraded to Lenny this stopped working. I had to change the resolution each time I changed from external screen to internal screen and back. I reported a bug about this but the answer came down to “It’s not a bug but a feature. Use XRandR to change your displays if you don’t like it.”

So, I dove into XRandR. I wanted this to work for all users, so simply sticking some XRandR commands in the gnome session startup wasn’t going to suffice. At the same time I also got interested in trying a dual-head setup instead of only an external monitor when one was connected. I was going to need completely different layouts depending on the external monitor. It took a while before I figured out how to do it, it took a little longer to fight GNOME into submission, but here is how to make it work.

Figure out the correct XRandR settings

I'm not going in to the gory details of XRandR. The Debian wiki has an excellent explanation of XRandR 1.2. It is possible to set XRandR configurations in the xorg.conf file but I didn’t do that. Such a setup does not allow me to specify things like “If monitor X is attached, do Y else do Z”. Here are the XRandR settings/commands that I want. LVDS is my internal monitor and VGA is the external 19" monitor.

When an external monitor is present, set up a dual head configuration. Note that I have placed the screens above each other instead of side-by-side and that they overlap by 26 pixels. That is because the Intel 945 chipset/driver only support a total combined size of 2048x2048 pixels. If you use any other driver or chipset (like an ATI or Intel 965) then you can use a bigger combined size, like 2560x1024 for two 19" monitors side-by-side.

  1. xrandr --output VGA --mode 1280x1024 --pos 0x0 --output LVSD --mode 1400x1050 --pos 0x998

If you want to use only the external monitor then you would use this instead:

  1. xrandr --output VGA --mode 1280x1024 --output LVDS --off

And this is the setting I want when there is no external monitor connected:

  1. xrandr --output LVDS --mode 1400x1050 --output VGA --off

Applying the XRandR settings at login time

I want these settings to apply when I log in. Like I said, I do not put anything in the xorg.conf. When the laptop boots with an external monitor attached then the GDM login screen will be shown on all monitors. After logging in the dual head setup will be configured. To do that I created a file called 45custom_xrandr in /etc/X11/Xsession.d/. That script will be executed when an Xsession starts (after logging in to GNOME, KDE or any other desktop environment.

  1. xrandr | grep VGA | grep " connected "
  2. if [$? -eq 0]; then
  3.     # External monitor is connected
  4.     xrandr --output VGA --mode 1280x1024 --pos 0x0 --output LVDS --mode 1400x1050 --pos 0x998
  5.     if [$? -ne 0]; then
  6.         # Something went wrong. Autoconfigure the internal monitor and disable the external one
  7.         xrandr --output LVDS --mode auto --output VGA --off
  8.     fi
  9. else
  10.     # External monitor is not connected
  11.     xrandr --output LVDS --mode 1400x1050 --output VGA --off
  12. fi

Now if you log in, the correct settings should be applied. Only it doesn’t work on GNOME…

Stopping GNOME from overriding your XRandR configuration

As it turns out, GNOME tries to guess the best XRandR settings for you based on your connected monitors and the display configuration you set. But the GNOME 2.20/2.22 mix on Debian Lenny does not support a dual-head configuration. That option only became available in GNOME 2.24 and thus is not available in Debian Lenny. You need to turn off the XRandR plugin for the gnome-settings-daemon. Open up a terminal and start gconf-editor. Now browse to /apps/gnome-settings-daemon/plugins/xrandr and uncheck the "active" entry.

Now GNOME will simply adapt to whatever XRandR layout configuration already exists. Many thanks to jm_ and enouf on #debian to help debug this issue. I hope this helps any of you!

References

Digg this article: This article on Digg

Creative Commons Attribution-ShareAlike

Comments

#1 Anonymous Coward

How does xrandr know the difference to what device I am referring to with the various initial commands?
> xrandr --output VGA --mode 1280x1024 --pos 0x0 --output LVSD --mode 1400x1050 --pos 0x998
> xrandr --output VGA --mode 1280x1024 --output LVDS --off

Additionally what type of setup is needed in xorg.conf — you seem to not need any? No various 'screen' sections? Is this dependent on a 3d-capable driver? I have an ATI 3450 card with dual outputs (analog, digital, or s-video) and I've been trying to figure this out to clone the monitor onto the tele with s-video; the radeonhd driver doesn't have many capabilities yet for my card, and the fglrx driver is just a headache...

well perhaps i should be reading the xrandr wiki on debian's pages...

#2 Sander Marechal (http://www.jejik.com)

Hi AC,

How does xrandr know the difference to what device I am referring to with the various initial commands?


This depends on your driver. It will always name the devices the same way. In my case my Intel driver always refers to the internal display as LVDS and to the external display as VGA.

Additionally what type of setup is needed in xorg.conf — you seem to not need any?


You only need a very bare bones xorg.conf file. A modern X server can autodetect pretty much anything, so you only need to meddle with the configuration file when you think that Xorg detected something wrong. Here are the relevant snippets from my xorg.conf:

Section "Device"
	Identifier	"Intel Corporation Mobile 945GM/GMS/940GML Express Integrated Graphics Controller"
	Driver		"intel"
	BusID		"PCI:0:2:0"
	Option		"Monitor-LVDS"	"Internal monitor"
	Option		"Monitor-VGA"	"External monitor"
EndSection

Section "Monitor"
	Identifier	"Internal monitor"
	Option		"DPMS"
EndSection

Section "Monitor"
	Identifier	"External monitor"
EndSection

Section "Screen"
	Identifier	"Default screen"
	Device		"Intel Corporation Mobile 945GM/GMS/940GML Express Integrated Graphics Controller"
	Monitor		"Internal monitor"
	DefaultDepth	24
	SubSection "Display"
		Depth		24
		Virtual		2048 2048
	EndSubSection
EndSection


The only changes I made were adding the Monitor-LVDS and Monitor-VGA options in the Devices section, and adding the Virtual option in Screen->Display. See my second reference to http://www.thinkwiki.org/wiki/Xorg_RandR_1.2 for more information about that.

#3 Anonymous Coward

In the true path of the if/else statement, you mispelled LVDS (Low-voltage differential signaling) in the posted script. The initial command will fail due to an unidentified output and on the nested if statement it will always return the default auto setup.

Just incase someone were to copy and paste.

#4 Sander Marechal (http://www.jejik.com)

Thanks, I corrected it!

#5 Familia

I've noticed, that intel graphic card 965 always choose the primary screen as the VGA or external one, which is kind of a messy if you're using gnome. And if that you're using two monitors with different resolutions, let's say 1280x800 and 1280x1024 it forms a square virtual screen, which also is annoying because now you have a bigger screen (vertically) in one of the monitors in which things get lost... like icons, windows, etc...
Have you found this problems, does anyone know how to workaround them?

#6 Sander Marechal (http://www.jejik.com)

@Familia: Using xrandr you can override which screen is the primary screen. The first output mentioned on the xrandr command will become the primary screen. Not that Gnome always puts your panels of the primary screen. So, to make the internal LVDS the primary one, use this:

xrandr --output LVDS <options> --output VGA <options>


I do not know of a solution to the square desktop problem. I did lay out my monitors in such a way that I do not lose icons though. Gnome always tries to add icons on the bottom first. If you have two screens side-by-side, simply make sure that the one with the highest vertical resolution (in your case the 1280x1024) is the left most screen.

#7 Kumar

Thanks for the simple xrandr fix. I am still unable to set different resolutions for my laptop and external monitor (when they are both on, the default resolution is that of the monitor,no matter which order they are specified in the xrandr call, but at least I can enjoy the full resolution of the monitor.

#8 Sander Marechal (http://www.jejik.com)

@Kumar: Do you have XrandR 1.2 and compatible Xserver? Older versions aren't really useful. Better use Xinerama or MergedFB on such configurations.

What does `xrandr -q` say? Does it list the resolutions that you want? If not, then you may want to add a few modelines to your xorg.conf so that xrandr can pick the correct resolution.

#9 emk2203

Many, many thanks for this. I have almost exactly the same problem (using a Dell D830 laptop in a docking station with Xfce on Sidux) and I have arrived at a similar way to solve it - but I suck at scripting, so your Xsession script is really a lifesaver for me!

Actually, I am going via the acpi event of the closed lid to switch between two monitors, avoiding the virtual screen altogether. I hope I can get it working, maybe I return for a question if you don't mind...

#10 Anonymous Coward

For whatever reason, I cannot login again (emk2203), but my new solution is the script 45lidclosedLVDSoff :

grep -q -w closed /proc/acpi/button/lid/LID/state && xrandr --output LVDS --off --output TMDS-1 --auto --output VGA --auto --output TV --auto

Only when the lid is closed when X starts, the internal panel gets switched off and whatever is connected gets autoconfigured.

#11 Amitz Sekali

After customizing my debian lenny, my xrandr no longer identify all possible connectors. If I run xrandr in terminal window on monitor A, the xrandr can only identify monitor A and it's connector (DVI,VGA, etc). If I run xrandr in terminal window on monitor B. the xrandr can only identify monitor B and it's connector.

Is this problem related to the gnome version of Lenny since I'm using XFCE instead of Gnome? How to check whether gnome libraries I use don't somehow make xrandr problematic? Thank you in advance, will really appreciate your pointer.

#12 Sander Marechal (http://www.jejik.com)

@Amtiz: XrandR is much lower level. It doesn't touch Gnome. It's straight X.org. Are you *sure* that your video driver supports XrandR? Only very few drivers do. Basically just the Intel drivers and teh X.org radeon drivers (for older ATI cards). In theory the closed-source ATI Catalyst drivers also support XrandR 1.2 but you have to do some commandline magic to enable it. I tried it on my machine but it simply crashed the entire machine. Anything else does not support XrandR.

@emk2203: There's a bug in my login script. I am hunting it down now. If you type in the correct username and password then it works, but if you enter the wrong password then something goes wrong and you get an Apache 500 internal server error instead of a nice error message telling you that you entered the wrong password.

#13 Sander Marechal (http://www.jejik.com)

@emk2203: The login bug has been fixed. I have some custom .htaccess rule meant to keep spammers out of the article comments. One of them was interfering with the HTTP POST request to login. Everything works now. Thanks for the report!

#14 Giorgos S

Thanks for this. I have an external monitor connected to a laptop (intel 945GM). What do I need to include in the xorg.conf so that the laptop's monitor is always off (even in the GDM login screen)??? Thanks in advance!

#15 Anonymous Coward

Thanks a lot, I've successfully configured two screen to use virtual desktop (2048x768 in my case) accordingly to your guide on my Ubuntu 8.04 box

But I still have one problem that my Internal Monitor now treated as "Virtual Screen" , and the Extend Monitor is now become my Primary screen, and I can't switch it other way round, I've tried the following without any luck:

> xrandr --output LVDS --left-of VGA
> xrandr --output LVDS --right-of VGA
> xrandr --output VGA --left-of LVDS
> xrandr --output VGA --right-of LVDS
> xrandr --output VGA --right-of LVDS --mode=1024x768

My OS is Ubuntu 8.04

Any suggestions will be highly appreciated.

#16 Bryan

I also observed that my "VGA" has been recognized as the "Internal Monitor",

and my "LVDS" has been recognized as the "External/Extend Monitor"

Is there a way to configure the xorg.conf or xrandr to choose the "LVDS" as the "Internal/Main Monitor"?

#17 Sander Marechal (http://www.jejik.com)

Sorry Bryan and Anonymous. XRandR has no notion of "primary" or "secondary" monitors. All monitors are equal. They appear whatever order the driver detects them which is entirely arbitrary and desktop environments shouldn't care what order they are in.

If you're using Gnome then you can simply drag your toolbars and panels to the other monitor and be done with it.

#18 Motin (http://motin.eu)

"If you're using Gnome then you can simply drag your toolbars and panels to the other monitor and be done with it. "

But this is exactly what we want to not have to do. I connect the laptop every afternoon, and dragging those toolbars doesn't get more amuzing over time...

Does anyone know maybe a way to tell Gnome what monitor is the primary one?

#19 Sander Marechal (http://www.jejik.com)

That's a known bug in Gnome. I suggest you file a bug about it or update/vote on an existing bug.

There does seem to be a workaround but it's kind of a nasty trick...

#20 Dourado

OK, I didn't read EVERY post, so this suggestion might not be for everyone involved.

However, I just installed the "grandr" package and then went to System->Administration->Multiple screens

This tool made it quite easy for me to make this work as desired.

#21 PapaJ

#1 This is perfect. For the past couple of days I've been customizing Xubuntu for my 2,1 MacBook, and this is the best solution so far for my external monitor. In my case I had to make some mod's; perhaps the default scripting language is not [precisely] the same as Debian's.
Here's how my "45custom_xrandr" looks now:
xrandr | grep "TMDS-1 connected"
if [ $? != 0 ]; then
xrandr --output LVDS --mode 1280x800 --pos 0x26 --output TMDS-1 --mode 1920x1200 --pos 1280x0
if [ $? -ne 0 ]; then
xrandr --output LVDS --mode auto --output TMDS-1 --off
fi
#debug echo "'if [ 1 != 0 ]; then' worked"
else
xrandr --output LVDS --mode 1280x800 --output TMDS-1 --off
#debug echo "'if [ 1 != 0 ]; then' did not work"
fi

#2 .. Looking forward to implementing the close-lid-script ...

#3 And yes, grandr rocks too.

#22 PapaJ

Sorry... Thought I needed "!=" instead of "-eq" - but I was [dead] wrong. (Sure would like to be able to edit my posts ;^)

BTW, occasionally a couple of horizontal pixel-lines are written to the wrong place on the screen(s). I monkey with settings and after a reboot it looks OK, but then comes back after some other reboot. My most recent tweak attempt is to set both monitors to "--rate 60", and once again the bad lines are gone. I thought since the MacBook screen might [inappropriately] be going into its 59.9 rate, then perhaps forcing it into its 60 rate (which the external 1920x1200 screen also reports) might make stuff just work.
(Just another example of why most people *buy* operating systems.)

#23 Anonymous Coward

I am running stock Jaunty on an IBM X40 (Intel video), xrandr 1.3. If I run IceWM or Xfce, xrandr works perfectly. I use xrandr to attach an external 1280x1024 VGA and let the internal LVDS pan over it while being 1024x768. Gnome, however, tries to be too smart. As soon as I issue the xrandr command, a 1280x1024 canvas is scaled down to fit into the 1024x768 LVDS (looking horrible). At the same time, mousing down or right lets me pan over a real 1280x1024 pixel area, except that an L-shaped boundary in 1280x1024 setminus 1024x768 is black.

I thought I'd hit the jackpot when I found this page and the xrandr gnome plugin configuration, but no luck. I restarted X, but do I have to reboot?

I am fine with Xfce except that even with UXA and DRI2, 2d rendering of GTK+ apps suck. Xterms leave a transient trail of destruction as you drag them across Firefox, and Google spreadsheets are horribly sluggish. The situation seems a little better in Gnome which is why I want to switch.

Many thanks for any tips.

#24 Sander Marechal (http://www.jejik.com)

What do you mean by canvas? The actual desktop? The wallpaper? When I used dual monitors of different size I didn't have problems with one desktop scrolling.

Perhaps you should try Gnome's built-in XRandR tool grandr. There's almost two years between Jaunty's Gnome and the Gnome version in Lenny that this article is about. It should work a lot better now.

#25 Tonypm

I have been trying to find out how to set the primary screen and any solutions I have found seem to be complex. Then I stubmled on this
http://www.phoronix.com/scan.php?page=news_item&px=NzAzMQ

Which says the new version of xrandr now has a --primary option. So on fedora 13 this worked to switch my primary screen.
xrandr --output DVI-I-2 --primary

#26 Amit

Thanks a lot , Its worked for me.

#27 Anonymous Coward

gconftool-2 --set --type=bool /apps/gnome_settings_daemon/plugins/xrandr false

#28 CPG

Thanks for the post. It helped me get a Dell Inspiron D630 1280x800 (laptop) display to work with an external 1280x1024 monitor.

#29 merwok

Thanks for your helpful post! I was using ARandR (a graphical frontend for xrandr) and thought I could not get the displays to be side-on-side instead of duplicating the same output, but using your first xrandr command it Just Worked™

Cheers

#30 merwok

Heh, today a game reset my screen configuration, and I found this post again to help me :) This time I bookmarked it and saved the command in a shell script for future use.

#31 merwok

This helpful page mentions the handy --left-of/etc options to xrandr: Debian wiki page on RandR

#32 frank

Hello Marechal i read your article about xrandr but i dont found a solution for my problem :-(

I have a ThinkPad T420 an HD300 Intel Card OS archlinux with no corg.conf and my Laptop is connected in a Dockingstation per Display Port.
The Problem is when i boot archlinux the resolution in the console is as big as the Laptop Window 1600x900 my NEC PA241W has 1920x1200.

i have in my .xinitrc
#!/bin/sh
#
# ~/.xinitrc
#
# Executed by startx (run your window manager from here)

xrandr --output DP2 --mode 1920x1200 --output LVDS --off
#xrandr --output DP2 --auto --output LVDS --right-of LVDS
#xrandr --output DP1 --auto --output LVDS --auto --same-as DP1
#xrandr -d DP2 -s 1920x1200
if [ -d /etc/X11/xinit/xinitrc.d ]; then
for f in /etc/X11/xinit/xinitrc.d/*; do
[ -x "$f" ] && . "$f"
done
unset f
fi

# exec gnome-session
# exec startkde
# exec startxfce4
# ...or the Window Manager of your choice
exec openbox-session

when i type startx as user my NEC shows me the 1920x1200 but when i will resize a window to Full Size it wont work and when i switch in a console and type for examble dmesg they will be not shown all the output to the NEC at the end of the Monitor so ca 4-5 cm are crop

what can i do? do i need an xorg if yes what has to be inside the conf?

#33 Sander Marechal (http://www.jejik.com)

It sounds like your laptop monitor isn't really off, but mirrorring your laptop screen. Are you sure that your laptop screen is referred to as LVDS? It's different per driver.

You also need to make sure that your desktop isn't overriding youd XRandR settings again. Gnome 2.x did that. I don't know if Gnome 3 or KDE 4 do something similar.

#34 Junkman

Excellent guide man! I found this because the arandr gui tool crashes when I connect a 2nd screen, and I'm not sure why. In any case doing it this way is much better than my previous arandr experiences. I only had to change the xrandr settings around a bit to suit my needs. In my case the default screen is LVSD1 and I wanted to use HDMI1 for the second screen. A little tweaking to what you provided and I can enjoy 1080p movies while still being able to do whatever on my laptop. My hdtv is usually hooked up to my ps3 so I decided to just paste my custom xrandr settings in a terminal when I need it. Very cool and many thanks.

#35 Anonymous Coward

Thanks for the valuable help. I fixed my resolution problem but I experience the following problem.

I created a 45custom_xrandr.sh file in /etc/X11/Xsession.d with the following lines

1 xrandr | grep VGA1 | grep " connected "
2 if [$? -eq 0]; then
3 # External monitor is connected
4 xrandr --newmode "1368x768_60.00" 85.25 1368 1440 1576 1784 768 771 781 798 -hsync +vsync
5 xrandr --addmode VGA1 1368x768_60.00
6 xrandr --output VGA1 --mode 1368x768_60.00
7 if [$? -ne 0]; then
8 # Something went wrong. Autoconfigure the internal monitor and disable the external one
9 xrandr --output LVDS1 --mode auto --output VGA1 --off
10 fi
11 else
12 # External monitor is not connected
13 xrandr --output LVDS1 --mode 1366x768 --output VGA1 --off
14 fi

saved it and then tried to run it as ./45custom_xrandr.sh . All i get is

VGA1 connected (normal left inverted right x axis y axis)
./45custom_xrandr.sh: line 2: [0: command not found

It seems that it cannot run the if command...Why?

#36 Sander Marechal (http://www.jejik.com)

Try adding a shebang to the top. Example #!/bin/bash. Are you running an alternative shell like csh or zsh?

#37 Jhonas Nascimento

Wonderful! Thanks A LOT BRO! I was looking for this for months! I Use LXDE and de LXRandR not is sufficiently smarth to do this then I really pleased.

#38 Jhonas Nascimento

Hello there! I've posted in a few hours a thankfulness for your code but I don't tested him, then I tested and doesn't work for me. It's for some syntax errors and for my "monitor numbers" (ex.: VGA0/LVDS0) doesn't be zero neither one neither no number (VGA/LVDS). I'm searching for this solution for months then I stay whole night trying to this *#%!@ works! and I get!

I do a condition while to search my 'monitor number' then I used a part his code to set the configuration of my monitors. Here's my code case anyone want test.


nm=-99 #contains the right(if found) number your monitors.

#Search for the right number your monitors.
x=-10
while [ $x -le 10 ]
do
x=$(( $x+1 ))
xrandr | grep &quot;VGA$x&quot;

if [ $? -eq 0 ]; then
#echo &quot;Monitor valido: $x&quot;
nm=$x
break
#else
# echo &quot;Monitor $x inválido...&quot;
fi
done

#verify if don't founded the right number
if [ $nm -eq -99 ]; then
xrandr --auto
else
#if founded the number then set the configuration for your monitor
xrandr | grep VGA | grep &quot; connected &quot;
if [ $? -eq 0 ]; then
#if VGA is connected set this configuration
xrandr --output &quot;VGA$nm&quot; --mode 1024x768 --rate 60.0 --output &quot;LVDS$nm&quot; --off
else
#if VGA isn't connected set this configuration
xrandr --output &quot;LVDS$nm&quot; --mode 1024x768 --rate 60.0 --output &quot;VGA$nm&quot; --off
fi
fi


Thanks bro' You've my inspiration to try make this works, and sorry for my bad English.

#39 milgrad

Sander, many thanks for the script!
- my problem is that both laptop monitor and external one are activated at start (and I only need the external one)
- the separate commands of script work in console, however, the script either gets overridden by smthg or doesn't work on restart
- after I manually change monitor settings and log-off, settings get saved on log on, but once I restart the problem comes back

Happy to hear your thoughts

I'm using Ubuntu 13.10 Unity.

#40 Sander Marechal (http://www.jejik.com)

@milgrad: I don't know how to help you, sorry. These days Ubuntu is very different from stock Debian (which I use). The problem has probably to do that the custom XSession file isn't executed by Ubuntu. I suggest you ask on the Ubuntu forums and show them this article. They can probably tell you what you should use instead of an XSession file.
Post a new comment

Registration is not required to post comments, but cookies must be enabled. One of the advantages of registration is that you can edit your comments later on (editing not yet implemented). You can register or login here.




Your e-mail address will not be published, but your website URL will. All links that you post will tagged rel="nofollow" to throw off spammers. You are allowed to use the following XHTML tags in your comment: <em> <strong> <u> <b> <i> <strike> <blockquote> <big> <small> <ul> <ol> <li> <a href=""> <pre> <code> <tt> <br>. Please allow up to 60 second processing time after you post a comment. Our spam filters may take some time.