Search code examples
linuxbashubuntupam

PAM module bash script on sudo breaks Zenity - /bin/bash failed: exit code 126 - /bin/bash failed: exit code 126


I've been messing around with PAM module on Kubuntu 18.04 (basically Ubuntu 18.04) and can't seem to figure out why suddenly I have permission issues running my bash script whenever user does sudo command.

Here is my /usr/local/bin/test.sh file code:

#!/bin/sh
/usr/bin/zenity --info --text="It Works!"

And here is my /etc/pam.d/sudo file code:

#%PAM-1.0

session    required   pam_env.so readenv=1 user_readenv=0
session    required   pam_env.so readenv=1 envfile=/etc/default/locale user_readenv=0

# My PAM test runs here
auth       required   pam_exec.so /bin/bash /usr/local/bin/test.sh

@include common-auth
@include common-account
@include common-session-noninteractive

Whenever I try sudo -i it returns me this error message:

bin/bash failed: exit code 126

asks for password and when correct password is entered, prints another error line:

bin/bash failed: exit code 126

I double checked that /usr/bin/test.sh has all necessary permissions.

My question is - what am I doing wrong here? How can I correctly call this and trigger zenity message box as a test?

Update 1

Here are some fixes i've tried without a success:

  • /usr/bin/test.sh permissions are: -rwx--x--x 1 root root
  • both #!/bin/sh and #!/bin/bash work
  • prepending "DISPLAY=:0" didn't help

Update 2

Allright one step closer to fixing it, but still having issues:

I added stdout after auth required and it fixed the issue if I use echo for example, but if I run /usr/bin/zenity --info --text="It Works!" it prompts out a new error:

Unable to init server: Could not connect: Connection refused  (zenity:5046):
Gtk-WARNING **: 05:52:37.200: cannot open display:  /bin/bash failed: exit code 1

Solution

  • Cause

    No display defined for Zenity to prompt the message in.

    Solution

    Add display for whatever visual scripting you're doing within bash - in my case it was appending --display=:0.0 for zenity like so:

    #!/bin/sh
    /usr/bin/zenity --info --text="It Works!" --display=:0.0