Skip to content

Instantly share code, notes, and snippets.

@RyanMillerC
Last active November 7, 2020 13:29
Show Gist options
  • Save RyanMillerC/c48d9f63f8fe386b22a7da18cf6913d2 to your computer and use it in GitHub Desktop.
Save RyanMillerC/c48d9f63f8fe386b22a7da18cf6913d2 to your computer and use it in GitHub Desktop.
Set correct primary monitor for login screen when lightdm greeter starts up on Elementary OS
#!/bin/bash
#
# Created: 2017-07-02 16:20
# Updated: 2018-02-09 12:23
# Creator: Ryan Miller
# Website: http://devopsmachine.com/
# File: /usr/local/bin/set-primary-monitor-pantheon-greeter
#
# Set correct primary monitor for login screen when lightdm greeter starts up on Elementary OS.
#
# Make sure to add the following line into /usr/share/lightdm/lightdm.conf.d/40-pantheon-greeter.conf (without the #)
# greeter-setup-script=/usr/local/bin/set-primary-monitor-pantheon-greeter
#
# Change this variable to your primary display (Run 'xrandr -q' to see available displays)
PRIMARY_DISPLAY='HDMI-0'
# Get list of connected monitors
x_out=$(xrandr -q | grep -e '\( \)connected' | awk -F' ' '{ print $1 }')
# Safety - Check if PRIMARY_DISPLAY is connected; if not, bail out of script
[[ $(grep ${PRIMARY_DISPLAY} <<< ${x_out}) ]] || exit 0
# Iterate over x_out and disable all monitors except for PRIMARY_DISPLAY
for monitor in ${x_out[@]} ; do
if [[ ${monitor} = ${PRIMARY_DISPLAY} ]] ; then
xrandr --output ${monitor} --primary
else
xrandr --output ${monitor} --off
fi
done
@toshe
Copy link

toshe commented Jul 22, 2018

Just a note: there is no need to create a /etc/lightdm/lightdm.conf file. It's enough to append /usr/share/lightdm/lightdm.conf.d/40-pantheon-greeter.conf with the line. If you create a lightdm.conf file make sure you do not add a greeter-setup-script line to both files as this will result in an endless loop.

Also make sure you chmod +x /usr/local/bin/set-primary-monitor-pantheon-greeter as mentioned in the above comment.

@esfourteen-zz
Copy link

this worked for login, but on juno locking the screen would result in both monitors being blank and what looked like and endless cycle of the primary display attempting to sync. adding the --auto option seemed to fix that

xrandr --output ${monitor} --primary --auto

@bugz8unny69
Copy link

Caveat, this worked:

#  cat /usr/share/lightdm/lightdm.conf.d/40-pantheon-greeter.conf
[Seat:*]
greeter-setup-script=/usr/local/bin/set-primary-monitor-pantheon-greeter

You might want to include that in the doc head as the current documentation doesn't work for me on Juno 5.0.

@superhac
Copy link

superhac commented Mar 28, 2019

Caveat, this worked:

#  cat /usr/share/lightdm/lightdm.conf.d/40-pantheon-greeter.conf
[Seat:*]
greeter-setup-script=/usr/local/bin/set-primary-monitor-pantheon-greeter

You might want to include that in the doc head as the current documentation doesn't work for me on Juno 5.0.

I was able to get it to work on Juno by also copying the monitors.xml file:

sudo cp /home/<_user_>/.config/monitors.xml /var/lib/lightdm/.config/

Reboot and it worked like a charm.

@nnmlss
Copy link

nnmlss commented May 30, 2019

Yes, it works like all other similar 'solutions' - until the next shutdown, or plug-in/plug-out the external monitor..

I was able to get it to work on Juno by also copying the monitors.xml file:

sudo cp /home/<_user_>/.config/monitors.xml /var/lib/lightdm/.config/

Reboot and it worked like a charm.

@nongvantinh
Copy link

nongvantinh commented Oct 17, 2019

if the screen turn to black. Just make sure the computer boot failed several time you will get into Grub2 and enter to recovery mode after that remove 2 file above /usr/local/bin/set-primary-monitor-pantheon-greeter and /usr/share/lightdm/lightdm.conf.d/40-pantheon-greeter.conf now black screen gone. Also all solution doesn't work for me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment