44

I found somewhere in a forum the solution to a problem where the only resolutions available were 800x600 and 1024x768. I managed to get 1440x900, which works very well. Every time I reboot, this mode disappears. I am forced to retype the commands, so how can I save this? I'm assuming it is a simple command, but I am the largest of noobs when it comes to Ubuntu. The commands are:

  • xrandr --newmode "1440x900_60.00" 106.50 1440 1528 1672 1904 900 903 909 934 -hsync +vsync
  • xrandr --addmode VGA1 1440x900_60.00
  • xrandr --output VGA1 --mode 1440x900_60.00
Jacob J.
  • 443

6 Answers6

45

The accepted answer applies the same configuration regardless of the status of connected displays. This didn't work for me, as I'm connected to different displays at work and at home. autorandr allows automatic xrandr configurations for different display setups. To use autorandr,

  1. Install with sudo apt install autorandr (tested on Ubuntu 18.04, Lubuntu 20.04)
  2. Configure your monitor to your liking with xrandr
  3. Store your configuration with autorandr --save work (I'm storing my work config, choose a name that suits you)
  4. Resume the config with autorandr --change work to choose config, or just autorandr --change to have it infer your config from your connected monitors.

XDG autostart .desktop is also provided, and installed into /etc/xdg/autostart/autorandr.desktop by default. When you reconnect your work monitor, the work setup will be reloaded.

Teodor
  • 571
  • 2
    It also worked for me the monitors configurations were edited using the default display settings UI. – AlikElzin-kilaka Dec 01 '20 at 00:03
  • 1
    Works great - this seems like the modern (in 2021) answer. – Mike McKay Oct 08 '21 at 12:55
  • Does not work for me with Ubuntu 22.04, unfortunately. It was able to save the mode and detects the screen, but fails to apply the profile: xrandr command failed. – w-sky Nov 01 '23 at 00:35
  • In my case I have a KVM to swap my active display between Linux/Windows. udevadm monitor shows the events when changing your display, and you can create a /etc/udev/rules.d/XXXXXX.rules file to listen and run autorandr --change XXXXX

    However, with an nvidia card I also needed to change nvidia_drm.modeset=1 in order for udev to get display change events

    – glitchyme Dec 21 '23 at 08:06
39

You have several choices but perhaps the easiest is to place your command exactly as you have given above in your $HOME/.xprofile file. From this location it will be executed every time you login.

By default this file does not exist in Ubuntu and so may need to be created manually and then be made executable. The following commands will do this:

touch $HOME/.xprofile
chmod +x $HOME/.xprofile

Note the 2 small shortcomings of this method:

  1. .xprofile is accessed occurs fairly late in the startup process so you may see some initial screen resolution resizing
  2. This is a 'per user' setting and may need to be repeated for other users on your system

If you wish to delve deeper there are a few other choices available in the reference link below, but the technique I have described here is still the safest and easiest.

References:

andrew.46
  • 38,003
  • 27
  • 156
  • 232
8

The following simple configuration works for me, and when connected, my monitor automatically uses the correct resolution without any manual intervention.

sudo nano /etc/X11/xorg.conf

Note that this file may or may not already exist. Add the following:

Section "Monitor"
    Identifier "VGA1"
    Modeline   "1440x900_60.00"  106.50  1440 1528 1672 1904  900 903 909 934 -hsync +vsync
EndSection    

Then reboot. If all goes well, then things should just work.

Background

I have a UX32VD laptop, and I wanted to have 4K over HDMI, even though there is no 3840x2160 option in the display settings.

My first task was to compute the modeline. Skip this step if you already know the modeline. (The modeline below may work for you.) I downloaded umc-0.2.tar.gz, extracted, and ran ./configure and make. To get the 25Hz Reverse Blanking Timing mode, I ran

umc-0.2/src/umc 3840 2160 25 --rbt

which outputs

    # 3840x2160x24.99 @ 54.625kHz
    Modeline "3840x2160x24.99"  218.500000  3840 3888 3920 4000  2160 2163 2167 2186  +HSync -VSync

Next I wanted to test this modeline. Running xrandr with no arguments, I saw that my HDMI device is named HDMI-1. I tested this mode by running commands analogous to Jacob's:

xrandr --newmode "3840x2160x24.99"  218.500000  3840 3888 3920 4000  2160 2163 2167 2186  +HSync -VSync
xrandr --addmode HDMI-1 "3840x2160x24.99"
xrandr --output HDMI-1 --mode "3840x2160x24.99" --preferred

Finally, to make the resolution permanent, I created /etc/X11/xorg.conf with the following contents:

Section "Monitor"
    Identifier "HDMI-1"
    Modeline   "3840x2160x24.99"  218.500000  3840 3888 3920 4000  2160 2163 2167 2186  +HSync -VSync
EndSection  

To test, restart the computer.

I should note that I am running nvidia-driver-390 on Ubuntu 18.04. Hopefully my technique generalizes well to other configurations. (Please let me know in the comments.)

Ben Mares
  • 207
3

You can use the autostart directory to apply the right xrandr settings when starting up the graphical environment:

  1. Determine the xrandr command to use. For example when using the GUI tool arandr, you can do "Layout → Save As …" and then open that file to find the command.

  2. Use your desktop environment's configuration GUI to set up a custom autostart application with the xrandr command you found.

  3. Restart the computer to see the effect.

Alternatively to no. 2, you can do the exact same thing manually by creating a file ~/.config/autostart/xrandr-settings.desktop with the following content:

[Desktop Entry]
Type=Application
Version=1.0
Name=custom xrandr settings

# Replace with your own xrandr command:
Exec=xrandr --output LVDS-1 --pos 0x1024 --output VGA-1 --pos 0x0

Advantages, Details

I like this mechanism better than putting the command into $HOME/.xprofile as proposed in another answer. Because pretty surely the autostart directory will already contain entries provided or even required by the desktop environment, and this way I have all autostarted stuff incl. xrandr together in one place rather than having to figure out where something is autostarted if I ever want to change it.

The autostart directory is a FreeDesktop mechanism, so it should work in all major Linux desktop environments. From another answer (by villapx, licenced under CC-BY-SA 4.0):

The autostart directory is a part of the freedesktop.org/XDG Desktop Application Autostart Specification. Per that spec, a compliant desktop environment will search $XDG_CONFIG_HOME/autostart for any .desktop files and execute them on startup.

tanius
  • 6,303
  • 1
  • 39
  • 49
2

Finally, after endlessly mucking around with xrandr and .xprofile and .xinitrc and /etc/xorg.conf, none of which worked for the greeter or would stick permanently, this is what worked:

Note the path is /usr/share/X11/xorg.conf.d/

Create a new file 10-my-monitor.conf there...

# sudo vi /usr/share/X11/xorg.conf.d/10-my-monitor.conf

And include the screen and monitor fragment of the xorg.conf.

Section "Monitor"
    Identifier  "LGThing"
    Modeline    "FourK30" 338.75  3840 4080 4488 5136  2160 2163 2168 2200 -hsync +vsync
    Option      "PreferredMode" "FourK30"
EndSection

Section "Screen" Identifier "MyScreen" Monitor "LGThing" DefaultDepth 24 SubSection "Display" Modes "FourK30" EndSubSection EndSection

I got the modeline from cvt.

John Mee
  • 953
  • 1
  • 8
  • 18
2

None of the answers here worked for me. I'm using Ubuntu 20.04 and gdm3 as display manager. This is how I persisted configuration across reboots and user sessions. I saved the xrandr command to a file '~/.config/autostart/setup_monitor_display.desktop', made it executable with chmod +x.

# Wait until GUI is ready
X-GNOME-Autostart-Delay=1
xrandr --output DP-0 --rotate left

Then I added the execution of the script to the startup application list.

  1. Open Startup Application Preferences
  2. Click Add
  3. Fill in the name and comment
  4. For command do something like sh -c '~/.config/autostart/setup_monitor_display.desktop'

Based off the answer here: https://askubuntu.com/a/1230863/1152625