Custom Screen Resolutions in Linux with xrandr (VirtualBox-Friendly Guide)

Jarret B

Well-Known Member
Staff member
Joined
May 22, 2017
Messages
454
Reaction score
519
Credits
19,511
I came across this issue when using VirtualBox. I know you may be thinking that this is something simple. All you need to do is press the HOST key and 'F' for full screen, but this is a completely different way without losing the windowed screen.

Personally, I prefer to leave it with the window so I have access to the minimize, restore and close buttons in the top right corner. It also leaves my menu at the top left, while the hardware information is at the bottom right. Somehow, I just can't stand the menu bar hiding in the bottom middle for full-screen mode.

Even if you aren't in a Virtual Machine, you may need to set your resolution to differ from what is being detected, or used as automatic sizes.

For instance, on an Ubuntu system, I can get the following automatic resolutions:
  • 4096x2160
  • 2560x1600
  • 1920x1440
  • 1856x1392
  • 1792x1344
  • 2048x1152
  • 1920x1200
  • 1920x1080
  • 1600x1200
  • 1680x1050
  • 1400x1050
  • 1600x900
  • 1280x1024
  • 1280x800
  • 1440x900
  • 1280x960
  • 1366x768
  • 1360x768
  • 1280x768
  • 1280x720
  • 1024x768
  • 800x600
  • 848x480
  • 640x480
What I've noticed on VirtualBox, is that when I install Fedora, is that Fedora finds a perfect screen resolution that fits the screen and still includes the window to fit perfectly.

Let me show an example of two different Ubuntu machines where one is default and the other fits. In Figure 1, you can see Ubuntu at the size of 1280x800. For comparison, my monitor size is 1920x1080.

Figure 1.JPG

FIGURE 1

Now, in Figure 2, you can see that the resolution fits the screen much better and is windowed.

Figure 2.JPG

FIGURE 2

You can see that the magic screen size is '1920x974'. Of course, except for Fedora, the specific resolution is not available on standard distros.

To find your proper full screen settings for VirtualBox, you can quickly install Fedora, get the values and remove the machine.

Keep in mind, these instructions work for Linux not just in VirtualBox, but using them in VirtualBox has an application. If you are running a specific application that requires a specific screen resolution that doesn't appear in your default list, then this is how you can add one to the listing.

Adding a Specific Resolution to your List

Looking above at the Ubuntu list of default screen resolution settings, you can definitely see that it does not list '1920x974'. All values end in zero and this is not we need to set a Virtual Machine to full screen.

So, how do we do this then? I'll cover how to set the options in Ubuntu and Arch. Once you understand the method, it is easy to perform it on other distros.

We need to cover one app before continuing. It is called 'cvt'.

CVT

The VESA Coordinated Video Timing is a program used to produce timing scheme listings for a specific resolution and refresh rate.

If the timing values are not correct, then the display would have issues like flashing or skipped frames that would interfere with using the display. A program, such as a game or watching videos, may have issues with skipped frames causing problems.

Ubuntu

By default, the program 'xrandr' should be present on the system. If it is not present, then run:

Code:
sudo apt install x11-xserver-utils -y

Now, we need to run the program 'xrandr' in a terminal. The command will give us some useful information we will need. Figure 3 shows the output.

Figure 3.JPG

FIGURE 3

Here, you see a listing of all available screen resolutions. You also see the name of the display, which is 'Virtual1'. This name is important for use later. It is also noticeable that I only have one display. Keep in mind that you can change resolution on specific displays as well. Another thing to note is the refresh rate of 60 Hz. Some listings show values in the '59' range, but the value to stick to is a whole number, so we will use '60'.

If you were not using VirtualBox, then the display name may be 'HDMI', etc. Again, the display name is important.

Another point to make is that the listing of screen resolutions matches the listing for the resolutions available for Linux.

We know we are looking to set the resolution to '1920974' using a refresh rate of 60 Hz. We need the values for this resolution from the 'cvt' program.

