I have a bash script to check user authentication which sets DISPLAY variable as:

export DISPLAY=:0.0
xhost +local:

After system bootup, at the login screen, this script fails and error is:

xhost: unable to open display ":0.0"
: cannot connect to X server :0.0

After login, the same script works but without any error.
So, how to set the DISPLAY variable properly or am I missing something else?


When the system is first booting up, the display is sometimes not available until other background scripts have finished running. My solution is to add a sleep command before your display related commands to give the system time to finish loading. Something like:

sleep 5 && export DISPLAY=:0.0 && xhost +local &

The '&&'s chain the commands so that they run one after another, and the command following the && will only run if the previous command was successful. The '&' at the end allows the command to run in the background so that the system can finish loading its necessary components. Without the '&' at the end, the system will not continue loading until the entire line has finished executing, which defeats the purpose of including the sleep command.

  • Thanks. I will try this and get back to you if any problem occurs. – abhi Apr 8 '19 at 23:26

Here's the function I use that waits for user to sign on:

WaitForSignOn () {

    # eyesome daemon is loaded during boot. The user name is required
    # for xrandr external monitor brightness and gamma control. We must
    # wait until user signs on to get .Xauthority file settings.

    SpamOn=10       # Causes 10 iterations of 2 second sleep
    [[ ! -f "$CurrentBrightnessFilename" ]] && rm -f \

    # Wait for user to sign on then get Xserver access for xrandr calls
    while [[ $UserName == "" ]]; do

        sleep "$SpamLength"
        TotalWait=$(( TotalWait + SpamLength ))

        # Find UserName currently logged in.
        UserName="$(who -u | grep -F '(:0)' | head -n 1 | awk '{print $1}')"

    log "Waited $TotalWait seconds for $UserName to login."

    xhost local:root
    export XAUTHORITY="/home/$UserName/.Xauthority"

    if [[ "$fUseDbusMonitor" == true ]] ; then
        echo "$UserName" > "$EyesomeUser"
        sync -d "$EyesomeUser"      # Flush buffer immediately

} # WaitForSignOn

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.