Command Line Magic


  Martin's virtual lair

A fairly common problem with monitors under Linux is that the screen resolutions supported by a particular monitor are not transmitted correctly to the operating system, thus resulting in less-than-ideal resolutions. To rectify this problem, one has to look up the monitor's supported resolutions in the manual, and then to manually set up these resolutions under Linux. Unfortunately, the last step is still far from straightforward in recent Ubuntu releases. Furthermore, many of the HOWTOs you find with Google only work for older Ubuntu releases (e.g. those using the now obsolete /etc/X11/xorg.conf), or they dig unnecessarily deep under the hood and might break things. In a nutshell, the correct way to do things these days is to use the xrandr command and to save the correct settings to the .xprofile file in the home directory. Superuser rights are not needed at all!

In my case, I wanted to connect an LG Flatron M228WD monitor to the VGA port of a ThinkPad X220 laptop. The maximum resolution of this monitor is 1680x1050, but Ubuntu 14.10 set it up as only 1024x768, and I was unable to select a higher resolution in the Displays settings window (Applications → System Tools → System Settings → Displays).

First, we need to know the name of the display whose resolution we want to modify. For this, connect the external monitor to the system, and run xrandr without any arguments:

$ xrandr
Screen 0: minimum 8 x 8, current 2390 x 768, maximum 32767 x 32767
LVDS1 connected primary 1366x768+0+0 (normal left inverted right x axis y axis) 277mm x 156mm
1366x768 60.0*+
1360x768 59.8 60.0
1024x768 60.0
800x600 60.3 56.2
640x480 59.9
VGA1 connected 1024x768+1366+0 (normal left inverted right x axis y axis) 0mm x 0mm
1024x768 60.0* 800x600 60.3 56.2
848x480 60.0
640x480 59.9
HDMI1 disconnected (normal left inverted right x axis y axis)
DP1 disconnected (normal left inverted right x axis y axis)

This tells us that the internal laptop display has the name LVDS1 and the external monitor display has the name VGA1. We also see that 1024x768 is the highest resolution currently available for the external display.

Next, we use the cvt tool to calculate the so-called modelines for the desired screen resolution. For 1680x1050 we get the following:

$ cvt 1680 1050
# 1680x1050 59.95 Hz (CVT 1.76MA) hsync: 65.29 kHz; pclk: 146.25 MHz
Modeline "1680x1050_60.00" 146.25 1680 1784 1960 2240 1050 1053 1059 1089 -hsync +vsync

Now we have all the necessary ingredients to set up a new display mode for the external monitor. We issue the following two xrandr commands:

$ xrandr --newmode "1680x1050_60.00"  146.25  1680 1784 1960 2240  1050 1053 1059 1089 -hsync +vsync
$ xrandr --addmode VGA1 "1680x1050_60.00"

The first command defines a new display mode with the name "1680x1050_60.00" (you could change this name to anything else), and the second command associates the new display mode with the external monitor. Now we can select the resolution 1680x1050 for the external monitor in the Displays settings window!

Since the above settings are valid only for the current session, we need to save them into a file that is executed every time we log into GNOME. Some HOWTOs suggest to create an autostart script for this, but this is not the proper way and may lead to complications, e.g. when the monitor cable is left plugged into the laptop during log-in. Instead, simply create an .xprofile file in your home directory with the two xrandr commands from above:

xrandr --newmode "1680x1050"  146.25  1680 1784 1960 2240  1050 1053 1059 1089 -hsync +vsync
xrandr --addmode VGA1 "1680x1050"

That's it, we're done! We don't even need to make .xprofile executable, because it is not a shell script in the classical sense.

On a final note, some HOWTOs additionally include a third line, which explicitly selects the desired screen resolution:

xrandr --output VGA1 --mode "1680x1050"

However, there is no good reason to add this line, because the desired resolution needs to be selected only once in the graphical Displays settings window, and is afterwards stored by Ubuntu in the .config/monitors.xml configuration file. In fact, adding this line to .xprofile could lead to problems when a different resolution is preferred or required in the future. In case something should break, deleting the .xprofile and .config/monitors.xml files will reset all changes.