7
June
2021
11:52

How to change display resolution from Linux command line

7 June 2021 11:52

If the image resolution on the screen does not match the display resolution, you can change the resolution using the command line.

На Working on a PC with the Linux Mint 19.3 operating system, I have a widescreen display with a resolution of 1366 by 768 pixels. One day I booted the computer with the display turned off and the resolution was set to 1024 by 768. Somehow, this resolution was remembered and the image became stretched.

Running diagnostics of the main display (displaying resolutions supported by the display):

xrandr -d :0 -q

Screen 0: minimum 8 x 8, current 1024 x 768, maximum 32767 x 32767
VGA1 connected primary 1024x768+0+0 (normal left inverted right x axis y axis) 0mm x 0mm
1024x768 60.00*
800x600 60.32 56.25
848x480 60.00
640x480 59.94

As you can see, the resolution of 1024x768 is selected by the system and is marked with an asterisk*.

 

Solving the problem

  1. Find out the display model (sticker on the back cover) - using the model from the specification, find out the maximum resolution supported by the monitor.
    In this case, 1366 by 768 px at a refresh rate of 60 Hz.

Connect to the Internet and install the xcvt package.

sudo apt install xcvt

  1. Run command cvt with parameters corresponding to resolution and refresh rate:
    cvt 1366 768 60

In my case the following was output:

#1368x768 59.88 Hz (CVT) hsync: 47.79 kHz; pclk: 85.25 MHz
Modeline "1368x768_60.00" 85.25 1368 1440 1576 1784 768 771 781 798 -hsync +vsync

  1. You need to take the line and feed it to the xrandr input to create a new video mode:

xrandr --newmode "1368x768_60.00" 85.25 1368 1440 1576 1784 768 771 781 798 -hsync +vsync

  1. Find out the name of the display - displayed by the xrandr command:

    xrandr -d :0 -q

in my case there is only one display and its name VGA1

  1. Add a new, previously created mode to the display:

xrandr --addmode VGA1 1368x768_60.00

  1. Finally, I ran the command to change the image resolution (switching the display to the new mode "1368x768_60.00"):

sudo xrandr --output VGA1 --mode 1368x768_60.00

The display may flash on or off. If everything went well, the image will become the selected resolution.

If necessary, restore the resolution to 1024x768:
Press Enter several times and enter the following command into the command line

sudo xrandr --output VGA1 --mode 1024x768

If necessary, repeat changing the display mode:

xrandr --output VGA1 --mode 1368x768_60.00

press "Enter"

You don't need to enter the sudo password every time.


Addition for monitor with 1280x1024 resolution:

cvt 1280 1024 60
xrandr --newmode "1280x1024_60.00"   109.00  1280 1368 1496 1712  1024 1027 1034 1063 -hsync +vsync
xrandr --addmode VGA-1 1280x1024_60.00
sudo xrandr --output VGA-1 --mode 1280x1024_60.00

Addition from 12/29/2023: To set the display resolution and save a ready-to-run shell script to change the screen resolution, you can use the program Arandr:
sudo apt install arandr
Arandr

Update from 01/27/2026: some video card drivers respond to the command to set the xrandr --output mode with the errors "xrandr: Failed to get size of gamma for output default", "xrandr: Configure crtc 0 failed" and "xrandr: Gamma size is 0". You can try to press “e” or “Tab” in the Grub menu when loading and add something like this to the GRUB_CMDLINE_LINUX_DEFAULT line parameter nvidia_drm.fbdev=1 nvidia_drm.modeset=1. Or in the case of the ATI Rage 128 video card:fb.fbdev=1 fb.modeset=1, depending on the contents of the /var/log/Xorg.0.log file. And continue loading F10 or Ctrl+X or Enter. An example of a complex command that still does not work with an old video card:sudo xrandr --output default --panning 1368x768 --crtc 0 --brightmess 1.0 --scale 1x1 --gamma 1:1:1, the error is the same: "xrandr: Gamma size is 0". Then the easiest way is to replace the video card. ;-[

Permanently setting display resolution when logging into Linux

I wrote the following three commands into the executable file.

First I created an empty file in my home folder

nano resolution.sh

And inserted 4 lines of text into it:

#!/bin/bash
xrandr --newmode "1368x768_60" 85.25 1368 1440 1576 1784 768 771 781 798 -hsync +vsync
xrandr --addmode VGA1 1368x768_60
xrandr --output VGA1 --mode 1368x768_60

Saved the file.

The same file, but for a 1280x1024 monitor, I attach:
resolution.sh.zip

Gave permissions - execution only from the current user (owner).

sudo chmod 700 resolution.sh

Added to startup (I have the LXQT shell):
Start - Options - LXQT Settings - Session Settings - Autostart - Add
sh /home/vladimir/resolution.sh.
Confirmed and closed the window.

Another option is to add the commands to a file in the user's home directory .xinitrc:

export GNOME_SHELL_SESSION_MODE=ubuntu
export XDG_CURRENT_DESKTOP=ubuntu:GNOME
export XDG_DATA_DIRS=/usr/share/openbox:/usr/local/share/:/usr/share/


xrandr --newmode "1280x1024_60.00" 109.00 1280 1368 1496 1712 1024 1027 1034 1063 -hsync +vsync
xrandr --addmode VGA-1 1280x1024_60.00
xrandr --output VGA-1 --mode 1280x1024_60.00

Logged out and logged in to check. Upon entry, the display blinked and the correct display resolution was applied.
Also, the command file resolution.sh can be run from the window terminal.

The case of HDMI connection and multiple monitors

Unlike the connection via a VGA cable discussed above, when using multiple monitors connected via HDMI interfaces, the command syntax is different:

xrandr --newmode DVI-I-1 mode_name1 ...
xrandr --newmode DVI-I-2 mode_name2 ...
xrandr --addmode DVI-I-1 mode_name1
xrandr --addmode DVI-I-2 mode_name2
xrandr --output DVI-I-1 --mode mode_name1
xrandr --output DVI-I-2 --mode mode_name1

The monitor interface names can be obtained using the same command

xrandr -q

Addition: shell command files for changing resolution, for several monitors at once, can be prepared using a graphics utility Arandr(installation:sudo apt install arandr). The name arandr stands for “Another XRandR GUI” (GUI utility for installing XRandR).

Adding: for desktop environment LXQT(in Lubuntu OS, Fedora Linux back or in a self-installed shell) - there is a graphical utility lxqt-config-monitor available in the menu "Options" - "LXQT Settings" -"Monitor settings". With its help, you can change the resolution and refresh rate of monitor screens and save the settings in a “profile” (settings file). However, screen resolution profiles in LXQT cannot be edited from the command line. Utility **lxqt-config-monitor**modifies text files:
./.config/autostart/lxqt-config-monitor-autostart.desktop
./.config/lxqt/lxqt-config-monitor.conf

The settings files listed here are used when starting an LxQT session from the file ~/.xinitrc, from which the script is called exec startlxqt.


Sources:
https://askubuntu.com/questions/281509/...
linuxmint.com forum



Related publications