In a terminal, issue the command:

Code:
cvt 1920 974 60

Figure 4 shows the output.

Figure 4.JPG

FIGURE 4

Here we see the timing schemes for the specified resolution we want to create.

Using these values, we can create a new mode:

Code:
xrandr --newmode "1920x974_60" 155.00 1920 2040 2240 2560 974 977 987 1011 -hsync +vsync

If you issue the command 'xrandr', you see the system added a new mode, shown at the bottom of the listing. It is at the bottom since it has not yet been associated with a display device.

Keep in mind that the mode name can be whatever you wish, as long as you do not use invalid characters.

We now need to link the mode with a device, in this case 'Virtual1':

Code:
xrandr --addmode Virtual1 1920x974_60

Running 'xrandr' now will show the new mode listed under Virtual1.

On you system, you need to go into the options for setting the Screen Resolution and you should now see the new mode we added. Set the value as the default setting.

If you log out or power off the machine, it loses the settings. So, we need to set them up to be added after each boot.

In the Home folder, you need to create a file named '.xprofile' and make is executable:

Code:
chmod +x .xprofile

Edit the file and place in the two commands we used:

Code:
 xrandr --newmode "1920x974_60" 155.00 1920 2040 2240 2560 974 977 987 1011 -hsync +vsync
xrandr --addmode Virtual1 1920x974_60

Be sure the file is executable so that the system can execute the commands after it boots.

Reboot your Virtual Machine, if you are doing this in VirtualBox, and once the system starts, you should be fill screen with it windowed.

There is a third line that you can add to '.xprofile' to force a resolution setting:

Code:
xrandr --output Virtual1 --mode 1920x974_60

This can be useful in a script if you want to change the resolution, run a program, then change the resolution back to something else once the program ends.

Arch Linux

For Arch Linux, everything is similar, except the name of the display is 'Virtual-1'.

There are some things to change to get the commands to stay when you reboot, but we can cover these.

After installation, 'xrandr' is not present by default. To install 'xrandr' use:

Code:
sudo pacman -S xorg-xrandr

Once you have run all the commands, as above, and changed the display settings to what you need, you need to place all the commands at the end of the file '.xprofile' in your Home folder.

After rebooting, you may need to change the desktop settings once more. One small trick is to duplicate the '--output' line a few times, or even add a 'sleep' command before the '--output' line. It took a few tries to get this to work properly.

CentOS 9

For my CentOS 9 Virtual Machine, I am using the GDM Display Manager. There were many things I tried, but the way I got it to work was a little different. The 'xrandr' commands were all the same. I had issues getting the script to run after the system loads the desktop.

If the script runs before it loads the desktop, then the settings had no effect on the screen resolution.

So, I created a script named 'startup.sh' with the following contents:

Code:
#!/bin/bash
xrandr --newmode "1920x974_60" 155.00 1920 2040 2240 2560 974 977 987 1011 -hsync +vsync
xrandr --addmode Virtual1 1920x974_60
xrandr --output Virtual1 --mode 1920x974_60

Make the shell script executable:

Code:
chmod +x /home/jarret/startup.sh

Next, you need to make the directories:

Code:
mkdir -p /home/jarret/.config/autostart/

Within the 'autostart' folder will be a file I named 'xrandr.desktop' and had the contents of:

Code:
[Desktop Entry]
Name=Xrandr
Type=Application
Exec=/home/jarret/startup.sh

Remember to change your file paths according to your username.

Make the 'xrandr.desktop' file executable. The scripts runs after the user logs into the system. After you log in the first time, you may need to change the screen resolution through the Display Settings.

After login, the screen resolution may change back and forth a bit, but hopefully stay at the proper resolution you set.

Conclusion

This is a good way to get your display resolution at a higher setting to fill the screen for VirtualBox. A script can be used when starting games or other programs that may require a different resolution.

Sometimes, this may be a trial-and-error option, but if you stick with it, and get it to work.
 


Follow Linux.org

Staff online

Members online


Top